__save_ebuild_env: filter __repo_key
[portage.git] / bin / egencache
index 15571cbdccc181c71cd52cd4cf5e5b1c0cb65a5b..2c5dddce9a7a53813326513976e350976c75ea70 100755 (executable)
@@ -1,8 +1,9 @@
 #!/usr/bin/python
-# Copyright 2009-2012 Gentoo Foundation
+# Copyright 2009-2013 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-from __future__ import print_function
+# unicode_literals for compat with TextIOWrapper in Python 2
+from __future__ import print_function, unicode_literals
 
 import platform
 import signal
@@ -44,12 +45,14 @@ from os import path as osp
 pym_path = osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym")
 sys.path.insert(0, pym_path)
 import portage
+portage._internal_caller = True
 from portage import os, _encodings, _unicode_encode, _unicode_decode
 from _emerge.MetadataRegen import MetadataRegen
 from portage.cache.cache_errors import CacheError, StatCollision
 from portage.manifest import guessManifestFileType
 from portage.package.ebuild._parallel_manifest.ManifestScheduler import ManifestScheduler
 from portage.util import cmp_sort_key, writemsg_level
+from portage.util._async.run_main_scheduler import run_main_scheduler
 from portage.util._eventloop.global_event_loop import global_event_loop
 from portage import cpv_getkey
 from portage.dep import Atom, isjustname
@@ -79,7 +82,7 @@ def parse_args(args):
        actions = optparse.OptionGroup(parser, 'Actions')
        actions.add_option("--update",
                action="store_true",
-               help="update metadata/cache/ (generate as necessary)")
+               help="update metadata/md5-cache/ (generate as necessary)")
        actions.add_option("--update-use-local-desc",
                action="store_true",
                help="update the use.local.desc file from metadata.xml")
@@ -105,11 +108,14 @@ def parse_args(args):
                help="override the PORTAGE_GPG_KEY variable",
                dest="gpg_key")
        common.add_option("--portdir",
-               help="override the portage tree location",
+               help="override the portage tree location (deprecated in favor of --repositories-configuration)",
                dest="portdir")
        common.add_option("--portdir-overlay",
-               help="override the PORTDIR_OVERLAY variable (requires that --repo is also specified)",
+               help="override the PORTDIR_OVERLAY variable (requires that --repo is also specified) (deprecated in favor of --repositories-configuration)",
                dest="portdir_overlay")
+       common.add_option("--repositories-configuration",
+               help="override configuration of repositories (in format of repos.conf) (requires that --repo is also specified)",
+               dest="repositories_configuration")
        common.add_option("--sign-manifests",
                type="choice",
                choices=('y', 'n'),
@@ -206,9 +212,11 @@ def parse_args(args):
                        parser.error("Write access denied: --cache-dir='%s'" % \
                                (options.cache_dir,))
 
-       if options.portdir_overlay is not None and \
-               options.repo is None:
-               parser.error("--portdir-overlay option requires --repo option")
+       if options.repo is None:
+               if options.repositories_configuration is not None:
+                       parser.error("--repositories-configuration option requires --repo option")
+               if options.portdir_overlay is not None:
+                       parser.error("--portdir-overlay option requires --repo option")
 
        for atom in args:
                try:
@@ -357,34 +365,9 @@ class GenCache(object):
                                        level=logging.ERROR, noiselevel=-1)
 
        def run(self):
-
-               received_signal = []
-
-               def sighandler(signum, frame):
-                       signal.signal(signal.SIGINT, signal.SIG_IGN)
-                       signal.signal(signal.SIGTERM, signal.SIG_IGN)
-                       self._regen.terminate()
-                       received_signal.append(128 + signum)
-
-               earlier_sigint_handler = signal.signal(signal.SIGINT, sighandler)
-               earlier_sigterm_handler = signal.signal(signal.SIGTERM, sighandler)
-
-               try:
-                       self._regen.start()
-                       self._regen.wait()
-               finally:
-                       # Restore previous handlers
-                       if earlier_sigint_handler is not None:
-                               signal.signal(signal.SIGINT, earlier_sigint_handler)
-                       else:
-                               signal.signal(signal.SIGINT, signal.SIG_DFL)
-                       if earlier_sigterm_handler is not None:
-                               signal.signal(signal.SIGTERM, earlier_sigterm_handler)
-                       else:
-                               signal.signal(signal.SIGTERM, signal.SIG_DFL)
-
-               if received_signal:
-                       sys.exit(received_signal[0])
+               signum = run_main_scheduler(self._regen)
+               if signum is not None:
+                       sys.exit(128 + signum)
 
                self.returncode |= self._regen.returncode
 
