easy_install.py 34.5 KB
Newer Older
1
#############################################################################
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# 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.
#
##############################################################################
"""Python easy_install API

This module provides a high-level Python API for installing packages.
It doesn't install scripts.  It uses setuptools and requires it to be
installed.

$Id$
"""

23
import glob, logging, os, re, shutil, sys, tempfile, urlparse, zipimport
24 25 26 27 28
import distutils.errors
import pkg_resources
import setuptools.command.setopt
import setuptools.package_index
import setuptools.archive_util
29
import zc.buildout
30

31
_oprp = getattr(os.path, 'realpath', lambda path: path)
32
def realpath(path):
33
    return os.path.normcase(os.path.abspath(_oprp(path)))
34

35 36
default_index_url = os.environ.get(
    'buildout-testing-index-url',
37
    'http://pypi.python.org/simple',
38
    )
39

40 41
logger = logging.getLogger('zc.buildout.easy_install')

42 43
url_match = re.compile('[a-z0-9+.-]+://').match

44 45 46 47
setuptools_loc = pkg_resources.working_set.find(
    pkg_resources.Requirement.parse('setuptools')
    ).location

48 49
# Include buildout and setuptools eggs in paths
buildout_and_setuptools_path = [
50
    setuptools_loc,
51 52
    pkg_resources.working_set.find(
        pkg_resources.Requirement.parse('zc.buildout')).location,
53 54
    ]

Jim Fulton's avatar
Jim Fulton committed
55 56 57 58
class IncompatibleVersionError(zc.buildout.UserError):
    """A specified version is incompatible with a given requirement.
    """

59 60 61 62 63
_versions = {sys.executable: '%d.%d' % sys.version_info[:2]}
def _get_version(executable):
    try:
        return _versions[executable]
    except KeyError:
64
        i, o = os.popen4(_safe_arg(executable) + ' -V')
65 66 67 68 69
        i.close()
        version = o.read().strip()
        o.close()
        pystring, version = version.split()
        assert pystring == 'Python'
70
        version = re.match('(\d[.]\d)([.].*\d)?$', version).group(1)
71 72 73
        _versions[executable] = version
        return version

74 75 76 77 78 79 80
_indexes = {}
def _get_index(executable, index_url, find_links):
    key = executable, index_url, tuple(find_links)
    index = _indexes.get(key)
    if index is not None:
        return index

81 82 83
    if index_url is None:
        index_url = default_index_url

84 85 86
    index = setuptools.package_index.PackageIndex(
        index_url, python=_get_version(executable)
        )
87 88 89 90 91 92 93
        
    if find_links:
        index.add_find_links(find_links)

    _indexes[key] = index
    return index

94
clear_index_cache = _indexes.clear
Jim Fulton's avatar
Jim Fulton committed
95 96 97 98 99 100 101 102 103 104 105 106 107

if sys.platform == 'win32':
    # work around spawn lamosity on windows
    # XXX need safe quoting (see the subproces.list2cmdline) and test
    def _safe_arg(arg):
        return '"%s"' % arg
else:
    _safe_arg = str

_easy_install_cmd = _safe_arg(
    'from setuptools.command.easy_install import main; main()'
    )

108 109
class Installer:

110
    _versions = {}
111
    _download_cache = None
112
    _install_from_cache = False
113
    _prefer_final = True
114
    _use_dependency_links = True
Jim Fulton's avatar
Jim Fulton committed
115
    _allow_picked_versions = True
116
    
117 118 119 120 121 122 123 124
    def __init__(self,
                 dest=None,
                 links=(),
                 index=None,
                 executable=sys.executable,
                 always_unzip=False,
                 path=None,
                 newest=True,
Jim Fulton's avatar
Jim Fulton committed
125
                 versions=None,
126
                 use_dependency_links=None,
127 128
                 ):
        self._dest = dest
129 130 131 132 133 134 135

        if self._install_from_cache:
            if not self._download_cache:
                raise ValueError("install_from_cache set to true with no"
                                 " download cache")
            links = ()
            index = 'file://' + self._download_cache
136

137 138
        if use_dependency_links is not None:
            self._use_dependency_links = use_dependency_links
139
        self._links = links = list(_fix_file_links(links))
140 141 142
        if self._download_cache and (self._download_cache not in links):
            links.insert(0, self._download_cache)

