1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import os
import os.path
import subprocess
import time
from sphinx.util import logging
from . import version
LOG = logging.getLogger(__name__)
_timeint = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
_default_last_updated = datetime.datetime.utcfromtimestamp(_timeint)
def _get_last_updated_file(src_file):
if not os.path.exists(src_file):
return None
try:
last_updated_t = subprocess.check_output(
[
'git', 'log', '-n1', '--format=%ad',
'--date=format:%Y-%m-%d %H:%M:%S',
'--', src_file,
]
).decode('utf-8').strip()
# NOTE: we catch any exception here (instead of
# subprocess.CalledProcessError and OSError) because some projects (eg.
# neutron) do import eventlet in docs/source/conf.py which will patch
# the subprocess module and with that, the exception is not catched
except Exception as err:
LOG.info(
'[openstackdocstheme] Could not get modification time of %s: %s',
src_file, err)
else:
if last_updated_t:
try:
return datetime.datetime.strptime(last_updated_t,
'%Y-%m-%d %H:%M:%S')
except ValueError:
LOG.info(
'[openstackdocstheme] '
'Could not parse modification time of %s: %r',
src_file, last_updated_t)
return None
def _get_last_updated(app, pagename):
last_updated = None
full_src_file = app.builder.env.doc2path(pagename)
candidates = []
# Strip the prefix from the filename so the git command recognizes
# the file as part of the current repository.
src_file = full_src_file[len(str(app.builder.env.srcdir)):].lstrip('/')
candidates.append(src_file)
if not os.path.exists(src_file):
# Some of the files are in doc/source and some are not. Some
# of the ones that are not are symlinked. If we can't find the
# file after stripping the full prefix, try looking for it in
# doc/source explicitly.
candidates.append(os.path.join('doc/source', src_file))
for filename in candidates:
last_updated = _get_last_updated_file(filename)
if last_updated:
LOG.debug(
'[openstackdocstheme] Last updated for %s is %s',
pagename, last_updated)
return last_updated
if pagename not in ('genindex', 'search'):
LOG.info(
'[openstackdocstheme] could not determine last_updated for %r',
pagename,
)
return _default_last_updated
def html_page_context(app, pagename, templatename, context, doctree):
# Use the last modified date from git instead of applying a single
# value to the entire site.
context['last_updated'] = _get_last_updated(app, pagename)
def setup(app):
LOG.info('[openstackdocstheme] connecting html-page-context event handler')
app.connect('html-page-context', html_page_context)
return {
'parallel_read_safe': True,
'parallel_write_safe': True,
'version': version.version_info.version_string(),
}
|