[GRASS-SVN] r61130 - sandbox/wenzeslaus/gunittest

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jul 2 14:21:32 PDT 2014


Author: wenzeslaus
Date: 2014-07-02 14:21:32 -0700 (Wed, 02 Jul 2014)
New Revision: 61130

Modified:
   sandbox/wenzeslaus/gunittest/loader.py
   sandbox/wenzeslaus/gunittest/main.py
Log:
gunittest: simple command line interface, function for mapset creation

Modified: sandbox/wenzeslaus/gunittest/loader.py
===================================================================
--- sandbox/wenzeslaus/gunittest/loader.py	2014-07-02 21:07:06 UTC (rev 61129)
+++ sandbox/wenzeslaus/gunittest/loader.py	2014-07-02 21:21:32 UTC (rev 61130)
@@ -11,7 +11,7 @@
 
 import suite
 
-GrassTestDir = collections.namedtuple('GrassTestDir', ['modules'])
+
 GrassTestPythonModule = collections.namedtuple('GrassTestPythonModule',
                                                ['name', 'module',
                                                 'tested_dir',

Modified: sandbox/wenzeslaus/gunittest/main.py
===================================================================
--- sandbox/wenzeslaus/gunittest/main.py	2014-07-02 21:07:06 UTC (rev 61129)
+++ sandbox/wenzeslaus/gunittest/main.py	2014-07-02 21:21:32 UTC (rev 61130)
@@ -6,6 +6,7 @@
 import errno
 import string
 import subprocess
+import datetime
 
 from unittest.main import TestProgram, USAGE_AS_MAIN
 TestProgram.USAGE = USAGE_AS_MAIN
@@ -56,6 +57,7 @@
     cov = coverage.coverage(omit="*testsuite*")
     cov.start()
 
+    # TODO: enable passing omit to exclude also gunittest or nothing
     program = GrassTestProgram(module='__main__', exit_at_end=False, grass_location='all')
 
     cov.stop()
@@ -129,13 +131,38 @@
         self.clean_before = clean_before
         self.testsuite_dir = testsuite_dir
 
+    def _create_mapset(self, module):
+        """Create mapset according to informations in module.
+        
+        :param loader.GrassTestPythonModule module:
+        """
+        # using path.sep but also / and \ for cases when it is confused
+        # (namely the case of Unix path on MS Windows)
+        # replace . to get rid of unclean path
+        # TODO: clean paths
+        dir_as_name = module.tested_dir.translate(string.maketrans('./\\', '___'))
+        #dir_as_name = module.tested_dir.replace('.', '_').replace('/', '_').replace('\\', '_').replace(os.path.sep, '_')
+        mapset = dir_as_name + '_' + module.name
+        # TODO: use grass module to do this? but we are not in the right gisdbase
+        mapset_dir = os.path.join(gisdbase, location, mapset)
+        if self.clean_before:
+            silent_rmtree(mapset_dir)
+        os.mkdir(mapset_dir)
+        # TODO: default region in mapset will be what?
+        # copy WIND file from PERMANENT
+        # TODO: this should be a function in grass.script (used also in gis_set.py, PyGRASS alos has its way with Mapset)
+        # TODO: are premisions an issue here?
+        shutil.copy(os.path.join(gisdbase, location, 'PERMANENT', 'WIND'),
+                    os.path.join(mapset_dir))
+        return mapset, mapset_dir
+
+        
+
     def run_in_location(self, gisdbase, location, location_shortcut,
                         results_dir):
         if os.path.abspath(results_dir) == os.path.abspath(self.start_dir):
             raise RuntimeError("Results root directory should not be the same"
                                " as discovery start directory")
-
-        import datetime
         main_start_time = datetime.datetime.now()
 
         # TODO: move constants out of loader class or even module
@@ -159,29 +186,10 @@
             ensure_dir(os.path.abspath(cwd))
             # TODO: put this to constructor and copy here again
             env = os.environ.copy()
-            # using path.sep but also / and \ for cases when it is confused
-            # (namely the case of Unix path on MS Windows)
-            # replace . to get rid of unclean path
-            # TODO: clean paths
-            dir_as_name = module.tested_dir.translate(string.maketrans('./\\', '___'))
-            #dir_as_name = module.tested_dir.replace('.', '_').replace('/', '_').replace('\\', '_').replace(os.path.sep, '_')
-            mapset = dir_as_name + '_' + module.name
-            location = location
+            mapset, mapset_dir = self._create_mapset(module)
             gisrc = gsetup.write_gisrc(gisdbase, location, mapset)
             env['GISRC'] = gisrc
-            # TODO: use grass module to do this?
-            mapset_dir = os.path.join(gisdbase, location, mapset)
-            # TODO: perhaps remove dir if exists
-            if self.clean_before:
-                silent_rmtree(mapset_dir)
-            os.mkdir(mapset_dir)
-            # TODO: default region in mapset will be what?
-            # copy WIND file from PERMANENT
-            # TODO: this should be a function in grass.script (used also in gis_set.py, PyGRASS alos has its way with Mapset)
-            # TODO: are premisions an issue here?
-            shutil.copy(os.path.join(gisdbase, location, 'PERMANENT', 'WIND'),
-                        os.path.join(mapset_dir))
-    
+
             # TODO: we don't do any HTML escaping, use txt file
             stdout = open(os.path.join(cwd, 'stdout.html'), 'w')
             stderr = open(os.path.join(cwd, 'stderr.html'), 'w')
@@ -189,8 +197,9 @@
             stderr.write('<html><body><h1>%s</h1><pre>' % (module.name + ' stderr'))
             stdout.flush()  # we need to flush to write our changes before stdout
             stderr.flush()
-    
+
             # TODO: we might clean the directory here before test if non-empty
+            # TODO: use some grass function to run?
             p = subprocess.Popen([sys.executable, module.abs_file_path],
                                  cwd=cwd, env=env,
                                  stdout=stdout, stderr=stderr)
@@ -221,18 +230,33 @@
             # TODO: only if clean up
             if self.clean_mapsets:
                 shutil.rmtree(mapset_dir)
-    
         main_index.write('</ul>'
                          '</body></html>')
         main_index.close()
 
 
+# TODO: create a full interface (using grass parser or argparse)
 if __name__ == '__main__':
-    gisdbase = gcore.gisenv()['GISDBASE']
+    if len(sys.argv) == 4:
+        gisdbase = sys.argv[1]
+        location = sys.argv[2]
+        location_shortcut = sys.argv[3]
+    elif len(sys.argv) == 3:
+        location = sys.argv[1]
+        location_shortcut = sys.argv[2]
+        gisdbase = gcore.gisenv()['GISDBASE']
+    else:
+        sys.stderr.write("Usage: %s [gisdbase] location location_shortcut\n" % sys.argv[0])
+        sys.exit(1)
     assert gisdbase
+    if not os.path.exists(gisdbase):
+        sys.stderr.write("GISDBASE <%s> does not exist\n" % gisdbase)
+        sys.exit(1)
     results_dir = 'testresults'
     shutil.rmtree(results_dir)  # TODO: too brute force?
 
     invoker = GrassTestFilesInvoker(start_dir='.')
-    invoker.run_in_location(gisdbase=gisdbase, location='nc_spm_08_grass7_tests', location_shortcut='nc',
+    invoker.run_in_location(gisdbase=gisdbase,
+                            location=location,
+                            location_shortcut=location_shortcut,
                             results_dir=results_dir)



More information about the grass-commit mailing list