143 144 145 146 147 148 149
        self._index_url = index
        self._executable = executable
        self._always_unzip = always_unzip
        path = (path and path[:] or []) + buildout_and_setuptools_path
        if dest is not None and dest not in path:
            path.insert(0, dest)
        self._path = path
150 151
        if self._dest is None:
            newest = False
152 153 154 155
        self._newest = newest
        self._env = pkg_resources.Environment(path,
                                              python=_get_version(executable))
        self._index = _get_index(executable, index, links)
156 157 158

        if versions is not None:
            self._versions = versions
159

160
    def _satisfied(self, req, source=None):
161 162
        dists = [dist for dist in self._env[req.project_name] if dist in req]
        if not dists:
163 164
            logger.debug('We have no distributions for %s that satisfies %r.',
                         req.project_name, str(req))
165
            return None, self._obtain(req, source)
166

167

168 169 170 171 172
        # Note that dists are sorted from best to worst, as promised by
        # env.__getitem__

        for dist in dists:
            if (dist.precedence == pkg_resources.DEVELOP_DIST):
173
                logger.debug('We have a develop egg: %s', dist)
174
                return dist, None
175

176 177 178 179 180 181
        # Special common case, we have a specification for a single version:
        specs = req.specs
        if len(specs) == 1 and specs[0][0] == '==':
            logger.debug('We have the distribution that satisfies %r.',
                         str(req))
            return dists[0], None
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196
        if self._prefer_final:
            fdists = [dist for dist in dists
                      if _final_version(dist.parsed_version)
                      ]
            if fdists:
                # There are final dists, so only use those
                dists = fdists

        if not self._newest:
            # We don't need the newest, so we'll use the newest one we
            # find, which is the first returned by
            # Environment.__getitem__.
            return dists[0], None

197
        best_we_have = dists[0] # Because dists are sorted from best to worst
Jim Fulton's avatar
Jim Fulton committed
198

199 200 201 202 203 204
        # We have some installed distros.  There might, theoretically, be
        # newer ones.  Let's find out which ones are available and see if
        # any are newer.  We only do this if we're willing to install
        # something, which is only true if dest is not None:
        
        if self._dest is not None:
205
            best_available = self._obtain(req, source)
206 207 208 209 210 211 212
        else:
            best_available = None

        if best_available is None:
            # That's a bit odd.  There aren't any distros available.
            # We should use the best one we have that meets the requirement.
            logger.debug(
213 214 215
                'There are no distros available that meet %r.\n'
                'Using our best, %s.',
                str(req), best_available)
216
            return best_we_have, None
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        if self._prefer_final:
            if _final_version(best_available.parsed_version):
                if _final_version(best_we_have.parsed_version):
                    if (best_we_have.parsed_version
                        <
                        best_available.parsed_version
                        ):
                        return None, best_available
                else:
                    return None, best_available
            else:
                if (not _final_version(best_we_have.parsed_version)
                    and
                    (best_we_have.parsed_version
                     <
                     best_available.parsed_version
                     )
                    ):
                    return None, best_available
        else:
            if (best_we_have.parsed_version
                <
                best_available.parsed_version
                ):
                return None, best_available
            
        logger.debug(
            'We have the best distribution that satisfies %r.',
            str(req))
        return best_we_have, None
248

249 250 251 252 253 254 255
    def _load_dist(self, dist):
        dists = pkg_resources.Environment(
            dist.location,
            python=_get_version(self._executable),
            )[dist.project_name]
        assert len(dists) == 1
        return dists[0]
256

257
    def _call_easy_install(self, spec, ws, dest, dist):
258

259 260 261
        tmp = tempfile.mkdtemp(dir=dest)
        try:
            path = self._get_dist(
Jim Fulton's avatar
Jim Fulton committed
262 263
                self._constrain(pkg_resources.Requirement.parse('setuptools')),
                ws, False,
264 265 266 267 268 269 270 271 272 273 274
                )[0].location

            args = ('-c', _easy_install_cmd, '-mUNxd', _safe_arg(tmp))
            if self._always_unzip:
                args += ('-Z', )
            level = logger.getEffectiveLevel()
            if level > 0:
                args += ('-q', )
            elif level < 0:
                args += ('-v', )

275
            args += (_safe_arg(spec), )
