This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Simplify the structure of the grass init module #7
Open
pmav99
wants to merge
22
commits into
GRASS-GIS:master
Choose a base branch
from
pmav99:simplify_grass_init
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
90639ae
lib/init/grass: Simplify removing the tmpdir.
pmav99 785492b
lib/init/grass: Fix erroneous comment.
pmav99 39d047b
lib/init/grass: Simplify encode()/decode() functions.
pmav99 e60e034
lib/init/grass: Move all the imports at the beginning of the file.
pmav99 47ff016
lib/init/grass: Simplify clean_env().
pmav99 193277d
lib/init/grass: Just shuffling stuff around
pmav99 821f43f
lib/init/grass: Group the logging functions together.
pmav99 3a5de69
lib/init/grass: Capitalize constants.
pmav99 906ff68
lib/init/grass: Move the info message out of the "show_info()" function.
pmav99 bb2d09e
lib/init/grass: Remove unused constants
pmav99 44ce258
lib/init/grass: Use six for cross version Python compatibility
pmav99 2c00830
lib/init/grass: Remove old comment
pmav99 d2ab2c2
lib/init/grass: Move all the gisbase stuff in a single place.
pmav99 7e737ee
lib/init/grass: Clarify comment.
pmav99 e5d0b31
lib/init/grass: Capitalize all constants
pmav99 d84f4c4
lib/init/grass: Fix typo
pmav99 6689f15
lib/init/grass: Simplify setting a value to CONFIG_PROJSHARE
pmav99 5682b16
lib/init/grass: Add comments about GISBASE
pmav99 070f9e1
lib/init/grass: Validate cmdline in a separate function
pmav99 520f4df
lib/init/grass: Inline variable substitution inside "help_message()"
pmav99 7229f5f
lib/init/grass: Ensure that the init process only happens inside main()
pmav99 1809611
lib/init/grass: Refuse to start if ~/.grass7 exists but is not a dire…
pmav99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
import sys | ||
import os | ||
import atexit | ||
import shutil | ||
import string | ||
import subprocess | ||
import re | ||
|
@@ -172,13 +173,6 @@ def try_remove(path): | |
pass | ||
|
||
|
||
def try_rmdir(path): | ||
try: | ||
os.rmdir(path) | ||
except: | ||
pass | ||
|
||
|
||
def clean_env(gisrc): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea for the parameter here is that this is one of the functions which should eventually go to grass.script.setup to be (re)used when a session is started using the grass package from Python. (Maybe less important now with grass-session package for external uses of G, but still very useful for subsession dealing with different projections or mapsets.) |
||
env_curr = read_gisrc(gisrc) | ||
env_new = {} | ||
|
@@ -190,32 +184,6 @@ def clean_env(gisrc): | |
write_gisrc(env_new, gisrc) | ||
|
||
|
||
def cleanup_dir(path): | ||
if not path: | ||
return | ||
|
||
for root, dirs, files in os.walk(path, topdown=False): | ||
for name in files: | ||
try_remove(os.path.join(root, name)) | ||
for name in dirs: | ||
try_rmdir(os.path.join(root, name)) | ||
|
||
|
||
class Cleaner(object): # pylint: disable=R0903 | ||
"""Holds directories and files which needs to be cleaned or deleted""" | ||
def __init__(self): | ||
self.tmpdir = None | ||
|
||
def cleanup(self): | ||
"""This can be registered with atexit | ||
|
||
Object can then still change and add or remove directories to clean""" | ||
# all exits after setting up tmp dirs (system/location) should | ||
# also tidy it up | ||
cleanup_dir(self.tmpdir) | ||
try_rmdir(self.tmpdir) | ||
|
||
|
||
def fatal(msg): | ||
sys.stderr.write("%s: " % _('ERROR') + msg + os.linesep) | ||
sys.exit(_("Exiting...")) | ||
|
@@ -2106,10 +2074,9 @@ def main(): | |
# Create the temporary directory and session grassrc file | ||
tmpdir = create_tmp(user, gis_lock) | ||
|
||
cleaner = Cleaner() | ||
cleaner.tmpdir = tmpdir | ||
# object is not destroyed when its method is registered | ||
atexit.register(cleaner.cleanup) | ||
# Remove the tmpdir | ||
# The removal will be executed when the python process terminates. | ||
atexit.register(lambda: shutil.rmtree(tmpdir, ignore_errors=True)) | ||
|
||
# Create the session grassrc file | ||
gisrc = create_gisrc(tmpdir, gisrcrc) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the idea was to have something like in
grass.script.utils
and writetry_rmdir(tmpdir)
instead oflambda: shutil.rmtree(tmpdir, ignore_errors=True)
, but I don't see clear rule here (brevity vs std library use). As for removing the Cleaner class, I agree, code was much simplified, no need for it anymore (see my fixes for the multiple/incorrect tmp dirs).