Selaa lähdekoodia

grass.py: Evaluate ^export lines only and expand variables in double/non-quoted values

Huidae Cho 5 vuotta sitten
vanhempi
commit
0e3e87fdbd
1 muutettua tiedostoa jossa 17 lisäystä ja 4 poistoa
  1. 17 4
      lib/init/grass.py

+ 17 - 4
lib/init/grass.py

@@ -1160,12 +1160,25 @@ def load_env(grass_env_file):
     if not os.access(grass_env_file, os.R_OK):
         return
 
+    export_re = re.compile('^export[ \t]([a-zA-Z_]+[a-zA-Z0-9_]*)=(.*)$')
+
     for line in readfile(grass_env_file).split(os.linesep):
-        try:
-            k, v = map(lambda x: x.strip(), line.strip().split(' ', 1)[1].split('=', 1))
-        except:
+        m = export_re.match(line)
+        if not m:
             continue
-
+        k = m[1]
+        v = m[2]
+        expand = True
+        if v.startswith("'") and v.endswith("'"):
+            v = v.strip("'")
+            expand = False
+        elif v.startswith('"') and v.endswith('"'):
+            v = v.strip('"')
+        elif v.startswith("'") or v.endswith("'") or v.startswith('"') or v.endswith('"'):
+            # multi-line variable
+            continue
+        if expand:
+            v = os.path.expanduser(os.path.expandvars(v.replace('\$', '\0')).replace('\0', '$'))
         debug("Environmental variable set {0}={1}".format(k, v))
         os.environ[k] = v