Browse Source

g.mremove: Changed the interface to that of g.mlist and added exclude= (ticket https://trac.osgeo.org/grass/ticket/2228)

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@61009 15284696-431f-4ddb-bdfa-cd5b030d7da7
Huidae Cho 10 years ago
parent
commit
f86a85bbd8

+ 9 - 3
general/g.mremove/g.mremove.html

@@ -5,16 +5,22 @@ POSIX Extended Regular Expressions. If the <b>-f</b> force flag is not given
 then nothing is removed, instead the list of selected file names is printed to
 then nothing is removed, instead the list of selected file names is printed to
 <tt>stdout</tt> as a preview of the files to be deleted.
 <tt>stdout</tt> as a preview of the files to be deleted.
 
 
-<h2>EXAMPLE</h2>
+<h2>EXAMPLES</h2>
 
 
 Delete all raster maps starting with "<tt>tmp_</tt>" in the current mapset:
 Delete all raster maps starting with "<tt>tmp_</tt>" in the current mapset:
 
 
 <div class="code"><pre>
 <div class="code"><pre>
 # show matching raster maps but do not delete yet (as verification)
 # show matching raster maps but do not delete yet (as verification)
-g.mremove rast="tmp_*"
+g.mremove type=rast pattern="tmp_*"
 
 
 # actually delete the matching raster maps
 # actually delete the matching raster maps
-g.mremove -f rast="tmp_*"
+g.mremove -f type=rast pattern="tmp_*"
+</pre></div>
+
+Delete all raster maps starting with "<tt>stream_</tt>" in the current mapset,
+but exclude those ending with "<tt>_final</tt>":
+<div class="code"><pre>
+g.mremove -f type=rast pattern="stream_*" exclude="*_final"
 </pre></div>
 </pre></div>
 
 
 <h2>SEE ALSO</h2>
 <h2>SEE ALSO</h2>

+ 120 - 58
general/g.mremove/main.c

@@ -17,7 +17,7 @@
  *
  *
  * PURPOSE:      lets users remove GRASS database files
  * PURPOSE:      lets users remove GRASS database files
  *
  *
- * COPYRIGHT:    (C) 1999-2011 by the GRASS Development Team
+ * COPYRIGHT:    (C) 1999-2014 by the GRASS Development Team
  *
  *
  *               This program is free software under the GNU General
  *               This program is free software under the GNU General
  *               Public License (>=v2). Read the file COPYING that
  *               Public License (>=v2). Read the file COPYING that
@@ -26,8 +26,8 @@
  *****************************************************************************/
  *****************************************************************************/
 
 
 #include <stdlib.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <unistd.h>
-
 #include <grass/gis.h>
 #include <grass/gis.h>
 #include <grass/manage.h>
 #include <grass/manage.h>
 #include <grass/glocale.h>
 #include <grass/glocale.h>
@@ -38,7 +38,12 @@ int check_reclass(const char *, const char *, int);
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
     struct GModule *module;
     struct GModule *module;
-    struct Option **opt, *o;
+    struct
+    {
+	struct Option *type;
+	struct Option *pattern;
+	struct Option *exclude;
+    } opt;
     struct
     struct
     {
     {
 	struct Flag *regex;
 	struct Flag *regex;
@@ -47,11 +52,9 @@ int main(int argc, char *argv[])
 	struct Flag *basemap;
 	struct Flag *basemap;
     } flag;
     } flag;
     const char *mapset;
     const char *mapset;
-    char *name, path[GPATH_MAX], **files;
-    const struct list *option;
-    int num_files, rast, result;
-    int i, j, n, nlist;
-    void *filter;
+    int result;
+    int i, all, num_types, nlist;
+    void *filter, *exclude;
 
 
     G_gisinit(argv[0]);
     G_gisinit(argv[0]);
 
 
@@ -64,6 +67,28 @@ int main(int argc, char *argv[])
     module->description =
     module->description =
 	_("Removes data base element files from "
 	_("Removes data base element files from "
 	  "the user's current mapset using the search pattern.");
 	  "the user's current mapset using the search pattern.");
+
+    M_read_list(FALSE, &nlist);
+
+    opt.type = G_define_standard_option(G_OPT_M_DATATYPE);
+    opt.type->multiple = YES;
+    opt.type->options = M_get_options(TRUE);
+    opt.type->descriptions = M_get_option_desc(TRUE);
+
+    opt.pattern = G_define_option();
+    opt.pattern->key = "pattern";
+    opt.pattern->type = TYPE_STRING;
+    opt.pattern->required = YES;
+    opt.pattern->description = _("Map name search pattern");
+    opt.pattern->guisection = _("Pattern");
+
+    opt.exclude = G_define_option();
+    opt.exclude->key = "exclude";
+    opt.exclude->type = TYPE_STRING;
+    opt.exclude->required = NO;
+    opt.exclude->description = _("Map name exclusion pattern (default: none)");
+    opt.exclude->guisection = _("Pattern");
+
     flag.regex = G_define_flag();
     flag.regex = G_define_flag();
     flag.regex->key = 'r';
     flag.regex->key = 'r';
     flag.regex->description =
     flag.regex->description =
@@ -84,14 +109,6 @@ int main(int argc, char *argv[])
     flag.basemap->description = _("Remove base raster maps");
     flag.basemap->description = _("Remove base raster maps");
     flag.basemap->guisection = _("Raster");
     flag.basemap->guisection = _("Raster");
     
     
-    M_read_list(FALSE, &nlist);
-
-    opt = (struct Option **)G_calloc(nlist, sizeof(struct Option *));
-
-    for (n = 0; n < nlist; n++) {
-	o = opt[n] = M_define_option(n, _("removed"), YES);
-    }
-
     if (G_parser(argc, argv))
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 	exit(EXIT_FAILURE);
 
 
@@ -99,60 +116,105 @@ int main(int argc, char *argv[])
 	G_fatal_error(_("-%c and -%c are mutually exclusive"),
 	G_fatal_error(_("-%c and -%c are mutually exclusive"),
 		      flag.regex->key, flag.extended->key);
 		      flag.regex->key, flag.extended->key);
 
 
-    if (!flag.force->answer)
-	G_message(_("The following data base element files would be deleted:"));
+    if (flag.regex->answer || flag.extended->answer)
+	filter = G_ls_regex_filter(opt.pattern->answer, 0,
+				   (int)flag.extended->answer);
+    else {
+	/* handle individual map names */
+	if (strchr(opt.pattern->answer, ',')) {
+	    char *pattern;
 
 
-    for (n = 0; n < nlist; n++) {
-	o = opt[n];
-	G_free((char *)o->gisprompt);
-	G_free((char *)o->description);
+	    pattern = (char *)G_malloc(strlen(opt.pattern->answer) + 3);
+	    sprintf(pattern, "{%s}", opt.pattern->answer);
+
+	    filter = G_ls_glob_filter(pattern, 0);
+	}
+	else
+	    filter = G_ls_glob_filter(opt.pattern->answer, 0);
     }
     }
+    if (!filter)
+	G_fatal_error(_("Unable to compile pattern <%s>"), opt.pattern->answer);
+
+    if (opt.exclude->answer) {
+	if (flag.regex->answer || flag.extended->answer)
+	    exclude = G_ls_regex_filter(opt.exclude->answer, 1,
+			    		(int)flag.extended->answer);
+	else {
+	    /* handle individual map names */
+	    if (strchr(opt.exclude->answer, ',')) {
+		char *pattern;
+
+		pattern = (char *)G_malloc(strlen(opt.exclude->answer) + 3);
+		sprintf(pattern, "{%s}", opt.exclude->answer);
+
+		exclude = G_ls_glob_filter(pattern, 1);
+	    }
+	    else
+		exclude = G_ls_glob_filter(opt.exclude->answer, 1);
+	}
+	if (!exclude)
+	    G_fatal_error(_("Unable to compile pattern <%s>"),
+			  opt.exclude->answer);
+    }
+    else
+	exclude = NULL;
+
+    if (!flag.force->answer)
+	G_message(_("The following data base element files would be deleted:"));
 
 
     mapset = G_mapset();
     mapset = G_mapset();
 
 
-    for (n = 0; n < nlist; n++) {
-	option = M_get_list(n);
-	if (opt[n]->answers) {
-	    G_file_name(path, M_get_list(n)->element[0], "", mapset);
-	    if (access(path, 0) != 0)
+    for (i = 0; opt.type->answers[i]; i++) {
+	if (strcmp(opt.type->answers[i], "all") == 0)
+	    break;
+    }
+    if (opt.type->answers[i]) {
+	all = 1;
+	num_types = nlist;
+    }
+    else {
+	all = 0;
+	num_types = i;
+    }
+
+    for (i = 0; i < num_types; i++) {
+	int n, rast, num_files, j;
+	const struct list *elem;
+    	char path[GPATH_MAX];
+	char **files;
+
+	n = all ? i : M_get_element(opt.type->answers[i]);
+	elem = M_get_list(n);
+
+	G_file_name(path, elem->element[0], "", mapset);
+	if (access(path, 0) != 0)
+	    continue;
+
+	rast = !G_strcasecmp(elem->alias, "rast");
+	files = G__ls(path, &num_files);
+	
+	for (j = 0; j < num_files; j++) {
+	    if (!flag.force->answer) {
+		fprintf(stdout, "%s/%s@%s\n", elem->alias, files[j], mapset);
 		continue;
 		continue;
-	    rast = !G_strcasecmp(option->alias, "rast");
-	    for (i = 0; (name = opt[n]->answers[i]); i++) {
-		if (!flag.regex->answer && !flag.extended->answer)
-		    filter = G_ls_glob_filter(name, 0);
-		else
-		    filter = G_ls_regex_filter(name, 0,
-					       (int) flag.extended->answer);
-		if (!filter)
-		    G_fatal_error(_("Unable to compile pattern <%s>"),
-				  name);
-
-		files = G__ls(path, &num_files);
-
-		G_free_ls_filter(filter);
-
-		for (j = 0; j < num_files; j++) {
-		    if (!flag.force->answer) {
-			fprintf(stdout, "%s/%s@%s\n", option->alias, files[j],
-				mapset);
-			continue;
-		    }
-		    if (rast &&
-			check_reclass(files[j], mapset, flag.basemap->answer))
-			continue;
-
-		    if (M_do_remove(n, (char *)files[j]) == 1)
-			result = EXIT_FAILURE;
-		}
 	    }
 	    }
+	    
+	    if (rast && check_reclass(files[j], mapset, flag.basemap->answer))
+		continue;
+	    
+	    if (M_do_remove(n, (char *)files[j]) == 1)
+		result = EXIT_FAILURE;
 	}
 	}
     }
     }
 
 
-    if (!flag.force->answer) {
+    G_free_ls_filter(filter);
+
+    if (exclude)
+	G_free_ls_filter(exclude);
+
+    if (!flag.force->answer)
 	G_important_message(_("You must use the force flag (-%c) to actually "
 	G_important_message(_("You must use the force flag (-%c) to actually "
 			      "remove them. Exiting."), flag.force->key);
 			      "remove them. Exiting."), flag.force->key);
-    }
 
 
     exit(result);
     exit(result);
 }
 }
