buildout.py 49.1 KB
Newer Older
1
##############################################################################
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# Copyright (c) 2005 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Buildout main script

$Id$
"""

19
import distutils.errors
jim's avatar
jim committed
20
import logging
21 22 23 24 25
import md5
import os
import pprint
import re
import shutil
26
import cStringIO
27
import sys
28
import tempfile
29
import urllib2
30
import ConfigParser
31
import UserDict
32 33

import pkg_resources
jim's avatar
jim committed
34
import zc.buildout
35 36
import zc.buildout.easy_install

37 38
from rmtree import rmtree

39
realpath = zc.buildout.easy_install.realpath
40

jim's avatar
jim committed
41 42
pkg_resources_loc = pkg_resources.working_set.find(
    pkg_resources.Requirement.parse('setuptools')).location
43

44
_isurl = re.compile('([a-zA-Z0-9+.-]+)://').match
45

jim's avatar
jim committed
46
class MissingOption(zc.buildout.UserError, KeyError):
47 48 49
    """A required option was missing
    """

jim's avatar
jim committed
50
class MissingSection(zc.buildout.UserError, KeyError):
51 52 53
    """A required section is missinh
    """

54 55
    def __str__(self):
        return "The referenced section, %r, was not defined." % self[0]
56

57 58 59 60 61 62 63 64 65
_buildout_default_options = {
    'eggs-directory': 'eggs',
    'develop-eggs-directory': 'develop-eggs',
    'bin-directory': 'bin',
    'parts-directory': 'parts',
    'installed': '.installed.cfg',
    'python': 'buildout',
    'executable': sys.executable,
    'log-level': 'INFO',
66
    'log-format': '',
67
    }
68

69
class Buildout(UserDict.DictMixin):
70

jim's avatar
jim committed
71
    def __init__(self, config_file, cloptions,
72
                 user_defaults=True, windows_restart=False, command=None):
73

74
        __doing__ = 'Initializing.'
jim's avatar
jim committed
75
        
jim's avatar
jim committed
76
        self.__windows_restart = windows_restart
77 78

        # default options
79
        data = dict(buildout=_buildout_default_options.copy())
80

81 82 83 84
        if not _isurl(config_file):
            config_file = os.path.abspath(config_file)
            base = os.path.dirname(config_file)
            if not os.path.exists(config_file):
85
                if command == 'init':
86
                    print 'Creating %r.' % config_file
87
                    open(config_file, 'w').write('[buildout]\nparts = \n')
jim's avatar
jim committed
88 89 90 91 92
                elif command == 'setup':
                    # Sigh. this model of a buildout nstance
                    # with methods is breaking down :(
                    config_file = None
                    data['buildout']['directory'] = '.'
93 94 95 96 97 98
                else:
                    raise zc.buildout.UserError(
                        "Couldn't open %s" % config_file)

            if config_file:
                data['buildout']['directory'] = os.path.dirname(config_file)
99 100 101
        else:
            base = None

102
        # load user defaults, which override defaults
103 104
        if user_defaults:
            user_config = os.path.join(os.path.expanduser('~'),
105 106 107 108 109 110
                                       '.buildout', 'default.cfg')
            if os.path.exists(user_config):
                _update(data, _open(os.path.dirname(user_config), user_config,
                                    []))

        # load configuration files
111 112
        if config_file:
            _update(data, _open(os.path.dirname(config_file), config_file, []))
113 114 115 116 117

        # apply command-line options
        for (section, option, value) in cloptions:
            options = data.get(section)
            if options is None:
jim's avatar
jim committed
118
                options = data[section] = {}
119
            options[option] = value
jim's avatar
jim committed
120
                # The egg dire
121

122 123 124
        self._raw = data
        self._data = {}
        self._parts = []
125 126 127 128 129 130 131 132
        
        # initialize some attrs and buildout directories.
        options = self['buildout']

        links = options.get('find-links', '')
        self._links = links and links.split() or ()

        self._buildout_dir = options['directory']
133
        for name in ('bin', 'parts', 'eggs', 'develop-eggs'):
134 135
            d = self._buildout_path(options[name+'-directory'])
            options[name+'-directory'] = d
136

jim's avatar
jim committed
137 138 139
        if options['installed']:
            options['installed'] = os.path.join(options['directory'],
                                                options['installed'])
jim's avatar
jim committed
140

jim's avatar
jim committed
141 142
        self._setup_logging()

143 144 145 146
        offline = options.get('offline', 'false')
        if offline not in ('true', 'false'):
            self._error('Invalid value for offline option: %s', offline)
        options['offline'] = offline
jim's avatar
jim committed
147
        self.offline = offline == 'true'
148

jim's avatar
jim committed
149 150 151 152 153 154 155 156
        if self.offline:
            newest = options['newest'] = 'false'
        else:
            newest = options.get('newest', 'true')
            if newest not in ('true', 'false'):
                self._error('Invalid value for newest option: %s', newest)
            options['newest'] = newest
        self.newest = newest == 'true'
157

158 159 160 161
        versions = options.get('versions')
        if versions:
            zc.buildout.easy_install.default_versions(dict(self[versions]))

162
        prefer_final = options.get('prefer-final', 'false')
163 164 165 166 167
        if prefer_final not in ('true', 'false'):
            self._error('Invalid value for prefer-final option: %s',
                        prefer_final)
        zc.buildout.easy_install.prefer_final(prefer_final=='true')
        
168 169 170 171 172 173
        use_dependency_links = options.get('use-dependency-links', 'true')
        if use_dependency_links not in ('true', 'false'):
            self._error('Invalid value for use-dependency-links option: %s',
                        use_dependency_links)
        zc.buildout.easy_install.use_dependency_links(
            use_dependency_links == 'true')
174

jim's avatar
jim committed
175 176 177 178 179 180 181
        allow_picked_versions = options.get('allow-picked-versions', 'true')
        if allow_picked_versions not in ('true', 'false'):
            self._error('Invalid value for allow-picked-versions option: %s',
                        allow_picked_versions)
        zc.buildout.easy_install.allow_picked_versions(
            allow_picked_versions=='true')

jim's avatar
jim committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        download_cache = options.get('download-cache')
        if download_cache:
            download_cache = os.path.join(options['directory'], download_cache)
            if not os.path.isdir(download_cache):
                raise zc.buildout.UserError(
                    'The specified download cache:\n'
                    '%r\n'
                    "Doesn't exist.\n"
                    % download_cache)
            download_cache = os.path.join(download_cache, 'dist')
            if not os.path.isdir(download_cache):
                os.mkdir(download_cache)
                
            zc.buildout.easy_install.download_cache(download_cache)

        install_from_cache = options.get('install-from-cache')
        if install_from_cache:
            if install_from_cache not in ('true', 'false'):
                self._error('Invalid value for install-from-cache option: %s',
                            install_from_cache)
            if install_from_cache == 'true':
                zc.buildout.easy_install.install_from_cache(True)

205 206 207 208
        # "Use" each of the defaults so they aren't reported as unused options.
        for name in _buildout_default_options:
            options[name]

209 210
        os.chdir(options['directory'])

211
    def _buildout_path(self, *names):
212 213
        return os.path.join(self._buildout_dir, *names)

jim's avatar
jim committed
214
    def bootstrap(self, args):
215
        __doing__ = 'Bootstraping.'
jim's avatar
jim committed
216

217
        self._setup_directories()
jim's avatar
jim committed
218 219 220 221 222 223 224

        # Now copy buildout and setuptools eggs, amd record destination eggs:
        entries = []
        for name in 'setuptools', 'zc.buildout':
            r = pkg_resources.Requirement.parse(name)
            dist = pkg_resources.working_set.find(r)
            if dist.precedence == pkg_resources.DEVELOP_DIST:
225
                dest = os.path.join(self['buildout']['develop-eggs-directory'],
jim's avatar
jim committed
226 227 228 229 230 231 232 233
                                    name+'.egg-link')
                open(dest, 'w').write(dist.location)
                entries.append(dist.location)
            else:
                dest = os.path.join(self['buildout']['eggs-directory'],
                                    os.path.basename(dist.location))
                entries.append(dest)
                if not os.path.exists(dest):
jim's avatar
jim committed
234 235 236 237
                    if os.path.isdir(dist.location):
                        shutil.copytree(dist.location, dest)
                    else:
                        shutil.copy2(dist.location, dest)
jim's avatar
jim committed
238 239 240 241 242 243 244 245

        # Create buildout script
        ws = pkg_resources.WorkingSet(entries)
        ws.require('zc.buildout')
        zc.buildout.easy_install.scripts(
            ['zc.buildout'], ws, sys.executable,
            self['buildout']['bin-directory'])

246 247
    init = bootstrap

248
    def install(self, install_args):
249
        __doing__ = 'Installing.'
jim's avatar
jim committed
250

jim's avatar
jim committed
251
        self._load_extensions()
252
        self._setup_directories()
jim's avatar
jim committed
253

254 255 256 257
        # Add develop-eggs directory to path so that it gets searched
        # for eggs:
        sys.path.insert(0, self['buildout']['develop-eggs-directory'])

258 259 260
        # Check for updates. This could cause the process to be rstarted
        self._maybe_upgrade()

261
        # load installed data
262 263
        (installed_part_options, installed_exists
         )= self._read_installed_part_options()
264

265 266 267 268 269 270 271 272
        # Remove old develop eggs
        self._uninstall(
            installed_part_options['buildout'].get(
                'installed_develop_eggs', '')
            )

        # Build develop eggs
        installed_develop_eggs = self._develop()
273 274 275 276 277 278
        installed_part_options['buildout']['installed_develop_eggs'
                                           ] = installed_develop_eggs
        
        if installed_exists:
            self._update_installed(
                installed_develop_eggs=installed_develop_eggs)
279

280 281 282 283 284
        # get configured and installed part lists
        conf_parts = self['buildout']['parts']
        conf_parts = conf_parts and conf_parts.split() or []
        installed_parts = installed_part_options['buildout']['parts']
        installed_parts = installed_parts and installed_parts.split() or []
285
        
286 287
        if install_args:
            install_parts = install_args
288 289 290 291 292
            uninstall_missing = False
        else:
            install_parts = conf_parts
            uninstall_missing = True

293 294
        # load and initialize recipes
        [self[part]['recipe'] for part in install_parts]
295 296
        if not install_args:
            install_parts = self._parts
297

298
        if self._log_level < logging.DEBUG:
299 300 301 302 303 304 305 306
            sections = list(self)
            sections.sort()
            print    
            print 'Configuration data:'
            for section in self._data:
                _save_options(section, self[section], sys.stdout)
            print    

307 308 309 310

        # compute new part recipe signatures
        self._compute_part_signatures(install_parts)

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        # uninstall parts that are no-longer used or who's configs
        # have changed
        for part in reversed(installed_parts):
            if part in install_parts:
                old_options = installed_part_options[part].copy()
                installed_files = old_options.pop('__buildout_installed__')
                new_options = self.get(part)
                if old_options == new_options:
                    # The options are the same, but are all of the
                    # installed files still there?  If not, we should
                    # reinstall.
                    if not installed_files:
                        continue
                    for f in installed_files.split('\n'):
                        if not os.path.exists(self._buildout_path(f)):
                            break
jim's avatar
jim committed
327
                    else:
328 329 330
                        continue

                # output debugging info
331 332 333 334 335 336 337 338 339 340 341 342 343 344
                if self._logger.getEffectiveLevel() < logging.DEBUG:
                    for k in old_options:
                        if k not in new_options:
                            self._logger.debug("Part %s, dropped option %s.",
                                               part, k)
                        elif old_options[k] != new_options[k]:
                            self._logger.debug(
                                "Part %s, option %s changed:\n%r != %r",
                                part, k, new_options[k], old_options[k],
                                )
                    for k in new_options:
                        if k not in old_options:
                            self._logger.debug("Part %s, new option %s.",
                                               part, k)
345 346 347

            elif not uninstall_missing:
                continue
jim's avatar
jim committed
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
            self._uninstall_part(part, installed_part_options)
            installed_parts = [p for p in installed_parts if p != part]

            if installed_exists:
                self._update_installed(parts=' '.join(installed_parts))

        # Check for unused buildout options:
        _check_for_unused_options_in_section(self, 'buildout')

        # install new parts
        for part in install_parts:
            signature = self[part].pop('__buildout_signature__')
            saved_options = self[part].copy()
            recipe = self[part].recipe
            if part in installed_parts: # update
                need_to_save_installed = False
365
                __doing__ = 'Updating %s.', part
366 367 368 369 370 371 372 373 374 375 376 377 378 379
                self._logger.info(*__doing__)
                old_options = installed_part_options[part]
                old_installed_files = old_options['__buildout_installed__']

                try:
                    update = recipe.update
                except AttributeError:
                    update = recipe.install
                    self._logger.warning(
                        "The recipe for %s doesn't define an update "
                        "method. Using its install method.",
                        part)

                try:
380
                    installed_files = self[part]._call(update)
381 382 383 384 385 386 387 388 389 390 391
                except:
                    installed_parts.remove(part)
                    self._uninstall(old_installed_files)
                    if installed_exists:
                        self._update_installed(
                            parts=' '.join(installed_parts))
                    raise

                old_installed_files = old_installed_files.split('\n')
                if installed_files is None:
                    installed_files = old_installed_files
jim's avatar
jim committed
392
                else:
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
                    if isinstance(installed_files, str):
                        installed_files = [installed_files]
                    else:
                        installed_files = list(installed_files)

                    need_to_save_installed = [
                        p for p in installed_files
                        if p not in old_installed_files]

                    if need_to_save_installed:
                        installed_files = (old_installed_files
                                           + need_to_save_installed)

            else: # install
                need_to_save_installed = True
408
                __doing__ = 'Installing %s.', part
409
                self._logger.info(*__doing__)
410
                installed_files = self[part]._call(recipe.install)
411 412 413 414 415 416 417
                if installed_files is None:
                    self._logger.warning(
                        "The %s install returned None.  A path or "
                        "iterable os paths should be returned.",
                        part)
                    installed_files = ()
                elif isinstance(installed_files, str):
418
                    installed_files = [installed_files]
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
                else:
                    installed_files = list(installed_files)

            installed_part_options[part] = saved_options
            saved_options['__buildout_installed__'
                          ] = '\n'.join(installed_files)
            saved_options['__buildout_signature__'] = signature

            installed_parts = [p for p in installed_parts if p != part]
            installed_parts.append(part)
            _check_for_unused_options_in_section(self, part)

            if need_to_save_installed:
                installed_part_options['buildout']['parts'] = (
                    ' '.join(installed_parts))            
                self._save_installed_options(installed_part_options)
                installed_exists = True
            else:
                assert installed_exists
                self._update_installed(parts=' '.join(installed_parts))
jim's avatar
jim committed
439

440 441 442 443 444
        if installed_develop_eggs:
            if not installed_exists:
                self._save_installed_options(installed_part_options)
        elif (not installed_parts) and installed_exists:
            os.remove(self['buildout']['installed'])
jim's avatar
jim committed
445

446 447 448 449 450 451 452
    def _update_installed(self, **buildout_options):
        installed = self['buildout']['installed']
        f = open(installed, 'a')
        f.write('\n[buildout]\n')
        for option, value in buildout_options.items():
            _save_option(option, value, f)
        f.close()
453

jim's avatar
jim committed
454 455
    def _uninstall_part(self, part, installed_part_options):
        # ununstall part
456
        __doing__ = 'Uninstalling %s.', part
jim's avatar
jim committed
457 458 459 460 461 462 463
        self._logger.info(*__doing__)

        # run uinstall recipe
        recipe, entry = _recipe(installed_part_options[part])
        try:
            uninstaller = _install_and_load(
                recipe, 'zc.buildout.uninstall', entry, self)
464
            self._logger.info('Running uninstall recipe.')
jim's avatar
jim committed
465 466 467 468 469 470 471 472 473
            uninstaller(part, installed_part_options[part])
        except (ImportError, pkg_resources.DistributionNotFound), v:
            pass

        # remove created files and directories
        self._uninstall(
            installed_part_options[part]['__buildout_installed__'])


474
    def _setup_directories(self):
jim's avatar
jim committed
475
        __doing__ = 'Setting up buildout directories'
476 477 478 479 480

        # Create buildout directories
        for name in ('bin', 'parts', 'eggs', 'develop-eggs'):
            d = self['buildout'][name+'-directory']
            if not os.path.exists(d):
481
                self._logger.info('Creating directory %r.', d)
482 483
                os.mkdir(d)

484 485 486
    def _develop(self):
        """Install sources by running setup.py develop on them
        """
jim's avatar
jim committed
487 488
        __doing__ = 'Processing directories listed in the develop option'

489
        develop = self['buildout'].get('develop')
490 491 492 493 494 495 496 497 498
        if not develop:
            return ''

        dest = self['buildout']['develop-eggs-directory']
        old_files = os.listdir(dest)

        env = dict(os.environ, PYTHONPATH=pkg_resources_loc)
        here = os.getcwd()
        try:
499 500
            try:
                for setup in develop.split():
501
                    setup = self._buildout_path(setup)
502 503
                    self._logger.info("Develop: %r", setup)
                    __doing__ = 'Processing develop directory %r.', setup
504
                    zc.buildout.easy_install.develop(setup, dest)
505 506 507 508 509 510 511 512 513
            except:
                # if we had an error, we need to roll back changes, by
                # removing any files we created.
                self._sanity_check_develop_eggs_files(dest, old_files)
                self._uninstall('\n'.join(
                    [os.path.join(dest, f)
                     for f in os.listdir(dest)
                     if f not in old_files
                     ]))
514 515
                raise
                     
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
            else:
                self._sanity_check_develop_eggs_files(dest, old_files)
                return '\n'.join([os.path.join(dest, f)
                                  for f in os.listdir(dest)
                                  if f not in old_files
                                  ])

        finally:
            os.chdir(here)


    def _sanity_check_develop_eggs_files(self, dest, old_files):
        for f in os.listdir(dest):
            if f in old_files:
                continue
            if not (os.path.isfile(os.path.join(dest, f))
                    and f.endswith('.egg-link')):
                self._logger.warning(
534
                    "Unexpected entry, %r, in develop-eggs directory.", f)
535

536 537 538 539 540 541
    def _compute_part_signatures(self, parts):
        # Compute recipe signature and add to options
        for part in parts:
            options = self.get(part)
            if options is None:
                options = self[part] = {}
542
            recipe, entry = _recipe(options)
543
            req = pkg_resources.Requirement.parse(recipe)
544
            sig = _dists_sig(pkg_resources.working_set.resolve([req]))
545 546 547
            options['__buildout_signature__'] = ' '.join(sig)

    def _read_installed_part_options(self):
jim's avatar
jim committed
548 549
        old = self['buildout']['installed']
        if old and os.path.isfile(old):
550
            parser = ConfigParser.RawConfigParser()
551
            parser.optionxform = lambda s: s
552
            parser.read(old)
553 554 555 556 557 558 559 560 561 562
            result = {}
            for section in parser.sections():
                options = {}
                for option, value in parser.items(section):
                    if '%(' in value:
                        for k, v in _spacey_defaults:
                            value = value.replace(k, v)
                    options[option] = value
                result[section] = Options(self, section, options)
                        
563
            return result, True
564
        else:
565 566 567
            return ({'buildout': Options(self, 'buildout', {'parts': ''})},
                    False,
                    )
568 569

    def _uninstall(self, installed):
jim's avatar
jim committed
570 571 572
        for f in installed.split('\n'):
            if not f:
                continue
573
            f = self._buildout_path(f)
574
            if os.path.isdir(f):
575
                rmtree(f)
576 577 578 579 580
            elif os.path.isfile(f):
                os.remove(f)
                
    def _install(self, part):
        options = self[part]
581
        recipe, entry = _recipe(options)
582 583 584 585 586 587 588
        recipe_class = pkg_resources.load_entry_point(
            recipe, 'zc.buildout', entry)
        installed = recipe_class(self, part, options).install()
        if installed is None:
            installed = []
        elif isinstance(installed, basestring):
            installed = [installed]
589
        base = self._buildout_path('')
590 591 592 593
        installed = [d.startswith(base) and d[len(base):] or d
                     for d in installed]
        return ' '.join(installed)

594

595
    def _save_installed_options(self, installed_options):
jim's avatar
jim committed
596 597 598 599
        installed = self['buildout']['installed']
        if not installed:
            return
        f = open(installed, 'w')
600 601 602 603 604
        _save_options('buildout', installed_options['buildout'], f)
        for part in installed_options['buildout']['parts'].split():
            print >>f
            _save_options(part, installed_options[part], f)
        f.close()
jim's avatar
jim committed
605

jim's avatar
jim committed
606 607
    def _error(self, message, *args):
        raise zc.buildout.UserError(message % args)
jim's avatar
jim committed
608 609 610

    def _setup_logging(self):
        root_logger = logging.getLogger()
611
        self._logger = logging.getLogger('zc.buildout')
jim's avatar
jim committed
612
        handler = logging.StreamHandler(sys.stdout)
613 614 615 616 617 618 619 620 621 622 623
        log_format = self['buildout']['log-format']
        if not log_format:
            # No format specified. Use different formatter for buildout
            # and other modules, showing logger name except for buildout
            log_format = '%(name)s: %(message)s'
            buildout_handler = logging.StreamHandler(sys.stdout)
            buildout_handler.setFormatter(logging.Formatter('%(message)s'))
            self._logger.propagate = False
            self._logger.addHandler(buildout_handler)
            
        handler.setFormatter(logging.Formatter(log_format))
jim's avatar
jim committed
624
        root_logger.addHandler(handler)
625

jim's avatar
jim committed
626 627 628 629 630 631 632 633 634 635 636 637 638
        level = self['buildout']['log-level']
        if level in ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'):
            level = getattr(logging, level)
        else:
            try:
                level = int(level)
            except ValueError:
                self._error("Invalid logging level %s", level)
        verbosity = self['buildout'].get('verbosity', 0)
        try:
            verbosity = int(verbosity)
        except ValueError:
            self._error("Invalid verbosity %s", verbosity)
639 640 641

        level -= verbosity
        root_logger.setLevel(level)
jim's avatar
jim committed
642
        self._log_level = level
643

644 645
    def _maybe_upgrade(self):
        # See if buildout or setuptools need to be upgraded.
jim's avatar
jim committed
646
        # If they do, do the upgrade and restart the buildout process.
647
        __doing__ = 'Checking for upgrades.'
jim's avatar
jim committed
648

jim's avatar
jim committed
649 650
        if not self.newest:
            return
jim's avatar
jim committed
651
        
652 653 654 655 656 657 658 659
        ws = zc.buildout.easy_install.install(
            [
            (spec + ' ' + self['buildout'].get(spec+'-version', '')).strip()
            for spec in ('zc.buildout', 'setuptools')
            ],
            self['buildout']['eggs-directory'],
            links = self['buildout'].get('find-links', '').split(),
            index = self['buildout'].get('index'),
660
            path = [self['buildout']['develop-eggs-directory']]
661 662 663 664 665 666 667 668 669 670
            )

        upgraded = []
        for project in 'zc.buildout', 'setuptools':
            req = pkg_resources.Requirement.parse(project)
            if ws.find(req) != pkg_resources.working_set.find(req):
                upgraded.append(ws.find(req))

        if not upgraded:
            return
jim's avatar
jim committed
671

672
        __doing__ = 'Upgrading.'
jim's avatar
jim committed
673

674 675 676 677 678 679 680 681
        should_run = realpath(
            os.path.join(os.path.abspath(self['buildout']['bin-directory']),
                         'buildout')
            )
        if sys.platform == 'win32':
            should_run += '-script.py'

        if (realpath(os.path.abspath(sys.argv[0])) != should_run):
682 683
            self._logger.debug("Running %r.", realpath(sys.argv[0]))
            self._logger.debug("Local buildout is %r.", should_run)
684
            self._logger.warn("Not upgrading because not running a local "
685
                              "buildout command.")
686 687
            return

jim's avatar
jim committed
688 689 690 691 692
        if sys.platform == 'win32' and not self.__windows_restart:
            args = map(zc.buildout.easy_install._safe_arg, sys.argv)
            args.insert(1, '-W')
            if not __debug__:
                args.insert(0, '-O')
693
            args.insert(0, zc.buildout.easy_install._safe_arg (sys.executable))
jim's avatar
jim committed
694
            os.execv(sys.executable, args)            
695
        
jim's avatar
jim committed
696 697
        self._logger.info("Upgraded:\n  %s;\nrestarting.",
                          ",\n  ".join([("%s version %s"
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
                                       % (dist.project_name, dist.version)
                                       )
                                      for dist in upgraded
                                      ]
                                     ),
                          )
                
        # the new dist is different, so we've upgraded.
        # Update the scripts and return True
        zc.buildout.easy_install.scripts(
            ['zc.buildout'], ws, sys.executable,
            self['buildout']['bin-directory'],
            )

        # Restart
        args = map(zc.buildout.easy_install._safe_arg, sys.argv)
        if not __debug__:
            args.insert(0, '-O')
716
        args.insert(0, zc.buildout.easy_install._safe_arg (sys.executable))
717 718
        sys.exit(os.spawnv(os.P_WAIT, sys.executable, args))

719
    def _load_extensions(self):
720
        __doing__ = 'Loading extensions.'
721 722
        specs = self['buildout'].get('extensions', '').split()
        if specs:
jim's avatar
jim committed
723
            path = [self['buildout']['develop-eggs-directory']]
jim's avatar
jim committed
724
            if self.offline:
725
                dest = None
jim's avatar
jim committed
726
                path.append(self['buildout']['eggs-directory'])
727 728
            else:
                dest = self['buildout']['eggs-directory']
jim's avatar
jim committed
729
                if not os.path.exists(dest):
730
                    self._logger.info('Creating directory %r.', dest)
jim's avatar
jim committed
731
                    os.mkdir(dest)
jim's avatar
jim committed
732

733
            zc.buildout.easy_install.install(
jim's avatar
jim committed
734
                specs, dest, path=path,
735
                working_set=pkg_resources.working_set,
736 737
                links = self['buildout'].get('find-links', '').split(),
                index = self['buildout'].get('index'),
jim's avatar
jim committed
738
                newest=self.newest)
739 740 741 742 743

            # Clear cache because extensions might now let us read pages we
            # couldn't read before.
            zc.buildout.easy_install.clear_index_cache()

744 745
            for ep in pkg_resources.iter_entry_points('zc.buildout.extension'):
                ep.load()(self)
746

jim's avatar
jim committed
747
    def setup(self, args):
748 749
        if not args:
            raise zc.buildout.UserError(
jim's avatar
jim committed
750 751
                "The setup command requires the path to a setup script or \n"
                "directory containing a setup script, and it's arguments."
752
                )
753 754 755 756
        setup = args.pop(0)
        if os.path.isdir(setup):
            setup = os.path.join(setup, 'setup.py')

757
        self._logger.info("Running setup script %r.", setup)
758 759 760 761
        setup = os.path.abspath(setup)

        fd, tsetup = tempfile.mkstemp()
        try:
762
            os.write(fd, zc.buildout.easy_install.runsetup_template % dict(
763
                setuptools=pkg_resources_loc,
764 765
                setupdir=os.path.dirname(setup),
                setup=setup,
766
                __file__ = setup,
767
                ))
768
            os.spawnl(os.P_WAIT, sys.executable, zc.buildout.easy_install._safe_arg (sys.executable), tsetup,
769 770 771 772 773
                      *[zc.buildout.easy_install._safe_arg(a)
                        for a in args])
        finally:
            os.close(fd)
            os.remove(tsetup)
jim's avatar
jim committed
774

775 776 777
    runsetup = setup # backward compat.

    def __getitem__(self, section):
778
        __doing__ = 'Getting section %s.', section
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
        try:
            return self._data[section]
        except KeyError:
            pass

        try:
            data = self._raw[section]
        except KeyError:
            raise MissingSection(section)

        options = Options(self, section, data)
        self._data[section] = options
        options._initialize()
        return options          

    def __setitem__(self, key, value):
        raise NotImplementedError('__setitem__')

    def __delitem__(self, key):
        raise NotImplementedError('__delitem__')

    def keys(self):
        return self._raw.keys()

    def __iter__(self):
        return iter(self._raw)

jim's avatar
jim committed
806 807

def _install_and_load(spec, group, entry, buildout):
808
    __doing__ = 'Loading recipe %r.', spec
jim's avatar
jim committed
809 810 811 812 813
    try:
        req = pkg_resources.Requirement.parse(spec)

        buildout_options = buildout['buildout']
        if pkg_resources.working_set.find(req) is None:
814
            __doing__ = 'Installing recipe %s.', spec
jim's avatar
jim committed
815
            if buildout.offline:
jim's avatar
jim committed
816 817 818 819 820 821 822 823 824 825 826 827 828 829
                dest = None
                path = [buildout_options['develop-eggs-directory'],
                        buildout_options['eggs-directory'],
                        ]
            else:
                dest = buildout_options['eggs-directory']
                path = [buildout_options['develop-eggs-directory']]

            zc.buildout.easy_install.install(
                [spec], dest,
                links=buildout._links,
                index=buildout_options.get('index'),
                path=path,
                working_set=pkg_resources.working_set,
jim's avatar
jim committed
830
                newest=buildout.newest,
jim's avatar
jim committed
831 832
                )

833
        __doing__ = 'Loading %s recipe entry %s:%s.', group, spec, entry
jim's avatar
jim committed
834 835 836 837 838 839 840 841 842 843
        return pkg_resources.load_entry_point(
            req.project_name, group, entry)

    except Exception, v:
        buildout._logger.log(
            1,
            "Could't load %s entry point %s\nfrom %s:\n%s.",
            group, entry, spec, v)
        raise

844 845 846 847 848 849
class Options(UserDict.DictMixin):

    def __init__(self, buildout, section, data):
        self.buildout = buildout
        self.name = section
        self._raw = data
850
        self._cooked = {}
851 852 853
        self._data = {}

    def _initialize(self):
jim's avatar
jim committed
854
        name = self.name
855
        __doing__ = 'Initializing section %s.', name
856
        
857
        # force substitutions
858 859 860
        for k, v in self._raw.items():
            if '${' in v:
                self._dosub(k, v)
861

862 863 864
        if self.name == 'buildout':
            return # buildout section can never be a part
        
865 866 867
        recipe = self.get('recipe')
        if not recipe:
            return
868
        
869 870
        reqs, entry = _recipe(self._data)
        buildout = self.buildout
jim's avatar
jim committed
871
        recipe_class = _install_and_load(reqs, 'zc.buildout', entry, buildout)
872

873
        __doing__ = 'Initializing part %s.', name
jim's avatar
jim committed
874 875
        self.recipe = recipe_class(buildout, name, self)
        buildout._parts.append(name)
876

877
    def _dosub(self, option, v):
878
        __doing__ = 'Getting option %s:%s.', self.name, option
879 880 881 882
        seen = [(self.name, option)]
        v = '$$'.join([self._sub(s, seen) for s in v.split('$$')])
        self._cooked[option] = v

883 884 885 886 887 888
    def get(self, option, default=None, seen=None):
        try:
            return self._data[option]
        except KeyError:
            pass

889
        v = self._cooked.get(option)
890
        if v is None:
891 892 893
            v = self._raw.get(option)
            if v is None:
                return default
894

895
        __doing__ = 'Getting option %s:%s.', self.name, option
jim's avatar
jim committed
896

897 898 899 900 901 902 903 904 905 906 907 908
        if '${' in v:
            key = self.name, option
            if seen is None:
                seen = [key]
            elif key in seen:
                raise zc.buildout.UserError(
                    "Circular reference in substitutions.\n"
                    )
            else:
                seen.append(key)
            v = '$$'.join([self._sub(s, seen) for s in v.split('$$')])
            seen.pop()
909

910 911 912 913 914
        self._data[option] = v
        return v

    _template_split = re.compile('([$]{[^}]*})').split
    _simple = re.compile('[-a-zA-Z0-9 ._]+$').match
915
    _valid = re.compile('\${[-a-zA-Z0-9 ._]+:[-a-zA-Z0-9 ._]+}$').match
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
    def _sub(self, template, seen):
        value = self._template_split(template)
        subs = []
        for ref in value[1::2]:
            s = tuple(ref[2:-1].split(':'))
            if not self._valid(ref):
                if len(s) < 2:
                    raise zc.buildout.UserError("The substitution, %s,\n"
                                                "doesn't contain a colon."
                                                % ref)
                if len(s) > 2:
                    raise zc.buildout.UserError("The substitution, %s,\n"
                                                "has too many colons."
                                                % ref)
                if not self._simple(s[0]):
                    raise zc.buildout.UserError(
                        "The section name in substitution, %s,\n"
                        "has invalid characters."
                        % ref)
                if not self._simple(s[1]):
                    raise zc.buildout.UserError(
                        "The option name in substitution, %s,\n"
                        "has invalid characters."
                        % ref)
                
            v = self.buildout[s[0]].get(s[1], None, seen)
            if v is None:
                raise MissingOption("Referenced option does not exist:", *s)
            subs.append(v)
        subs.append('')

        return ''.join([''.join(v) for v in zip(value[::2], subs)])
        
    def __getitem__(self, key):
        try:
            return self._data[key]
        except KeyError:
            pass

        v = self.get(key)
        if v is None:
jim's avatar
jim committed
957
            raise MissingOption("Missing option: %s:%s" % (self.name, key))
958 959 960 961 962 963 964 965 966 967 968 969
        return v

    def __setitem__(self, option, value):
        if not isinstance(value, str):
            raise TypeError('Option values must be strings', value)
        self._data[option] = value

    def __delitem__(self, key):
        if key in self._raw:
            del self._raw[key]
            if key in self._data:
                del self._data[key]
970 971
            if key in self._cooked:
                del self._cooked[key]
972 973 974 975 976 977 978 979 980 981
        elif key in self._data:
            del self._data[key]
        else:
            raise KeyError, key

    def keys(self):
        raw = self._raw
        return list(self._raw) + [k for k in self._data if k not in raw]

    def copy(self):
982 983 984 985
        result = self._raw.copy()
        result.update(self._cooked)
        result.update(self._data)
        return result
986

987
    def _call(self, f):
988
        buildout_directory = self.buildout['buildout']['directory']
989 990 991
        self._created = []
        try:
            try:
992
                os.chdir(buildout_directory)
993 994 995 996
                return f()
            except:
                for p in self._created:
                    if os.path.isdir(p):
997
                        rmtree(p)
998 999 1000
                    elif os.path.isfile(p):
                        os.remove(p)
                    else:
1001
                        self._buildout._logger.warn("Couldn't clean up %r.", p)
1002 1003 1004
                raise
        finally:
            self._created = None
1005
            os.chdir(buildout_directory)
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

    def created(self, *paths):
        try:
            self._created.extend(paths)
        except AttributeError:
            raise TypeError(
                "Attempt to register a created path while not installing",
                self.name)
        return self._created

1016 1017 1018
_spacey_nl = re.compile('[ \t\r\f\v]*\n[ \t\r\f\v\n]*'
                        '|'
                        '^[ \t\r\f\v]+'
1019 1020 1021 1022
                        '|'
                        '[ \t\r\f\v]+$'
                        )

1023 1024 1025 1026 1027 1028 1029 1030
_spacey_defaults = [
    ('%(__buildout_space__)s',   ' '),
    ('%(__buildout_space_n__)s', '\n'),
    ('%(__buildout_space_r__)s', '\r'),
    ('%(__buildout_space_f__)s', '\f'),
    ('%(__buildout_space_v__)s', '\v'),
    ]

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
def _quote_spacey_nl(match):
    match = match.group(0).split('\n', 1)
    result = '\n\t'.join(
        [(s
          .replace(' ', '%(__buildout_space__)s')
          .replace('\r', '%(__buildout_space_r__)s')
          .replace('\f', '%(__buildout_space_f__)s')
          .replace('\v', '%(__buildout_space_v__)s')
          .replace('\n', '%(__buildout_space_n__)s')
          )
         for s in match]
        )
    return result

1045 1046 1047 1048 1049 1050 1051 1052
def _save_option(option, value, f):
    value = _spacey_nl.sub(_quote_spacey_nl, value)
    if value.startswith('\n\t'):
        value = '%(__buildout_space_n__)s' + value[2:]
    if value.endswith('\n\t'):
        value = value[:-2] + '%(__buildout_space_n__)s'
    print >>f, option, '=', value
    
1053 1054 1055 1056 1057
def _save_options(section, options, f):
    print >>f, '[%s]' % section
    items = options.items()
    items.sort()
    for option, value in items:
1058
        _save_option(option, value, f)
1059 1060 1061 1062 1063 1064 1065

def _open(base, filename, seen):
    """Open a configuration file and return the result as a dictionary,

    Recursively open other files based on buildout options found.
    """

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    if _isurl(filename):
        fp = urllib2.urlopen(filename)
        base = filename[:filename.rfind('/')]
    elif _isurl(base):
        if os.path.isabs(filename):
            fp = open(filename)
            base = os.path.dirname(filename)
        else:
            filename = base + '/' + filename
            fp = urllib2.urlopen(filename)
            base = filename[:filename.rfind('/')]
    else:
        filename = os.path.join(base, filename)
        fp = open(filename)
        base = os.path.dirname(filename)

1082
    if filename in seen:
jim's avatar
jim committed
1083
        raise zc.buildout.UserError("Recursive file include", seen, filename)
1084 1085 1086 1087 1088

    seen.append(filename)

    result = {}

1089
    parser = ConfigParser.RawConfigParser()
1090
    parser.optionxform = lambda s: s
1091
    parser.readfp(fp)
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    extends = extended_by = None
    for section in parser.sections():
        options = dict(parser.items(section))
        if section == 'buildout':
            extends = options.pop('extends', extends)
            extended_by = options.pop('extended-by', extended_by)
        result[section] = options

    if extends:
        extends = extends.split()
        extends.reverse()
        for fname in extends:
            result = _update(_open(base, fname, seen), result)

    if extended_by:
1107 1108 1109
        self._logger.warn(
            "The extendedBy option is deprecated.  Stop using it."
            )
1110 1111 1112 1113 1114 1115 1116
        for fname in extended_by.split():
            result = _update(result, _open(base, fname, seen))

    seen.pop()
    return result
    

jim's avatar
jim committed
1117
ignore_directories = '.svn', 'CVS'
1118 1119 1120
def _dir_hash(dir):
    hash = md5.new()
    for (dirpath, dirnames, filenames) in os.walk(dir):
jim's avatar
jim committed
1121
        dirnames[:] = [n for n in dirnames if n not in ignore_directories]
1122 1123 1124 1125 1126 1127 1128 1129 1130
        filenames[:] = [f for f in filenames
                        if not (f.endswith('pyc') or f.endswith('pyo'))
                        ]
        hash.update(' '.join(dirnames))
        hash.update(' '.join(filenames))
        for name in filenames:
            hash.update(open(os.path.join(dirpath, name)).read())
    return hash.digest().encode('base64').strip()
    
1131
def _dists_sig(dists):
1132 1133 1134 1135 1136 1137
    result = []
    for dist in dists:
        location = dist.location
        if dist.precedence == pkg_resources.DEVELOP_DIST:
            result.append(dist.project_name + '-' + _dir_hash(location))
        else:
1138
            result.append(os.path.basename(location))
1139 1140
    return result

1141 1142 1143 1144 1145 1146 1147 1148
def _update(d1, d2):
    for section in d2:
        if section in d1:
            d1[section].update(d2[section])
        else:
            d1[section] = d2[section]
    return d1

1149 1150 1151 1152 1153 1154 1155 1156 1157
def _recipe(options):
    recipe = options['recipe']
    if ':' in recipe:
        recipe, entry = recipe.split(':')
    else:
        entry = 'default'

    return recipe, entry

jim's avatar
jim committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
def _doing():
    _, v, tb = sys.exc_info()
    message = str(v)
    doing = []
    while tb is not None:
        d = tb.tb_frame.f_locals.get('__doing__')
        if d:
            doing.append(d)
        tb = tb.tb_next
        
    if doing:
        sys.stderr.write('While:\n')
        for d in doing:
            if not isinstance(d, str):
                d = d[0] % d[1:]
            sys.stderr.write('  %s\n' % d)

1175
def _error(*message):
1176
    sys.stderr.write('Error: ' + ' '.join(message) +'\n')
1177 1178
    sys.exit(1)

jim's avatar
jim committed
1179 1180 1181 1182 1183 1184 1185 1186
_internal_error_template = """
An internal error occured due to a bug in either zc.buildout or in a
recipe being used:

