From 126fe9a6de1493f0dfcd0707cf6e36d3620026d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Sat, 28 Aug 2010 23:59:34 +0200 Subject: egencache: Add comment preserving to --update-use-local-desc. --- bin/egencache | 37 ++++++++++++++++++++++++++++++++----- man/egencache.1 | 10 +++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/bin/egencache b/bin/egencache index 5ae15bc4f..b0d727e96 100755 --- a/bin/egencache +++ b/bin/egencache @@ -98,6 +98,9 @@ def parse_args(args): parser.add_option_group(update) uld = optparse.OptionGroup(parser, '--update-use-local-desc options') + uld.add_option("--preserve-comments", + action="store_true", + help="preserve the comments from the existing use.local.desc file") uld.add_option("--use-local-desc-output", help="output file for use.local.desc data (or '-' for stdout)", dest="uld_output") @@ -161,6 +164,9 @@ def parse_args(args): except NameError: parser.error('--update-use-local-desc requires python with USE=xml!') + if options.uld_output == '-' and options.preserve_comments: + parser.error('--preserve-comments can not be used when outputting to stdout') + return parser, options, args class GenCache(object): @@ -313,10 +319,12 @@ class GenCache(object): level=logging.ERROR, noiselevel=-1) class GenUseLocalDesc(object): - def __init__(self, portdb, output=None): + def __init__(self, portdb, output=None, + preserve_comments=False): self.returncode = os.EX_OK self._portdb = portdb self._output = output + self._preserve_comments = preserve_comments def run(self): repo_path = self._portdb.porttrees[0] @@ -333,18 +341,36 @@ class GenUseLocalDesc(object): else: desc_path = self._output + if self._preserve_comments: + output_mode = 'r+' + else: + output_mode = 'w' + try: - output = open(desc_path, 'w') + output = open(desc_path, output_mode) except IOError as e: writemsg_level( - "ERROR: failed to open output file: %s\n" % (e,), + "ERROR: failed to open output file %s: %s\n" % (output_mode,e,), level=logging.ERROR, noiselevel=-1) self.returncode |= 2 return else: output = sys.stdout - output.write(''' + if self._preserve_comments: + while True: + l = output.readline() + # Seek through comments, until the first non-comment. + # If we hit empty line, then great. + # Otherwise, we need to seek back and add an empty line. + if not l.startswith('#'): + if l.rstrip() != '': + output.seek(-len(l), os.SEEK_CUR) + output.write('\n') + break + output.truncate() + else: + output.write(''' # This file is deprecated as per GLEP 56 in favor of metadata.xml. Please add # your descriptions to your package's metadata.xml ONLY. # * generated automatically using egencache * @@ -498,7 +524,8 @@ def egencache_main(args): if options.update_use_local_desc: gen_desc = GenUseLocalDesc(portdb, - output=options.uld_output) + output=options.uld_output, + preserve_comments=options.preserve_comments) gen_desc.run() ret.append(gen_desc.returncode) diff --git a/man/egencache.1 b/man/egencache.1 index ec5121eee..f32427bbe 100644 --- a/man/egencache.1 +++ b/man/egencache.1 @@ -41,15 +41,19 @@ Also see the related \fB\-\-load\-average\fR option. .BR \-\-load\-average=LOAD Specifies that maximum load allowed when spawning multiple jobs. .TP +.BR "\-\-portdir=PORTDIR" +Override the portage tree location. +.TP +.BR "\-\-preserve\-comments" +Preserve the comments found in the output use.local.desc file. This requires +the output file to exist before egencache is called. +.TP .BR "\-\-repo=REPO" Name of the repo to operate on (default repo is located at \fBPORTDIR\fR). The name should correspond the value of a \fBrepo_name\fR entry (see \fBportage\fR(5)) from one of the repositories that is configured via the \fBPORTDIR\fR or \fBPORTDIR_OVERLAY\fR variables (see \fBmake.conf\fR(5)). .TP -.BR "\-\-portdir=PORTDIR" -Override the portage tree location. -.TP .BR "\-\-rsync" When used together with the \fB\-\-update\fR action, this enables a workaround for cases in which the content of a cache entry changes and neither the file -- cgit v1.2.3-1-g7c22