You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by su...@apache.org on 2018/05/04 01:42:15 UTC

[incubator-echarts] 01/03: update license tool

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

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

commit 8d2d7e67b71c47cc5a21fb8d0645815174f3498b
Author: sushuang <su...@gmail.com>
AuthorDate: Fri May 4 07:39:47 2018 +0800

    update license tool
---
 .rat-excludes             |  42 ++++++++++
 build/addHeader.js        | 206 ++++++++++++++++++++++++++++++++--------------
 build/apache-rat-0.12.jar | Bin 0 -> 1592593 bytes
 build/config.js           |   2 +-
 build/pre-publish.js      |   6 +-
 build/preamble.js         | 162 +++++++++++++++++++++++++++++++++++-
 build/rat.sh              |   6 ++
 7 files changed, 357 insertions(+), 67 deletions(-)

diff --git a/.rat-excludes b/.rat-excludes
new file mode 100644
index 0000000..7e62673
--- /dev/null
+++ b/.rat-excludes
@@ -0,0 +1,42 @@
+node_modules
+\.git
+\.github
+\.editorconfig
+\.gitignore
+\.jshintrc
+\.jshintrc-dist
+\.npmignore
+\.rat-excludes
+\.DS_Store
+__MAC_OS
+README.md
+DISCLAIMER
+NOTICE
+KEYS
+LICENSE
+licenses
+benchmark/dep
+map/js
+map/json
+test/ut/lib
+test/data$
+test/lib/esl\.js$
+test/lib/perlin\.js$
+jquery
+configure
+IAxisPointer$
+ICoordinateSystem$
+.+\.json$
+.+\.map$
+.+\.gexf$
+.+\.jar$
+.+\.bin$
+.+\.csv$
+.+\.png$
+.+\.PNG$
+.+\.jpg$
+.+\.JPG$
+.+\.jpeg$
+.+\.JPEG$
+.+\.gif$
+.+\.GIF$
diff --git a/build/addHeader.js b/build/addHeader.js
index 202184b..7a8f31e 100644
--- a/build/addHeader.js
+++ b/build/addHeader.js
@@ -17,77 +17,159 @@
 * under the License.
 */
 
-const glob = require('glob');
 const fs = require('fs');
