[GRASS-SVN] r65756 - in grass/trunk/scripts/g.extension: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 21 07:00:53 PDT 2015


Author: wenzeslaus
Date: 2015-07-21 07:00:53 -0700 (Tue, 21 Jul 2015)
New Revision: 65756

Modified:
   grass/trunk/scripts/g.extension/g.extension.py
   grass/trunk/scripts/g.extension/testsuite/doctest.sh
Log:
g.extension: proper implementation for tar related archives

Modified: grass/trunk/scripts/g.extension/g.extension.py
===================================================================
--- grass/trunk/scripts/g.extension/g.extension.py	2015-07-21 11:53:43 UTC (rev 65755)
+++ grass/trunk/scripts/g.extension/g.extension.py	2015-07-21 14:00:53 UTC (rev 65756)
@@ -915,6 +915,7 @@
     a different directory in the way that if there was one direcory extracted,
     the contained files are moved.
     """
+    gscript.debug("move_extracted_files({})".format(locals()))
     if len(files) == 1:
         shutil.copytree(os.path.join(extract_dir, files[0]), target_dir)
     else:
@@ -954,30 +955,40 @@
 
 def extract_zip(name, directory, tmpdir):
     """Extract a ZIP file into a directory"""
-    zip_file = zipfile.ZipFile(name, mode='r')
-    file_list = zip_file.namelist()
-    # we suppose we can write to parent of the given dir (supposing a tmp dir)
-    extract_dir = os.path.join(tmpdir, 'extract_dir')
-    os.mkdir(extract_dir)
-    for subfile in file_list:
-        # this should be safe in Python 2.7.4
-        zip_file.extract(subfile, extract_dir)
-    files = os.listdir(extract_dir)
-    move_extracted_files(extract_dir=extract_dir,
-                         target_dir=directory, files=files)
+    try:
+        zip_file = zipfile.ZipFile(name, mode='r')
+        file_list = zip_file.namelist()
+        # we suppose we can write to parent of the given dir (supposing a tmp dir)
+        extract_dir = os.path.join(tmpdir, 'extract_dir')
+        os.mkdir(extract_dir)
+        for subfile in file_list:
+            # this should be safe in Python 2.7.4
+            zip_file.extract(subfile, extract_dir)
+        files = os.listdir(extract_dir)
+        move_extracted_files(extract_dir=extract_dir,
+                             target_dir=directory, files=files)
+    except zipfile.BadZipfile as error:
+        gscript.fatal(_("ZIP file is unreadable: {}").format(error))
 
 
 # TODO: solve the other related formats
 def extract_tar(name, directory, tmpdir):
     """Extract a TAR or a similar file into a directory"""
-    import tarfile
-    tar = tarfile.open(name, "r:gz")
-    tar.extractall()
-    files = os.listdir(tmpdir)
-    move_extracted_files(extract_dir=tmpdir,
-                         target_dir=directory, files=files)
+    try:
+        import tarfile  # we don't need it anywhere else
+        tar = tarfile.open(name)
+        extract_dir = os.path.join(tmpdir, 'extract_dir')
+        os.mkdir(extract_dir)
+        tar.extractall(path=extract_dir)
+        files = os.listdir(extract_dir)
+        move_extracted_files(extract_dir=extract_dir,
+                             target_dir=directory, files=files)
+    except tarfile.TarError as error:
+        gscript.fatal(_("Archive file is unreadable: {}").format(error))
 
+extract_tar.supported_formats = ['tar.gz', 'gz', 'bz2', 'tar', 'gzip','targz']
 
+
 def download_source_code(source, url, name, outdev,
                          directory=None, tmpdir=None):
     """Get source code to a local directory for compilation"""
@@ -989,16 +1000,18 @@
         urlretrieve(url, zip_name)
         extract_zip(name=zip_name, directory=directory, tmpdir=tmpdir)
         fix_newlines(directory)
-    elif source == 'remote_tar.gz':
+    elif source.startswith('remote_') and \
+            source.split('_')[1] in extract_tar.supported_formats:
         # we expect that the module.tar.gz file is not by chance in the archive
-        archive_name = os.path.join(tmpdir, 'extension.tar.gz')
+        archive_name = os.path.join(tmpdir,
+                                    'extension.' + source.split('_')[1])
         urlretrieve(url, archive_name)
         extract_tar(name=archive_name, directory=directory, tmpdir=tmpdir)
         fix_newlines(directory)
     elif source == 'zip':
         extract_zip(name=url, directory=directory, tmpdir=tmpdir)
         fix_newlines(directory)
-    elif source == 'tar':
+    elif source in extract_tar.supported_formats:
         extract_tar(name=url, directory=directory, tmpdir=tmpdir)
         fix_newlines(directory)
     elif source == 'dir':
@@ -1006,8 +1019,8 @@
         fix_newlines(directory)
     else:
         # probably programmer error
-        grass.fatal(_("Unknown extension (addon) source '{}'."
-                      " Please report this to grass-user mailing list.")
+        grass.fatal(_("Unknown extension (addon) source type '{}'."
+                      " Please report this to the grass-user mailing list.")
                     .format(source))
 
 
@@ -1515,9 +1528,11 @@
     if os.path.isdir(url):
         return 'dir', os.path.abspath(url)
     elif os.path.exists(url):
-        for suffix in ['.zip', '.tar.gz']:
-            if url.endswith(suffix):
-                return suffix.lstrip('.'), os.path.abspath(url)
+        if url.endswith('.zip'):
+                return 'zip', os.path.abspath(url)
+        for suffix in extract_tar.supported_formats:
+            if url.endswith('.' + suffix):
+                return suffix, os.path.abspath(url)
     else:
         result = resolve_known_host_service(url)
         if result:
@@ -1525,7 +1540,9 @@
         # we allow URL to end with =zip or ?zip and not only .zip
         # unfortunately format=zip&version=89612 would require something else
         # special option to force the source type would solve it
-        for suffix in ['zip', 'tar.gz']:
+        if url.endswith('zip'):
+            return 'remote_zip', url
+        for suffix in extract_tar.supported_formats:
             if url.endswith(suffix):
                 return 'remote_' + suffix, url
         # fallback to the classic behavior

Modified: grass/trunk/scripts/g.extension/testsuite/doctest.sh
===================================================================
--- grass/trunk/scripts/g.extension/testsuite/doctest.sh	2015-07-21 11:53:43 UTC (rev 65755)
+++ grass/trunk/scripts/g.extension/testsuite/doctest.sh	2015-07-21 14:00:53 UTC (rev 65756)
@@ -3,4 +3,4 @@
 set -e
 set -x
 
-$GRASS_PYTHON g.extension.py --doctest
+g.extension --doctest



More information about the grass-commit mailing list