Przeglądaj źródła

g.extension: support tar without root directory, fix install prefix for relative paths, add tests

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@65757 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 9 lat temu
rodzic
commit
cdfe17ef35

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

@@ -919,13 +919,14 @@ def move_extracted_files(extract_dir, target_dir, files):
     if len(files) == 1:
     if len(files) == 1:
         shutil.copytree(os.path.join(extract_dir, files[0]), target_dir)
         shutil.copytree(os.path.join(extract_dir, files[0]), target_dir)
     else:
     else:
+        os.mkdir(target_dir)
         for file_name in files:
         for file_name in files:
             actual_file = os.path.join(extract_dir, file_name)
             actual_file = os.path.join(extract_dir, file_name)
             if os.path.isdir(actual_file):
             if os.path.isdir(actual_file):
                 shutil.copytree(actual_file,
                 shutil.copytree(actual_file,
                                 os.path.join(target_dir, file_name))
                                 os.path.join(target_dir, file_name))
             else:
             else:
-                shutil.copy(actual_file, target_dir)
+                shutil.copy(actual_file, os.path.join(target_dir, file_name))
 
 
 
 
 # Original copyright and license of the original version of the CRLF function
 # Original copyright and license of the original version of the CRLF function
@@ -1022,6 +1023,7 @@ def download_source_code(source, url, name, outdev,
         grass.fatal(_("Unknown extension (addon) source type '{}'."
         grass.fatal(_("Unknown extension (addon) source type '{}'."
                       " Please report this to the grass-user mailing list.")
                       " Please report this to the grass-user mailing list.")
                     .format(source))
                     .format(source))
+    assert os.path.isdir(directory)
 
 
 
 
 def install_extension_std_platforms(name, source, url):
 def install_extension_std_platforms(name, source, url):
@@ -1372,7 +1374,7 @@ def resolve_install_prefix(path, to_system):
     # together with file names
     # together with file names
     if not path.endswith(os.path.sep):
     if not path.endswith(os.path.sep):
         path = path + os.path.sep
         path = path + os.path.sep
-    return path
+    return os.path.abspath(path)  # make likes absolute paths
 
 
 
 
 def resolve_xmlurl_prefix(url):
 def resolve_xmlurl_prefix(url):
@@ -1447,7 +1449,7 @@ def resolve_known_host_service(url):
                             _("Not using {service} as known hosting service"
                             _("Not using {service} as known hosting service"
                               " because the URL ends with '{suffix}'")
                               " because the URL ends with '{suffix}'")
                                 .format(service=key, suffix=suffix))
                                 .format(service=key, suffix=suffix))
-                        return None
+                        return None, None
     if match:
     if match:
         if not actual_start:
         if not actual_start:
             actual_start = match['url_start']
             actual_start = match['url_start']
@@ -1460,7 +1462,7 @@ def resolve_known_host_service(url):
                         .format(url))
                         .format(url))
         return 'remote_zip', url
         return 'remote_zip', url
     else:
     else:
-        return None
+        return None, None
 
 
 
 
 def resolve_source_code(url):
 def resolve_source_code(url):
@@ -1529,14 +1531,14 @@ def resolve_source_code(url):
         return 'dir', os.path.abspath(url)
         return 'dir', os.path.abspath(url)
     elif os.path.exists(url):
     elif os.path.exists(url):
         if url.endswith('.zip'):
         if url.endswith('.zip'):
-                return 'zip', os.path.abspath(url)
+            return 'zip', os.path.abspath(url)
         for suffix in extract_tar.supported_formats:
         for suffix in extract_tar.supported_formats:
             if url.endswith('.' + suffix):
             if url.endswith('.' + suffix):
                 return suffix, os.path.abspath(url)
                 return suffix, os.path.abspath(url)
     else:
     else:
-        result = resolve_known_host_service(url)
-        if result:
-            return result
+        source, url = resolve_known_host_service(url)
+        if source:
+            return source, url
         # we allow URL to end with =zip or ?zip and not only .zip
         # we allow URL to end with =zip or ?zip and not only .zip
         # unfortunately format=zip&version=89612 would require something else
         # unfortunately format=zip&version=89612 would require something else
         # special option to force the source type would solve it
         # special option to force the source type would solve it

BIN
scripts/g.extension/testsuite/data/sample_modules/r.plus.example.tar.gz


BIN
scripts/g.extension/testsuite/data/sample_modules/r.plus.example.zip


+ 7 - 0
scripts/g.extension/testsuite/data/sample_modules/r.plus.example/Makefile

@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = r.plus.example
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

+ 25 - 0
scripts/g.extension/testsuite/data/sample_modules/r.plus.example/r.plus.example.html

@@ -0,0 +1,25 @@
+<h2>DESCRIPTION</h2>
+
+<em>r.plus.example</em>
+
+<h2>NOTES</h2>
+
+<h2>EXAMPLES</h2>
+
+<div class="code"><pre>
+r.plus.example
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="g.region.html">g.region</a>
+</em>
+
+
+<h2>AUTHORS</h2>
+
+Vaclav Petras, NCSU OSGeoREL<br>
+
+<p>
+<i>Last changed: $Date: 2015-06-07 07:21:31 -0400 (Sun, 07 Jun 2015) $</i>

+ 36 - 0
scripts/g.extension/testsuite/data/sample_modules/r.plus.example/r.plus.example.py

