2013-11-21 00:37:22 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, # You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-11-10 23:37:38 +03:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2013-11-21 00:37:22 +04:00
|
|
|
|
|
|
|
import os
|
2015-11-10 23:37:38 +03:00
|
|
|
import sys
|
2018-04-06 17:52:56 +03:00
|
|
|
from functools import partial
|
2013-11-21 00:37:22 +04:00
|
|
|
|
|
|
|
from mach.decorators import (
|
2015-11-10 23:37:38 +03:00
|
|
|
Command,
|
2013-11-21 00:37:22 +04:00
|
|
|
CommandArgument,
|
|
|
|
CommandProvider,
|
|
|
|
)
|
|
|
|
|
2017-09-14 23:17:09 +03:00
|
|
|
import which
|
2013-11-21 00:37:22 +04:00
|
|
|
from mozbuild.base import MachCommandBase
|
|
|
|
|
2018-02-12 17:36:12 +03:00
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
2013-11-21 00:37:22 +04:00
|
|
|
|
|
|
|
@CommandProvider
|
|
|
|
class Documentation(MachCommandBase):
|
|
|
|
"""Helps manage in-tree documentation."""
|
|
|
|
|
2015-11-03 23:39:50 +03:00
|
|
|
@Command('doc', category='devenv',
|
2018-04-06 17:52:56 +03:00
|
|
|
description='Generate and serve documentation from the tree.')
|
2018-04-06 17:30:31 +03:00
|
|
|
@CommandArgument('path', default=None, metavar='DIRECTORY', nargs='?',
|
|
|
|
help='Path to documentation to build and display.')
|
2018-04-06 17:52:56 +03:00
|
|
|
@CommandArgument('--format', default='html', dest='fmt',
|
2017-08-24 20:11:55 +03:00
|
|
|
help='Documentation format to write.')
|
2015-11-10 23:37:38 +03:00
|
|
|
@CommandArgument('--outdir', default=None, metavar='DESTINATION',
|
2017-08-24 20:11:55 +03:00
|
|
|
help='Where to write output.')
|
2017-08-24 20:37:53 +03:00
|
|
|
@CommandArgument('--archive', action='store_true',
|
2018-04-06 17:52:56 +03:00
|
|
|
help='Write a gzipped tarball of generated docs.')
|
2017-08-24 20:11:55 +03:00
|
|
|
@CommandArgument('--no-open', dest='auto_open', default=True,
|
|
|
|
action='store_false',
|
|
|
|
help="Don't automatically open HTML docs in a browser.")
|
2018-04-06 17:52:56 +03:00
|
|
|
@CommandArgument('--no-serve', dest='serve', default=True, action='store_false',
|
|
|
|
help="Don't serve the generated docs after building.")
|
|
|
|
@CommandArgument('--http', default='localhost:5500', metavar='ADDRESS',
|
|
|
|
help='Serve documentation on the specified host and port, '
|
|
|
|
'default "localhost:5500".')
|
2017-08-24 21:31:54 +03:00
|
|
|
@CommandArgument('--upload', action='store_true',
|
2018-04-06 17:52:56 +03:00
|
|
|
help='Upload generated files to S3.')
|
|
|
|
def build_docs(self, path=None, fmt='html', outdir=None, auto_open=True,
|
|
|
|
serve=True, http=None, archive=False, upload=False):
|
2017-09-14 23:17:09 +03:00
|
|
|
try:
|
2018-03-15 13:34:03 +03:00
|
|
|
which.which('jsdoc')
|
2017-09-14 23:17:09 +03:00
|
|
|
except which.WhichError:
|
|
|
|
return die('jsdoc not found - please install from npm.')
|
|
|
|
|
2013-11-21 00:37:22 +04:00
|
|
|
self._activate_virtualenv()
|
2018-04-06 17:23:49 +03:00
|
|
|
self.virtualenv_manager.install_pip_requirements(
|
|
|
|
os.path.join(here, 'requirements.txt'), quiet=True)
|
2013-11-21 00:37:22 +04:00
|
|
|
|
2017-08-24 20:37:53 +03:00
|
|
|
import moztreedocs
|
2018-04-06 17:52:56 +03:00
|
|
|
import webbrowser
|
|
|
|
from livereload import Server
|
2013-11-21 00:37:22 +04:00
|
|
|
|
2018-04-06 17:30:31 +03:00
|
|
|
outdir = outdir or os.path.join(self.topobjdir, 'docs')
|
2018-04-06 17:52:56 +03:00
|
|
|
format_outdir = os.path.join(outdir, fmt)
|
2013-11-21 00:37:22 +04:00
|
|
|
|
2018-04-06 17:30:31 +03:00
|
|
|
path = path or os.path.join(self.topsrcdir, 'tools')
|
|
|
|
path = os.path.normpath(os.path.abspath(path))
|
|
|
|
|
|
|
|
docdir = self._find_doc_dir(path)
|
|
|
|
if not docdir:
|
|
|
|
return die('failed to generate documentation:\n'
|
|
|
|
'%s: could not find docs at this location' % path)
|
|
|
|
|
|
|
|
props = self._project_properties(docdir)
|
|
|
|
savedir = os.path.join(format_outdir, props['project'])
|
|
|
|
|
2018-04-06 17:52:56 +03:00
|
|
|
run_sphinx = partial(self._run_sphinx, docdir, savedir, fmt)
|
|
|
|
result = run_sphinx()
|
2018-04-06 17:30:31 +03:00
|
|
|
if result != 0:
|
|
|
|
return die('failed to generate documentation:\n'
|
|
|
|
'%s: sphinx return code %d' % (path, result))
|
|
|
|
else:
|
|
|
|
print('\nGenerated documentation:\n%s' % savedir)
|
|
|
|
|
|
|
|
if archive:
|
|
|
|
archive_path = os.path.join(outdir,
|
|
|
|
'%s.tar.gz' % props['project'])
|
|
|
|
moztreedocs.create_tarball(archive_path, savedir)
|
|
|
|
print('Archived to %s' % archive_path)
|
|
|
|
|
|
|
|
if upload:
|
|
|
|
self._s3_upload(savedir, props['project'], props['version'])
|
|
|
|
|
2018-04-06 17:52:56 +03:00
|
|
|
if not serve:
|
|
|
|
index_path = os.path.join(savedir, 'index.html')
|
|
|
|
if auto_open and os.path.isfile(index_path):
|
|
|
|
webbrowser.open(index_path)
|
|
|
|
return
|
2015-11-10 23:37:38 +03:00
|
|
|
|
2018-04-06 17:52:56 +03:00
|
|
|
# Create livereload server. Any files modified in the specified docdir
|
|
|
|
# will cause a re-build and refresh of the browser (if open).
|
|
|
|
try:
|
2015-11-10 23:37:38 +03:00
|
|
|
host, port = http.split(':', 1)
|
2018-04-06 17:52:56 +03:00
|
|
|
port = int(port)
|
|
|
|
except ValueError:
|
|
|
|
return die('invalid address: %s' % http)
|
|
|
|
|
|
|
|
server = Server()
|
|
|
|
server.watch(docdir, run_sphinx)
|
|
|
|
server.serve(host=host, port=port, root=savedir,
|
|
|
|
open_url_delay=0.1 if auto_open else None)
|
|
|
|
|
|
|
|
def _run_sphinx(self, docdir, savedir, fmt='html'):
|
|
|
|
import sphinx
|
|
|
|
args = [
|
|
|
|
'sphinx',
|
|
|
|
'-b', fmt,
|
|
|
|
docdir,
|
|
|
|
savedir,
|
|
|
|
]
|
|
|
|
return sphinx.build_main(args)
|
2015-11-03 23:39:50 +03:00
|
|
|
|
2017-08-24 21:12:21 +03:00
|
|
|
def _project_properties(self, path):
|
2015-11-03 23:39:50 +03:00
|
|
|
import imp
|
|
|
|
path = os.path.join(path, 'conf.py')
|
|
|
|
with open(path, 'r') as fh:
|
|
|
|
conf = imp.load_module('doc_conf', fh, path,
|
|
|
|
('.py', 'r', imp.PY_SOURCE))
|
|
|
|
|
2017-08-24 21:12:21 +03:00
|
|
|
# Prefer the Mozilla project name, falling back to Sphinx's
|
|
|
|
# default variable if it isn't defined.
|
|
|
|
project = getattr(conf, 'moz_project_name', None)
|
|
|
|
if not project:
|
|
|
|
project = conf.project.replace(' ', '_')
|
|
|
|
|
|
|
|
return {
|
|
|
|
'project': project,
|
|
|
|
'version': getattr(conf, 'version', None)
|
|
|
|
}
|
2015-11-03 23:39:50 +03:00
|
|
|
|
|
|
|
def _find_doc_dir(self, path):
|
|
|
|
search_dirs = ('doc', 'docs')
|
|
|
|
for d in search_dirs:
|
|
|
|
p = os.path.join(path, d)
|
|
|
|
if os.path.isfile(os.path.join(p, 'conf.py')):
|
|
|
|
return p
|
2015-11-10 23:37:38 +03:00
|
|
|
|
2017-08-24 21:12:21 +03:00
|
|
|
def _s3_upload(self, root, project, version=None):
|
2017-07-28 21:32:04 +03:00
|
|
|
self.virtualenv_manager.install_pip_package('boto3==1.4.4')
|
|
|
|
|
2017-08-24 21:07:10 +03:00
|
|
|
from moztreedocs import distribution_files
|
2017-08-24 20:49:28 +03:00
|
|
|
from moztreedocs.upload import s3_upload
|
2017-08-24 21:12:21 +03:00
|
|
|
|
|
|
|
# Files are uploaded to multiple locations:
|
|
|
|
#
|
|
|
|
# <project>/latest
|
|
|
|
# <project>/<version>
|
|
|
|
#
|
|
|
|
# This allows multiple projects and versions to be stored in the
|
|
|
|
# S3 bucket.
|
|
|
|
|
|
|
|
files = list(distribution_files(root))
|
|
|
|
|
|
|
|
s3_upload(files, key_prefix='%s/latest' % project)
|
|
|
|
if version:
|
|
|
|
s3_upload(files, key_prefix='%s/%s' % (project, version))
|
|
|
|
|
|
|
|
# Until we redirect / to main/latest, upload the main docs
|
|
|
|
# to the root.
|
|
|
|
if project == 'main':
|
|
|
|
s3_upload(files)
|
2017-08-24 20:49:28 +03:00
|
|
|
|
2015-11-10 23:37:38 +03:00
|
|
|
|
|
|
|
def die(msg, exit_code=1):
|
|
|
|
msg = '%s: %s' % (sys.argv[0], msg)
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
return exit_code
|