Bug 1454640 - [docs] Lazy load the package and version properties draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Wed, 18 Apr 2018 17:31:10 -0400
changeset 793776 80bd7d63ebb3f56e07e378a873f039f56779b691
parent 793775 3d7db5c7db49b75aa47c4de3ca4c6c1af8371de1
push id109495
push userahalberstadt@mozilla.com
push dateThu, 10 May 2018 18:20:10 +0000
bugs1454640
milestone62.0a1
Bug 1454640 - [docs] Lazy load the package and version properties We no longer store the docs under a project name (since all the docs are now built using the root conf.py). This mean the name and version are only used for packaging and uploading, which typically is only used in CI. This allows us to lazy load the package name and version, so we only read the conf.py when we need to. MozReview-Commit-ID: DV5Jxrbskoh
tools/docs/mach_commands.py
--- a/tools/docs/mach_commands.py
+++ b/tools/docs/mach_commands.py
@@ -23,16 +23,18 @@ here = os.path.abspath(os.path.dirname(_
 @CommandProvider
 class Documentation(MachCommandBase):
     """Helps manage in-tree documentation."""
 
     def __init__(self, context):
         super(Documentation, self).__init__(context)
 
         self._manager = None
+        self._project = None
+        self._version = None
 
     @Command('doc', category='devenv',
              description='Generate and serve documentation from the tree.')
     @CommandArgument('path', default=None, metavar='DIRECTORY', nargs='?',
                      help='Path to documentation to build and display.')
     @CommandArgument('--format', default='html', dest='fmt',
                      help='Documentation format to write.')
     @CommandArgument('--outdir', default=None, metavar='DESTINATION',
@@ -70,32 +72,30 @@ class Documentation(MachCommandBase):
         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)
         result = self._run_sphinx(docdir, savedir, fmt=fmt)
         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'])
+            archive_path = os.path.join(outdir, '%s.tar.gz' % self.project)
             create_tarball(archive_path, savedir)
             print('Archived to %s' % archive_path)
 
         if upload:
-            self._s3_upload(savedir, props['project'], props['version'])
+            self._s3_upload(savedir, self.project, self.version)
 
         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
 
         # Create livereload server. Any files modified in the specified docdir
@@ -129,33 +129,43 @@ class Documentation(MachCommandBase):
 
     @property
     def manager(self):
         if not self._manager:
             from moztreedocs import manager
             self._manager = manager
         return self._manager
 
-    def _project_properties(self, path):
+    def _read_project_properties(self):
         import imp
         path = os.path.normpath(self.manager.conf_py_path)
         with open(path, 'r') as fh:
             conf = imp.load_module('doc_conf', fh, path,
                                    ('.py', 'r', imp.PY_SOURCE))
 
         # 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)
-        }
+        self._project = project,
+        self._version = getattr(conf, 'version', None)
+
+    @property
+    def project(self):
+        if not self._project:
+            self._read_project_properties()
+        return self._project
+
+    @property
+    def version(self):
+        if not self._version:
+            self._read_project_properties()
+        return self._version
 
     def _find_doc_dir(self, path):
         if os.path.isfile(path):
             return
 
         valid_doc_dirs = ('doc', 'docs')
         if os.path.basename(path) in valid_doc_dirs:
             return path