276 277 278 279 280 281 282 283

            if level <= logging.DEBUG:
                logger.debug('Running easy_install:\n%s "%s"\npath=%s\n',
                             self._executable, '" "'.join(args), path)

            args += (dict(os.environ, PYTHONPATH=path), )
            sys.stdout.flush() # We want any pending output first
            exit_code = os.spawnle(
284
                os.P_WAIT, self._executable, _safe_arg (self._executable),
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
                *args)

            dists = []
            env = pkg_resources.Environment(
                [tmp],
                python=_get_version(self._executable),
                )
            for project in env:
                dists.extend(env[project])
                
            if exit_code:
                logger.error(
                    "An error occured when trying to install %s."
                    "Look above this message for any errors that"
                    "were output by easy_install.",
                    dist)

            if not dists:
                raise zc.buildout.UserError("Couldn't install: %s" % dist)

            if len(dists) > 1:
                logger.warn("Installing %s\n"
                            "caused multiple distributions to be installed:\n"
                            "%s\n",
                            dist, '\n'.join(map(str, dists)))
            else:
                d = dists[0]
                if d.project_name != dist.project_name:
                    logger.warn("Installing %s\n"
                                "Caused installation of a distribution:\n"
                                "%s\n"
                                "with a different project name.",
                                dist, d)
                if d.version != dist.version:
                    logger.warn("Installing %s\n"
                                "Caused installation of a distribution:\n"
                                "%s\n"
                                "with a different version.",
                                dist, d)

            result = []
            for d in dists:
                newloc = os.path.join(dest, os.path.basename(d.location))
                if os.path.exists(newloc):
Jim Fulton's avatar
Jim Fulton committed
329
                    if os.path.isdir(newloc):
330 331 332 333 334 335 336 337 338 339 340 341 342
                        shutil.rmtree(newloc)
                    else:
                        os.remove(newloc)
                os.rename(d.location, newloc)

                [d] = pkg_resources.Environment(
                    [newloc],
                    python=_get_version(self._executable),
                    )[d.project_name]
                    
                result.append(d)

            return result
343

344 345 346 347
        finally:
            shutil.rmtree(tmp)
            
    def _obtain(self, requirement, source=None):
348 349

        # initialize out index for this project:
350 351
        index = self._index
        if index.obtain(requirement) is None:
352
            # Nothing is available.
353
            return None
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

        # Filter the available dists for the requirement and source flag
        dists = [dist for dist in index[requirement.project_name]
                 if ((dist in requirement)
                     and
                     ((not source) or
                      (dist.precedence == pkg_resources.SOURCE_DIST)
                      )
                     )
                 ]

        # If we prefer final dists, filter for final and use the
        # result if it is non empty.
        if self._prefer_final:
            fdists = [dist for dist in dists
                      if _final_version(dist.parsed_version)
                      ]
            if fdists:
                # There are final dists, so only use those
                dists = fdists

        # Now find the best one:
376 377
        best = []
        bestv = ()
378
        for dist in dists:
379 380 381 382 383 384 385 386 387
            distv = dist.parsed_version
            if distv > bestv:
                best = [dist]
                bestv = distv
            elif distv == bestv:
                best.append(dist)

        if not best:
            return None
388

389 390 391 392 393
        if len(best) == 1:
            return best[0]
        
        if self._download_cache:
            for dist in best:
394 395
                if (realpath(os.path.dirname(dist.location))
                    ==
396
                    self._download_cache
397
                    ):
398
                    return dist
399

400 401
        best.sort()
        return best[-1]
402

Jim Fulton's avatar
Jim Fulton committed
403 404
    def _fetch(self, dist, tmp, download_cache):
        if (download_cache
405
            and (realpath(os.path.dirname(dist.location)) == download_cache)
Jim Fulton's avatar
Jim Fulton committed
406 407 408 409 410
            ):
            return dist

        new_location = self._index.download(dist.location, tmp)
        if (download_cache
411
            and (realpath(new_location) == realpath(dist.location))
Jim Fulton's avatar
Jim Fulton committed
412 413 414 415 416 417 418 419
            and os.path.isfile(new_location)
            ):
            # setuptools avoids making extra copies, but we want to copy
            # to the download cache
            shutil.copy2(new_location, tmp)
            new_location = os.path.join(tmp, os.path.basename(new_location))
            
        return dist.clone(location=new_location)
420 421 422

    def _get_dist(self, requirement, ws, always_unzip):

423
        __doing__ = 'Getting distribution for %r.', str(requirement)
