Commit f0b0aa45 authored by Jaap Roes's avatar Jaap Roes

Refactored picked versions logic to make it easier to use for plugins

This includes renaming `store_picked_versions` to `store_required_by`
to better signify its intent and adding `get_picked_versions`
to `zc.buidlout.easy_install`.
parent 284e2470
......@@ -120,6 +120,24 @@ def _unannotate(data):
data[key] = _unannotate_section(data[key])
return data
def _format_picked_versions(picked_versions, required_by):
output = ['[versions]']
required_output = []
for dist_, version in picked_versions:
if dist_ in required_by:
required_output.append('')
required_output.append('# Required by:')
for req_ in sorted(required_by[dist_]):
required_output.append('# '+req_)
target = required_output
else:
target = output
target.append("%s = %s" % (dist_, version))
output.extend(required_output)
return '\n'.join(output)
_buildout_default_options = _annotate_section({
'allow-hosts': '*',
'allow-picked-versions': 'true',
......@@ -323,8 +341,8 @@ class Buildout(DictMixin):
self.show_picked_versions = bool_option(options,
'show-picked-versions')
self.update_versions_file = options['update-versions-file']
zc.buildout.easy_install.store_picked_versions(
self.show_picked_versions or self.update_versions_file)
zc.buildout.easy_install.store_required_by(self.show_picked_versions or
self.update_versions_file)
download_cache = options.get('download-cache')
if download_cache:
......@@ -637,7 +655,8 @@ class Buildout(DictMixin):
elif (not installed_parts) and installed_exists:
os.remove(self['buildout']['installed'])
self._print_picked_versions()
if self.show_picked_versions or self.update_versions_file:
self._print_picked_versions()
self._unload_extensions()
def _update_installed(self, **buildout_options):
......@@ -987,40 +1006,27 @@ class Buildout(DictMixin):
ep.load()(self)
def _print_picked_versions(self):
Installer = zc.buildout.easy_install.Installer
if not Installer._picked_versions:
picked_versions, required_by = (zc.buildout.easy_install
.get_picked_versions())
if not picked_versions:
# Don't print empty output.
return
output = ['[versions]']
required_output = []
for dist_, version in sorted(Installer._picked_versions.items()):
if dist_ in Installer._required_by:
required_output.append('')
required_output.append('# Required by:')
for req_ in sorted(Installer._required_by[dist_]):
required_output.append('# '+req_)
target = required_output
else:
target = output
target.append("%s = %s" % (dist_, version))
output.extend(required_output)
output = _format_picked_versions(picked_versions, required_by)
if self.show_picked_versions:
print_("Versions had to be automatically picked.")
print_("The following part definition lists the versions picked:")
print_('\n'.join(output))
print_(output)
if self.update_versions_file:
# Write to the versions file.
if os.path.exists(self.update_versions_file):
output[:1] = [
'',
'# Added by buildout at %s' % datetime.datetime.now(),
]
output.append('')
output += ('\n# Added by buildout at %s' %
datetime.datetime.now())
output += '\n'
f = open(self.update_versions_file, 'a')
f.write('\n'.join(output))
f.write(output)
f.close()
print_("Picked versions have been written to " +
self.update_versions_file)
......
......@@ -150,7 +150,7 @@ class Installer:
_prefer_final = True
_use_dependency_links = True
_allow_picked_versions = True
_store_picked_versions = False
_store_required_by = False
def __init__(self,
dest=None,
......@@ -755,12 +755,17 @@ def allow_picked_versions(setting=None):
Installer._allow_picked_versions = bool(setting)
return old
def store_picked_versions(setting=None):
old = Installer._store_picked_versions
def store_required_by(setting=None):
old = Installer._store_required_by
if setting is not None:
Installer._store_picked_versions = bool(setting)
Installer._store_required_by = bool(setting)
return old
def get_picked_versions():
picked_versions = sorted(Installer._picked_versions.items())
required_by = Installer._required_by
return (picked_versions, required_by)
def install(specs, dest,
links=(), index=None,
......@@ -1283,7 +1288,7 @@ class MissingDistribution(zc.buildout.UserError):
def _log_requirement(ws, req):
if (not logger.isEnabledFor(logging.DEBUG) and
not Installer._store_picked_versions):
not Installer._store_required_by):
# Sorting the working set and iterating over it's requirements
# is expensive, so short circuit the work if it won't even be
# logged. When profiling a simple buildout with 10 parts with
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment