>From 1f0e03e5a3e83d271c4aef05dbbb4850f66cee42 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 5 Aug 2022 14:17:35 +0200 Subject: [PATCH 02/12] gnulib-tool.py: Make option --version work. * pygnulib/constants.py (__copyright__): Bump copyright year. * pygnulib/GLInfo.py (GLInfo.authors): Add a comma after the second-to-last author. (GLInfo.copyright): Show only the last modification year. (GLInfo.date): Check whether git and GNU date are available. Use 'git log ChangeLog', not 'git log'. Run 'git log' in the gnulib directory, not in the current directory. Search for 'Date:' only at the beginning of a line. As a fallback, look at the first ChangeLog entry. (GLInfo.version): Check whether git is available. Run git-version-gen in the gnulib directory, not in the current directory. Replace '-dirty' with '-modified'. As a fallback, return the empty string. * gnulib-tool.py (main) [--version]: Add a space before the version. --- ChangeLog | 14 +++++++ gnulib-tool.py | 5 ++- gnulib-tool.py.TODO | 1 - pygnulib/GLInfo.py | 90 ++++++++++++++++++++++++++++--------------- pygnulib/constants.py | 2 +- 5 files changed, 78 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index e504d85215..81871735d2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,19 @@ 2022-08-05 Bruno Haible + gnulib-tool.py: Make option --version work. + * pygnulib/constants.py (__copyright__): Bump copyright year. + * pygnulib/GLInfo.py (GLInfo.authors): Add a comma after the + second-to-last author. + (GLInfo.copyright): Show only the last modification year. + (GLInfo.date): Check whether git and GNU date are available. Use + 'git log ChangeLog', not 'git log'. Run 'git log' in the gnulib + directory, not in the current directory. Search for 'Date:' only at the + beginning of a line. As a fallback, look at the first ChangeLog entry. + (GLInfo.version): Check whether git is available. Run git-version-gen in + the gnulib directory, not in the current directory. Replace '-dirty' + with '-modified'. As a fallback, return the empty string. + * gnulib-tool.py (main) [--version]: Add a space before the version. + gnulib-tool.py: Simplify. * pygnulib/constants.py (compiler): Remove function. * gnulib-tool.py: Use re.compile directly instead. diff --git a/gnulib-tool.py b/gnulib-tool.py index 1a6bd9e948..c4386133a6 100755 --- a/gnulib-tool.py +++ b/gnulib-tool.py @@ -397,8 +397,11 @@ def main(): print(info.usage()) sys.exit(0) if cmdargs.version != None: + version = info.version() + if version != '': + version = ' ' + version message = '''gnulib-tool (%s %s)%s\n%s\n%s\n\nWritten by %s.''' \ - % (info.package(), info.date(), info.version(), info.copyright(), + % (info.package(), info.date(), version, info.copyright(), info.license(), info.authors()) print(message) sys.exit(0) diff --git a/gnulib-tool.py.TODO b/gnulib-tool.py.TODO index 7170268b0e..de3a90dc37 100644 --- a/gnulib-tool.py.TODO +++ b/gnulib-tool.py.TODO @@ -40,7 +40,6 @@ Implement the options: -S | --more-symlinks -H | --more-hardlinks --help (same output) - --version -------------------------------------------------------------------------------- diff --git a/pygnulib/GLInfo.py b/pygnulib/GLInfo.py index 34b38db7eb..5e386e7c34 100644 --- a/pygnulib/GLInfo.py +++ b/pygnulib/GLInfo.py @@ -65,9 +65,7 @@ class GLInfo(object): The special __author__ variable is used (type is list).''' result = '' for item in __author__: - if item == __author__[-2]: - result += '%s ' % item - elif item == __author__[-1]: + if item == __author__[-1]: result += 'and %s' % item else: result += '%s, ' % item @@ -75,38 +73,58 @@ class GLInfo(object): def license(self): '''Return formatted string which contains license and its description.''' - result = 'License GPLv3+: GNU GPL version 3 or later' - result += ' \n' - result += 'This is free software: you are free' - result += ' to change and redistribute it.\n' + result = 'License GPLv3+: GNU GPL version 3 or later \n' + result += 'This is free software: you are free to change and redistribute it.\n' result += 'There is NO WARRANTY, to the extent permitted by law.' return result def copyright(self): '''Return formatted string which contains copyright. The special __copyright__ variable is used (type is str).''' - result = 'Copyright (C) %s' % __copyright__ + copyright = __copyright__ + # Per the GNU Coding Standards, show only the last year. + copyright = re.compile('^[0-9]*-').sub('', copyright) + result = 'Copyright (C) %s' % copyright return result def date(self): '''Return formatted string which contains date and time in GMT format.''' if isdir(DIRS['git']): - counter = int() # Create counter - result = '' # Create string - args = ['git', 'log'] - result = sp.check_output(args).decode("UTF-8") - # Get date as "Fri Mar 21 07:16:51 2008 -0600" from string - pattern = re.compile('Date:[\t ]*(.*?)$', re.S | re.M) - result = pattern.findall(result)[0] - # Turn "Fri Mar 21 07:16:51 2008 -0600" into "Mar 21 2008 07:16:51 -0600" - pattern = re.compile('^[^ ]* ([^ ]*) ([0-9]*) ([0-9:]*) ([0-9]*) ') - result = pattern.sub('\\1 \\2 \\4 \\3 ', result) - # Use GNU date to compute the time in GMT - args = ['date', '-d', result, '-u', '+%Y-%m-%d %H:%M:%S'] - proc = sp.check_output(args) - result = str(proc, "UTF-8") - result = result.rstrip(os.linesep) - return result + have_git = None + try: + sp.check_call(['git', '--version'], stdout=sp.DEVNULL) + have_git = True + except: + have_git = False + if have_git: + have_GNU_date = None + try: + sp.check_call(['date', '--version'], stdout=sp.DEVNULL) + have_GNU_date = True + except: + have_GNU_date = False + if have_GNU_date: + args = ['git', 'log', '-n', '1', 'ChangeLog'] + result = sp.check_output(args, cwd=DIRS['root']).decode("UTF-8") + # Get date as "Fri Mar 21 07:16:51 2008 -0600" from string + pattern = re.compile('^Date:[\t ]*(.*?)$', re.M) + result = pattern.findall(result)[0] + # Turn "Fri Mar 21 07:16:51 2008 -0600" into "Mar 21 2008 07:16:51 -0600" + pattern = re.compile('^[^ ]* ([^ ]*) ([0-9]*) ([0-9:]*) ([0-9]*) ') + result = pattern.sub('\\1 \\2 \\4 \\3 ', result) + # Use GNU date to compute the time in GMT + args = ['date', '-d', result, '-u', '+%Y-%m-%d %H:%M:%S'] + proc = sp.check_output(args) + result = str(proc, "UTF-8") + result = result.rstrip(os.linesep) + return result + # gnulib copy without versioning information. + first_changelog_line = None + with codecs.open(os.path.join(DIRS['root'], 'ChangeLog'), 'rb', 'UTF-8') as file: + line = file.readline() + first_changelog_line = line.rstrip() + result = re.compile(' .*').sub('', first_changelog_line) + return result def usage(self): '''Show help message.''' @@ -289,10 +307,20 @@ Report bugs to .''' def version(self): '''Return formatted string which contains git version.''' if isdir(DIRS['git']): - version_gen = joinpath(DIRS['build-aux'], 'git-version-gen') - args = [version_gen, DIRS['root']] - result = sp.check_output(args).decode("UTF-8") - result = result.strip() - if result == 'UNKNOWN': - result = '' - return result + have_git = None + try: + sp.check_call(['git', '--version'], stdout=sp.DEVNULL) + have_git = True + except: + have_git = False + if have_git: + version_gen = joinpath(DIRS['build-aux'], 'git-version-gen') + args = [version_gen, '/dev/null'] + result = sp.check_output(args, cwd=DIRS['root']).decode("UTF-8") + result = result.strip() + result = result.replace('-dirty', '-modified') + if result == 'UNKNOWN': + result = '' + return result + # gnulib copy without versioning information. + return '' diff --git a/pygnulib/constants.py b/pygnulib/constants.py index 4eff6da8bc..ffee05846e 100644 --- a/pygnulib/constants.py +++ b/pygnulib/constants.py @@ -41,7 +41,7 @@ __author__ = \ 'Dmitriy Selyutin', ] __license__ = 'GNU GPLv3+' -__copyright__ = '2002-2017 Free Software Foundation, Inc.' +__copyright__ = '2002-2022 Free Software Foundation, Inc.' #=============================================================================== -- 2.34.1