Jan Borsodi avatar Jan Borsodi committed 4e873d6

scope-doc: The service and specs repos have been merged, the scope-doc command now only needs one repo/dir to work with.

Comments (0)

Files changed (2)

     def capture(self, args, **kwargs):
         return capture(args, cwd=self.cwd, **kwargs)
 
+def walkParents(path):
+    if not isdir(path):
+        path = dirname(path)
+    while path:
+        yield path
+        parent = dirname(path)
+        if parent == path or not parent:
+            break
+        path = parent
+
+
+def isHgDir(path):
+    for root in walkParents(path):
+        if exists(join(root, ".hg")):
+            return True
+    return False
+
 class DocGenerator(object):
     def __init__(self):
         self.sectionCount = 0
+        self.projects = {}
+        self.root = None
+        self.out_dir = None
+        self.build = None
+        self.build_dir = None
+        self.target = None
+        self.specs = None
+        self.specs_dir = None
+        self.doc_dir = None
+        self.service_source_dir = "services"
+        self.service_source_path = None
 
     def init(self):
         for path in (self.build, self.out_dir):
             if not isdir(path):
                 makedirs(path)
 
-        self.service_source_dir = self.projects["services"]["build"] or self.projects["services"]["path"]
         self.specs = self.projects["specs"]["context"]
         self.specs_dir = self.projects["specs"]["build"] or self.projects["specs"]["path"]
-        if self.service_dir:
-            if isabs(self.service_dir):
-                self.service_path = self.service_dir
-            else:
-                self.service_path = abspath(join(self.specs_dir, self.service_dir))
+        self.doc_dir = join(self.specs_dir, "doc")
+        if isabs(self.service_source_dir):
+            self.service_source_path = self.service_source_dir
         else:
-            self.service_path = self.specs_dir
+            self.service_source_path = join(self.specs_dir, self.service_source_dir)
+        self.service_out_path = join(self.doc_dir, "services")
 
         self.updateRepos()
 
+        if isHgDir(self.out_dir):
+            self.updateDoc(self.out_dir)
+
     def updateRepos(self):
         # Sync working directories from hg repos
         for project in self.projects.itervalues():
         rst_opts = []
         hob_version = capture(["hob", "--version"])[1].splitlines()[0]
         if Version(hob_version) > "0.1":
-            hob_opts = ["--project", join(abspath(self.service_source_dir), "hob.conf")]
+            hob_opts = ["--project", join(abspath(self.service_source_path), "hob.conf")]
             rst_opts = ["--syntax", self.syntax]
         else:
-            hob_opts = ["--config-file", join(abspath(self.service_dir), "hob.conf")]
+            hob_opts = ["--config-file", join(abspath(self.service_source_path), "hob.conf")]
         hob_opts += ["--target", self.target]
 
         if self.clean_service_dir:
-            self.newSection("CLEAN SERVICES %s" % self.service_path)
+            self.newSection("CLEAN SERVICES %s" % self.service_out_path)
             # Remove old service files
-            if self.service_path and exists(self.service_path):
-                for fname in listdir(self.service_path):
-                    fpath = join(self.service_path, fname)
+            if self.service_out_path and exists(self.service_out_path):
+                for fname in listdir(self.service_out_path):
+                    fpath = join(self.service_out_path, fname)
                     if isfile(fpath):
                         unlink(fpath)
         # Create rst-docs from .proto files using hob
-        self.newSection("SERVICES %s" % self.service_path)
+        self.newSection("SERVICES %s" % self.service_out_path)
         # TODO: It would been preferred if we could execute rst-doc directly, but it requires the ability
         #       to clone the current hob instance and setting basic settings such as --project
-        call(["hob"] + hob_opts + ["rst-doc", "--out-dir", self.service_path] + rst_opts)
+        call(["hob"] + hob_opts + ["rst-doc", "--out-dir", self.service_out_path] + rst_opts)
 
     def createDoc(self):
         # Create documentation with sphinx
         for sphinx_builder in self.sphinx_builders:
             self.newSection("SPHINX %s" % self.out_dir)
-            self.specs.call([self.sphinx, "-b", sphinx_builder, "-c", self.specs_dir, "-d", join(self.out_dir, "doctrees"), self.specs_dir, self.out_dir])
+            self.specs.call([self.sphinx, "-b", sphinx_builder, "-c", self.doc_dir, "-d", join(self.out_dir, "doctrees"), self.doc_dir, self.out_dir])
 
     def createIndex(self, index_script):
         if exists(index_script):
         else:
             call(["hg", "clone", root, context.cwd])
 
+    def updateDoc(self, path):
+        self.newSection("PULL UPSTREAM %s" % path)
+        context = Context(path)
+        context.call(["hg", "pull", "-u"])
+
     def commitDoc(self, context, message=None):
         if not message:
             message = "Updated scope documentation"
     proj["context"] = Context(proj["build"])
     return proj
 