%s:
%s
"""

1187 1188 1189 1190
def _check_for_unused_options_in_section(buildout, section):
    options = buildout[section]
    unused = [option for option in options._raw if option not in options._data]
    if unused:
1191
        buildout._logger.warn("Unused options for %s: %s."
1192 1193 1194
                              % (section, ' '.join(map(repr, unused)))
                              )

jim's avatar
jim committed
1195 1196
def _internal_error(v):
    sys.stderr.write(_internal_error_template % (v.__class__.__name__, v))
1197
    sys.exit(1)
jim's avatar
jim committed
1198 1199
    

jim's avatar
jim committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
_usage = """\
Usage: buildout [options] [assignments] [command [command arguments]]

Options:

  -h, --help

     Print this message and exit.

  -v

     Increase the level of verbosity.  This option can be used multiple times.

  -q

alga's avatar
alga committed
1215
     Decrease the level of verbosity.  This option can be used multiple times.
jim's avatar
jim committed
1216 1217 1218 1219

  -c config_file

     Specify the path to the buildout configuration file to be used.
alga's avatar
alga committed
1220 1221
     This defaults to the file named "buildout.cfg" in the current
     working directory.
jim's avatar
jim committed
1222

1223 1224 1225 1226
  -t socket_timeout

     Specify the socket timeout in seconds.