@@ -409,8 +392,8 @@ class GenCache(object):
                                self.returncode |= 1
                                writemsg_level(
                                        "Error listing cache entries for " + \
-                                       "'%s/metadata/cache': %s, continuing...\n" % \
-                                       (self._portdb.porttree_root, ce),
+                                       "'%s': %s, continuing...\n" % \
+                                       (trg_cache.location, ce),
                                        level=logging.ERROR, noiselevel=-1)
 
                else:
@@ -431,8 +414,8 @@ class GenCache(object):
                                self.returncode |= 1
                                writemsg_level(
                                        "Error listing cache entries for " + \
-                                       "'%s/metadata/cache': %s, continuing...\n" % \
-                                       (self._portdb.porttree_root, ce),
+                                       "'%s': %s, continuing...\n" % \
+                                       (trg_cache.location, ce),
                                        level=logging.ERROR, noiselevel=-1)
 
                if cp_missing:
@@ -474,7 +457,7 @@ class GenUseLocalDesc(object):
                self._portdb = portdb
                self._output = output
                self._preserve_comments = preserve_comments
-       
+
        def run(self):
                repo_path = self._portdb.porttrees[0]
                ops = {'<':0, '<=':1, '=':2, '>=':3, '>':4}
@@ -547,14 +530,14 @@ class GenUseLocalDesc(object):
                                encoding=_encodings['fs'], errors='strict'),
                                mode='a', encoding=_encodings['repo.content'],
                                errors='backslashreplace')
-                       output.write(_unicode_decode('\n'))
+                       output.write('\n')
                else:
