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