[GRASS-SVN] r73107 - in grass-addons/grass7/imagery/i.sentinel: i.sentinel.download i.sentinel.import

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Aug 15 23:46:12 PDT 2018


Author: martinl
Date: 2018-08-15 23:46:12 -0700 (Wed, 15 Aug 2018)
New Revision: 73107

Modified:
   grass-addons/grass7/imagery/i.sentinel/i.sentinel.download/i.sentinel.download.py
   grass-addons/grass7/imagery/i.sentinel/i.sentinel.import/i.sentinel.import.py
Log:
i.sentinel.download: do not unzip downloaded files
i.sentinel.import: read zip files, perform cleanup (remove uncompressed files)


Modified: grass-addons/grass7/imagery/i.sentinel/i.sentinel.download/i.sentinel.download.py
===================================================================
--- grass-addons/grass7/imagery/i.sentinel/i.sentinel.download/i.sentinel.download.py	2018-08-15 20:21:04 UTC (rev 73106)
+++ grass-addons/grass7/imagery/i.sentinel/i.sentinel.download/i.sentinel.download.py	2018-08-16 06:46:12 UTC (rev 73107)
@@ -122,7 +122,6 @@
 import os
 import sys
 import logging
-import zipfile
 
 from collections import OrderedDict
 
@@ -241,13 +240,6 @@
             # download
             self._api.download(self._products_df_sorted['uuid'][idx], output)
 
-            # unzip
-            if os.path.exists(os.path.join(output, self._products_df_sorted['identifier'][idx])):
-                continue
-            filename = self._products_df_sorted['identifier'][idx] + '.zip'
-            with zipfile.ZipFile(os.path.join(output, filename), 'r') as zip_ref:
-                zip_ref.extractall(output)
-
     def save_footprints(self, map_name):
         if self._products_df_sorted is None:
             return

Modified: grass-addons/grass7/imagery/i.sentinel/i.sentinel.import/i.sentinel.import.py
===================================================================
--- grass-addons/grass7/imagery/i.sentinel/i.sentinel.import/i.sentinel.import.py	2018-08-15 20:21:04 UTC (rev 73106)
+++ grass-addons/grass7/imagery/i.sentinel/i.sentinel.import/i.sentinel.import.py	2018-08-16 06:46:12 UTC (rev 73107)
@@ -51,6 +51,9 @@
 import os
 import sys
 import re
+import glob
+import shutil
+from zipfile import ZipFile
 
 import grass.script as gs
 from grass.exceptions import CalledModuleError
@@ -61,6 +64,15 @@
             gs.fatal(_('{} not exists').format(input_dir))
         self.input_dir = input_dir
 
+        # list of directories to cleanup
+        self._dir_list = []
+
+    def __del__(self):
+        for dirname in self._dir_list:
+            dirpath = os.path.join(self.input_dir, dirname)
+            gs.debug('Removing <{}>'.format(dirpath))
+            shutil.rmtree(dirpath)
+            
     def filter(self, pattern=None):
         if pattern:
             filter_p = '.*' + options['pattern'] + '.*.jp2$'
@@ -69,7 +81,27 @@
 
         self.files = self._filter(filter_p)
 
+    @staticmethod
+    def _read_zip_file(filepath):
+        # scan zip file, return first member (root directory)
+        with ZipFile(filepath) as fd:
+            file_list = fd.namelist()
+
+        return file_list
+
+    def _unzip(self):
+        # extract all zip files from input directory
+        for filepath in glob.glob(os.path.join(self.input_dir, '*.zip')):
+            gs.verbose('Reading <{}>...'.format(filepath))
+            self._dir_list.append(self._read_zip_file(filepath)[0])
+
+            with ZipFile(filepath) as fd:
+                fd.extractall(path=self.input_dir)
+
     def _filter(self, filter_p):
+        # unzip archives before filtering
+        self._unzip()
+
         pattern = re.compile(filter_p)
         files = []
         for rec in os.walk(self.input_dir):



More information about the grass-commit mailing list