jim's avatar
jim committed
1227 1228 1229 1230
  -U

     Don't read user defaults.

jim's avatar
jim committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
  -o
  
    Run in off-line mode.  This is equivalent to the assignment 
    buildout:offline=true.

  -O

    Run in non-off-line mode.  This is equivalent to the assignment 
    buildout:offline=false.  This is the default buildout mode.  The
    -O option would normally be used to override a true offline
    setting in a configuration file.

  -n

    Run in newest mode.  This is equivalent to the assignment
    buildout:newest=true.  With this setting, which is the default,
    buildout will try to find the newest versions of distributions
    available that satisfy its requirements.

  -N

    Run in non-newest mode.  This is equivalent to the assignment 
    buildout:newest=false.  With this setting, buildout will not seek
    new distributions if installed distributions satisfy it's
    requirements. 

jim's avatar
jim committed
1257 1258 1259 1260 1261 1262
  -D

    Debug errors.  If an error occurs, then the post-mortem debugger
    will be started. This is especially useful for debuging recipe
    problems.

jim's avatar
jim committed
1263
Assignments are of the form: section:option=value and are used to
alga's avatar
alga committed
1264
provide configuration options that override those given in the
jim's avatar
jim committed
1265 1266 1267 1268 1269
configuration file.  For example, to run the buildout in offline mode,
use buildout:offline=true.

