Browse Source

fix g.extension for python3, see https://trac.osgeo.org/grass/ticket/3446

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@73793 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 6 years ago
parent
commit
dd97248156
1 changed files with 16 additions and 8 deletions
  1. 16 8
      scripts/g.extension/g.extension.py

+ 16 - 8
scripts/g.extension/g.extension.py

@@ -1036,20 +1036,28 @@ def fix_newlines(directory):
 
     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"""