-

+ 2 - 1
lib/python/temporal/temporal_raster3d_algebra.py

@@ -50,7 +50,8 @@ class TemporalRaster3DAlgebraParser(TemporalRasterBaseAlgebraParser):
 
 
                 if self.run:
                 if self.run:
                     m = copy.deepcopy(self.m_mremove)
                     m = copy.deepcopy(self.m_mremove)
-                    m.inputs["rast3d"].value = stringlist
+                    m.inputs["type"].value = "rast3d"
+                    m.inputs["pattern"].value = stringlist
                     m.flags["f"].value = True
                     m.flags["f"].value = True
                     m.run()
                     m.run()
 
 

+ 2 - 1
lib/python/temporal/temporal_raster_algebra.py

@@ -91,7 +91,8 @@ class TemporalRasterAlgebraParser(TemporalRasterBaseAlgebraParser):
 
 
                 if self.run:
                 if self.run:
                     m = copy.deepcopy(self.m_mremove)
                     m = copy.deepcopy(self.m_mremove)
-                    m.inputs["rast"].value = stringlist
+                    m.inputs["type"].value = "rast"
+                    m.inputs["pattern"].value = stringlist
                     m.flags["f"].value = True
                     m.flags["f"].value = True
                     m.run()
                     m.run()
 
 

