Index: lib/init/grass.py =================================================================== --- lib/init/grass.py (revision 72386) +++ lib/init/grass.py (working copy) @@ -290,6 +290,7 @@ [-e] [-f] [-text | -gtext | -gui] [--config param] [[[GISDBASE/]LOCATION_NAME/]MAPSET] $CMD_NAME [FLAG]... GISDBASE/LOCATION_NAME/MAPSET --exec EXECUTABLE [EPARAM]... + $CMD_NAME [FLAG]... -c [CPARAM] --tmp-location [--no-clean] --exec EXECUTABLE [EPARAM]... {flags}: -h or -help or --help or --h {help_flag} @@ -307,6 +308,8 @@ {config_detail} --exec EXECUTABLE {exec_} {exec_detail} + --tmp-location {tmp_location} + --no-clean {no_clean} {params}: GISDBASE {gisdbase} @@ -363,6 +366,8 @@ executable=_("GRASS module, script or any other executable"), executable_params=_("parameters of the executable"), standard_flags=_("standard flags"), + tmp_location=_("create temporary location with -c and --exec flags"), + no_clean=_("not clean .tmp directory in the mapset (for parallel execution)") ) @@ -886,45 +891,54 @@ mapset=mapset, loc=location) -def set_mapset(gisrc, arg, geofile=None, create_new=False): +def set_mapset(gisrc, arg=None, geofile=None, create_new=False, tmp_location=False): """Selected Location and Mapset are checked and created if requested The gisrc (GRASS environment file) is written at the end (nothing is returned). """ - l = None + if arg: + l = None + if arg == '-': + # TODO: repair or remove behavior env vars + `grass75 -` (see doc) + # this is broken for some time (before refactoring, e.g. r65235) + # is some code is added, it should be a separate function, probably + # called here + # older comment for global vars: + # TODO: it does not seem that these would be ever set before calling this + # function, so we may just delete them here (the globals are set from + # the gisrc later on). But where is setting from environmental variables? + # the following probable means if env var defined, use it + # originates from r37863 (Convert grass70 script to Python) but even + # there it seems that it will never succeed + # it would have to be defined also for other vars + # if location: + # l = location + pass + else: + l = arg + if l: + mapset = None + if l == '.': + l = os.getcwd() + elif not os.path.isabs(l): + l = os.path.abspath(l) + l, mapset = os.path.split(l) + if not mapset: + l, mapset = os.path.split(l) + l, location_name = os.path.split(l) + gisdbase = l - if arg == '-': - # TODO: repair or remove behavior env vars + `grass75 -` (see doc) - # this is broken for some time (before refactoring, e.g. r65235) - # is some code is added, it should be a separate function, probably - # called here - # older comment for global vars: - # TODO: it does not seem that these would be ever set before calling this - # function, so we may just delete them here (the globals are set from - # the gisrc later on). But where is setting from environmental variables? - # the following probable means if env var defined, use it - # originates from r37863 (Convert grass70 script to Python) but even - # there it seems that it will never succeed - # it would have to be defined also for other vars - # if location: - # l = location - pass - else: - l = arg - - if l: - if l == '.': - l = os.getcwd() - elif not os.path.isabs(l): - l = os.path.abspath(l) - - l, mapset = os.path.split(l) - if not mapset: - l, mapset = os.path.split(l) - l, location_name = os.path.split(l) - gisdbase = l - + if tmp_location: + import random + # set gisdbase to temporal directory + # use $TMPDIR if it exists, then $TEMP, otherwise /tmp + gisdbase = os.getenv('TMPDIR') + location_name = "".join(random.choice(string.ascii_letters + string.digits) + for x in range(10)) + mapset = "PERMANENT" + message(_("Using temporary location <{gdb}{sep}{lc}>").format( + gdb = gisdbase, lc = location_name, sep = os.path.sep)) if gisdbase and location_name and mapset: location = os.path.join(gisdbase, location_name, mapset) @@ -938,11 +952,12 @@ # 'location' is not valid, the user wants to create # mapset on the fly if not is_location_valid(gisdbase, location_name): - # 'location_name' is not a valid GRASS location, - # create new location and 'PERMANENT' mapset - gisdbase = os.path.join(gisdbase, location_name) - location_name = mapset - mapset = "PERMANENT" + if not tmp_location: + # 'location_name' is not a valid GRASS location, + # create new location and 'PERMANENT' mapset + gisdbase = os.path.join(gisdbase, location_name) + location_name = mapset + mapset = "PERMANENT" if is_location_valid(gisdbase, location_name): fatal(_("Failed to create new location. " "The location <%s> already exists." % location_name)) @@ -1774,8 +1789,9 @@ self.force_gislock_removal = None self.mapset = None self.geofile = None + self.tmp_location = None + self.clean = True - def parse_cmdline(argv, default_gui): """Parse the standard part of command line parameters""" params = Parameters() @@ -1812,12 +1828,18 @@ elif i == "--config": print_params() sys.exit() + elif i == "--tmp-location": + params.tmp_location = True + elif i == "--no-clean": + params.clean = False else: args.append(i) if len(args) > 1: params.mapset = args[1] params.geofile = args[0] - elif len(args) == 1: + elif len(args) == 1 and params.tmp_location == True: + params.geofile = args[0] + elif len(args) == 1: params.mapset = args[0] else: params.mapset = None @@ -1948,7 +1970,8 @@ else: # clean only for user who is no first time # (measured by presence of rc file) - clean_temp() + if params.clean == True: + clean_temp() if params.create_new: message(_("Creating new GRASS GIS location/mapset...")) @@ -1969,7 +1992,7 @@ save_gui(gisrc, grass_gui) # Parsing argument to get LOCATION - if not params.mapset: + if not params.mapset and not params.tmp_location: # Try interactive startup # User selects LOCATION and MAPSET if not set if not set_mapset_interactive(grass_gui): @@ -1979,7 +2002,11 @@ "'grass-gui').").format(grass_gui)) else: # Try non-interactive start up - if params.create_new and params.geofile: + if params.create_new and params.geofile and params.tmp_location: + set_mapset(gisrc=gisrc, geofile=params.geofile, + create_new=True, tmp_location=params.tmp_location) + + elif params.create_new and params.geofile: set_mapset(gisrc=gisrc, arg=params.mapset, geofile=params.geofile, create_new=True) else: @@ -2016,10 +2043,12 @@ # only non-error, interactive version continues from here if batch_job: returncode = run_batch_job(batch_job) - clean_temp() + if params.clean == True: + clean_temp() sys.exit(returncode) elif params.exit_grass: - clean_temp() + if params.clean == True: + clean_temp() sys.exit(0) else: clear_screen() @@ -2058,7 +2087,8 @@ clear_screen() # TODO: can we just register this atexit? # TODO: and what is difference to deleting .tmp which we do? - clean_temp() + if params.clean == True: + clean_temp() # save 'last used' GISRC after removing variables which shouldn't # be saved, e.g. d.mon related clean_env(gisrc) Index: lib/init/grass7.html =================================================================== --- lib/init/grass7.html (revision 72386) +++ lib/init/grass7.html (working copy) @@ -51,6 +51,12 @@
+grass75 -c elevation.tiff --tmp-location --exec test.sh +
+grass75 -c elevation.tiff --tmp-location --no-clean --exec test.sh +