Skip to content

Commit

Permalink
Corrects copyright year using SOURCE_DATE_EPOCH if set
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Bienvenüe committed Apr 28, 2016
1 parent 390ee6f commit 7a89015
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
12 changes: 11 additions & 1 deletion sphinx/config.py
Expand Up @@ -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
Expand All @@ -19,6 +19,7 @@
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]')

Expand Down Expand Up @@ -298,6 +299,15 @@ def __init__(self, dirname, filename, overrides, tags):
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
Expand Down
2 changes: 1 addition & 1 deletion sphinx/util/i18n.py
Expand Up @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions tests/roots/test-correct-year/conf.py
@@ -0,0 +1,3 @@

copyright = u'2006-2009, Author'

4 changes: 4 additions & 0 deletions tests/roots/test-correct-year/contents.rst
@@ -0,0 +1,4 @@
=================
test-correct-year
=================

47 changes: 47 additions & 0 deletions tests/test_correct_year.py
@@ -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

0 comments on commit 7a89015

Please sign in to comment.