424

425 426
        # Maybe an existing dist is already the best dist that satisfies the
        # requirement
427
        dist, avail = self._satisfied(requirement)
428 429 430

        if dist is None:
            if self._dest is not None:
431
                logger.info(*__doing__)
432

433 434
            # Retrieve the dist:
            if avail is None:
435
                raise MissingDistribution(requirement, ws)
436

437 438 439
            # We may overwrite distributions, so clear importer
            # cache.
            sys.path_importer_cache.clear()
440

441
            tmp = self._download_cache
Jim Fulton's avatar
Jim Fulton committed
442 443 444
            if tmp is None:
                tmp = tempfile.mkdtemp('get_dist')

445
            try:
Jim Fulton's avatar
Jim Fulton committed
446
                dist = self._fetch(avail, tmp, self._download_cache)
447

448 449 450
                if dist is None:
                    raise zc.buildout.UserError(
                        "Couln't download distribution %s." % avail)
451

452 453
                if dist.precedence == pkg_resources.EGG_DIST:
                    # It's already an egg, just fetch it into the dest
454

455 456
                    newloc = os.path.join(
                        self._dest, os.path.basename(dist.location))
457

458 459 460 461 462
                    if os.path.isdir(dist.location):
                        # we got a directory. It must have been
                        # obtained locally.  Just copy it.
                        shutil.copytree(dist.location, newloc)
                    else:
463

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
                        if self._always_unzip:
                            should_unzip = True
                        else:
                            metadata = pkg_resources.EggMetadata(
                                zipimport.zipimporter(dist.location)
                                )
                            should_unzip = (
                                metadata.has_metadata('not-zip-safe')
                                or
                                not metadata.has_metadata('zip-safe')
                                )

                        if should_unzip:
                            setuptools.archive_util.unpack_archive(
                                dist.location, newloc)
                        else:
                            shutil.copyfile(dist.location, newloc)

                    # Getting the dist from the environment causes the
                    # distribution meta data to be read.  Cloning isn't
                    # good enough.
                    dists = pkg_resources.Environment(
                        [newloc],
                        python=_get_version(self._executable),
                        )[dist.project_name]
                else:
                    # It's some other kind of dist.  We'll let easy_install
                    # deal with it:
                    dists = self._call_easy_install(
                        dist.location, ws, self._dest, dist)
494

495 496 497
            finally:
                if tmp != self._download_cache:
                    shutil.rmtree(tmp)
498

499 500
            self._env.scan([self._dest])
            dist = self._env.best_match(requirement, ws)
501
            logger.info("Got %s.", dist)            
502

503 504
        else:
            dists = [dist]
505

506
        for dist in dists:
507 508
            if (dist.has_metadata('dependency_links.txt')
                and not self._install_from_cache
509
                and self._use_dependency_links
510
                ):
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
                for link in dist.get_metadata_lines('dependency_links.txt'):
                    link = link.strip()
                    if link not in self._links:
                        logger.debug('Adding find link %r from %s', link, dist)
                        self._links.append(link)
                        self._index = _get_index(self._executable,
                                                 self._index_url, self._links)

        for dist in dists:
            # Check whether we picked a version and, if we did, report it:
            if not (
                dist.precedence == pkg_resources.DEVELOP_DIST
                or
                (len(requirement.specs) == 1
                 and
                 requirement.specs[0][0] == '==')
                ):
528 529
                logger.debug('Picked: %s = %s',
                             dist.project_name, dist.version)
Jim Fulton's avatar
Jim Fulton committed
530 531 532 533
                if not self._allow_picked_versions:
                    raise zc.buildout.UserError(
                        'Picked: %s = %s' % (dist.project_name, dist.version)
                        )
534 535

        return dists
536 537 538 539 540 541 542 543 544 545

    def _maybe_add_setuptools(self, ws, dist):
        if dist.has_metadata('namespace_packages.txt'):
            for r in dist.requires():
                if r.project_name == 'setuptools':
                    break
            else:
                # We have a namespace package but no requirement for setuptools
                if dist.precedence == pkg_resources.DEVELOP_DIST:
                    logger.warn(
546
                        "Develop distribution: %s\n"
547 548 549
                        "uses namespace packages but the distribution "
                        "does not require setuptools.",
                        dist)
Jim Fulton's avatar
Jim Fulton committed
550 551 552
                requirement = self._constrain(
                    pkg_resources.Requirement.parse('setuptools')
                    )
