Browse Source

g.extension: add '-j' flag which generates JSON file containing the download URLs of the official Addons (#1009)

* g.extension: rename get Add-Ons paths json file function

* mkhtml: fix get correct Add-On path

* g.extension: fix function arg syntax

* g.extension: add '-j' flag which generates JSON file containing the download URLs of the official Addons
Tomas Zigo 4 years ago
parent
commit
93f6ee6c05
2 changed files with 25 additions and 10 deletions
  1. 22 8
      scripts/g.extension/g.extension.py
  2. 3 2
      tools/mkhtml.py

+ 22 - 8
scripts/g.extension/g.extension.py

@@ -118,9 +118,16 @@
 #% description: Operate on toolboxes instead of single modules (experimental)
 #% suppress_required: yes
 #%end
+#%flag
+#% key: j
+#% description: Generates JSON file containing the download URLs of the official Addons
+#% guisection: Install
+#% suppress_required: yes
+#%end
+
 
 #%rules
-#% required: extension, -l, -c, -g, -a
+#% required: extension, -l, -c, -g, -a, -j
 #% exclusive: extension, -l, -c, -g
 #% exclusive: extension, -l, -c, -a
 #%end
@@ -166,9 +173,10 @@ PROXIES = {}
 HTTP_STATUS_CODES = list(http.HTTPStatus)
 
 
-def urlopen_(url, response_format, headers={}, *args, **kwargs):
-    """Wrapper around urlopen. Same function as 'urlopen', but with the
-    ability to define headers.
+def download_addons_paths_file(
+        url, response_format, headers={}, *args, **kwargs):
+    """Generates JSON file containing the download URLs of the official
+    Addons
 
     :param str url: url address
     :param str response_format: content type
@@ -2175,22 +2183,24 @@ def resolve_source_code(url=None, name=None):
         return 'svn', url
 
 
-def get_addons_paths():
+def get_addons_paths(gg_addons_base_dir):
     """Get and save extensions paths as 'extensions_paths.json' json file
     in the $GRASS_ADDON_BASE dir. The file serves as a list of all addons,
     and their paths (mkhmtl.py tool)
     """
+    get_addons_paths.json_file = 'addons_paths.json'
+
     url = 'https://api.github.com/repos/OSGeo/grass-addons/git/trees/'\
         'master?recursive=1'
     addons_paths = json.loads(
         gscript.decode(
-            urlopen_(
+            download_addons_paths_file(
                 url=url,
                 response_format='application/json',
             ).read(),
         )
     )
-    with open(os.path.join(options['prefix'], 'addons_paths.json'),
+    with open(os.path.join(gg_addons_base_dir, get_addons_paths.json_file),
               'w') as f:
         json.dump(addons_paths, f)
 
@@ -2216,6 +2226,10 @@ def main():
     options['prefix'] = resolve_install_prefix(path=options['prefix'],
                                                to_system=flags['s'])
 
+    if flags['j']:
+        get_addons_paths(gg_addons_base_dir=options['prefix'])
+        return 0
+
     # list available extensions
     if flags['l'] or flags['c'] or (flags['g'] and not flags['a']):
         # using dummy extension, we don't need any extension URL now,
@@ -2242,7 +2256,7 @@ def main():
 
     if options['operation'] == 'add':
         check_dirs()
-        get_addons_paths()
+        get_addons_paths(gg_addons_base_dir=options['prefix'])
         source, url = resolve_source_code(name=options['extension'],
                                           url=original_url)
         xmlurl = resolve_xmlurl_prefix(original_url, source=source)

+ 3 - 2
tools/mkhtml.py

@@ -35,7 +35,6 @@ try:
 except:
     import urllib.parse as urlparse
 
-
 if sys.version_info[0] == 2:
     PY2 = True
 else:
@@ -281,7 +280,9 @@ def get_addon_path(pgm):
             with open(addons_paths, 'r') as f:
                 addons_paths = json.load(f)
             for addon in addons_paths['tree']:
-                if pgm in addon['path']:
+                split_path = addon['path'].split('/')
+                root_dir, module_dir = split_path[0], split_path[-1]
+                if 'grass7' == root_dir and pgm == module_dir:
                     return True, addon['path']
     return None, None