Options and assignments can be interspersed.

alga's avatar
alga committed
1270
Commands:
jim's avatar
jim committed
1271 1272 1273 1274 1275 1276 1277

  install [parts]

    Install parts.  If no command arguments are given, then the parts
    definition from the configuration file is used.  Otherwise, the
    arguments specify the parts to be installed.

1278 1279 1280 1281 1282 1283
    Note that the semantics differ depending on whether any parts are
    specified.  If parts are specified, then only those parts will be
    installed. If no parts are specified, then the parts specified by
    the buildout parts option will be installed along with all of
    their dependencies.

jim's avatar
jim committed
1284 1285 1286 1287 1288 1289
  bootstrap

    Create a new buildout in the current working directory, copying
    the buildout and setuptools eggs and, creating a basic directory
    structure and a buildout-local buildout script.

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
  init

    Initialize a buildout, creating a buildout.cfg file if it doesn't
    exist and then performing the same actions as for the buildout
    command.

  setup script [setup command and options]

    Run a given setup script arranging that setuptools is in the
    script's path and and that it has been imported so that
    setuptools-provided commands (like bdist_egg) can be used even if
    the setup script doesn't import setuptools itself.

    The script can be given either as a script path or a path to a
    directory containing a setup.py script.
    
jim's avatar
jim committed
1306 1307 1308 1309 1310
"""
def _help():
    print _usage
    sys.exit(0)

1311 1312 1313 1314
def main(args=None):
    if args is None:
        args = sys.argv[1:]

jim's avatar
jim committed
1315 1316
    config_file = 'buildout.cfg'
    verbosity = 0
1317
    options = []
jim's avatar
jim committed
1318
    windows_restart = False
jim's avatar
jim committed
1319
    user_defaults = True
jim's avatar
jim committed
1320
    debug = False
jim's avatar
jim committed
1321 1322 1323 1324
    while args:
        if args[0][0] == '-':
            op = orig_op = args.pop(0)
            op = op[1:]
jim's avatar
jim committed
1325
            while op and op[0] in 'vqhWUoOnND':
jim's avatar
jim committed
1326 1327
                if op[0] == 'v':
                    verbosity += 10
jim's avatar
jim committed
1328
                elif op[0] == 'q':
jim's avatar
jim committed
1329
                    verbosity -= 10
jim's avatar
jim committed
1330 1331
                elif op[0] == 'W':
                    windows_restart = True
jim's avatar
jim committed
1332 1333
                elif op[0] == 'U':
                    user_defaults = False
jim's avatar
jim committed
1334 1335
                elif op[0] == 'o':
                    options.append(('buildout', 'offline', 'true'))
jim's avatar
jim committed
1336 1337 1338 1339 1340 1341
                elif op[0] == 'O':
                    options.append(('buildout', 'offline', 'false'))
                elif op[0] == 'n':
                    options.append(('buildout', 'newest', 'true'))
                elif op[0] == 'N':
                    options.append(('buildout', 'newest', 'false'))
jim's avatar
jim committed
1342 1343
                elif op[0] == 'D':
                    debug = True
jim's avatar
jim committed
1344 1345
                else:
                    _help()
jim's avatar
jim committed
1346
                op = op[1:]
1347
                
1348 1349
            if op[:1] in  ('c', 't'):
                op_ = op[:1]
jim's avatar
jim committed
1350
                op = op[1:]
1351 1352 1353 1354

                if op_ == 'c':
                    if op:
                        config_file = op
jim's avatar
jim committed
1355
                    else:
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
                        if args:
                            config_file = args.pop(0)
                        else:
                            _error("No file name specified for option", orig_op)
                elif op_ == 't':
                    try:
                        timeout = int(args.pop(0))
                    except IndexError:
                        _error("No timeout value specified for option", orig_op)
                    except ValueError:
                        _error("No timeout value must be numeric", orig_op)

                    import socket
                    print 'Setting socket time out to %d seconds' % timeout
                    socket.setdefaulttimeout(timeout)

jim's avatar
jim committed
1372
            elif op:
jim's avatar
jim committed
1373 1374
                if orig_op == '--help':
                    _help()
jim's avatar
jim committed
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
                _error("Invalid option", '-'+op[0])
        elif '=' in args[0]:
            option, value = args.pop(0).split('=', 1)
            if len(option.split(':')) != 2:
                _error('Invalid option:', option)
            section, option = option.split(':')
            options.append((section.strip(), option.strip(), value.strip()))
        else:
            # We've run out of command-line options and option assignnemnts
            # The rest should be commands, so we'll stop here
            break

    if verbosity:
        options.append(('buildout', 'verbosity', str(verbosity)))
1389 1390 1391

    if args:
        command = args.pop(0)
1392 1393 1394
        if command not in (
            'install', 'bootstrap', 'runsetup', 'setup', 'init',
            ):
1395 1396 1397 1398
            _error('invalid command:', command)
    else:
        command = 'install'

jim's avatar
jim committed
1399
    try:
1400
        try:
jim's avatar
jim committed
1401
            buildout = Buildout(config_file, options,
1402
                                user_defaults, windows_restart, command)
1403
            getattr(buildout, command)(args)
jim's avatar
jim committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
        except SystemExit:
            pass
        except Exception, v:
            _doing()
            if debug:
                exc_info = sys.exc_info()
                import pdb, traceback
                traceback.print_exception(*exc_info)
                sys.stderr.write('\nStarting pdb:\n')
                pdb.post_mortem(exc_info[2])
            else:
1415 1416 1417 1418
                if isinstance(v, (zc.buildout.UserError,
                                  distutils.errors.DistutilsError,
                                  )
                              ):
jim's avatar
jim committed
1419 1420 1421
                    _error(str(v))
                else:
                    _internal_error(v)
1422
            
jim's avatar
jim committed
1423
    finally:
1424
            logging.shutdown()
1425 1426 1427 1428 1429 1430

if sys.version_info[:2] < (2, 4):
    def reversed(iterable):
        result = list(iterable);
        result.reverse()
        return result