553
                if ws.find(requirement) is None:
554 555
                    for dist in self._get_dist(requirement, ws, False):
                        ws.add(dist)
556 557


Jim Fulton's avatar
Jim Fulton committed
558 559 560 561 562
    def _constrain(self, requirement):
        version = self._versions.get(requirement.project_name)
        if version:
            if version not in requirement:
                logger.error("The version, %s, is not consistent with the "
563
                             "requirement, %r.", version, str(requirement))
Jim Fulton's avatar
Jim Fulton committed
564 565 566 567 568 569 570
                raise IncompatibleVersionError("Bad version", version)
            
            requirement = pkg_resources.Requirement.parse(
                "%s ==%s" % (requirement.project_name, version))

        return requirement

571 572
    def install(self, specs, working_set=None):

573
        logger.debug('Installing %s.', repr(specs)[1:-1])
574 575 576 577 578 579

        path = self._path
        dest = self._dest
        if dest is not None and dest not in path:
            path.insert(0, dest)

Jim Fulton's avatar
Jim Fulton committed
580
        requirements = [self._constrain(pkg_resources.Requirement.parse(spec))
581 582
                        for spec in specs]

Jim Fulton's avatar
Jim Fulton committed
583 584
        

585 586
        if working_set is None:
            ws = pkg_resources.WorkingSet([])
Jim Fulton's avatar
Jim Fulton committed
587
        else:
588
            ws = working_set
589

590
        for requirement in requirements:
591 592 593
            for dist in self._get_dist(requirement, ws, self._always_unzip):
                ws.add(dist)
                self._maybe_add_setuptools(ws, dist)
594 595 596 597 598 599

        # OK, we have the requested distributions and they're in the working
        # set, but they may have unmet requirements.  We'll simply keep
        # trying to resolve requirements, adding missing requirements as they
        # are reported.
        #
600 601 602
        # Note that we don't pass in the environment, because we want
        # to look for new eggs unless what we have is the best that
        # matches the requirement.
603 604 605 606 607
        while 1:
            try:
                ws.resolve(requirements)
            except pkg_resources.DistributionNotFound, err:
                [requirement] = err
Jim Fulton's avatar
Jim Fulton committed
608
                requirement = self._constrain(requirement)
609
                if dest:
610 611 612 613 614
                    logger.debug('Getting required %r', str(requirement))
                else:
                    logger.debug('Adding required %r', str(requirement))
                _log_requirement(ws, requirement)
                    
615 616
                for dist in self._get_dist(requirement, ws, self._always_unzip
                                           ):
617
                        
618 619
                    ws.add(dist)
                    self._maybe_add_setuptools(ws, dist)
620 621
            except pkg_resources.VersionConflict, err:
                raise VersionConflict(err, ws)
622 623
            else:
                break
624

625
        return ws
626

627
    def build(self, spec, build_ext):
628

Jim Fulton's avatar
Jim Fulton committed
629
        requirement = self._constrain(pkg_resources.Requirement.parse(spec))
630

631
        dist, avail = self._satisfied(requirement, 1)
632
        if dist is not None:
633
            return [dist.location]
634

635 636 637
        # Retrieve the dist:
        if avail is None:
            raise zc.buildout.UserError(
638 639
                "Couldn't find a source distribution for %r."
                % str(requirement))
640 641

        logger.debug('Building %r', spec)
642

643
        tmp = self._download_cache
Jim Fulton's avatar
Jim Fulton committed
644 645 646
        if tmp is None:
            tmp = tempfile.mkdtemp('get_dist')

647
        try:
Jim Fulton's avatar
Jim Fulton committed
648
            dist = self._fetch(avail, tmp, self._download_cache)
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

            build_tmp = tempfile.mkdtemp('build')
            try:
                setuptools.archive_util.unpack_archive(dist.location,
                                                       build_tmp)
                if os.path.exists(os.path.join(build_tmp, 'setup.py')):
                    base = build_tmp
                else:
                    setups = glob.glob(
                        os.path.join(build_tmp, '*', 'setup.py'))
                    if not setups:
                        raise distutils.errors.DistutilsError(
                            "Couldn't find a setup script in %s"
                            % os.path.basename(dist.location)
                            )
                    if len(setups) > 1:
                        raise distutils.errors.DistutilsError(
                            "Multiple setup scripts in %s"
                            % os.path.basename(dist.location)
                            )
                    base = os.path.dirname(setups[0])
            
                setup_cfg = os.path.join(base, 'setup.cfg')
                if not os.path.exists(setup_cfg):
                    f = open(setup_cfg, 'w')
                    f.close()
                setuptools.command.setopt.edit_config(
                    setup_cfg, dict(build_ext=build_ext))

                dists = self._call_easy_install(
                    base, pkg_resources.WorkingSet(),
                    self._dest, dist)

                return [dist.location for dist in dists]
            finally:
                shutil.rmtree(build_tmp)