-def scope_doc(ui, temp_dir, doc_root, scope_doc, target, specs_dir, specs_repo, syntax, index_script,
+def scope_doc(ui, temp_dir, root, scope_doc, target,
+              specs_dir, specs_repo, syntax, index_script,
               sphinx_cmd, sphinx_builder,
-              service_dir, service_repo, service_out_dir, service_cleanup,
+              service_path, service_out_dir, service_cleanup,
               commit, commit_message, push, push_dest,
               **kwargs):
     """Generate documentation for the scope protocol and related services.
     """
     gen = DocGenerator()
 
-    if not doc_root:
-        raise ProgramError("No --doc-root specified, cannot create documentation without it.")
-    gen.doc_root = doc_root
-    gen.out_dir = abspath(gen.doc_root)
+    if not root:
+        raise ProgramError("No --root specified, cannot create documentation without it.")
+    gen.root = root
+    gen.out_dir = abspath(gen.root)
     if scope_doc:
         if isabs(scope_doc):
             gen.out_dir = scope_doc
         sphinx_builder = ["html"]
     gen.sphinx_builders = sphinx_builder
     
-    gen.service_dir = service_out_dir
     gen.clean_service_dir = service_cleanup
 
     gen.projects = {}
         if not specs_dir:
             specs_dir = ""
         gen.projects["specs"] = pathProject(specs_dir)
-
-    if service_repo:
-        gen.projects["services"] = repoProject(service_repo, abspath(join(gen.build, "services")))
-    elif service_dir:
-        if not exists(service_dir):
-            raise ProgramError("--service-dir does not point to a valid folder")
-        gen.projects["services"] = pathProject(service_dir)
-    else:
-        raise ProgramError("No service files specified, either specify --service-dir or --service-repo")
+    if service_path:
+        self.service_source_dir = service_dir
 
     gen.init()
 
         gen.createIndex(index_script)
 
     if commit:
-        doc_context = Context(gen.doc_root)
+        doc_context = Context(gen.root)
         # TODO: Should figure out if there are any modified files present and only commit when modified
         gen.commitDoc(doc_context, commit_message)
         if push:
             gen.pushDoc(doc_context, push_dest)
+
+    gen.newSection("FINISHED")
+    print "Documentation has been generated in %s" % gen.out_dir
     [
       ('', 'temp_dir', None,
        _("Directory to use for temporary files (e.g. local clones of Mercurial repos). Default is to create one in the home folder under .hob (~/.hob)")),
-      ('', 'doc_root', None,
+      ('', 'root', None,
         _("Root directory where final documentation is located. See also --scope-doc.")),
       ('', 'scope_doc', 'scope',
-        _("Sub-directory where generated HTML documentation for scope is placed, this is either relative to --doc_root or absolute. Default is a sub-directory named 'scope'.")),
+        _("Sub-directory where generated HTML documentation for scope is placed, this is either relative to --root or absolute. Default is a sub-directory named 'scope'.")),
       ('', 'index_script', None,
         _("Path to script which is used to make the main documentation index. If a relative path (or just name) is used it will assume it is located in --doc-root")),
       ('', 'specs_dir', None,
-        _("Path to specs and documentation files. Selected services will be generated in a sub-folder called 'services'.")),
+        _("Path to specs, documentation and service files. Selected services will be generated in a sub-folder called 'doc/services'.")),
       ('', 'specs_repo', None,
-        _("""URL or path to Mercurial repository specs and documentation files. It will be cloned to a temporary folder.
+        _("""URL or path to Mercurial repository containing specs, documentation and service files. It will be cloned to a temporary folder.
           Overrides --specs-dir""")),
       ('', 'syntax', 'scope',
        _("Syntax to use for exported proto files. Default is 'scope'.")),
        _("The name of the sphinx tool to execute. Default is 'sphinx-build'")),
       ('', 'sphinx_builder', [],
        _("Which sphinx builder to use, default is 'html'. Specify multiple times for additional builders.")),
-      ('', 'service_dir', None,
-       _("Directory path where service files and project file (hob.conf) can be found.")),
-      ('', 'service_repo', None,
-        _("""URL or path to Mercurial repository containing service files. It will be cloned to a temporary folder.
-          Overrides --service-dir""")),
+      ('', 'service_path', None,
+       _("Relative or absolute directory path where service files and project file (hob.conf) can be found. Default is to look in the sub-folder 'services' in --specs-dir")),
       ('', 'service_out_dir', 'services',
        _("Path to where the generated service documentation files should be placed, either relative from specs folder or absolute. Default is 'services'")),
       ('', 'service_cleanup', False,
     [
      ],
     mutex=[('specs_dir', 'specs_repo'),
-           ('service_dir', 'service_repo')],
+           #('service_dir', 'service_repo'),
+           ],
     )
-def scope_doc(ui, temp_dir, doc_root, scope_doc, target, specs_dir, specs_repo, syntax, index_script,
+def scope_doc(ui, temp_dir, root, scope_doc, target,
+              specs_dir, specs_repo, syntax, index_script,
               sphinx_cmd, sphinx_builder,
-              service_dir, service_repo, service_out_dir, service_cleanup,
+              service_path, service_out_dir, service_cleanup,
               commit, commit_message, push, push_dest,
               **kwargs):
     """Generate documentation for the scope protocol and related services.
     """
     import hob._scope
-    hob._scope.scope_doc(ui, temp_dir, doc_root, scope_doc, target, specs_dir, specs_repo, syntax, index_script,
+    hob._scope.scope_doc(ui, temp_dir, root, scope_doc, target,
+                         specs_dir, specs_repo, syntax, index_script,
                          sphinx_cmd, sphinx_builder,
-                         service_dir, service_repo, service_out_dir, service_cleanup,
+                         service_path, service_out_dir, service_cleanup,
                          commit, commit_message, push, push_dest,
                          **kwargs)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.