marcoabreu closed pull request #9849: License tool
URL: https://github.com/apache/incubator-mxnet/pull/9849
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/Jenkinsfile b/Jenkinsfile
index 17d546c87f..81ddb73f32 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -182,7 +182,7 @@ try {
       node('mxnetlinux-cpu') {
         ws('workspace/sanity') {
           init_git()
-          sh "python tools/license_header.py check"
+          sh "tools/license_header.py check"
           make('lint', 'cpplint rcpplint jnilint')
           make('lint', 'pylint')
         }
diff --git a/tools/license_header.py b/tools/license_header.py
old mode 100644
new mode 100755
index 5a0c0295c1..14d3f587d9
--- a/tools/license_header.py
+++ b/tools/license_header.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
@@ -32,6 +34,9 @@
 import re
 import os
 import argparse
+from itertools import chain
+import logging
+import sys
 
 # the default apache license
 _LICENSE = """Licensed to the Apache Software Foundation (ASF) under one
@@ -62,7 +67,7 @@
                'dmlc-core/',
                'mshadow/',
                'nnvm',
-               '3rdparty',   
+               '3rdparty',
                'ps-lite',
                'src/operator/mkl/',
                'src/operator/special_functions-inl.h',
@@ -89,7 +94,7 @@
 _OLD_LICENSE = re.compile('.*Copyright.*by Contributors')
 
 def _has_license(lines):
-    return any([any([p in l.decode('utf-8') for p in _LICENSE_PATTERNS]) for l 
in lines])
+    return any([any([p in l for p in _LICENSE_PATTERNS]) for l in lines])
 
 def _get_license(comment_mark):
     if comment_mark == '*':
@@ -112,19 +117,19 @@ def _get_license(comment_mark):
 def _valid_file(fname, verbose=False):
     if any([l in fname for l in _WHITE_LIST]):
         if verbose:
-            print('skip ' + fname + ', it matches the white list')
+            logging.info('skip ' + fname + ', it matches the white list')
         return False
     _, ext = os.path.splitext(fname)
     if ext not in _LANGS:
         if verbose:
-            print('skip ' + fname + ', unknown file extension')
+            logging.info('skip ' + fname + ', unknown file extension')
         return False
     return True
 
 def process_file(fname, action, verbose=True):
     if not _valid_file(fname, verbose):
         return True
-    with open(fname, 'rb') as f:
+    with open(fname, 'r', encoding="utf-8") as f:
         lines = f.readlines()
     if not lines:
         return True
@@ -133,16 +138,16 @@ def process_file(fname, action, verbose=True):
     elif action == 'check':
         return False
     _, ext = os.path.splitext(fname)
-    with open(fname, 'wb') as f:
+    with open(fname, 'w', encoding="utf-8") as f:
         # shebang line
-        if lines[0].startswith(b'#!'):
-            f.write(lines[0].rstrip()+b'\n\n')
+        if lines[0].startswith('#!'):
+            f.write(lines[0].rstrip()+'\n\n')
             del lines[0]
-        f.write(str.encode(_get_license(_LANGS[ext])))
+        f.write(_get_license(_LANGS[ext]))
         for l in lines:
-            f.write(l.rstrip()+b'\n')
-    print('added license header to ' + fname)
-    return False
+            f.write(l.rstrip()+'\n')
+    logging.info('added license header to ' + fname)
+    return True
 
 def process_folder(root, action):
     excepts = []
@@ -152,16 +157,42 @@ def process_folder(root, action):
             if not process_file(fname, action):
                 excepts.append(fname)
     if action == 'check' and excepts:
-        raise Exception('The following files do not contain a valid license, '+
-                        'you can use `python tools/license_header.py add` to 
add'+
-                        'them automatically', excepts)
+        logging.warning('The following files do not contain a valid license, '+
+                        'you can use `python tools/license_header.py add 
[file]` to add'+
+                        'them automatically: ')
+        for x in excepts:
+            logging.warning(x)
+        return False
+    return True
 
 if __name__ == '__main__':
+    logging.getLogger().setLevel(logging.INFO)
+    logging.basicConfig(format='%(asctime)-15s %(message)s')
     parser = argparse.ArgumentParser(
         description='Add or check source license header')
     parser.add_argument(
         'action', nargs=1, type=str,
         choices=['add', 'check'], default='add',
         help = 'add or check')
+
+    parser.add_argument(
+        'file', nargs='*', type=str, action='append',
+        help='Files to add license header to')
+
     args = parser.parse_args()
-    process_folder(os.path.join(os.path.dirname(__file__), '..'), 
args.action[0])
+    files = list(chain(*args.file))
+    action = args.action[0]
+    has_license = True
+    if len(files) > 0:
+        for file in files:
+            has_license = process_file(file, action)
+            if action == 'check' and not has_license:
+                logging.warn("{} doesn't have a license".format(file))
+                has_license = False
+    else:
+        has_license = process_folder(os.path.join(os.path.dirname(__file__), 
'..'), action)
+    if not has_license:
+        sys.exit(1)
+    else:
+        sys.exit(0)
+


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to