Pārlūkot izejas kodu

utils/mkhtml.py: fix get and set addon manual page source and history URL link (#1892)

During g.extension compile (make command) addon, SOURCE_URL (which is addon
URL for downloading) global variable is setted, e.g. for i.sentinel addon is
SOURCE_URL=https://github.com/OSGeo/grass-addons/branches/grass8/src/imagery/
i.sentinel.

mkhtml.py script is runned and handle SOURCE_URL.

Next is find addon (get_addon_path() function) in GRASS GIS GitHub official addon
repository and return addon path e.g. for i.sentinel.coverage is addon path
/src/imagery/i.sentinel/i.sentinel.coverage.

Create final URL e.g. for i.sentinel.coverage  url_source=https://github.com/OSGeo/
grass-addons/branches/grass8/src/imagery/i.sentinel/i.sentinel.coverage.

For addon source code URL e.g. i.sentinel.coverage replace in url_source var, branches
string with tree string https://github.com/OSGeo/grass-addons/tree/grass8/src/imagery/
i.sentinel/i.sentinel.coverage.

For addon source code history URL e.g. i.sentinel.coverage replace in url_source var,
branches string with commits string https://github.com/OSGeo/grass-addons/commits/
grass8/src/imagery/i.sentinel/i.sentinel.coverage.
Tomas Zigo 3 gadi atpakaļ
vecāks
revīzija
563797cd3b
1 mainītis faili ar 27 papildinājumiem un 37 dzēšanām
  1. 27 37
      utils/mkhtml.py

+ 27 - 37
utils/mkhtml.py

@@ -23,6 +23,7 @@ import re
 from datetime import datetime
 from datetime import datetime
 import locale
 import locale
 import json
 import json
+import pathlib
 
 
 try:
 try:
     # Python 2 import
     # Python 2 import
@@ -288,28 +289,22 @@ def update_toc(data):
     return "\n".join(ret_data)
     return "\n".join(ret_data)
 
 
 
 
-def get_addon_path(pgm):
-    """Check if pgm is in addons list and get addon path
+def get_addon_path():
+    """Check if pgm is in the addons list and get addon path
 
 
-    :param pgm str: pgm
-
-    :return tuple: (True, path) if pgm is addon else (None, None)
+    return: pgm path if pgm is addon else None
     """
     """
     addon_base = os.getenv("GRASS_ADDON_BASE")
     addon_base = os.getenv("GRASS_ADDON_BASE")
     if addon_base:
     if addon_base:
-        """'addons_paths.json' is file created during install extension
-        check get_addons_paths() function in the g.extension.py file
-        """
+        # addons_paths.json is file created during install extension
+        # check get_addons_paths() function in the g.extension.py file
         addons_paths = os.path.join(addon_base, "addons_paths.json")
         addons_paths = os.path.join(addon_base, "addons_paths.json")
         if os.path.exists(addons_paths):
         if os.path.exists(addons_paths):
-            with open(addons_paths, "r") as f:
+            with open(addons_paths) as f:
                 addons_paths = json.load(f)
                 addons_paths = json.load(f)
             for addon in addons_paths["tree"]:
             for addon in addons_paths["tree"]:
-                split_path = addon["path"].split("/")
-                root_dir, module_dir = split_path[0], split_path[-1]
-                if "grass8" == root_dir and pgm == module_dir:
-                    return True, addon["path"]
-    return None, None
+                if pgm == pathlib.Path(addon["path"]).name:
+                    return addon["path"]
 
 
 
 
 # process header
 # process header
@@ -437,38 +432,33 @@ else:
     pgmdir = os.path.sep.join(curdir.split(os.path.sep)[-3:])
     pgmdir = os.path.sep.join(curdir.split(os.path.sep)[-3:])
 url_source = ""
 url_source = ""
 if os.getenv("SOURCE_URL", ""):
 if os.getenv("SOURCE_URL", ""):
-    # addons
-    for prefix in index_names.keys():
-        cwd = os.getcwd()
-        idx = cwd.find("{0}{1}.".format(os.path.sep, prefix))
-        if idx > -1:
-            pgmname = cwd[idx + 1 :]
-            classname = index_names[prefix]
-            url_source = urlparse.urljoin(
-                "{0}{1}/".format(os.environ["SOURCE_URL"], classname), pgmname
-            )
-            break
+    addon_path = get_addon_path()
+    if addon_path:
+        url_source = urlparse.urljoin(
+            os.environ["SOURCE_URL"].split("src")[0],
+            addon_path,
+        )
 else:
 else:
     url_source = urlparse.urljoin(source_url, pgmdir)
     url_source = urlparse.urljoin(source_url, pgmdir)
 if sys.platform == "win32":
 if sys.platform == "win32":
     url_source = url_source.replace(os.path.sep, "/")
     url_source = url_source.replace(os.path.sep, "/")
 
 
 if index_name:
 if index_name:
-    tree = "grass/tree"
-    commits = "grass/commits"
-    is_addon, addon_path = get_addon_path(pgm=pgm)
-    if is_addon:
-        # Fix gui/wxpython addon url path
-        url_source = urlparse.urljoin(
-            os.environ["SOURCE_URL"],
-            addon_path.split("/", 1)[1],
-        )
-        tree = "grass-addons/tree"
-        commits = "grass-addons/commits"
+    branches = "branches"
+    tree = "tree"
+    commits = "commits"
+
+    if branches in url_source:
+        url_log = url_source.replace(branches, commits)
+        url_source = url_source.replace(branches, tree)
+    else:
+        url_log = url_source.replace(tree, commits)
 
 
     sys.stdout.write(
     sys.stdout.write(
         sourcecode.substitute(
         sourcecode.substitute(
-            URL_SOURCE=url_source, PGM=pgm, URL_LOG=url_source.replace(tree, commits)
+            URL_SOURCE=url_source,
+            PGM=pgm,
+            URL_LOG=url_log,
         )
         )
     )
     )
     sys.stdout.write(
     sys.stdout.write(