+ 3 - 2
lib/python/temporal/temporal_vector_algebra.py

@@ -520,11 +520,12 @@ class TemporalVectorAlgebraParser(TemporalAlgebraParser):
             for chunk in chunklist:
             for chunk in chunklist:
                 stringlist = ",".join(chunk)
                 stringlist = ",".join(chunk)
                 if self.debug:
                 if self.debug:
-                    print "g.mremove vect=%s"%(stringlist)
+                    print "g.mremove type=vect pattern=%s"%(stringlist)
 
 
                 if self.run:
                 if self.run:
                     m = copy.deepcopy(self.m_mremove)
                     m = copy.deepcopy(self.m_mremove)
-                    m.inputs["vect"].value = stringlist
+                    m.inputs["type"].value = "vect"
+                    m.inputs["pattern"].value = stringlist
                     m.flags["f"].value = True
                     m.flags["f"].value = True
                     m.run()
                     m.run()
 
 

+ 2 - 2
scripts/i.pansharpen/i.pansharpen.py

@@ -300,7 +300,7 @@ def main():
 
 
         
         
         # Cleanup
         # Cleanup
-        grass.run_command('g.mremove', flags='f', quiet=True, rast='tmp%s*,%s' % (pid,panmatch))
+        grass.run_command('g.mremove', flags='f', quiet=True, type="rast", pattern='tmp%s*,%s' % (pid,panmatch))
         
         
     #Could add other sharpening algorithms here, e.g. wavelet transformation
     #Could add other sharpening algorithms here, e.g. wavelet transformation
 
 
@@ -341,7 +341,7 @@ def main():
         grass.raster_history("%s_%s" % (out, ch))
         grass.raster_history("%s_%s" % (out, ch))
 
 
     # Cleanup        
     # Cleanup        
-    grass.run_command('g.mremove', flags="f", rast="tmp%s*" % pid, quiet=True)
+    grass.run_command('g.mremove', flags="f", type="rast", pattern="tmp%s*" % pid, quiet=True)
 
 
         
         
 def matchhist(original, target, matched):
 def matchhist(original, target, matched):

+ 1 - 1
scripts/r3.in.xyz/r3.in.xyz.py

@@ -179,7 +179,7 @@ from grass.script import core as grass
 
 
 def cleanup():
 def cleanup():
     grass.run_command('g.mremove', flags = 'f',
     grass.run_command('g.mremove', flags = 'f',
-		      rast = 'tmp.r3xyz.%d.*' % os.getpid(),
+		      type = "rast", pattern = 'tmp.r3xyz.%d.*' % os.getpid(),
 		      quiet = True)
 		      quiet = True)