This is an automated email from the ASF dual-hosted git repository.

marcoabreu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new e9a590f  License header (#13178)
e9a590f is described below

commit e9a590fa6554231fba404dad08acee5cd3e786a8
Author: Pedro Larroy <928489+lar...@users.noreply.github.com>
AuthorDate: Thu Nov 8 11:47:52 2018 +0100

    License header (#13178)
    
    * Minor fix to license_header documentation
    
    * Handle UnicodeError when checking license
---
 tools/license_header.py | 43 +++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/tools/license_header.py b/tools/license_header.py
index 2f12a14..10ba8b9 100755
--- a/tools/license_header.py
+++ b/tools/license_header.py
@@ -24,11 +24,11 @@ Usuage:
 - add the default license header to source files that do not contain a valid
   license:
 
-  python license_header.py add
+  license_header.py add
 
 - check if every files has a license header
 
-  python license_header.py check
+  license_header.py check
 """
 
 import re
@@ -126,24 +126,27 @@ def _valid_file(fname, verbose=False):
 def process_file(fname, action, verbose=True):
     if not _valid_file(fname, verbose):
         return True
-    with open(fname, 'r', encoding="utf-8") as f:
-        lines = f.readlines()
-    if not lines:
+    try:
+        with open(fname, 'r', encoding="utf-8") as f:
+            lines = f.readlines()
+        if not lines:
+            return True
+        if _has_license(lines):
+            return True
+        elif action == 'check':
+            return False
+        _, ext = os.path.splitext(fname)
+        with open(fname, 'w', encoding="utf-8") as f:
+            # shebang line
+            if lines[0].startswith('#!'):
+                f.write(lines[0].rstrip()+'\n\n')
+                del lines[0]
+            f.write(_get_license(_LANGS[ext]))
+            for l in lines:
+                f.write(l.rstrip()+'\n')
+        logging.info('added license header to ' + fname)
+    except UnicodeError:
         return True
-    if _has_license(lines):
-        return True
-    elif action == 'check':
-        return False
-    _, ext = os.path.splitext(fname)
-    with open(fname, 'w', encoding="utf-8") as f:
-        # shebang line
-        if lines[0].startswith('#!'):
-            f.write(lines[0].rstrip()+'\n\n')
-            del lines[0]
-        f.write(_get_license(_LANGS[ext]))
-        for l in lines:
-            f.write(l.rstrip()+'\n')
-    logging.info('added license header to ' + fname)
     return True
 
 def process_folder(root, action):
@@ -155,7 +158,7 @@ def process_folder(root, action):
                 excepts.append(fname)
     if action == 'check' and excepts:
         logging.warning('The following files do not contain a valid license, '+
-                        'you can use `python tools/license_header.py add 
[file]` to add'+
+                        'you can use `tools/license_header.py add [file]` to 
add'+
                         'them automatically: ')
         for x in excepts:
             logging.warning(x)

Reply via email to