-const headerStr = require('./preamble');
-
-const lists = [
-    '../src/**/*.js',
-    '../build/*.js',
-    '../benchmark/src/*.js',
-    '../benchmark/src/gulpfile.js',
-    '../extension-src/**/*.js',
-    '../extension/**/*.js',
-    '../map/js/**/*.js',
-    '../test/build/**/*.js',
-    '../test/node/**/*.js',
-    '../test/ut/core/*.js',
-    '../test/ut/spe/*.js',
-    '../test/ut/ut.js',
-    '../test/*.js',
-    '../theme/*.js',
-    '../theme/tool/**/*.js',
-    '../echarts.all.js',
-    '../echarts.blank.js',
-    '../echarts.common.js',
-    '../echarts.simple.js',
-    '../index.js',
-    '../index.common.js',
-    '../index.simple.js'
-];
-
-function extractLicense(str) {
-    str = str.trim();
-    const regex = new RegExp('/\\*[\\S\\s]*?\\*/', 'm');
-    const res = regex.exec(str);
-    const commentText = res && res[0];
-    if (commentText) {
-        if(commentText.toLowerCase().includes('apache license') || commentText.toLowerCase().includes('apache commons')) {
-            return 'Apache';
+const preamble = require('./preamble');
+const pathTool = require('path');
+const {color} = require('zrender/build/helper');
+// For consistency, we use `rat-excludes` for both Apache Rat and this tool.
+// Each item is a string of RegExp
+const excludesPath = pathTool.join(__dirname, '../.rat-excludes');
+const ecBasePath = pathTool.join(__dirname, '../');
+
+// const lists = [
+//     '../src/**/*.js',
+//     '../build/*.js',
+//     '../benchmark/src/*.js',
+//     '../benchmark/src/gulpfile.js',
+//     '../extension-src/**/*.js',
+//     '../extension/**/*.js',
+//     '../map/js/**/*.js',
+//     '../test/build/**/*.js',
+//     '../test/node/**/*.js',
+//     '../test/ut/core/*.js',
+//     '../test/ut/spe/*.js',
+//     '../test/ut/ut.js',
+//     '../test/*.js',
+//     '../theme/*.js',
+//     '../theme/tool/**/*.js',
+//     '../echarts.all.js',
+//     '../echarts.blank.js',
+//     '../echarts.common.js',
+//     '../echarts.simple.js',
+//     '../index.js',
+//     '../index.common.js',
+//     '../index.simple.js'
+// ];
+
+function run() {
+    const updatedFiles = [];
+    const passFiles = [];
+    const pendingFiles = [];
+
+    eachFile(function (absolutePath, fileExt) {
+        const fileStr = fs.readFileSync(absolutePath, 'utf-8');
+
+        const existLicense = preamble.extractLicense(fileStr, fileExt);
+        // !existLicense && console.log(absolutePath);
+
+        if (existLicense) {
+            passFiles.push(absolutePath);
+            return;
         }
-        else if(commentText.toUpperCase().includes('BSD')) {
-            return 'BSD';
+
+        // Conside binary files, only add for files with known ext.
+        if (!preamble.hasPreamble(fileExt)) {
+            pendingFiles.push(absolutePath);
+            return;
         }
-        else if(commentText.toUpperCase().includes('LGPL')) {
-            return 'LGPL';
+
+        fs.writeFileSync(absolutePath, preamble.addPreamble(fileStr, fileExt), 'utf-8');
+        updatedFiles.push(absolutePath);
+    });
+
+    if (passFiles.length) {
+        console.log('\n\n');
+        console.log('----------------------------');
+        console.log(' Files that exists license: ');
+        console.log('----------------------------');
+        passFiles.forEach(function (path) {
+            console.log(color('fgGreen', 'dim')(path));
+        });
+    }
+
+    if (updatedFiles.length) {
+        console.log('\n\n');
+        console.log('--------------------');
+        console.log(' License added for: ');
+        console.log('--------------------');
+        updatedFiles.forEach(function (path) {
+            console.log(color('fgGreen', 'bright')(path));
+        });
+    }
+
+    if (pendingFiles.length) {
+        console.log('\n\n');
+        console.log('----------------');
+        console.log(' Pending files: ');
+        console.log('----------------');
+        pendingFiles.forEach(function (path) {
+            console.log(color('fgRed', 'dim')(path));
+        });
+    }
+
+    console.log('\n Done.');
+}
+
+function eachFile(visit) {
+
+    const excludePatterns = [];
+    const extReg = /\.([a-zA-Z0-9_-]+)$/;
+
+    prepareExcludePatterns();
+    travel('./');
+
+    function travel(relativePath) {
+        if (isExclude(relativePath)) {
+            return;
         }
-        else if(commentText.toUpperCase().includes('GPL')) {
-            return 'GPL';
+
+        const absolutePath = pathTool.join(ecBasePath, relativePath);
+        const stat = fs.statSync(absolutePath);
+
+        if (stat.isFile()) {
+            visit(absolutePath, getExt(absolutePath));
         }
-        else if(commentText.toLowerCase().includes('mozilla public')) {
-            return 'Mozilla';
+        else if (stat.isDirectory()) {
+            fs.readdirSync(relativePath).forEach(function (file) {
+                travel(pathTool.join(relativePath, file));
+            });
         }
-        else if(commentText.toLowerCase().includes('mit license')) {
-            return 'MIT';
+    }
+
+    function prepareExcludePatterns() {
+        const content = fs.readFileSync(excludesPath, {encoding: 'utf-8'});
+        content.replace(/\r/g, '\n').split('\n').forEach(function (line) {
+            line = line.trim();
+            if (line) {
+                excludePatterns.push(new RegExp(line));
+            }
+        });
+    }
+
+    // In Apache Rat, the ecludes file is check against the relative path
+    // (based on the base directory specified by the "--dir")
+    // See <https://github.com/sonatype/plexus-utils/blob/master/src/main/java/org/codehaus/plexus/util/DirectoryScanner.java#L400>
+    // Here we assume that the base directory is the `ecBasePath`.
+    function isExclude(relativePath) {
+        for (let i = 0; i < excludePatterns.length; i++) {
+            if (excludePatterns[i].test(relativePath)) {
+                return true;
+            }
         }
     }
-}
 
-lists.forEach(function (pattern) {
-    glob(pattern, function (err, fileList) {
-        if (err) {
-            throw new Error();
+    function getExt(path) {
+        if (path) {
+            const mathResult = path.match(extReg);
+            return mathResult && mathResult[1];
         }
-        fileList.forEach(function (fileUrl) {
-            const str = fs.readFileSync(fileUrl, 'utf-8');
+    }
+}
 
-            const existLicense = extractLicense(str);
-            if (existLicense) {
-                console.log('File ' + fileUrl + ' already have license ' + existLicense);
-                return;
-            }
+// function assert(cond, msg) {
+//     if (!cond) {
+//         throw new Error(msg);
+//     }
+// }
 
-            fs.writeFileSync(fileUrl, headerStr + str, 'utf-8');
-        });
-    });
-});
\ No newline at end of file
+run();
diff --git a/build/apache-rat-0.12.jar b/build/apache-rat-0.12.jar
new file mode 100644
index 0000000..8cbb23b
Binary files /dev/null and b/build/apache-rat-0.12.jar differ
diff --git a/build/config.js b/build/config.js
index b7f5127..04f6b3f 100644
--- a/build/config.js
+++ b/build/config.js
@@ -61,7 +61,7 @@ function getPlugins({min, lang, sourcemap, removeDev, addBundleVersion}) {
             'dead_code': true
         },
         output: {
-            preamble: preamble
+            preamble: preamble.js
         }
     }));
 
diff --git a/build/pre-publish.js b/build/pre-publish.js
index d0d5291..5a4953e 100644
--- a/build/pre-publish.js
+++ b/build/pre-publish.js
@@ -60,17 +60,17 @@ module.exports = function () {
     prePulishSrc({
         inputPath: path.resolve(ecDir, 'echarts.all.js'),
         outputPath: path.resolve(ecDir, 'index.js'),
-        preamble: preamble
+        preamble: preamble.js
     });
     prePulishSrc({
         inputPath: path.resolve(ecDir, 'echarts.common.js'),
         outputPath: path.resolve(ecDir, 'index.common.js'),
-        preamble: preamble
+        preamble: preamble.js
     });
     prePulishSrc({
         inputPath: path.resolve(ecDir, 'echarts.simple.js'),
         outputPath: path.resolve(ecDir, 'index.simple.js'),
-        preamble: preamble
+        preamble: preamble.js
     });
 
     function transform({code, inputPath, outputPath}) {
diff --git a/build/preamble.js b/build/preamble.js
index 8a545a6..3b0c5e8 100644
--- a/build/preamble.js
+++ b/build/preamble.js
@@ -1,4 +1,23 @@
-module.exports = `
+/*
+* 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
+* regarding copyright ownership.  The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License.  You may obtain a copy of the License at
+*
+*   http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied.  See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*/
+
+const cStyleComment = `
 /*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
@@ -19,3 +38,144 @@ module.exports = `
 */
 
 `;
+
+const hashComment = `
+# 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
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+`;
+
+const mlComment = `
+<!--
+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
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+`;
+
+function hasPreamble(fileExt) {
+    return fileExt && preambleMap[fileExt];
+}
+
+function addPreamble(fileStr, fileExt) {
+    if (fileStr && fileExt) {
+        const addFn = addFns[fileExt];
+        const headStr = preambleMap[fileExt];
+        return addFn && headStr && addFn(headStr, fileStr);
+    }
+}
+
+const addFns = {
+
+    js: function (headStr, fileStr) {
+        return headStr + fileStr;
+    },
+
+    css: function (headStr, fileStr) {
+        return headStr + fileStr;
+    },
+
+    sh: function (headStr, fileStr) {
+        // Git diff enables manual check.
+        if (/^#\!/.test(fileStr)) {
+            const lines = fileStr.split('\n');
+            lines.splice(1, 0, headStr);
+            return lines.join('\n');
+        }
+        else {
+            return headStr + fileStr;
+        }
+    },
+
+    html: function (headStr, fileStr) {
+        // Git diff enables manual check.
+        let resultStr = fileStr.replace(/^\s*<!DOCTYPE\s[^<>]+>/i, '$&' + headStr);
+        // If no doctype
+        if (resultStr.length === fileStr.length) {
+            resultStr = headStr + fileStr;
+        }
+
+        return resultStr;
+    }
+};
+
+const preambleMap = {
+    js: cStyleComment,
+    css: cStyleComment,
+    sh: hashComment,
+    html: mlComment
+};
+
+const licenseReg = [
+    {name: 'Apache', reg: /apache (license|commons)/i},
+    {name: 'BSD', reg: /BSD/},
+    {name: 'LGPL', reg: /LGPL/},
+    {name: 'GPL', reg: /GPL/},
+    {name: 'Mozilla', reg: /mozilla public/i},
+    {name: 'MIT', reg: /mit license/i}
+];
+
+function extractLicense(fileStr, fileExt) {
+    let commentText = extractComment(fileStr.trim(), fileExt);
+    if (!commentText) {
+        return;
+    }
+    for (let i = 0; i < licenseReg.length; i++) {
+        if (licenseReg[i].reg.test(commentText)) {
+            return licenseReg[i].name;
+        }
+    }
+}
+
+const cStyleCommentReg = /\/\*[\S\s]*?\*\//;
+const hashCommentReg = /^\s*#.*$/m;
+const mlCommentReg = /<\!\-\-[\S\s]*?\-\->/;
+const commentReg = {
+    js: cStyleCommentReg,
+    css: cStyleCommentReg,
+    sh: hashCommentReg,
+    html: mlCommentReg
+};
+
+function extractComment(str, fileExt) {
+    const reg = commentReg[fileExt];
+    if (!fileExt || !reg || !str) {
+        return;
+    }
+    let result = cStyleCommentReg.exec(str);
+    return result && result[0];
+}
+
+module.exports = Object.assign({
+    extractLicense,
+    hasPreamble,
+    addPreamble
+}, preambleMap);
diff --git a/build/rat.sh b/build/rat.sh
new file mode 100644
index 0000000..117ac92
--- /dev/null
+++ b/build/rat.sh
@@ -0,0 +1,6 @@
+basePath=$(cd `dirname $0`; pwd)
+ecPath=${basePath}/..
+
+java -jar "${basePath}/apache-rat-0.12.jar" --exclude-file "${ecPath}/.rat-excludes" --dir ${ecPath}
+#  > "${basePath}/rat-result.txt"
+# java -jar "${basePath}/apache-rat/apache-rat-0.12.jar" -h
\ No newline at end of file

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

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org