@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+#%module
+#% description: Adds the values of two rasters (A + B)
+#% keyword: raster
+#% keyword: algebra
+#% keyword: sum
+#%end
+#%option G_OPT_R_INPUT
+#% key: araster
+#% description: Name of input raster A in an expression A + B
+#%end
+#%option G_OPT_R_INPUT
+#% key: braster
+#% description: Name of input raster B in an expression A + B
+#%end
+#%option G_OPT_R_OUTPUT
+#%end
+
+
+import grass.script as gscript
+
+
+def main():
+    options, flags = gscript.parser()
+    araster = options['araster']
+    braster = options['braster']
+    output = options['output']
+
+    gscript.mapcalc('{r} = {a} + {b}'.format(r=output, a=araster, b=braster))
+
+    return 0
+
+
+if __name__ == "__main__":
+    main()

BIN
scripts/g.extension/testsuite/data/sample_modules/r.plus.example_sep.tar.gz


+ 65 - 1
scripts/g.extension/testsuite/test_addons_modules.py

@@ -15,6 +15,7 @@ COPYRIGHT: (C) 2015 Vaclav Petras, and by the GRASS Development Team
 from grass.gunittest.case import TestCase
 from grass.gunittest.case import TestCase
 from grass.gunittest.main import test
 from grass.gunittest.main import test
 from grass.gunittest.gmodules import SimpleModule
 from grass.gunittest.gmodules import SimpleModule
+from grass.gunittest.utils import silent_rmtree
 
 
 import os
 import os
 
 
@@ -49,7 +50,7 @@ class TestModulesMetadata(TestCase):
 
 
     url = 'file://' + os.path.abspath('data')
     url = 'file://' + os.path.abspath('data')
 
 
-    def test_limits(self):
+    def test_listing(self):
         """Test if results is in expected limits"""
         """Test if results is in expected limits"""
         module = SimpleModule('g.extension', flags='l', svnurl=self.url)
         module = SimpleModule('g.extension', flags='l', svnurl=self.url)
         self.assertModule(module)
         self.assertModule(module)
@@ -57,5 +58,68 @@ class TestModulesMetadata(TestCase):
         self.assertMultiLineEqual(stdout, MODULES_OUTPUT)
         self.assertMultiLineEqual(stdout, MODULES_OUTPUT)
 
 
 
 
+class TestModulesFromDifferentSources(TestCase):
+
+    url = 'file://' + os.path.abspath('data/sample_modules')
+    path = os.path.join('data', 'sample_modules')
+    install_prefix = 'gextension_test_install_path'
+    # TODO: this is wrong for MS Win
+    files = [
+        os.path.join(install_prefix, 'scripts', 'r.plus.example'),
+        os.path.join(install_prefix, 'docs', 'html', 'r.plus.example.html'),
+    ]
+    # to create archives from the source, the following was used:
+    # zip r.plus.example.zip r.plus.example/*
+    # tar czvf r.plus.example.tar.gz r.plus.example
+    # cd r.plus.example/
+    # tar czvf ../r.plus.example_sep.tar.gz *    
+
+    def setUp(self):
+        """Make sure we are not dealing with some old files"""
+        if os.path.exists(self.install_prefix):
+            files = os.listdir(self.install_prefix)
+            if files:
+                RuntimeError("Install prefix path '{}' contains files {}"
+                             .format(self.install_prefix, files))
+
+    def tearDown(self):
+        """Remove created files"""
+        silent_rmtree(self.install_prefix)
+
+    def test_directory_install(self):
+        """Test if results is in expected limits"""
+        self.assertModule('g.extension', extension='r.plus.example',
+                          svnurl=os.path.join(self.path, 'r.plus.example'),
+                          prefix=self.install_prefix)
+        # TODO: this is wrong for MS Win
+        for file in self.files:
+            self.assertFileExists(file)
+
+    def test_targz_install(self):
+        """Test if results is in expected limits"""
+        self.assertModule('g.extension', extension='r.plus.example',
+                          svnurl=os.path.join(self.path,
+                                              'r.plus.example.tar.gz'),
+                          prefix=self.install_prefix)
+        for file in self.files:
+            self.assertFileExists(file)
+
+    def test_remote_targz_without_dir_install(self):
+        """Test if results is in expected limits"""
+        self.assertModule('g.extension', extension='r.plus.example',
+                          svnurl=self.url + '/' + 'r.plus.example_sep.tar.gz',
+                          prefix=self.install_prefix, verbose=True)
+        for file in self.files:
+            self.assertFileExists(file)
+
+    def test_remote_zip_install(self):
+        """Test if results is in expected limits"""
+        self.assertModule('g.extension', extension='r.plus.example',
+                          svnurl=self.url + '/' + 'r.plus.example.zip',
+                          prefix=self.install_prefix)
+        for file in self.files:
+            self.assertFileExists(os.path.join(file))
+
+
 if __name__ == '__main__':
 if __name__ == '__main__':
     test()
     test()

+ 1 - 1
scripts/g.extension/testsuite/test_addons_toolboxes.py

@@ -40,7 +40,7 @@ class TestToolboxesMetadata(TestCase):
 
 
     url = 'file://' + os.path.abspath('data')
     url = 'file://' + os.path.abspath('data')
 
 
-    def test_limits(self):
+    def test_listing(self):
         """Test if results is in expected limits"""
         """Test if results is in expected limits"""
         module = SimpleModule('g.extension', flags='lt', svnurl=self.url)
         module = SimpleModule('g.extension', flags='lt', svnurl=self.url)
         self.assertModule(module)
         self.assertModule(module)