685

686
        finally:
687 688 689
            if tmp != self._download_cache:
                shutil.rmtree(tmp)

690 691 692 693 694
def default_versions(versions=None):
    old = Installer._versions
    if versions is not None:
        Installer._versions = versions
    return old
695

696 697 698
def download_cache(path=-1):
    old = Installer._download_cache
    if path != -1:
699
        if path:
700
            path = realpath(path)
701 702 703
        Installer._download_cache = path
    return old

704 705 706 707 708 709
def install_from_cache(setting=None):
    old = Installer._install_from_cache
    if setting is not None:
        Installer._install_from_cache = bool(setting)
    return old

710 711 712 713 714 715
def prefer_final(setting=None):
    old = Installer._prefer_final
    if setting is not None:
        Installer._prefer_final = bool(setting)
    return old

716 717 718 719 720 721
def use_dependency_links(setting=None):
    old = Installer._use_dependency_links
    if setting is not None:
        Installer._use_dependency_links = bool(setting)
    return old

Jim Fulton's avatar
Jim Fulton committed
722 723 724 725 726 727
def allow_picked_versions(setting=None):
    old = Installer._allow_picked_versions
    if setting is not None:
        Installer._allow_picked_versions = bool(setting)
    return old

728 729 730
def install(specs, dest,
            links=(), index=None,
            executable=sys.executable, always_unzip=False,
731 732
            path=None, working_set=None, newest=True, versions=None,
            use_dependency_links=None):
733
    installer = Installer(dest, links, index, executable, always_unzip, path,
734
                          newest, versions, use_dependency_links)
735 736 737 738 739 740
    return installer.install(specs, working_set)


def build(spec, dest, build_ext,
          links=(), index=None,
          executable=sys.executable,
Jim Fulton's avatar
Jim Fulton committed
741 742 743
          path=None, newest=True, versions=None):
    installer = Installer(dest, links, index, executable, True, path, newest,
                          versions)
744 745
    return installer.build(spec, build_ext)

746
        
747

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
def _rm(*paths):
    for path in paths:
        if os.path.isdir(path):
            shutil.rmtree(path)
        elif os.path.exists(path):
            os.remove(path)

def _copyeggs(src, dest, suffix, undo):
    result = []
    undo.append(lambda : _rm(*result))
    for name in os.listdir(src):
        if name.endswith(suffix):
            new = os.path.join(dest, name)
            _rm(new)
            os.rename(os.path.join(src, name), new)
            result.append(new)
764 765
        
    assert len(result) == 1, str(result)
766 767 768 769 770 771 772 773 774 775 776 777 778 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 806 807 808 809 810 811 812 813 814 815 816 817
    undo.pop()
    
    return result[0]

def develop(setup, dest,
            build_ext=None,
            executable=sys.executable):

    if os.path.isdir(setup):
        directory = setup
        setup = os.path.join(directory, 'setup.py')
    else:
        directory = os.path.dirname(setup)
        
    undo = []
    try:
        if build_ext:
            setup_cfg = os.path.join(directory, 'setup.cfg')
            if os.path.exists(setup_cfg):
                os.rename(setup_cfg, setup_cfg+'-develop-aside')
                def restore_old_setup():
                    if os.path.exists(setup_cfg):
                        os.remove(setup_cfg)
                    os.rename(setup_cfg+'-develop-aside', setup_cfg)
                undo.append(restore_old_setup)
            else:
                open(setup_cfg, 'w')
                undo.append(lambda: os.remove(setup_cfg))
            setuptools.command.setopt.edit_config(
                setup_cfg, dict(build_ext=build_ext))

        fd, tsetup = tempfile.mkstemp()
        undo.append(lambda: os.remove(tsetup))
        undo.append(lambda: os.close(fd))

        os.write(fd, runsetup_template % dict(
            setuptools=setuptools_loc,
            setupdir=directory,
            setup=setup,
            __file__ = setup,
            ))

        tmp3 = tempfile.mkdtemp('build', dir=dest)
        undo.append(lambda : shutil.rmtree(tmp3)) 

        args = [
            zc.buildout.easy_install._safe_arg(tsetup),
            '-q', 'develop', '-mxN',
            '-d', _safe_arg(tmp3),
            ]

        log_level = logger.getEffectiveLevel()
818 819
        if log_level <= 0:
            if log_level == 0:
820 821 822
                del args[1]
            else:
                args[1] == '-v'
823
        if log_level < logging.DEBUG:
824
            logger.debug("in: %r\n%s", directory, ' '.join(args))
825

826
        assert os.spawnl(os.P_WAIT, executable, _safe_arg (executable), *args) == 0
827 828 829 830 831 832 833 834

        return _copyeggs(tmp3, dest, '.egg-link', undo)

    finally:
        undo.reverse()
        [f() for f in undo]
            
            
835 836 837
def working_set(specs, executable, path):
    return install(specs, None, executable=executable, path=path)

838 839 840 841
def scripts(reqs, working_set, executable, dest,
            scripts=None,
            extra_paths=(),
            arguments='',
842
            interpreter=None,
843
            initialization='',
844
            ):
845
    
846 847
    path = [dist.location for dist in working_set]
    path.extend(extra_paths)
Jim Fulton's avatar
Jim Fulton committed
848
    path = repr(path)[1:-1].replace(', ', ',\n  ')
849 850
    generated = []

851 852 853 854
    if isinstance(reqs, str):
        raise TypeError('Expected iterable of requirements or entry points,'
                        ' got string.')

855 856 857
    if initialization:
        initialization = '\n'+initialization+'\n'

858 859 860 861 862
    entry_points = []
    for req in reqs:
        if isinstance(req, str):
            req = pkg_resources.Requirement.parse(req)
            dist = working_set.find(req)
863
            for name in pkg_resources.get_entry_map(dist, 'console_scripts'):
864 865
                entry_point = dist.get_entry_info('console_scripts', name)
                entry_points.append(
866 867
                    (name, entry_point.module_name,
                     '.'.join(entry_point.attrs))
Jim Fulton's avatar
Jim Fulton committed
868
                    )
869 870 871 872 873 874 875 876 877 878
        else:
            entry_points.append(req)
                
    for name, module_name, attrs in entry_points:
        if scripts is not None:
            sname = scripts.get(name)
            if sname is None:
                continue
        else:
            sname = name
879

880 881
        sname = os.path.join(dest, sname)
        generated.extend(
882 883
            _script(module_name, attrs, path, sname, executable, arguments,
                    initialization)
884
            )
885

886 887 888
    if interpreter:
        sname = os.path.join(dest, interpreter)
        generated.extend(_pyscript(path, sname, executable))
889 890 891

    return generated

892 893
def _script(module_name, attrs, path, dest, executable, arguments,
            initialization):
Jim Fulton's avatar
Jim Fulton committed
894
    generated = []
895
    script = dest
Jim Fulton's avatar
Jim Fulton committed
896 897
    if sys.platform == 'win32':
        dest += '-script.py'
898

899
    contents = script_template % dict(
's avatar
committed
900
        python = _safe_arg(executable),
901
        path = path,
902 903
        module_name = module_name,
        attrs = attrs,
904
        arguments = arguments,
905
        initialization = initialization,
906 907 908 909 910
        )
    changed = not (os.path.exists(dest) and open(dest).read() == contents)

    if sys.platform == 'win32':
        # generate exe file and give the script a magic name:
911 912
        exe = script+'.exe'
        open(exe, 'wb').write(
913 914
            pkg_resources.resource_string('setuptools', 'cli.exe')
            )
915
        generated.append(exe)
916 917 918 919 920 921 922 923 924 925
        
    if changed:
        open(dest, 'w').write(contents)
        logger.info("Generated script %r.", script)

        try:
            os.chmod(dest, 0755)
        except (AttributeError, os.error):
            pass
        
Jim Fulton's avatar
Jim Fulton committed
926 927
    generated.append(dest)
    return generated
928 929 930 931 932 933

