Corrects copyright year using SOURCE_DATE_EPOCH if set

This commit is contained in:
Alexis Bienvenüe
2016-04-28 23:37:36 +02:00
parent 390ee6fcd8
commit 7a89015a54
5 changed files with 66 additions and 2 deletions

View File

@@ -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,6 +19,7 @@ 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]')
@@ -298,6 +299,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 getenv('SOURCE_DATE_EPOCH') is not None:
for k in ['copyright','epub_copyright']:
if k in config:
config[k] = re.sub('^((\d{4}-)?)(\d{4})(?=[ ,])',
'\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

View File

@@ -188,7 +188,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()

View File

@@ -0,0 +1,3 @@
copyright = u'2006-2009, Author'

View File

@@ -0,0 +1,4 @@
=================
test-correct-year
=================

View File

@@ -0,0 +1,47 @@
# -*- 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():
# 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
# Restores SOURCE_DATE_EPOCH
if sde == None:
os.environ.pop('SOURCE_DATE_EPOCH',None)
else:
os.environ['SOURCE_DATE_EPOCH'] = sde