2 # Copyright 2009-2013 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
5 # unicode_literals for compat with TextIOWrapper in Python 2
6 from __future__ import print_function, unicode_literals
11 # This block ensures that ^C interrupts are handled quietly.
14 def exithandler(signum,frame):
15 signal.signal(signal.SIGINT, signal.SIG_IGN)
16 signal.signal(signal.SIGTERM, signal.SIG_IGN)
17 sys.exit(128 + signum)
19 signal.signal(signal.SIGINT, exithandler)
20 signal.signal(signal.SIGTERM, exithandler)
22 except KeyboardInterrupt:
23 sys.exit(128 + signal.SIGINT)
25 def debug_signal(signum, frame):
29 if platform.python_implementation() == 'Jython':
30 debug_signum = signal.SIGUSR2 # bug #424259
32 debug_signum = signal.SIGUSR1
34 signal.signal(debug_signum, debug_signal)
44 from os import path as osp
45 pym_path = osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym")
46 sys.path.insert(0, pym_path)
48 portage._internal_caller = True
49 from portage import os, _encodings, _unicode_encode, _unicode_decode
50 from _emerge.MetadataRegen import MetadataRegen
51 from portage.cache.cache_errors import CacheError, StatCollision
52 from portage.manifest import guessManifestFileType
53 from portage.package.ebuild._parallel_manifest.ManifestScheduler import ManifestScheduler
54 from portage.util import cmp_sort_key, writemsg_level
55 from portage.util._async.run_main_scheduler import run_main_scheduler
56 from portage.util._eventloop.global_event_loop import global_event_loop
57 from portage import cpv_getkey
58 from portage.dep import Atom, isjustname
59 from portage.versions import pkgsplit, vercmp
62 from xml.etree import ElementTree
67 from xml.parsers.expat import ExpatError
71 from repoman.utilities import parse_metadata_use
73 from repoman.utilities import FindVCS
75 if sys.hexversion >= 0x3000000:
79 usage = "egencache [options] <action> ... [atom] ..."
80 parser = optparse.OptionParser(usage=usage)
82 actions = optparse.OptionGroup(parser, 'Actions')
83 actions.add_option("--update",
85 help="update metadata/md5-cache/ (generate as necessary)")
86 actions.add_option("--update-use-local-desc",
88 help="update the use.local.desc file from metadata.xml")
89 actions.add_option("--update-changelogs",
91 help="update the ChangeLog files from SCM logs")
92 actions.add_option("--update-manifests",
94 help="update manifests")
95 parser.add_option_group(actions)
97 common = optparse.OptionGroup(parser, 'Common options')
98 common.add_option("--repo",
100 help="name of repo to operate on")
101 common.add_option("--config-root",
102 help="location of portage config files",
103 dest="portage_configroot")
104 common.add_option("--gpg-dir",
105 help="override the PORTAGE_GPG_DIR variable",
107 common.add_option("--gpg-key",
108 help="override the PORTAGE_GPG_KEY variable",
110 common.add_option("--portdir",
111 help="override the PORTDIR variable (deprecated in favor of --repositories-configuration)",
113 common.add_option("--portdir-overlay",
114 help="override the PORTDIR_OVERLAY variable (deprecated in favor of --repositories-configuration)",
115 dest="portdir_overlay")
116 common.add_option("--repositories-configuration",
117 help="override configuration of repositories (in format of repos.conf)",
118 dest="repositories_configuration")
119 common.add_option("--sign-manifests",
123 help="manually override layout.conf sign-manifests setting")
124 common.add_option("--strict-manifests",
128 help="manually override \"strict\" FEATURES setting")
129 common.add_option("--thin-manifests",
133 help="manually override layout.conf thin-manifests setting")
134 common.add_option("--tolerant",
136 help="exit successfully if only minor errors occurred")
137 common.add_option("--ignore-default-opts",
139 help="do not use the EGENCACHE_DEFAULT_OPTS environment variable")
140 parser.add_option_group(common)
142 update = optparse.OptionGroup(parser, '--update options')
143 update.add_option("--cache-dir",
144 help="location of the metadata cache",
146 update.add_option("-j", "--jobs",
148 help="max ebuild processes to spawn")
149 update.add_option("--load-average",
151 help="max load allowed when spawning multiple jobs",
153 update.add_option("--rsync",
155 help="enable rsync stat collision workaround " + \
156 "for bug 139134 (use with --update)")
157 parser.add_option_group(update)
159 uld = optparse.OptionGroup(parser, '--update-use-local-desc options')
160 uld.add_option("--preserve-comments",
162 help="preserve the comments from the existing use.local.desc file")
163 uld.add_option("--use-local-desc-output",
164 help="output file for use.local.desc data (or '-' for stdout)",
166 parser.add_option_group(uld)
168 options, args = parser.parse_args(args)
173 jobs = int(options.jobs)
178 parser.error("Invalid: --jobs='%s'" % \
186 if options.load_average:
188 load_average = float(options.load_average)
192 if load_average <= 0.0:
193 parser.error("Invalid: --load-average='%s'" % \
194 (options.load_average,))
196 options.load_average = load_average
199 options.load_average = None
201 options.config_root = options.portage_configroot
202 if options.config_root is not None and \
203 not os.path.isdir(options.config_root):
204 parser.error("Not a directory: --config-root='%s'" % \
205 (options.config_root,))
207 if options.cache_dir is not None:
208 if not os.path.isdir(options.cache_dir):
209 parser.error("Not a directory: --cache-dir='%s'" % \
210 (options.cache_dir,))
211 if not os.access(options.cache_dir, os.W_OK):
212 parser.error("Write access denied: --cache-dir='%s'" % \
213 (options.cache_dir,))
215 if options.repo is None:
216 parser.error("--repo option is required")
218 if options.portdir is not None:
219 writemsg_level("egencache: warning: --portdir option is deprecated in favor of --repositories-configuration option\n",
220 level=logging.WARNING, noiselevel=-1)
221 if options.portdir_overlay is not None:
222 writemsg_level("egencache: warning: --portdir-overlay option is deprecated in favor of --repositories-configuration option\n",
223 level=logging.WARNING, noiselevel=-1)
227 atom = portage.dep.Atom(atom)
228 except portage.exception.InvalidAtom:
229 parser.error('Invalid atom: %s' % (atom,))
231 if not isjustname(atom):
232 parser.error('Atom is too specific: %s' % (atom,))
234 if options.update_use_local_desc:
239 parser.error('--update-use-local-desc requires python with USE=xml!')
241 if options.uld_output == '-' and options.preserve_comments:
242 parser.error('--preserve-comments can not be used when outputting to stdout')
244 return parser, options, args
246 class GenCache(object):
247 def __init__(self, portdb, cp_iter=None, max_jobs=None, max_load=None,
249 # The caller must set portdb.porttrees in order to constrain
250 # findname, cp_list, and cpv_list to the desired tree.
251 tree = portdb.porttrees[0]
252 self._portdb = portdb
253 self._eclass_db = portdb.repositories.get_repo_for_location(tree).eclass_db
254 self._auxdbkeys = portdb._known_keys
255 # We can globally cleanse stale cache only if we
256 # iterate over every single cp.
257 self._global_cleanse = cp_iter is None
258 if cp_iter is not None:
259 self._cp_set = set(cp_iter)
260 cp_iter = iter(self._cp_set)
261 self._cp_missing = self._cp_set.copy()
264 self._cp_missing = set()
265 write_auxdb = "metadata-transfer" in portdb.settings.features
266 self._regen = MetadataRegen(portdb, cp_iter=cp_iter,
267 consumer=self._metadata_callback,
268 max_jobs=max_jobs, max_load=max_load,
269 write_auxdb=write_auxdb, main=True)
270 self.returncode = os.EX_OK
271 conf = portdb.repositories.get_repo_for_location(tree)
272 self._trg_caches = tuple(conf.iter_pregenerated_caches(
273 self._auxdbkeys, force=True, readonly=False))
274 if not self._trg_caches:
275 raise Exception("cache formats '%s' aren't supported" %
276 (" ".join(conf.cache_formats),))
279 for trg_cache in self._trg_caches:
280 if hasattr(trg_cache, 'raise_stat_collision'):
281 trg_cache.raise_stat_collision = True
282 # Make _metadata_callback write this cache first, in case
283 # it raises a StatCollision and triggers mtime
285 self._trg_caches = tuple([trg_cache] +
286 [x for x in self._trg_caches if x is not trg_cache])
288 self._existing_nodes = set()
290 def _metadata_callback(self, cpv, repo_path, metadata,
291 ebuild_hash, eapi_supported):
292 self._existing_nodes.add(cpv)
293 self._cp_missing.discard(cpv_getkey(cpv))
295 # Since we're supposed to be able to efficiently obtain the
296 # EAPI from _parse_eapi_ebuild_head, we don't write cache
297 # entries for unsupported EAPIs.
298 if metadata is not None and eapi_supported:
299 if metadata.get('EAPI') == '0':
301 for trg_cache in self._trg_caches:
302 self._write_cache(trg_cache,
303 cpv, repo_path, metadata, ebuild_hash)
305 def _write_cache(self, trg_cache, cpv, repo_path, metadata, ebuild_hash):
307 if not hasattr(trg_cache, 'raise_stat_collision'):
308 # This cache does not avoid redundant writes automatically,
309 # so check for an identical existing entry before writing.
310 # This prevents unnecessary disk writes and can also prevent
311 # unnecessary rsync transfers.
313 dest = trg_cache[cpv]
314 except (KeyError, CacheError):
317 if trg_cache.validate_entry(dest,
318 ebuild_hash, self._eclass_db):
320 for k in self._auxdbkeys:
321 if dest.get(k, '') != metadata.get(k, ''):
328 chf = trg_cache.validation_chf
329 metadata['_%s_' % chf] = getattr(ebuild_hash, chf)
331 trg_cache[cpv] = metadata
332 except StatCollision as sc:
333 # If the content of a cache entry changes and neither the
334 # file mtime nor size changes, it will prevent rsync from
335 # detecting changes. Cache backends may raise this
336 # exception from _setitem() if they detect this type of stat
337 # collision. These exceptions are handled by bumping the
338 # mtime on the ebuild (and the corresponding cache entry).
339 # See bug #139134. It is convenient to include checks for
340 # redundant writes along with the internal StatCollision
341 # detection code, so for caches with the
342 # raise_stat_collision attribute, we do not need to
343 # explicitly check for redundant writes like we do for the
344 # other cache types above.
346 for ec, ec_hash in metadata['_eclasses_'].items():
347 if max_mtime < ec_hash.mtime:
348 max_mtime = ec_hash.mtime
349 if max_mtime == sc.mtime:
351 max_mtime = long(max_mtime)
353 os.utime(ebuild_hash.location, (max_mtime, max_mtime))
357 "%s writing target: %s\n" % (cpv, e),
358 level=logging.ERROR, noiselevel=-1)
360 ebuild_hash.mtime = max_mtime
361 metadata['_mtime_'] = max_mtime
362 trg_cache[cpv] = metadata
363 self._portdb.auxdb[repo_path][cpv] = metadata
365 except CacheError as ce:
368 "%s writing target: %s\n" % (cpv, ce),
369 level=logging.ERROR, noiselevel=-1)
372 signum = run_main_scheduler(self._regen)
373 if signum is not None:
374 sys.exit(128 + signum)
376 self.returncode |= self._regen.returncode
378 for trg_cache in self._trg_caches:
379 self._cleanse_cache(trg_cache)
381 def _cleanse_cache(self, trg_cache):
382 cp_missing = self._cp_missing
384 if self._global_cleanse:
386 for cpv in trg_cache:
391 "Unable to parse cp for '%s'\n" % (cpv,),
392 level=logging.ERROR, noiselevel=-1)
395 except CacheError as ce:
398 "Error listing cache entries for " + \
399 "'%s': %s, continuing...\n" % \
400 (trg_cache.location, ce),
401 level=logging.ERROR, noiselevel=-1)
404 cp_set = self._cp_set
406 for cpv in trg_cache:
411 "Unable to parse cp for '%s'\n" % (cpv,),
412 level=logging.ERROR, noiselevel=-1)
414 cp_missing.discard(cp)
417 except CacheError as ce:
420 "Error listing cache entries for " + \
421 "'%s': %s, continuing...\n" % \
422 (trg_cache.location, ce),
423 level=logging.ERROR, noiselevel=-1)
427 for cp in sorted(cp_missing):
429 "No ebuilds or cache entries found for '%s'\n" % (cp,),
430 level=logging.ERROR, noiselevel=-1)
433 dead_nodes.difference_update(self._existing_nodes)
439 except CacheError as ce:
442 "%s deleting stale cache: %s\n" % (k, ce),
443 level=logging.ERROR, noiselevel=-1)
445 if not trg_cache.autocommits:
448 except CacheError as ce:
451 "committing target: %s\n" % (ce,),
452 level=logging.ERROR, noiselevel=-1)
454 if hasattr(trg_cache, '_prune_empty_dirs'):
455 trg_cache._prune_empty_dirs()
457 class GenUseLocalDesc(object):
458 def __init__(self, portdb, output=None,
459 preserve_comments=False):
460 self.returncode = os.EX_OK
461 self._portdb = portdb
462 self._output = output
463 self._preserve_comments = preserve_comments
466 repo_path = self._portdb.porttrees[0]
467 ops = {'<':0, '<=':1, '=':2, '>=':3, '>':4}
469 if self._output is None or self._output != '-':
470 if self._output is None:
471 prof_path = os.path.join(repo_path, 'profiles')
472 desc_path = os.path.join(prof_path, 'use.local.desc')
478 desc_path = self._output
481 if self._preserve_comments:
482 # Probe in binary mode, in order to avoid
483 # potential character encoding issues.
484 output = open(_unicode_encode(desc_path,
485 encoding=_encodings['fs'], errors='strict'), 'r+b')
487 output = io.open(_unicode_encode(desc_path,
488 encoding=_encodings['fs'], errors='strict'),
489 mode='w', encoding=_encodings['repo.content'],
490 errors='backslashreplace')
492 if not self._preserve_comments or \
493 os.path.isfile(desc_path):
495 "ERROR: failed to open output file %s: %s\n" \
496 % (desc_path, e), level=logging.ERROR, noiselevel=-1)
500 # Open in r+b mode failed because the file doesn't
501 # exist yet. We can probably recover if we disable
502 # preserve_comments mode now.
504 "WARNING: --preserve-comments enabled, but " + \
505 "output file not found: %s\n" % (desc_path,),
506 level=logging.WARNING, noiselevel=-1)
507 self._preserve_comments = False
509 output = io.open(_unicode_encode(desc_path,
510 encoding=_encodings['fs'], errors='strict'),
511 mode='w', encoding=_encodings['repo.content'],
512 errors='backslashreplace')
515 "ERROR: failed to open output file %s: %s\n" \
516 % (desc_path, e), level=logging.ERROR, noiselevel=-1)
522 if self._preserve_comments:
525 if not output.readline().startswith(b'#'):
531 # Finished probing comments in binary mode, now append
533 output = io.open(_unicode_encode(desc_path,
534 encoding=_encodings['fs'], errors='strict'),
535 mode='a', encoding=_encodings['repo.content'],
536 errors='backslashreplace')
539 output.write(textwrap.dedent('''\
540 # This file is deprecated as per GLEP 56 in favor of metadata.xml. Please add
541 # your descriptions to your package's metadata.xml ONLY.
542 # * generated automatically using egencache *
546 # The cmp function no longer exists in python3, so we'll
547 # implement our own here under a slightly different name
548 # since we don't want any confusion given that we never
549 # want to rely on the builtin cmp function.
551 if a is None or b is None:
552 # None can't be compared with other types in python3.
553 if a is None and b is None:
559 return (a > b) - (a < b)
561 class _MetadataTreeBuilder(ElementTree.TreeBuilder):
563 Implements doctype() as required to avoid deprecation warnings
566 def doctype(self, name, pubid, system):
569 for cp in self._portdb.cp_all():
570 metadata_path = os.path.join(repo_path, cp, 'metadata.xml')
572 metadata = ElementTree.parse(_unicode_encode(metadata_path,
573 encoding=_encodings['fs'], errors='strict'),
574 parser=ElementTree.XMLParser(
575 target=_MetadataTreeBuilder()))
578 except (ExpatError, EnvironmentError) as e:
580 "ERROR: failed parsing %s/metadata.xml: %s\n" % (cp, e),
581 level=logging.ERROR, noiselevel=-1)
585 usedict = parse_metadata_use(metadata)
586 except portage.exception.ParseError as e:
588 "ERROR: failed parsing %s/metadata.xml: %s\n" % (cp, e),
589 level=logging.ERROR, noiselevel=-1)
592 for flag in sorted(usedict):
593 def atomcmp(atoma, atomb):
594 # None is better than an atom, that's why we reverse the args
595 if atoma is None or atomb is None:
596 return cmp_func(atomb, atoma)
597 # Same for plain PNs (.operator is None then)
598 elif atoma.operator is None or atomb.operator is None:
599 return cmp_func(atomb.operator, atoma.operator)
601 elif atoma.cpv != atomb.cpv:
602 return vercmp(atoma.version, atomb.version)
603 # Versions match, let's fallback to operator matching
605 return cmp_func(ops.get(atoma.operator, -1),
606 ops.get(atomb.operator, -1))
613 resdict = usedict[flag]
614 if len(resdict) == 1:
615 resdesc = next(iter(resdict.items()))[1]
618 reskeys = dict((_Atom(k), k) for k in resdict)
619 except portage.exception.InvalidAtom as e:
621 "ERROR: failed parsing %s/metadata.xml: %s\n" % (cp, e),
622 level=logging.ERROR, noiselevel=-1)
624 resdesc = next(iter(resdict.items()))[1]
626 resatoms = sorted(reskeys, key=cmp_sort_key(atomcmp))
627 resdesc = resdict[reskeys[resatoms[-1]]]
629 output.write('%s:%s - %s\n' % (cp, flag, resdesc))
633 if sys.hexversion < 0x3000000:
634 _filename_base = unicode
638 class _special_filename(_filename_base):
640 Helps to sort file names by file type and other criteria.
642 def __new__(cls, status_change, file_name):
643 return _filename_base.__new__(cls, status_change + file_name)
645 def __init__(self, status_change, file_name):
646 _filename_base.__init__(status_change + file_name)
647 self.status_change = status_change
648 self.file_name = file_name
649 self.file_type = guessManifestFileType(file_name)
651 def file_type_lt(self, a, b):
653 Defines an ordering between file types.
660 if first == "EBUILD":
662 elif first == "MISC":
663 return second in ("EBUILD",)
665 return second in ("EBUILD", "MISC")
666 elif first == "DIST":
667 return second in ("EBUILD", "MISC", "AUX")
671 raise ValueError("Unknown file type '%s'" % first)
673 def __lt__(self, other):
675 Compare different file names, first by file type and then
676 for ebuilds by version and lexicographically for others.
677 EBUILD < MISC < AUX < DIST < None
679 if self.__class__ != other.__class__:
680 raise NotImplementedError
682 # Sort by file type as defined by file_type_lt().
683 if self.file_type_lt(self, other):
685 elif self.file_type_lt(other, self):
688 # Files have the same type.
689 if self.file_type == "EBUILD":
690 # Sort by version. Lowest first.
691 ver = "-".join(pkgsplit(self.file_name[:-7])[1:3])
692 other_ver = "-".join(pkgsplit(other.file_name[:-7])[1:3])
693 return vercmp(ver, other_ver) < 0
695 # Sort lexicographically.
696 return self.file_name < other.file_name
698 class GenChangeLogs(object):
699 def __init__(self, portdb):
700 self.returncode = os.EX_OK
701 self._portdb = portdb
702 self._wrapper = textwrap.TextWrapper(
704 initial_indent = ' ',
705 subsequent_indent = ' '
710 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
711 return _unicode_decode(p.communicate()[0],
712 encoding=_encodings['stdio'], errors='strict')
714 def generate_changelog(self, cp):
716 output = io.open('ChangeLog',
717 mode='w', encoding=_encodings['repo.content'],
718 errors='backslashreplace')
721 "ERROR: failed to open ChangeLog for %s: %s\n" % (cp,e,),
722 level=logging.ERROR, noiselevel=-1)
726 output.write(textwrap.dedent('''\
728 # Copyright 1999-%s Gentoo Foundation; Distributed under the GPL v2
731 ''' % (cp, time.strftime('%Y'))))
733 # now grab all the commits
734 commits = self.grab(['git', 'rev-list', 'HEAD', '--', '.']).split()
737 # Explaining the arguments:
738 # --name-status to get a list of added/removed files
739 # --no-renames to avoid getting more complex records on the list
740 # --format to get the timestamp, author and commit description
741 # --root to make it work fine even with the initial commit
742 # --relative to get paths relative to ebuilddir
743 # -r (recursive) to get per-file changes
744 # then the commit-id and path.
746 cinfo = self.grab(['git', 'diff-tree', '--name-status', '--no-renames',
747 '--format=%ct %cN <%cE>%n%B', '--root', '--relative', '-r',
748 c, '--', '.']).rstrip('\n').split('\n')
751 # timestamp Author Name <author@email>
761 for n, l in enumerate(reversed(cinfo)):
767 if f[1] == 'Manifest':
768 pass # XXX: remanifest commits?
769 elif f[1] == 'ChangeLog':
771 elif f[0].startswith('A'):
772 changed.append(_special_filename("+", f[1]))
773 elif f[0].startswith('D'):
774 changed.append(_special_filename("-", f[1]))
775 elif f[0].startswith('M'):
776 changed.append(_special_filename("", f[1]))
779 "ERROR: unexpected git file status for %s: %s\n" % (cp,f,),
780 level=logging.ERROR, noiselevel=-1)
786 (ts, author) = cinfo[0].split(' ', 1)
787 date = time.strftime('%d %b %Y', time.gmtime(float(ts)))
789 changed = [str(x) for x in sorted(changed)]
792 # Reverse the sort order for headers.
793 for c in reversed(changed):
794 if c.startswith('+') and c.endswith('.ebuild'):
795 output.write('*%s (%s)\n' % (c[1:-7], date))
800 # strip '<cp>: ', '[<cp>] ', and similar
801 body[0] = re.sub(r'^\W*' + re.escape(cp) + r'\W+', '', body[0])
802 # strip trailing newline
806 if body[-1].startswith('git-svn-id:') and not body[-2]:
808 # strip the repoman version/manifest note
809 if body[-1] == ' (Signed Manifest commit)' or body[-1] == ' (Unsigned Manifest commit)':
811 if body[-1].startswith('(Portage version:') and body[-1].endswith(')'):
816 # don't break filenames on hyphens
817 self._wrapper.break_on_hyphens = False
818 output.write(self._wrapper.fill(
819 '%s; %s %s:' % (date, author, ', '.join(changed))))
820 # but feel free to break commit messages there
821 self._wrapper.break_on_hyphens = True
823 '\n%s\n\n' % '\n'.join(self._wrapper.fill(x) for x in body))
828 repo_path = self._portdb.porttrees[0]
831 if 'git' not in FindVCS():
833 "ERROR: --update-changelogs supported only in git repos\n",
834 level=logging.ERROR, noiselevel=-1)
835 self.returncode = 127
838 for cp in self._portdb.cp_all():
839 os.chdir(os.path.join(repo_path, cp))
840 # Determine whether ChangeLog is up-to-date by comparing
841 # the newest commit timestamp with the ChangeLog timestamp.
842 lmod = self.grab(['git', 'log', '--format=%ct', '-1', '.'])
844 # This cp has not been added to the repo.
848 cmod = os.stat('ChangeLog').st_mtime
852 if float(cmod) < float(lmod):
853 self.generate_changelog(cp)
855 def egencache_main(args):
857 # The calling environment is ignored, so the program is
858 # completely controlled by commandline arguments.
861 if not sys.stdout.isatty():
862 portage.output.nocolor()
863 env['NOCOLOR'] = 'true'
865 parser, options, atoms = parse_args(args)
867 config_root = options.config_root
869 if options.repositories_configuration is not None:
870 env['PORTAGE_REPOSITORIES'] = options.repositories_configuration
871 elif options.portdir_overlay:
872 env['PORTDIR_OVERLAY'] = options.portdir_overlay
874 if options.cache_dir is not None:
875 env['PORTAGE_DEPCACHEDIR'] = options.cache_dir
877 if options.portdir is not None:
878 env['PORTDIR'] = options.portdir
880 settings = portage.config(config_root=config_root,
881 local_config=False, env=env)
884 if not options.ignore_default_opts:
885 default_opts = portage.util.shlex_split(
886 settings.get('EGENCACHE_DEFAULT_OPTS', ''))
889 parser, options, args = parse_args(default_opts + args)
891 if options.cache_dir is not None:
892 env['PORTAGE_DEPCACHEDIR'] = options.cache_dir
894 settings = portage.config(config_root=config_root,
895 local_config=False, env=env)
897 if not (options.update or options.update_use_local_desc or
898 options.update_changelogs or options.update_manifests):
899 parser.error('No action specified')
902 repo_path = settings.repositories.treemap.get(options.repo)
903 if repo_path is None:
904 parser.error("Unable to locate repository named '%s'" % (options.repo,))
907 repo_config = settings.repositories.get_repo_for_location(repo_path)
909 if options.strict_manifests is not None:
910 if options.strict_manifests == "y":
911 settings.features.add("strict")
913 settings.features.discard("strict")
915 if options.update and 'metadata-transfer' not in settings.features:
916 # Forcibly enable metadata-transfer if portdbapi has a pregenerated
917 # cache that does not support eclass validation.
918 cache = repo_config.get_pregenerated_cache(
919 portage.dbapi.dbapi._known_keys, readonly=True)
920 if cache is not None and not cache.complete_eclass_entries:
921 settings.features.add('metadata-transfer')
926 portdb = portage.portdbapi(mysettings=settings)
928 # Limit ebuilds to the specified repo.
929 portdb.porttrees = [repo_path]
932 if options.cache_dir is not None:
933 # already validated earlier
936 # We check write access after the portdbapi constructor
937 # has had an opportunity to create it. This ensures that
938 # we don't use the cache in the "volatile" mode which is
939 # undesirable for egencache.
940 if not os.access(settings["PORTAGE_DEPCACHEDIR"], os.W_OK):
941 writemsg_level("ecachegen: error: " + \
942 "write access denied: %s\n" % (settings["PORTAGE_DEPCACHEDIR"],),
943 level=logging.ERROR, noiselevel=-1)
946 if options.sign_manifests is not None:
947 repo_config.sign_manifest = options.sign_manifests == 'y'
949 if options.thin_manifests is not None:
950 repo_config.thin_manifest = options.thin_manifests == 'y'
954 force_sign_key = None
956 if options.update_manifests:
957 if repo_config.sign_manifest:
961 gpg_cmd = settings.get("PORTAGE_GPG_SIGNING_COMMAND")
963 writemsg_level("egencache: error: "
964 "PORTAGE_GPG_SIGNING_COMMAND is unset! "
965 "Is make.globals missing?\n",
966 level=logging.ERROR, noiselevel=-1)
968 elif "${PORTAGE_GPG_KEY}" in gpg_cmd and \
969 options.gpg_key is None and \
970 "PORTAGE_GPG_KEY" not in settings:
971 writemsg_level("egencache: error: "
972 "PORTAGE_GPG_KEY is unset!\n",
973 level=logging.ERROR, noiselevel=-1)
975 elif "${PORTAGE_GPG_DIR}" in gpg_cmd:
976 if options.gpg_dir is not None:
977 gpg_dir = options.gpg_dir
978 elif "PORTAGE_GPG_DIR" not in settings:
979 gpg_dir = os.path.expanduser("~/.gnupg")
981 gpg_dir = os.path.expanduser(settings["PORTAGE_GPG_DIR"])
982 if not os.access(gpg_dir, os.X_OK):
983 writemsg_level(("egencache: error: "
984 "Unable to access directory: "
985 "PORTAGE_GPG_DIR='%s'\n") % gpg_dir,
986 level=logging.ERROR, noiselevel=-1)
990 writemsg_level("egencache: You may disable manifest "
991 "signatures with --sign-manifests=n or by setting "
992 "\"sign-manifests = false\" in metadata/layout.conf\n",
993 level=logging.ERROR, noiselevel=-1)
997 if gpg_dir is not None:
998 gpg_vars["PORTAGE_GPG_DIR"] = gpg_dir
1000 if options.gpg_key is None:
1001 gpg_var_names.append("PORTAGE_GPG_KEY")
1003 gpg_vars["PORTAGE_GPG_KEY"] = options.gpg_key
1005 for k in gpg_var_names:
1010 force_sign_key = gpg_vars.get("PORTAGE_GPG_KEY")
1017 cp_iter = iter(atoms)
1019 gen_cache = GenCache(portdb, cp_iter=cp_iter,
1020 max_jobs=options.jobs,
1021 max_load=options.load_average,
1022 rsync=options.rsync)
1024 if options.tolerant:
1025 ret.append(os.EX_OK)
1027 ret.append(gen_cache.returncode)
1029 if options.update_manifests:
1033 cp_iter = iter(atoms)
1035 event_loop = global_event_loop()
1036 scheduler = ManifestScheduler(portdb, cp_iter=cp_iter,
1037 gpg_cmd=gpg_cmd, gpg_vars=gpg_vars,
1038 force_sign_key=force_sign_key,
1039 max_jobs=options.jobs,
1040 max_load=options.load_average,
1041 event_loop=event_loop)
1043 signum = run_main_scheduler(scheduler)
1044 if signum is not None:
1045 sys.exit(128 + signum)
1047 if options.tolerant:
1048 ret.append(os.EX_OK)
1050 ret.append(scheduler.returncode)
1052 if options.update_use_local_desc:
1053 gen_desc = GenUseLocalDesc(portdb,
1054 output=options.uld_output,
1055 preserve_comments=options.preserve_comments)
1057 ret.append(gen_desc.returncode)
1059 if options.update_changelogs:
1060 gen_clogs = GenChangeLogs(portdb)
1062 ret.append(gen_clogs.returncode)
1066 if __name__ == "__main__":
1067 portage._disable_legacy_globals()
1068 portage.util.noiselimit = -1
1069 sys.exit(egencache_main(sys.argv[1:]))