script_template = '''\
#!%(python)s

import sys
sys.path[0:0] = [
934
  %(path)s,
935
  ]
936
%(initialization)s
937 938 939
import %(module_name)s

if __name__ == '__main__':
940
    %(module_name)s.%(attrs)s(%(arguments)s)
941 942 943 944
'''


def _pyscript(path, dest, executable):
Jim Fulton's avatar
Jim Fulton committed
945
    generated = []
946
    script = dest
947 948 949 950
    if sys.platform == 'win32':
        dest += '-script.py'

    contents = py_script_template % dict(
's avatar
committed
951
        python = _safe_arg(executable),
952 953 954 955
        path = path,
        )
    changed = not (os.path.exists(dest) and open(dest).read() == contents)

Jim Fulton's avatar
Jim Fulton committed
956 957
    if sys.platform == 'win32':
        # generate exe file and give the script a magic name:
958 959
        exe = script + '.exe'
        open(exe, 'wb').write(
Jim Fulton's avatar
Jim Fulton committed
960 961
            pkg_resources.resource_string('setuptools', 'cli.exe')
            )
962
        generated.append(exe)
Jim Fulton's avatar
Jim Fulton committed
963

964 965 966 967 968 969 970 971
    if changed:
        open(dest, 'w').write(contents)
        try:
            os.chmod(dest,0755)
        except (AttributeError, os.error):
            pass
        logger.info("Generated interpreter %r.", script)

Jim Fulton's avatar
Jim Fulton committed
972 973
    generated.append(dest)
    return generated
974 975

py_script_template = '''\
976
#!%(python)s
977
import sys
978
    
979
sys.path[0:0] = [
980
  %(path)s,
981
  ]
982

Jim Fulton's avatar
Jim Fulton committed
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
_interactive = True
if len(sys.argv) > 1:
    import getopt
    _options, _args = getopt.getopt(sys.argv[1:], 'ic:')
    _interactive = False
    for (_opt, _val) in _options:
        if _opt == '-i':
            _interactive = True
        elif _opt == '-c':
            exec _val
            
    if _args:
        sys.argv[:] = _args
        execfile(sys.argv[0])

if _interactive:
    import code
    code.interact(banner="", local=globals())
1001
'''
1002 1003 1004
        
runsetup_template = """
import sys
1005
sys.path.insert(0, %(setupdir)r)
1006 1007
sys.path.insert(0, %(setuptools)r)
import os, setuptools
Jim Fulton's avatar
Jim Fulton committed
1008

1009
__file__ = %(__file__)r
Jim Fulton's avatar
Jim Fulton committed
1010

1011 1012 1013 1014
os.chdir(%(setupdir)r)
sys.argv[0] = %(setup)r
execfile(%(setup)r)
"""
1015 1016 1017 1018

class VersionConflict(zc.buildout.UserError):

    def __init__(self, err, ws):
1019 1020
        ws = list(ws)
        ws.sort()
1021 1022 1023 1024 1025 1026 1027 1028 1029
        self.err, self.ws = err, ws

    def __str__(self):
        existing_dist, req = self.err
        result = ["There is a version conflict.",
                  "We already have: %s" % existing_dist,
                  ]
        for dist in self.ws:
            if req in dist.requires():
1030
                result.append("but %s requires %r." % (dist, str(req)))
1031 1032 1033 1034 1035
        return '\n'.join(result)

class MissingDistribution(zc.buildout.UserError):

    def __init__(self, req, ws):
1036 1037
        ws = list(ws)
        ws.sort()
1038 1039 1040 1041
        self.data = req, ws

    def __str__(self):
        req, ws = self.data
1042
        return "Couldn't find a distribution for %r." % str(req)
1043

1044 1045 1046
def _log_requirement(ws, req):
    ws = list(ws)
    ws.sort()
1047
    for dist in ws:
1048 1049 1050
        if req in dist.requires():
            logger.debug("  required by %s." % dist)
    
1051 1052 1053 1054 1055 1056 1057
def _fix_file_links(links):
    for link in links:
        if link.startswith('file://') and link[-1] != '/':
            if os.path.isdir(link[7:]):
                # work around excessive restriction in setuptools:
                link += '/'
        yield link
1058

1059 1060 1061 1062 1063 1064
_final_parts = '*final-', '*final'
def _final_version(parsed_version):
    for part in parsed_version:
        if (part[:1] == '*') and (part not in _final_parts):
            return False
    return True