浏览代码

g.extension: match module files on Windows (#1565)

* match module files on Windows

* make sure py/exe are file extensions

* replace shebang on Windows in UTF8

* fix variable name typo

* add temporal module to filter
Stefan Blumentrath 3 年之前
父节点
当前提交
37925ef559
共有 1 个文件被更改,包括 28 次插入11 次删除
  1. 28 11
      scripts/g.extension/g.extension.py

+ 28 - 11
scripts/g.extension/g.extension.py

@@ -154,6 +154,7 @@ from __future__ import print_function
 import fileinput
 import http
 import os
+import codecs
 import sys
 import re
 import atexit
@@ -190,6 +191,29 @@ HEADERS = {
 HTTP_STATUS_CODES = list(http.HTTPStatus)
 
 
+def replace_shebang_win(python_file):
+    """
+    Replaces "python" with "python3" in python files
+    using UTF8 encoding on MS Windows
+    """
+
+    cur_dir = os.path.dirname(python_file)
+    tmp_name = os.path.join(cur_dir, gscript.tempname(12))
+
+    with codecs.open(python_file, "r", encoding="utf8") as in_file, codecs.open(
+        tmp_name, "w", encoding="utf8"
+    ) as out_file:
+
+        for line in in_file:
+            new_line = line.replace(
+                "#!/usr/bin/env python\n", "#!/usr/bin/env python3\n"
+            )
+            out_file.write(new_line)
+
+    os.remove(python_file)  # remove original
+    os.rename(tmp_name, python_file)  # rename temp to original name
+
+
 def urlretrieve(url, filename, *args, **kwargs):
     """Same function as 'urlretrieve', but with the ability to
     define headers.
@@ -1291,11 +1315,9 @@ def install_extension_win(name):
     module_list = list()
     for r, d, f in os.walk(srcdir):
         for file in f:
-            if file.endswith(".py"):
-                modulename = file.rsplit(".py")[0]
-                module_list.append(modulename)
-            if file.endswith(".exe"):
-                modulename = file.rsplit(".exe")[0]
+            # Filter GRASS module name patterns
+            if re.search(r"^[d,db,g,i,m,p,ps,r,r3,s,t,v,wx]\..*[\.py,\.exe]$", file):
+                modulename = os.path.splitext(file)[0]
                 module_list.append(modulename)
     # remove duplicates in case there are .exe wrappers for python scripts
     module_list = set(module_list)
@@ -1308,12 +1330,7 @@ def install_extension_win(name):
                 pyfiles.append(os.path.join(r, file))
 
     for filename in pyfiles:
-        with fileinput.FileInput(filename, inplace=True) as file:
-            for line in file:
-                print(
-                    line.replace("#!/usr/bin/env python\n", "#!/usr/bin/env python3\n"),
-                    end="",
-                )
+        replace_shebang_win(filename)
 
     # collect old files
     old_file_list = list()