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 6d2b6e3  Improve license_header.py, add license to individual files 
(#9849)
6d2b6e3 is described below

commit 6d2b6e3463b0839cc5d3025a23c4d9c3f4cc564f
Author: Pedro Larroy <928489+lar...@users.noreply.github.com>
AuthorDate: Thu Feb 22 15:15:23 2018 +0100

    Improve license_header.py, add license to individual files (#9849)
---
 Jenkinsfile             |  2 +-
 tools/license_header.py | 63 ++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 48 insertions(+), 17 deletions(-)

diff --git a/Jenkinsfile b/Jenkinsfile
index 17d546c..81ddb73 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 5a0c029..14d3f58
--- 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 @@ Usuage:
 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 @@ _WHITE_LIST = ['R-package/',
                'dmlc-core/',
                'mshadow/',
                'nnvm',
-               '3rdparty',   
+               '3rdparty',
                'ps-lite',
                'src/operator/mkl/',
                'src/operator/special_functions-inl.h',
@@ -89,7 +94,7 @@ _LANGS = {'.cc':'*', '.h':'*', '.cu':'*', '.cuh':'*', 
'.py':'#',
 _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)
+

-- 
To stop receiving notification emails like this one, please contact
marcoab...@apache.org.

Reply via email to