-                       output.write(textwrap.dedent(_unicode_decode('''\
+                       output.write(textwrap.dedent('''\
                                # This file is deprecated as per GLEP 56 in favor of metadata.xml. Please add
                                # your descriptions to your package's metadata.xml ONLY.
                                # * generated automatically using egencache *
 
-                               ''')))
+                               '''))
 
                # The cmp function no longer exists in python3, so we'll
                # implement our own here under a slightly different name
@@ -639,8 +622,7 @@ class GenUseLocalDesc(object):
                                                                resatoms = sorted(reskeys, key=cmp_sort_key(atomcmp))
                                                                resdesc = resdict[reskeys[resatoms[-1]]]
 
-                                               output.write(_unicode_decode(
-                                                       '%s:%s - %s\n' % (cp, flag, resdesc)))
+                                               output.write('%s:%s - %s\n' % (cp, flag, resdesc))
 
                output.close()
 
@@ -737,12 +719,12 @@ class GenChangeLogs(object):
                        self.returncode |= 2
                        return
 
-               output.write(textwrap.dedent(_unicode_decode('''\
+               output.write(textwrap.dedent('''\
                        # ChangeLog for %s
                        # Copyright 1999-%s Gentoo Foundation; Distributed under the GPL v2
                        # $Header: $
 
-                       ''' % (cp, time.strftime('%Y')))))
+                       ''' % (cp, time.strftime('%Y'))))
 
                # now grab all the commits
                commits = self.grab(['git', 'rev-list', 'HEAD', '--', '.']).split()
@@ -806,11 +788,10 @@ class GenChangeLogs(object):
                        # Reverse the sort order for headers.
                        for c in reversed(changed):
                                if c.startswith('+') and c.endswith('.ebuild'):
-                                       output.write(_unicode_decode(
-                                               '*%s (%s)\n' % (c[1:-7], date)))
+                                       output.write('*%s (%s)\n' % (c[1:-7], date))
                                        wroteheader = True
                        if wroteheader:
-                               output.write(_unicode_decode('\n'))
+                               output.write('\n')
 
                        # strip '<cp>: ', '[<cp>] ', and similar
                        body[0] = re.sub(r'^\W*' + re.escape(cp) + r'\W+', '', body[0])
@@ -830,13 +811,12 @@ class GenChangeLogs(object):
 
                        # don't break filenames on hyphens
                        self._wrapper.break_on_hyphens = False
-                       output.write(_unicode_decode(
-                               self._wrapper.fill(
-                               '%s; %s %s:' % (date, author, ', '.join(changed)))))
+                       output.write(self._wrapper.fill(
+                               '%s; %s %s:' % (date, author, ', '.join(changed))))
                        # but feel free to break commit messages there
                        self._wrapper.break_on_hyphens = True
-                       output.write(_unicode_decode(
-                               '\n%s\n\n' % '\n'.join(self._wrapper.fill(x) for x in body)))
+                       output.write(
+                               '\n%s\n\n' % '\n'.join(self._wrapper.fill(x) for x in body))
 
                output.close()
 
@@ -869,16 +849,23 @@ class GenChangeLogs(object):
                                self.generate_changelog(cp)
 
 def egencache_main(args):
-       parser, options, atoms = parse_args(args)
-
-       config_root = options.config_root
 
        # The calling environment is ignored, so the program is
        # completely controlled by commandline arguments.
        env = {}
 
+       if not sys.stdout.isatty():
+               portage.output.nocolor()
+               env['NOCOLOR'] = 'true'
+
+       parser, options, atoms = parse_args(args)
+
+       config_root = options.config_root
+
        if options.repo is None:
                env['PORTDIR_OVERLAY'] = ''
+       elif options.repositories_configuration is not None:
+               env['PORTAGE_REPOSITORIES'] = options.repositories_configuration
        elif options.portdir_overlay:
                env['PORTDIR_OVERLAY'] = options.portdir_overlay
 
@@ -893,7 +880,8 @@ def egencache_main(args):
 
        default_opts = None
        if not options.ignore_default_opts:
-               default_opts = settings.get('EGENCACHE_DEFAULT_OPTS', '').split()
+               default_opts = portage.util.shlex_split(
+                       settings.get('EGENCACHE_DEFAULT_OPTS', ''))
 
        if default_opts:
                parser, options, args = parse_args(default_opts + args)
@@ -928,7 +916,7 @@ def egencache_main(args):
                if options.strict_manifests == "y":
                        settings.features.add("strict")
                else:
-                       settings.features.add("discard")
+                       settings.features.discard("strict")
 
        if options.update and 'metadata-transfer' not in settings.features:
                # Forcibly enable metadata-transfer if portdbapi has a pregenerated
@@ -969,6 +957,7 @@ def egencache_main(args):
 
        gpg_cmd = None
        gpg_vars = None
+       force_sign_key = None
 
        if options.update_manifests:
                if repo_config.sign_manifest:
@@ -1024,6 +1013,8 @@ def egencache_main(args):
                                if v is not None:
                                        gpg_vars[k] = v
 
+                       force_sign_key = gpg_vars.get("PORTAGE_GPG_KEY")
+
        ret = [os.EX_OK]
 
        if options.update:
@@ -1050,37 +1041,14 @@ def egencache_main(args):
                event_loop = global_event_loop()
                scheduler = ManifestScheduler(portdb, cp_iter=cp_iter,
                        gpg_cmd=gpg_cmd, gpg_vars=gpg_vars,
+                       force_sign_key=force_sign_key,
                        max_jobs=options.jobs,
                        max_load=options.load_average,
                        event_loop=event_loop)
 
-               received_signal = []
-
-               def sighandler(signum, frame):
-                       signal.signal(signal.SIGINT, signal.SIG_IGN)
-                       signal.signal(signal.SIGTERM, signal.SIG_IGN)
-                       received_signal.append(128 + signum)
-                       scheduler.terminate()
-
-               earlier_sigint_handler = signal.signal(signal.SIGINT, sighandler)
-               earlier_sigterm_handler = signal.signal(signal.SIGTERM, sighandler)
-
-               try:
-                       scheduler.start()
-                       scheduler.wait()
-               finally:
-                       # Restore previous handlers
-                       if earlier_sigint_handler is not None:
-                               signal.signal(signal.SIGINT, earlier_sigint_handler)
-                       else:
-                               signal.signal(signal.SIGINT, signal.SIG_DFL)
-                       if earlier_sigterm_handler is not None:
-                               signal.signal(signal.SIGTERM, earlier_sigterm_handler)
-                       else:
-                               signal.signal(signal.SIGTERM, signal.SIG_DFL)
-
-               if received_signal:
-                       sys.exit(received_signal[0])
+               signum = run_main_scheduler(scheduler)
+               if signum is not None:
+                       sys.exit(128 + signum)
 
                if options.tolerant:
                        ret.append(os.EX_OK)