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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 21 00:35:16 PDT 2015


Author: wenzeslaus
Date: 2015-07-21 00:35:16 -0700 (Tue, 21 Jul 2015)
New Revision: 65737

Modified:
   grass/trunk/scripts/g.extension/g.extension.py
Log:
g.extension: replace CRFL in downloaded files by LF

Motivation are Trac download links for directories as ZIP files where all files have CRLF. Iterates over all files and rewrites all of them every time (suboptimal but straightforward). Base for code taken from Python crlf.py file.

g.extension r.sample.category svnurl=https://trac.osgeo.org/grass/browser/grass-addons/grass7/raster/r.sample.category?format=zip


Modified: grass/trunk/scripts/g.extension/g.extension.py
===================================================================
--- grass/trunk/scripts/g.extension/g.extension.py	2015-07-21 06:53:41 UTC (rev 65736)
+++ grass/trunk/scripts/g.extension/g.extension.py	2015-07-21 07:35:16 UTC (rev 65737)
@@ -924,6 +924,31 @@
                 shutil.copy(actual_file, target_dir)
 
 
+# Original copyright and license of the original version of the CRLF function
+# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+# Python Software Foundation; All Rights Reserved
+# Python Software Foundation License Version 2
+# http://svn.python.org/projects/python/trunk/Tools/scripts/crlf.py
+def fix_newlines(directory):
+    """Replace CRLF with LF in all files in the directory
+
+    Binary files are ignored. Recurses into subdirectories.
+    """
+    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:
+                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
+            newdata = data.replace('\r\n', '\n')
+            if newdata != data:
+                f = open(filename, 'wb')
+                f.write(newdata)
+                f.close()
+
+
 def extract_zip(name, directory, tmpdir):
     """Extract a ZIP file into a directory"""
     zip_file = zipfile.ZipFile(name, mode='r')
@@ -960,19 +985,24 @@
         zip_name = os.path.join(tmpdir, 'module.zip')
         urlretrieve(url, zip_name)
         extract_zip(name=zip_name, directory=directory, tmpdir=tmpdir)
+        fix_newlines(directory)
     elif source == 'remote_tar.gz':
         # we expect that the module.tar.gz file is not by chance in the archive
         archive_name = os.path.join(tmpdir, 'module.tar.gz')
         urlretrieve(url, archive_name)
         extract_tar(name=archive_name, directory=directory, tmpdir=tmpdir)
+        fix_newlines(directory)
     elif source == 'zip':
         os.mkdir(directory)
         extract_zip(name=url, directory=directory)
+        fix_newlines(directory)
     elif source == 'tar':
         os.mkdir(directory)
         extract_tar(name=url, directory=directory)
+        fix_newlines(directory)
     elif source == 'dir':
         shutil.copytree(url, directory)
+        fix_newlines(directory)
     else:
         # probably programmer error
         grass.fatal(_("Unknown extension (addon) source '{}'."



More information about the grass-commit mailing list