[GRASS-SVN] r73793 - grass/trunk/scripts/g.extension

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Dec 11 01:57:10 PST 2018


Author: martinl
Date: 2018-12-11 01:57:10 -0800 (Tue, 11 Dec 2018)
New Revision: 73793

Modified:
   grass/trunk/scripts/g.extension/g.extension.py
Log:
fix g.extension for python3, see #3446

Modified: grass/trunk/scripts/g.extension/g.extension.py
===================================================================
--- grass/trunk/scripts/g.extension/g.extension.py	2018-12-11 06:30:28 UTC (rev 73792)
+++ grass/trunk/scripts/g.extension/g.extension.py	2018-12-11 09:57:10 UTC (rev 73793)
@@ -1036,21 +1036,29 @@
 
     Binary files are ignored. Recurses into subdirectories.
     """
+    # skip binary files
+    # see https://stackoverflow.com/a/7392391
+    textchars = bytearray({7,8,9,10,12,13,27} | set(range(0x20, 0x100)) - {0x7f})
+    is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))
+
     for root, unused, files in os.walk(directory):
         for name in files:
             filename = os.path.join(root, name)
-            data = open(filename, 'rb').read()
-            if '\0' in data:
+            if is_binary_string(open(filename, 'rb').read(1024)):
                 continue  # ignore binary files
-            # we don't expect there would be CRLF file by purpose
-            # if we want to allow CRLF files we would have to whitelite .py etc
+
+            # read content of text file
+            with open(filename, 'rb') as fd:
+                data = fd.read()
+
+            # we don't expect there would be CRLF file by
+            # purpose if we want to allow CRLF files we would
+            # have to whitelite .py etc
             newdata = data.replace(b'\r\n', b'\n')
             if newdata != data:
-                newfile = open(filename, 'wb')
-                newfile.write(newdata)
-                newfile.close()
+                with open(filename, 'wb') as newfile:
+                    newfile.write(newdata)
 
-
 def extract_zip(name, directory, tmpdir):
     """Extract a ZIP file into a directory"""
     gscript.debug("extract_zip(name={name}, directory={directory},"



More information about the grass-commit mailing list