Skip to content

Commit

Permalink
Problem: buildout script not sorted
Browse files Browse the repository at this point in the history
Solution: sort accordingly
  • Loading branch information
gotcha committed May 24, 2020
1 parent 86818e5 commit e3ee34e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 37 deletions.
18 changes: 16 additions & 2 deletions src/zc/buildout/buildout.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,16 @@ def bootstrap(self, args):
ws = pkg_resources.WorkingSet(entries)
ws.require('zc.buildout')
options = self['buildout']
eggs_dir = options['eggs-directory']
develop_eggs_dir = options['develop-eggs-directory']
ws = zc.buildout.easy_install.sort_working_set(
ws,
eggs_dir=eggs_dir,
develop_eggs_dir=develop_eggs_dir
)
zc.buildout.easy_install.scripts(
['zc.buildout'], ws, sys.executable,
self['buildout']['bin-directory'],
options['bin-directory'],
relative_paths = (
bool_option(options, 'relative-paths', False)
and options['directory']
Expand Down Expand Up @@ -1147,9 +1154,16 @@ def _maybe_upgrade(self):
# the new dist is different, so we've upgraded.
# Update the scripts and return True
options = self['buildout']
eggs_dir = options['eggs-directory']
develop_eggs_dir = options['develop-eggs-directory']
ws = zc.buildout.easy_install.sort_working_set(
ws,
eggs_dir=eggs_dir,
develop_eggs_dir=develop_eggs_dir
)
zc.buildout.easy_install.scripts(
['zc.buildout'], ws, sys.executable,
self['buildout']['bin-directory'],
options['bin-directory'],
relative_paths = (
bool_option(options, 'relative-paths', False)
and options['directory']
Expand Down
34 changes: 27 additions & 7 deletions src/zc/buildout/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,19 +1141,14 @@ def scripts(reqs, working_set, executable, dest=None,
):
assert executable == sys.executable, (executable, sys.executable)

# ensure eggs are inserted before ``site-packages`` in ``sys.path``.
# TODO ensure develop-eggs are also inserted before ``site-packages``.
dists = [dist for dist in working_set]
dists.sort(key=lambda dist: (-dist.precedence, dist.project_name))

path = [dist.location for dist in dists]
path = [dist.location for dist in working_set]
path.extend(extra_paths)
# order preserving unique
unique_path = []
for p in path:
if p not in unique_path:
unique_path.append(p)
path = list(map(realpath, unique_path))
path = [realpath(p) for p in unique_path]

generated = []

Expand Down Expand Up @@ -1929,3 +1924,28 @@ def _move_to_eggs_dir_and_compile(dist, dest):
if installed_with_pip:
newdist.precedence = pkg_resources.EGG_DIST
return newdist


def sort_working_set(ws, eggs_dir, develop_eggs_dir):
develop_paths = set()
pattern = os.path.join(develop_eggs_dir, '*.egg-link')
for egg_link in glob.glob(pattern):
with open(egg_link, 'rt') as f:
path = f.readline().strip()
if path:
develop_paths.add(path)

sorted_paths = []
egg_paths = []
other_paths = []
for dist in ws:
path = dist.location
if path in develop_paths:
sorted_paths.append(path)
elif os.path.commonprefix([path, eggs_dir]) == eggs_dir:
egg_paths.append(path)
else:
other_paths.append(path)
sorted_paths.extend(egg_paths)
sorted_paths.extend(other_paths)
return pkg_resources.WorkingSet(sorted_paths)
4 changes: 2 additions & 2 deletions src/zc/buildout/easy_install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,8 @@ to pass a common base directory of the scripts and eggs:
<BLANKLINE>
import sys
sys.path[0:0] = [
join(base, 'eggs/demo-0.3-pyN.N.egg'),
join(base, 'eggs/demoneeded-1.1-pyN.N.egg'),
join(base, 'eggs/demo-0.3-pyN.N.egg'),
'/ba',
join(base, 'bar'),
base,
Expand Down Expand Up @@ -954,8 +954,8 @@ We specified an interpreter and its paths are adjusted too:
import sys
<BLANKLINE>
sys.path[0:0] = [
join(base, 'eggs/demo-0.3-pyN.N.egg'),
join(base, 'eggs/demoneeded-1.1-pyN.N.egg'),
join(base, 'eggs/demo-0.3-pyN.N.egg'),
'/ba',
join(base, 'bar'),
base,
Expand Down
29 changes: 3 additions & 26 deletions zc.recipe.egg_/src/zc/recipe/egg/egg.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,6 @@ def install(self):

update = install

def _sort_working_set(self, ws):
develop_paths = set()
pattern = os.path.join(self.options['develop-eggs-directory'], '*.egg-link')
for egg_link in glob.glob(pattern):
with open(egg_link, 'rt') as f:
path = f.readline().strip()
if path:
develop_paths.add(path)

egg_directory = os.path.join(self.options['eggs-directory'], '')
sorted_paths = []
egg_paths = []
other_paths = []
for dist in ws:
path = dist.location
if path in develop_paths:
sorted_paths.append(path)
elif os.path.commonprefix([path, egg_directory]) == egg_directory:
egg_paths.append(path)
else:
other_paths.append(path)
sorted_paths.extend(egg_paths)
sorted_paths.extend(other_paths)
return pkg_resources.WorkingSet(sorted_paths)

def _working_set(
self,
distributions,
Expand Down Expand Up @@ -166,7 +141,9 @@ def _working_set(
newest=newest,
allow_hosts=allow_hosts,
allow_unknown_extras=allow_unknown_extras)
ws = self._sort_working_set(ws)
ws = zc.buildout.easy_install.sort_working_set(
ws, eggs_dir, develop_eggs_dir
)
cache_storage[cache_key] = ws

# `pkg_resources.WorkingSet` instances are mutable, so we need to return
Expand Down

0 comments on commit e3ee34e

Please sign in to comment.