egencache: handle empty --portdir-overlay
[portage.git] / bin / egencache
1 #!/usr/bin/python
2 # Copyright 2009-2013 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # unicode_literals for compat with TextIOWrapper in Python 2
6 from __future__ import print_function, unicode_literals
7
8 import platform
9 import signal
10 import sys
11 # This block ensures that ^C interrupts are handled quietly.
12 try:
13
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)
18
19         signal.signal(signal.SIGINT, exithandler)
20         signal.signal(signal.SIGTERM, exithandler)
21
22 except KeyboardInterrupt:
23         sys.exit(128 + signal.SIGINT)
24
25 def debug_signal(signum, frame):
26         import pdb
27         pdb.set_trace()
28
29 if platform.python_implementation() == 'Jython':
30         debug_signum = signal.SIGUSR2 # bug #424259
31 else:
32         debug_signum = signal.SIGUSR1
33
34 signal.signal(debug_signum, debug_signal)
35
36 import io
37 import logging
38 import subprocess
39 import time
40 import textwrap
41 import re
42
43 from os import path as osp
44 pym_path = osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym")
45 sys.path.insert(0, pym_path)
46 import portage
47 portage._internal_caller = True
48 from portage import os, _encodings, _unicode_encode, _unicode_decode
49 from _emerge.MetadataRegen import MetadataRegen
50 from portage.cache.cache_errors import CacheError, StatCollision
51 from portage.manifest import guessManifestFileType
52 from portage.package.ebuild._parallel_manifest.ManifestScheduler import ManifestScheduler
53 from portage.util import cmp_sort_key, writemsg_level
54 from portage.util._argparse import ArgumentParser
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
60
61 try:
62         from xml.etree import ElementTree
63 except ImportError:
64         pass
65 else:
66         try:
67                 from xml.parsers.expat import ExpatError
68         except ImportError:
69                 pass
70         else:
71                 from repoman.utilities import parse_metadata_use
72
73 from repoman.utilities import FindVCS
74
75 if sys.hexversion >= 0x3000000:
76         long = int
77
78 def parse_args(args):
79         usage = "egencache [options] <action> ... [atom] ..."
80         parser = ArgumentParser(usage=usage)
81
82         actions = parser.add_argument_group('Actions')
83         actions.add_argument("--update",
84                 action="store_true",
85                 help="update metadata/md5-cache/ (generate as necessary)")
86         actions.add_argument("--update-use-local-desc",
87                 action="store_true",
88                 help="update the use.local.desc file from metadata.xml")
89         actions.add_argument("--update-changelogs",
90                 action="store_true",
91                 help="update the ChangeLog files from SCM logs")
92         actions.add_argument("--update-manifests",
93                 action="store_true",
94                 help="update manifests")
95
96         common = parser.add_argument_group('Common options')
97         common.add_argument("--repo",
98                 action="store",
99                 help="name of repo to operate on")
100         common.add_argument("--config-root",
101                 help="location of portage config files",
102                 dest="portage_configroot")
103         common.add_argument("--gpg-dir",
104                 help="override the PORTAGE_GPG_DIR variable",
105                 dest="gpg_dir")
106         common.add_argument("--gpg-key",
107                 help="override the PORTAGE_GPG_KEY variable",
108                 dest="gpg_key")
109         common.add_argument("--portdir",
110                 help="override the PORTDIR variable (deprecated in favor of --repositories-configuration)",
111                 dest="portdir")
112         common.add_argument("--portdir-overlay",
113                 help="override the PORTDIR_OVERLAY variable (deprecated in favor of --repositories-configuration)",
114                 dest="portdir_overlay")
115         common.add_argument("--repositories-configuration",
116                 help="override configuration of repositories (in format of repos.conf)",
117                 dest="repositories_configuration")
118         common.add_argument("--sign-manifests",
119                 choices=('y', 'n'),
120                 metavar="<y|n>",
121                 help="manually override layout.conf sign-manifests setting")
122         common.add_argument("--strict-manifests",
123                 choices=('y', 'n'),
124                 metavar="<y|n>",
125                 help="manually override \"strict\" FEATURES setting")
126         common.add_argument("--thin-manifests",
127                 choices=('y', 'n'),
128                 metavar="<y|n>",
129                 help="manually override layout.conf thin-manifests setting")
130         common.add_argument("--tolerant",
131                 action="store_true",
132                 help="exit successfully if only minor errors occurred")
133         common.add_argument("--ignore-default-opts",
134                 action="store_true",
135                 help="do not use the EGENCACHE_DEFAULT_OPTS environment variable")
136
137         update = parser.add_argument_group('--update options')
138         update.add_argument("--cache-dir",
139                 help="location of the metadata cache",
140                 dest="cache_dir")
141         update.add_argument("-j", "--jobs",
142                 type=int,
143                 action="store",
144                 help="max ebuild processes to spawn")
145         update.add_argument("--load-average",
146                 type=float,
147                 action="store",
148                 help="max load allowed when spawning multiple jobs",
149                 dest="load_average")
150         update.add_argument("--rsync",
151                 action="store_true",
152                 help="enable rsync stat collision workaround " + \
153                         "for bug 139134 (use with --update)")
154
155         uld = parser.add_argument_group('--update-use-local-desc options')
156         uld.add_argument("--preserve-comments",
157                 action="store_true",
158                 help="preserve the comments from the existing use.local.desc file")
159         uld.add_argument("--use-local-desc-output",
160                 help="output file for use.local.desc data (or '-' for stdout)",
161                 dest="uld_output")
162
163         options, args = parser.parse_known_args(args)
164
165         if options.jobs:
166                 jobs = None
167                 try:
168                         jobs = int(options.jobs)
169                 except ValueError:
170                         jobs = -1
171
172                 if jobs < 1:
173                         parser.error("Invalid: --jobs='%s'" % \
174                                 (options.jobs,))
175
176                 options.jobs = jobs
177
178         else:
179                 options.jobs = None
180
181         if options.load_average:
182                 try:
183                         load_average = float(options.load_average)
184                 except ValueError:
185                         load_average = 0.0
186
187                 if load_average <= 0.0:
188                         parser.error("Invalid: --load-average='%s'" % \
189                                 (options.load_average,))
190
191                 options.load_average = load_average
192
193         else:
194                 options.load_average = None
195
196         options.config_root = options.portage_configroot
197         if options.config_root is not None and \
198                 not os.path.isdir(options.config_root):
199                 parser.error("Not a directory: --config-root='%s'" % \
200                         (options.config_root,))
201
202         if options.cache_dir is not None:
203                 if not os.path.isdir(options.cache_dir):
204                         parser.error("Not a directory: --cache-dir='%s'" % \
205                                 (options.cache_dir,))
206                 if not os.access(options.cache_dir, os.W_OK):
207                         parser.error("Write access denied: --cache-dir='%s'" % \
208                                 (options.cache_dir,))
209
210         if options.portdir is not None:
211                 writemsg_level("egencache: warning: --portdir option is deprecated in favor of --repositories-configuration option\n",
212                         level=logging.WARNING, noiselevel=-1)
213         if options.portdir_overlay is not None:
214                 writemsg_level("egencache: warning: --portdir-overlay option is deprecated in favor of --repositories-configuration option\n",
215                         level=logging.WARNING, noiselevel=-1)
216
217         for atom in args:
218                 try:
219                         atom = portage.dep.Atom(atom)
220                 except portage.exception.InvalidAtom:
221                         parser.error('Invalid atom: %s' % (atom,))
222
223                 if not isjustname(atom):
224                         parser.error('Atom is too specific: %s' % (atom,))
225
226         if options.update_use_local_desc:
227                 try:
228                         ElementTree
229                         ExpatError
230                 except NameError:
231                         parser.error('--update-use-local-desc requires python with USE=xml!')
232
233         if options.uld_output == '-' and options.preserve_comments:
234                 parser.error('--preserve-comments can not be used when outputting to stdout')
235
236         return parser, options, args
237
238 class GenCache(object):
239         def __init__(self, portdb, cp_iter=None, max_jobs=None, max_load=None,
240                 rsync=False):
241                 # The caller must set portdb.porttrees in order to constrain
242                 # findname, cp_list, and cpv_list to the desired tree.
243                 tree = portdb.porttrees[0]
244                 self._portdb = portdb
245                 self._eclass_db = portdb.repositories.get_repo_for_location(tree).eclass_db
246                 self._auxdbkeys = portdb._known_keys
247                 # We can globally cleanse stale cache only if we
248                 # iterate over every single cp.
249                 self._global_cleanse = cp_iter is None
250                 if cp_iter is not None:
251                         self._cp_set = set(cp_iter)
252                         cp_iter = iter(self._cp_set)
253                         self._cp_missing = self._cp_set.copy()
254                 else:
255                         self._cp_set = None
256                         self._cp_missing = set()
257                 write_auxdb = "metadata-transfer" in portdb.settings.features
258                 self._regen = MetadataRegen(portdb, cp_iter=cp_iter,
259                         consumer=self._metadata_callback,
260                         max_jobs=max_jobs, max_load=max_load,
261                         write_auxdb=write_auxdb, main=True)
262                 self.returncode = os.EX_OK
263                 conf = portdb.repositories.get_repo_for_location(tree)
264                 self._trg_caches = tuple(conf.iter_pregenerated_caches(
265                         self._auxdbkeys, force=True, readonly=False))
266                 if not self._trg_caches:
267                         raise Exception("cache formats '%s' aren't supported" %
268                                 (" ".join(conf.cache_formats),))
269
270                 if rsync:
271                         for trg_cache in self._trg_caches:
272                                 if hasattr(trg_cache, 'raise_stat_collision'):
273                                         trg_cache.raise_stat_collision = True
274                                         # Make _metadata_callback write this cache first, in case
275                                         # it raises a StatCollision and triggers mtime
276                                         # modification.
277                                         self._trg_caches = tuple([trg_cache] +
278                                                 [x for x in self._trg_caches if x is not trg_cache])
279
280                 self._existing_nodes = set()
281
282         def _metadata_callback(self, cpv, repo_path, metadata,
283                 ebuild_hash, eapi_supported):
284                 self._existing_nodes.add(cpv)
285                 self._cp_missing.discard(cpv_getkey(cpv))
286
287                 # Since we're supposed to be able to efficiently obtain the
288                 # EAPI from _parse_eapi_ebuild_head, we don't write cache
289                 # entries for unsupported EAPIs.
290                 if metadata is not None and eapi_supported:
291                         if metadata.get('EAPI') == '0':
292                                 del metadata['EAPI']
293                         for trg_cache in self._trg_caches:
294                                 self._write_cache(trg_cache,
295                                         cpv, repo_path, metadata, ebuild_hash)
296
297         def _write_cache(self, trg_cache, cpv, repo_path, metadata, ebuild_hash):
298
299                         if not hasattr(trg_cache, 'raise_stat_collision'):
300                                 # This cache does not avoid redundant writes automatically,
301                                 # so check for an identical existing entry before writing.
302                                 # This prevents unnecessary disk writes and can also prevent
303                                 # unnecessary rsync transfers.
304                                 try:
305                                         dest = trg_cache[cpv]
306                                 except (KeyError, CacheError):
307                                         pass
308                                 else:
309                                         if trg_cache.validate_entry(dest,
310                                                 ebuild_hash, self._eclass_db):
311                                                 identical = True
312                                                 for k in self._auxdbkeys:
313                                                         if dest.get(k, '') != metadata.get(k, ''):
314                                                                 identical = False
315                                                                 break
316                                                 if identical:
317                                                         return
318
319                         try:
320                                 chf = trg_cache.validation_chf
321                                 metadata['_%s_' % chf] = getattr(ebuild_hash, chf)
322                                 try:
323                                         trg_cache[cpv] = metadata
324                                 except StatCollision as sc:
325                                         # If the content of a cache entry changes and neither the
326                                         # file mtime nor size changes, it will prevent rsync from
327                                         # detecting changes. Cache backends may raise this
328                                         # exception from _setitem() if they detect this type of stat
329                                         # collision. These exceptions are handled by bumping the
330                                         # mtime on the ebuild (and the corresponding cache entry).
331                                         # See bug #139134. It is convenient to include checks for
332                                         # redundant writes along with the internal StatCollision
333                                         # detection code, so for caches with the
334                                         # raise_stat_collision attribute, we do not need to
335                                         # explicitly check for redundant writes like we do for the
336                                         # other cache types above.
337                                         max_mtime = sc.mtime
338                                         for ec, ec_hash in metadata['_eclasses_'].items():
339                                                 if max_mtime < ec_hash.mtime:
340                                                         max_mtime = ec_hash.mtime
341                                         if max_mtime == sc.mtime:
342                                                 max_mtime += 1
343                                         max_mtime = long(max_mtime)
344                                         try:
345                                                 os.utime(ebuild_hash.location, (max_mtime, max_mtime))
346                                         except OSError as e:
347                                                 self.returncode |= 1
348                                                 writemsg_level(
349                                                         "%s writing target: %s\n" % (cpv, e),
350                                                         level=logging.ERROR, noiselevel=-1)
351                                         else:
352                                                 ebuild_hash.mtime = max_mtime
353                                                 metadata['_mtime_'] = max_mtime
354                                                 trg_cache[cpv] = metadata
355                                                 self._portdb.auxdb[repo_path][cpv] = metadata
356
357                         except CacheError as ce:
358                                 self.returncode |= 1
359                                 writemsg_level(
360                                         "%s writing target: %s\n" % (cpv, ce),
361                                         level=logging.ERROR, noiselevel=-1)
362
363         def run(self):
364                 signum = run_main_scheduler(self._regen)
365                 if signum is not None:
366                         sys.exit(128 + signum)
367
368                 self.returncode |= self._regen.returncode
369
370                 for trg_cache in self._trg_caches:
371                         self._cleanse_cache(trg_cache)
372
373         def _cleanse_cache(self, trg_cache):
374                 cp_missing = self._cp_missing
375                 dead_nodes = set()
376                 if self._global_cleanse:
377                         try:
378                                 for cpv in trg_cache:
379                                         cp = cpv_getkey(cpv)
380                                         if cp is None:
381                                                 self.returncode |= 1
382                                                 writemsg_level(
383                                                         "Unable to parse cp for '%s'\n"  % (cpv,),
384                                                         level=logging.ERROR, noiselevel=-1)
385                                         else:
386                                                 dead_nodes.add(cpv)
387                         except CacheError as ce:
388                                 self.returncode |= 1
389                                 writemsg_level(
390                                         "Error listing cache entries for " + \
391                                         "'%s': %s, continuing...\n" % \
392                                         (trg_cache.location, ce),
393                                         level=logging.ERROR, noiselevel=-1)
394
395                 else:
396                         cp_set = self._cp_set
397                         try:
398                                 for cpv in trg_cache:
399                                         cp = cpv_getkey(cpv)
400                                         if cp is None:
401                                                 self.returncode |= 1
402                                                 writemsg_level(
403                                                         "Unable to parse cp for '%s'\n"  % (cpv,),
404                                                         level=logging.ERROR, noiselevel=-1)
405                                         else:
406                                                 cp_missing.discard(cp)
407                                                 if cp in cp_set:
408                                                         dead_nodes.add(cpv)
409                         except CacheError as ce:
410                                 self.returncode |= 1
411                                 writemsg_level(
412                                         "Error listing cache entries for " + \
413                                         "'%s': %s, continuing...\n" % \
414                                         (trg_cache.location, ce),
415                                         level=logging.ERROR, noiselevel=-1)
416
417                 if cp_missing:
418                         self.returncode |= 1
419                         for cp in sorted(cp_missing):
420                                 writemsg_level(
421                                         "No ebuilds or cache entries found for '%s'\n"  % (cp,),
422                                         level=logging.ERROR, noiselevel=-1)
423
424                 if dead_nodes:
425                         dead_nodes.difference_update(self._existing_nodes)
426                         for k in dead_nodes:
427                                 try:
428                                         del trg_cache[k]
429                                 except KeyError:
430                                         pass
431                                 except CacheError as ce:
432                                         self.returncode |= 1
433                                         writemsg_level(
434                                                 "%s deleting stale cache: %s\n" % (k, ce),
435                                                 level=logging.ERROR, noiselevel=-1)
436
437                 if not trg_cache.autocommits:
438                         try:
439                                 trg_cache.commit()
440                         except CacheError as ce:
441                                 self.returncode |= 1
442                                 writemsg_level(
443                                         "committing target: %s\n" % (ce,),
444                                         level=logging.ERROR, noiselevel=-1)
445
446                 if hasattr(trg_cache, '_prune_empty_dirs'):
447                         trg_cache._prune_empty_dirs()
448
449 class GenUseLocalDesc(object):
450         def __init__(self, portdb, output=None,
451                         preserve_comments=False):
452                 self.returncode = os.EX_OK
453                 self._portdb = portdb
454                 self._output = output
455                 self._preserve_comments = preserve_comments
456
457         def run(self):
458                 repo_path = self._portdb.porttrees[0]
459                 ops = {'<':0, '<=':1, '=':2, '>=':3, '>':4}
460
461                 if self._output is None or self._output != '-':
462                         if self._output is None:
463                                 prof_path = os.path.join(repo_path, 'profiles')
464                                 desc_path = os.path.join(prof_path, 'use.local.desc')
465                                 try:
466                                         os.mkdir(prof_path)
467                                 except OSError:
468                                         pass
469                         else:
470                                 desc_path = self._output
471
472                         try:
473                                 if self._preserve_comments:
474                                         # Probe in binary mode, in order to avoid
475                                         # potential character encoding issues.
476                                         output = open(_unicode_encode(desc_path,
477                                                 encoding=_encodings['fs'], errors='strict'), 'r+b')
478                                 else:
479                                         output = io.open(_unicode_encode(desc_path,
480                                                 encoding=_encodings['fs'], errors='strict'),
481                                                 mode='w', encoding=_encodings['repo.content'],
482                                                 errors='backslashreplace')
483                         except IOError as e:
484                                 if not self._preserve_comments or \
485                                         os.path.isfile(desc_path):
486                                         writemsg_level(
487                                                 "ERROR: failed to open output file %s: %s\n" \
488                                                 % (desc_path, e), level=logging.ERROR, noiselevel=-1)
489                                         self.returncode |= 2
490                                         return
491
492                                 # Open in r+b mode failed because the file doesn't
493                                 # exist yet. We can probably recover if we disable
494                                 # preserve_comments mode now.
495                                 writemsg_level(
496                                         "WARNING: --preserve-comments enabled, but " + \
497                                         "output file not found: %s\n" % (desc_path,),
498                                         level=logging.WARNING, noiselevel=-1)
499                                 self._preserve_comments = False
500                                 try:
501                                         output = io.open(_unicode_encode(desc_path,
502                                                 encoding=_encodings['fs'], errors='strict'),
503                                                 mode='w', encoding=_encodings['repo.content'],
504                                                 errors='backslashreplace')
505                                 except IOError as e:
506                                         writemsg_level(
507                                                 "ERROR: failed to open output file %s: %s\n" \
508                                                 % (desc_path, e), level=logging.ERROR, noiselevel=-1)
509                                         self.returncode |= 2
510                                         return
511                 else:
512                         output = sys.stdout
513
514                 if self._preserve_comments:
515                         while True:
516                                 pos = output.tell()
517                                 if not output.readline().startswith(b'#'):
518                                         break
519                         output.seek(pos)
520                         output.truncate()
521                         output.close()
522
523                         # Finished probing comments in binary mode, now append
524                         # in text mode.
525                         output = io.open(_unicode_encode(desc_path,
526                                 encoding=_encodings['fs'], errors='strict'),
527                                 mode='a', encoding=_encodings['repo.content'],
528                                 errors='backslashreplace')
529                         output.write('\n')
530                 else:
531                         output.write(textwrap.dedent('''\
532                                 # This file is deprecated as per GLEP 56 in favor of metadata.xml. Please add
533                                 # your descriptions to your package's metadata.xml ONLY.
534                                 # * generated automatically using egencache *
535
536                                 '''))
537
538                 # The cmp function no longer exists in python3, so we'll
539                 # implement our own here under a slightly different name
540                 # since we don't want any confusion given that we never
541                 # want to rely on the builtin cmp function.
542                 def cmp_func(a, b):
543                         if a is None or b is None:
544                                 # None can't be compared with other types in python3.
545                                 if a is None and b is None:
546                                         return 0
547                                 elif a is None:
548                                         return -1
549                                 else:
550                                         return 1
551                         return (a > b) - (a < b)
552
553                 class _MetadataTreeBuilder(ElementTree.TreeBuilder):
554                         """
555                         Implements doctype() as required to avoid deprecation warnings
556                         since Python >=2.7
557                         """
558                         def doctype(self, name, pubid, system):
559                                 pass
560
561                 for cp in self._portdb.cp_all():
562                         metadata_path = os.path.join(repo_path, cp, 'metadata.xml')
563                         try:
564                                 metadata = ElementTree.parse(_unicode_encode(metadata_path,
565                                         encoding=_encodings['fs'], errors='strict'),
566                                         parser=ElementTree.XMLParser(
567                                         target=_MetadataTreeBuilder()))
568                         except IOError:
569                                 pass
570                         except (ExpatError, EnvironmentError) as e:
571                                 writemsg_level(
572                                         "ERROR: failed parsing %s/metadata.xml: %s\n" % (cp, e),
573                                         level=logging.ERROR, noiselevel=-1)
574                                 self.returncode |= 1
575                         else:
576                                 try:
577                                         usedict = parse_metadata_use(metadata)
578                                 except portage.exception.ParseError as e:
579                                         writemsg_level(
580                                                 "ERROR: failed parsing %s/metadata.xml: %s\n" % (cp, e),
581                                                 level=logging.ERROR, noiselevel=-1)
582                                         self.returncode |= 1
583                                 else:
584                                         for flag in sorted(usedict):
585                                                 def atomcmp(atoma, atomb):
586                                                         # None is better than an atom, that's why we reverse the args
587                                                         if atoma is None or atomb is None:
588                                                                 return cmp_func(atomb, atoma)
589                                                         # Same for plain PNs (.operator is None then)
590                                                         elif atoma.operator is None or atomb.operator is None:
591                                                                 return cmp_func(atomb.operator, atoma.operator)
592                                                         # Version matching
593                                                         elif atoma.cpv != atomb.cpv:
594                                                                 return vercmp(atoma.version, atomb.version)
595                                                         # Versions match, let's fallback to operator matching
596                                                         else:
597                                                                 return cmp_func(ops.get(atoma.operator, -1),
598                                                                         ops.get(atomb.operator, -1))
599
600                                                 def _Atom(key):
601                                                         if key is not None:
602                                                                 return Atom(key)
603                                                         return None
604
605                                                 resdict = usedict[flag]
606                                                 if len(resdict) == 1:
607                                                         resdesc = next(iter(resdict.items()))[1]
608                                                 else:
609                                                         try:
610                                                                 reskeys = dict((_Atom(k), k) for k in resdict)
611                                                         except portage.exception.InvalidAtom as e:
612                                                                 writemsg_level(
613                                                                         "ERROR: failed parsing %s/metadata.xml: %s\n" % (cp, e),
614                                                                         level=logging.ERROR, noiselevel=-1)
615                                                                 self.returncode |= 1
616                                                                 resdesc = next(iter(resdict.items()))[1]
617                                                         else:
618                                                                 resatoms = sorted(reskeys, key=cmp_sort_key(atomcmp))
619                                                                 resdesc = resdict[reskeys[resatoms[-1]]]
620
621                                                 output.write('%s:%s - %s\n' % (cp, flag, resdesc))
622
623                 output.close()
624
625 if sys.hexversion < 0x3000000:
626         _filename_base = unicode
627 else:
628         _filename_base = str
629
630 class _special_filename(_filename_base):
631         """
632         Helps to sort file names by file type and other criteria.
633         """
634         def __new__(cls, status_change, file_name):
635                 return _filename_base.__new__(cls, status_change + file_name)
636
637         def __init__(self, status_change, file_name):
638                 _filename_base.__init__(status_change + file_name)
639                 self.status_change = status_change
640                 self.file_name = file_name
641                 self.file_type = guessManifestFileType(file_name)
642
643         def file_type_lt(self, a, b):
644                 """
645                 Defines an ordering between file types.
646                 """
647                 first = a.file_type
648                 second = b.file_type
649                 if first == second:
650                         return False
651
652                 if first == "EBUILD":
653                         return True
654                 elif first == "MISC":
655                         return second in ("EBUILD",)
656                 elif first == "AUX":
657                         return second in ("EBUILD", "MISC")
658                 elif first == "DIST":
659                         return second in ("EBUILD", "MISC", "AUX")
660                 elif first is None:
661                         return False
662                 else:
663                         raise ValueError("Unknown file type '%s'" % first)
664
665         def __lt__(self, other):
666                 """
667                 Compare different file names, first by file type and then
668                 for ebuilds by version and lexicographically for others.
669                 EBUILD < MISC < AUX < DIST < None
670                 """
671                 if self.__class__ != other.__class__:
672                         raise NotImplementedError
673
674                 # Sort by file type as defined by file_type_lt().
675                 if self.file_type_lt(self, other):
676                         return True
677                 elif self.file_type_lt(other, self):
678                         return False
679
680                 # Files have the same type.
681                 if self.file_type == "EBUILD":
682                         # Sort by version. Lowest first.
683                         ver = "-".join(pkgsplit(self.file_name[:-7])[1:3])
684                         other_ver = "-".join(pkgsplit(other.file_name[:-7])[1:3])
685                         return vercmp(ver, other_ver) < 0
686                 else:
687                         # Sort lexicographically.
688                         return self.file_name < other.file_name
689
690 class GenChangeLogs(object):
691         def __init__(self, portdb):
692                 self.returncode = os.EX_OK
693                 self._portdb = portdb
694                 self._wrapper = textwrap.TextWrapper(
695                                 width = 78,
696                                 initial_indent = '  ',
697                                 subsequent_indent = '  '
698                         )
699
700         @staticmethod
701         def grab(cmd):
702                 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
703                 return _unicode_decode(p.communicate()[0],
704                                 encoding=_encodings['stdio'], errors='strict')
705
706         def generate_changelog(self, cp):
707                 try:
708                         output = io.open('ChangeLog',
709                                 mode='w', encoding=_encodings['repo.content'],
710                                 errors='backslashreplace')
711                 except IOError as e:
712                         writemsg_level(
713                                 "ERROR: failed to open ChangeLog for %s: %s\n" % (cp,e,),
714                                 level=logging.ERROR, noiselevel=-1)
715                         self.returncode |= 2
716                         return
717
718                 output.write(textwrap.dedent('''\
719                         # ChangeLog for %s
720                         # Copyright 1999-%s Gentoo Foundation; Distributed under the GPL v2
721                         # $Header: $
722
723                         ''' % (cp, time.strftime('%Y'))))
724
725                 # now grab all the commits
726                 commits = self.grab(['git', 'rev-list', 'HEAD', '--', '.']).split()
727
728                 for c in commits:
729                         # Explaining the arguments:
730                         # --name-status to get a list of added/removed files
731                         # --no-renames to avoid getting more complex records on the list
732                         # --format to get the timestamp, author and commit description
733                         # --root to make it work fine even with the initial commit
734                         # --relative to get paths relative to ebuilddir
735                         # -r (recursive) to get per-file changes
736                         # then the commit-id and path.
737
738                         cinfo = self.grab(['git', 'diff-tree', '--name-status', '--no-renames',
739                                         '--format=%ct %cN <%cE>%n%B', '--root', '--relative', '-r',
740                                         c, '--', '.']).rstrip('\n').split('\n')
741
742                         # Expected output:
743                         # timestamp Author Name <author@email>
744                         # commit message l1
745                         # ...
746                         # commit message ln
747                         #
748                         # status1       filename1
749                         # ...
750                         # statusn       filenamen
751
752                         changed = []
753                         for n, l in enumerate(reversed(cinfo)):
754                                 if not l:
755                                         body = cinfo[1:-n-1]
756                                         break
757                                 else:
758                                         f = l.split()
759                                         if f[1] == 'Manifest':
760                                                 pass # XXX: remanifest commits?
761                                         elif f[1] == 'ChangeLog':
762                                                 pass
763                                         elif f[0].startswith('A'):
764                                                 changed.append(_special_filename("+", f[1]))
765                                         elif f[0].startswith('D'):
766                                                 changed.append(_special_filename("-", f[1]))
767                                         elif f[0].startswith('M'):
768                                                 changed.append(_special_filename("", f[1]))
769                                         else:
770                                                 writemsg_level(
771                                                         "ERROR: unexpected git file status for %s: %s\n" % (cp,f,),
772                                                         level=logging.ERROR, noiselevel=-1)
773                                                 self.returncode |= 1
774
775                         if not changed:
776                                 continue
777
778                         (ts, author) = cinfo[0].split(' ', 1)
779                         date = time.strftime('%d %b %Y', time.gmtime(float(ts)))
780
781                         changed = [str(x) for x in sorted(changed)]
782
783                         wroteheader = False
784                         # Reverse the sort order for headers.
785                         for c in reversed(changed):
786                                 if c.startswith('+') and c.endswith('.ebuild'):
787                                         output.write('*%s (%s)\n' % (c[1:-7], date))
788                                         wroteheader = True
789                         if wroteheader:
790                                 output.write('\n')
791
792                         # strip '<cp>: ', '[<cp>] ', and similar
793                         body[0] = re.sub(r'^\W*' + re.escape(cp) + r'\W+', '', body[0])
794                         # strip trailing newline
795                         if not body[-1]:
796                                 body = body[:-1]
797                         # strip git-svn id
798                         if body[-1].startswith('git-svn-id:') and not body[-2]:
799                                 body = body[:-2]
800                         # strip the repoman version/manifest note
801                         if body[-1] == ' (Signed Manifest commit)' or body[-1] == ' (Unsigned Manifest commit)':
802                                 body = body[:-1]
803                         if body[-1].startswith('(Portage version:') and body[-1].endswith(')'):
804                                 body = body[:-1]
805                                 if not body[-1]:
806                                         body = body[:-1]
807
808                         # don't break filenames on hyphens
809                         self._wrapper.break_on_hyphens = False
810                         output.write(self._wrapper.fill(
811                                 '%s; %s %s:' % (date, author, ', '.join(changed))))
812                         # but feel free to break commit messages there
813                         self._wrapper.break_on_hyphens = True
814                         output.write(
815                                 '\n%s\n\n' % '\n'.join(self._wrapper.fill(x) for x in body))
816
817                 output.close()
818
819         def run(self):
820                 repo_path = self._portdb.porttrees[0]
821                 os.chdir(repo_path)
822
823                 if 'git' not in FindVCS():
824                         writemsg_level(
825                                 "ERROR: --update-changelogs supported only in git repos\n",
826                                 level=logging.ERROR, noiselevel=-1)
827                         self.returncode = 127
828                         return
829
830                 for cp in self._portdb.cp_all():
831                         os.chdir(os.path.join(repo_path, cp))
832                         # Determine whether ChangeLog is up-to-date by comparing
833                         # the newest commit timestamp with the ChangeLog timestamp.
834                         lmod = self.grab(['git', 'log', '--format=%ct', '-1', '.'])
835                         if not lmod:
836                                 # This cp has not been added to the repo.
837                                 continue
838
839                         try:
840                                 cmod = os.stat('ChangeLog').st_mtime
841                         except OSError:
842                                 cmod = 0
843
844                         if float(cmod) < float(lmod):
845                                 self.generate_changelog(cp)
846
847 def egencache_main(args):
848
849         # The calling environment is ignored, so the program is
850         # completely controlled by commandline arguments.
851         env = {}
852
853         if not sys.stdout.isatty():
854                 portage.output.nocolor()
855                 env['NOCOLOR'] = 'true'
856
857         parser, options, atoms = parse_args(args)
858
859         config_root = options.config_root
860
861         if options.repositories_configuration is not None:
862                 env['PORTAGE_REPOSITORIES'] = options.repositories_configuration
863         elif options.portdir_overlay is not None:
864                 env['PORTDIR_OVERLAY'] = options.portdir_overlay
865
866         if options.cache_dir is not None:
867                 env['PORTAGE_DEPCACHEDIR'] = options.cache_dir
868
869         if options.portdir is not None:
870                 env['PORTDIR'] = options.portdir
871
872         settings = portage.config(config_root=config_root,
873                 local_config=False, env=env)
874
875         default_opts = None
876         if not options.ignore_default_opts:
877                 default_opts = portage.util.shlex_split(
878                         settings.get('EGENCACHE_DEFAULT_OPTS', ''))
879
880         if default_opts:
881                 parser, options, args = parse_args(default_opts + args)
882
883                 if options.cache_dir is not None:
884                         env['PORTAGE_DEPCACHEDIR'] = options.cache_dir
885
886                 settings = portage.config(config_root=config_root,
887                         local_config=False, env=env)
888
889         if not (options.update or options.update_use_local_desc or
890                         options.update_changelogs or options.update_manifests):
891                 parser.error('No action specified')
892                 return 1
893
894         if options.repo is None:
895                 if len(settings.repositories.prepos) == 2:
896                         for repo in settings.repositories:
897                                 if repo.name != "DEFAULT":
898                                         options.repo = repo.name
899                                         break
900
901                 if options.repo is None:
902                         parser.error("--repo option is required")
903
904         repo_path = settings.repositories.treemap.get(options.repo)
905         if repo_path is None:
906                 parser.error("Unable to locate repository named '%s'" % (options.repo,))
907                 return 1
908
909         repo_config = settings.repositories.get_repo_for_location(repo_path)
910
911         if options.strict_manifests is not None:
912                 if options.strict_manifests == "y":
913                         settings.features.add("strict")
914                 else:
915                         settings.features.discard("strict")
916
917         if options.update and 'metadata-transfer' not in settings.features:
918                 # Forcibly enable metadata-transfer if portdbapi has a pregenerated
919                 # cache that does not support eclass validation.
920                 cache = repo_config.get_pregenerated_cache(
921                         portage.dbapi.dbapi._known_keys, readonly=True)
922                 if cache is not None and not cache.complete_eclass_entries:
923                         settings.features.add('metadata-transfer')
924                 cache = None
925
926         settings.lock()
927
928         portdb = portage.portdbapi(mysettings=settings)
929
930         # Limit ebuilds to the specified repo.
931         portdb.porttrees = [repo_path]
932
933         if options.update:
934                 if options.cache_dir is not None:
935                         # already validated earlier
936                         pass
937                 else:
938                         # We check write access after the portdbapi constructor
939                         # has had an opportunity to create it. This ensures that
940                         # we don't use the cache in the "volatile" mode which is
941                         # undesirable for egencache.
942                         if not os.access(settings["PORTAGE_DEPCACHEDIR"], os.W_OK):
943                                 writemsg_level("ecachegen: error: " + \
944                                         "write access denied: %s\n" % (settings["PORTAGE_DEPCACHEDIR"],),
945                                         level=logging.ERROR, noiselevel=-1)
946                                 return 1
947
948         if options.sign_manifests is not None:
949                 repo_config.sign_manifest = options.sign_manifests == 'y'
950
951         if options.thin_manifests is not None:
952                 repo_config.thin_manifest = options.thin_manifests == 'y'
953
954         gpg_cmd = None
955         gpg_vars = None
956         force_sign_key = None
957
958         if options.update_manifests:
959                 if repo_config.sign_manifest:
960
961                         sign_problem = False
962                         gpg_dir = None
963                         gpg_cmd = settings.get("PORTAGE_GPG_SIGNING_COMMAND")
964                         if gpg_cmd is None:
965                                 writemsg_level("egencache: error: "
966                                         "PORTAGE_GPG_SIGNING_COMMAND is unset! "
967                                         "Is make.globals missing?\n",
968                                         level=logging.ERROR, noiselevel=-1)
969                                 sign_problem = True
970                         elif "${PORTAGE_GPG_KEY}" in gpg_cmd and \
971                                 options.gpg_key is None and \
972                                 "PORTAGE_GPG_KEY" not in settings:
973                                 writemsg_level("egencache: error: "
974                                         "PORTAGE_GPG_KEY is unset!\n",
975                                         level=logging.ERROR, noiselevel=-1)
976                                 sign_problem = True
977                         elif "${PORTAGE_GPG_DIR}" in gpg_cmd:
978                                 if options.gpg_dir is not None:
979                                         gpg_dir = options.gpg_dir
980                                 elif "PORTAGE_GPG_DIR" not in settings:
981                                         gpg_dir = os.path.expanduser("~/.gnupg")
982                                 else:
983                                         gpg_dir = os.path.expanduser(settings["PORTAGE_GPG_DIR"])
984                                 if not os.access(gpg_dir, os.X_OK):
985                                         writemsg_level(("egencache: error: "
986                                                 "Unable to access directory: "
987                                                 "PORTAGE_GPG_DIR='%s'\n") % gpg_dir,
988                                                 level=logging.ERROR, noiselevel=-1)
989                                         sign_problem = True
990
991                         if sign_problem:
992                                 writemsg_level("egencache: You may disable manifest "
993                                         "signatures with --sign-manifests=n or by setting "
994                                         "\"sign-manifests = false\" in metadata/layout.conf\n",
995                                         level=logging.ERROR, noiselevel=-1)
996                                 return 1
997
998                         gpg_vars = {}
999                         if gpg_dir is not None:
1000                                 gpg_vars["PORTAGE_GPG_DIR"] = gpg_dir
1001                         gpg_var_names = []
1002                         if options.gpg_key is None:
1003                                 gpg_var_names.append("PORTAGE_GPG_KEY")
1004                         else:
1005                                 gpg_vars["PORTAGE_GPG_KEY"] = options.gpg_key
1006
1007                         for k in gpg_var_names:
1008                                 v = settings.get(k)
1009                                 if v is not None:
1010                                         gpg_vars[k] = v
1011
1012                         force_sign_key = gpg_vars.get("PORTAGE_GPG_KEY")
1013
1014         ret = [os.EX_OK]
1015
1016         if options.update:
1017                 cp_iter = None
1018                 if atoms:
1019                         cp_iter = iter(atoms)
1020
1021                 gen_cache = GenCache(portdb, cp_iter=cp_iter,
1022                         max_jobs=options.jobs,
1023                         max_load=options.load_average,
1024                         rsync=options.rsync)
1025                 gen_cache.run()
1026                 if options.tolerant:
1027                         ret.append(os.EX_OK)
1028                 else:
1029                         ret.append(gen_cache.returncode)
1030
1031         if options.update_manifests:
1032
1033                 cp_iter = None
1034                 if atoms:
1035                         cp_iter = iter(atoms)
1036
1037                 event_loop = global_event_loop()
1038                 scheduler = ManifestScheduler(portdb, cp_iter=cp_iter,
1039                         gpg_cmd=gpg_cmd, gpg_vars=gpg_vars,
1040                         force_sign_key=force_sign_key,
1041                         max_jobs=options.jobs,
1042                         max_load=options.load_average,
1043                         event_loop=event_loop)
1044
1045                 signum = run_main_scheduler(scheduler)
1046                 if signum is not None:
1047                         sys.exit(128 + signum)
1048
1049                 if options.tolerant:
1050                         ret.append(os.EX_OK)
1051                 else:
1052                         ret.append(scheduler.returncode)
1053
1054         if options.update_use_local_desc:
1055                 gen_desc = GenUseLocalDesc(portdb,
1056                         output=options.uld_output,
1057                         preserve_comments=options.preserve_comments)
1058                 gen_desc.run()
1059                 ret.append(gen_desc.returncode)
1060
1061         if options.update_changelogs:
1062                 gen_clogs = GenChangeLogs(portdb)
1063                 gen_clogs.run()
1064                 ret.append(gen_clogs.returncode)
1065
1066         return max(ret)
1067
1068 if __name__ == "__main__":
1069         portage._disable_legacy_globals()
1070         portage.util.noiselimit = -1
1071         sys.exit(egencache_main(sys.argv[1:]))