[Python-modules-commits] [sphinx] 01/03: Extend SOURCE_DATE_EPOCH support

Dmitry Shachnev mitya57 at moszumanska.debian.org
Tue May 17 11:36:02 UTC 2016


This is an automated email from the git hooks/post-receive script.

mitya57 pushed a commit to branch master
in repository sphinx.

commit 0281df8904d5caaff3c42b908bd057f42e6731bb
Author: Alexis Bienvenüe <pado at passoire.fr>
Date:   Tue May 17 14:28:48 2016 +0300

    Extend SOURCE_DATE_EPOCH support
    
    Taken from: https://github.com/sphinx-doc/sphinx/pull/2503
    Fixes: https://bugs.debian.org/820895
---
 sphinx/builders/gettext.py |  8 +++++++-
 sphinx/config.py           | 13 +++++++++++-
 sphinx/util/i18n.py        |  3 +--
 tests/test_correct_year.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/sphinx/builders/gettext.py b/sphinx/builders/gettext.py
index fce6c8c..1c47893 100644
--- a/sphinx/builders/gettext.py
+++ b/sphinx/builders/gettext.py
@@ -11,7 +11,7 @@
 
 from __future__ import unicode_literals
 
-from os import path, walk
+from os import path, walk, getenv
 from codecs import open
 from time import time
 from datetime import datetime, tzinfo, timedelta
@@ -130,6 +130,12 @@ class I18nBuilder(Builder):
 timestamp = time()
 tzdelta = datetime.fromtimestamp(timestamp) - \
     datetime.utcfromtimestamp(timestamp)
+# set timestamp from SOURCE_DATE_EPOCH if set
+# see https://reproducible-builds.org/specs/source-date-epoch/
+source_date_epoch = getenv('SOURCE_DATE_EPOCH')
+if source_date_epoch is not None:
+    timestamp = float(source_date_epoch)
+    tzdelta = 0
 
 
 class LocalTimeZone(tzinfo):
diff --git a/sphinx/config.py b/sphinx/config.py
index e199e7d..707f162 100644
--- a/sphinx/config.py
+++ b/sphinx/config.py
@@ -10,7 +10,7 @@
 """
 
 import re
-from os import path, environ
+from os import path, environ, getenv
 import shlex
 
 from six import PY2, PY3, iteritems, string_types, binary_type, text_type, integer_types
@@ -19,8 +19,10 @@ from sphinx.errors import ConfigError
 from sphinx.locale import l_
 from sphinx.util.osutil import make_filename, cd
 from sphinx.util.pycompat import execfile_, NoneType
+from sphinx.util.i18n import format_date
 
 nonascii_re = re.compile(br'[\x80-\xff]')
+copyright_year_re = re.compile(r'^((\d{4}-)?)(\d{4})(?=[ ,])')
 
 CONFIG_SYNTAX_ERROR = "There is a syntax error in your configuration file: %s"
 if PY3:
@@ -298,6 +300,15 @@ class Config(object):
         self.setup = config.get('setup', None)
         self.extensions = config.get('extensions', [])
 
+        # correct values of copyright year that are not coherent with
+        # the SOURCE_DATE_EPOCH environment variable (if set)
+        # See https://reproducible-builds.org/specs/source-date-epoch/
+        if getenv('SOURCE_DATE_EPOCH') is not None:
+            for k in ('copyright', 'epub_copyright'):
+                if k in config:
+                    config[k] = copyright_year_re.sub('\g<1>%s' % format_date('%Y'),
+                                                      config[k])
+
     def check_types(self, warn):
         # check all values for deviation from the default value's type, since
         # that can result in TypeErrors all over the place
diff --git a/sphinx/util/i18n.py b/sphinx/util/i18n.py
index f4a8bce..d0cb1f4 100644
--- a/sphinx/util/i18n.py
+++ b/sphinx/util/i18n.py
@@ -14,7 +14,6 @@ import os
 import re
 import warnings
 from os import path
-from time import gmtime
 from datetime import datetime
 from collections import namedtuple
 
@@ -188,7 +187,7 @@ def format_date(format, date=None, language=None, warn=None):
         # See https://wiki.debian.org/ReproducibleBuilds/TimestampsProposal
         source_date_epoch = os.getenv('SOURCE_DATE_EPOCH')
         if source_date_epoch is not None:
-            date = gmtime(float(source_date_epoch))
+            date = datetime.utcfromtimestamp(float(source_date_epoch))
         else:
             date = datetime.now()
 
diff --git a/tests/test_correct_year.py b/tests/test_correct_year.py
new file mode 100644
index 0000000..7a156cb
--- /dev/null
+++ b/tests/test_correct_year.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+"""
+    test_correct_year
+    ~~~~~~~~~~~~~~~~~
+
+    Test copyright year adjustment
+
+    :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+import os
+
+from util import TestApp
+
+
+def test_correct_year():
+    try:
+        # save current value of SOURCE_DATE_EPOCH
+        sde = os.environ.pop('SOURCE_DATE_EPOCH',None)
+
+        # test with SOURCE_DATE_EPOCH unset: no modification
+        app = TestApp(buildername='html',testroot='correct-year')
+        app.builder.build_all()
+        content = (app.outdir / 'contents.html').text()
+        app.cleanup()
+        assert '2006-2009' in content
+
+        # test with SOURCE_DATE_EPOCH set: copyright year should be
+        # updated
+        os.environ['SOURCE_DATE_EPOCH'] = "1293840000"
+        app = TestApp(buildername='html',testroot='correct-year')
+        app.builder.build_all()
+        content = (app.outdir / 'contents.html').text()
+        app.cleanup()
+        assert '2006-2011' in content
+
+        os.environ['SOURCE_DATE_EPOCH'] = "1293839999"
+        app = TestApp(buildername='html',testroot='correct-year')
+        app.builder.build_all()
+        content = (app.outdir / 'contents.html').text()
+        app.cleanup()
+        assert '2006-2010' in content
+
+    finally:
+        # Restores SOURCE_DATE_EPOCH
+        if sde == None:
+            os.environ.pop('SOURCE_DATE_EPOCH',None)
+        else:
+            os.environ['SOURCE_DATE_EPOCH'] = sde

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/sphinx.git



More information about the Python-modules-commits mailing list