From 28eaf976113e7d21630bbe108b72f8079375833d Mon Sep 17 00:00:00 2001 From: Michal Michalski Date: Tue, 22 Oct 2019 16:48:15 +0200 Subject: [PATCH] [tools] Rewritten mergejs.py and generate_api.py scripts. + tools/generate_api.py has been rewritten to be more readable. + tools/mergejs.py has been rewritten to work more like C++ include directive,\ to be more of a in-place copy-paste mechanism. Previously the only way to use //=require() was to create a file in which no code other than the require() directives was allowed to be. If there was some code it was removed during the preprocessing phase. After this change it is possible to mix code and require() commands in the same file. [Verification] I've tested this change by running TCTs for all modules that use require() directive (all modules with js/ subdir). tct-filesystem-tizen-tests 100% pass tct-calendar-tizen-tests 100% pass tct-contact-tizen-tests 100% pass tct-content-tizen-tests 100% pass Signed-off-by: Michal Michalski Change-Id: I71436797b78a1746b9ea105bc466a75d42d26ddb --- packaging/webapi-plugins.spec | 1 - tools/generate_api.py | 55 +++++++++++++----- tools/js_minimize.py | 7 --- tools/mergejs.py | 129 +++++++++++++++--------------------------- 4 files changed, 87 insertions(+), 105 deletions(-) delete mode 100755 tools/js_minimize.py diff --git a/packaging/webapi-plugins.spec b/packaging/webapi-plugins.spec index e81f571..145badb 100644 --- a/packaging/webapi-plugins.spec +++ b/packaging/webapi-plugins.spec @@ -1292,7 +1292,6 @@ install -p -m 644 src/common/XW_Extension.cc %{buildroot}%{_includedir}/%{name}/ mkdir -p %{buildroot}%{_includedir}/%{name}/tools install -p -m 644 tools/generate_api.py %{buildroot}%{_includedir}/%{name}/tools install -p -m 644 tools/mergejs.py %{buildroot}%{_includedir}/%{name}/tools -install -p -m 644 tools/js_minimize.py %{buildroot}%{_includedir}/%{name}/tools cp -a tools/gyp %{buildroot}%{_includedir}/%{name}/tools/gyp cp -a tools/slimit %{buildroot}%{_includedir}/%{name}/tools/slimit cp -a out/Default/desc_gentool %{buildroot}%{_includedir}/%{name}/tools/desc_gentool diff --git a/tools/generate_api.py b/tools/generate_api.py index d9dfc02..f6c87c0 100755 --- a/tools/generate_api.py +++ b/tools/generate_api.py @@ -1,22 +1,49 @@ -# Copyright (c) 2013 Intel Corporation. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved +# +# Licensed 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. import os import sys -import subprocess +import argparse +import mergejs + TEMPLATE = """\ -extern const char %s[]; -const char %s[] = { %s, 0 }; +extern const char {}[]; +const char {}[] = {{ {}, 0 }}; """ -js_code = sys.argv[1] -cmd = "python " + os.path.dirname(__file__) + "/mergejs.py -f" + js_code -lines = subprocess.check_output(cmd, shell=True) -c_code = ', '.join(str(ord(c)) for c in lines) -symbol_name = sys.argv[2] -output = open(sys.argv[3], "w") -output.write(TEMPLATE % (symbol_name, symbol_name, c_code)) -output.close() +def encode(text): + return ', '.join(str(ord(c)) for c in text) + + +def generate_cpp_code(input_file, symbol): + code = mergejs.process_file(input_file) + return TEMPLATE.format(symbol, symbol, encode(code)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument("input", help="input js file path") + parser.add_argument("symbol", help="symbol name to use") + parser.add_argument("output", help="output file path") + opts = parser.parse_args() + + with open(opts.output, "w") as output: + cpp = generate_cpp_code(opts.input, opts.symbol) + output.write(cpp) + diff --git a/tools/js_minimize.py b/tools/js_minimize.py deleted file mode 100755 index f78126f..0000000 --- a/tools/js_minimize.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/python - -import slimit - -def minimize(code): - return slimit.minify(code) - diff --git a/tools/mergejs.py b/tools/mergejs.py index 36b776a..53372a0 100755 --- a/tools/mergejs.py +++ b/tools/mergejs.py @@ -1,95 +1,58 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved +# +# Licensed 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. + -# Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved -import fileinput import sys -import getopt -import glob import os -import js_minimize +import re +import argparse +import slimit + + +MATCHER = re.compile("^//=\s*require\(['\"](.*)['\"]\)") + -class Utils: - reqfiles = [] - searchfile = '*_api.js' - startwith = "//= require('" - endwith = "')" - code = "" +def _process_file(path, includes_directory="js"): + lines = [] + with open(path, "r") as source_file: + for line in source_file: + m = MATCHER.match(line.strip()) + if m != None: + include_path = os.path.join(includes_directory, m.group(1)) + lines += _process_file(include_path, includes_directory) + else: + lines.append(line.strip()) + return lines - @classmethod - def get_require(self, s): - try: - start = s.index(self.startwith) + len(self.startwith) - end = s.index(self.endwith, start) - filename = s[start:end] - self.reqfiles.append(filename) - except ValueError: - return "" - @classmethod - def find_require(self): - p = os.path.join('./', self.searchfile) - filenames = glob.glob(self.searchfile) - for fname in filenames: - with open(fname, 'r') as myfile: - for line in myfile: - self.get_require(line) +def process_file(path, includes_directory="js"): + lines = _process_file(path, includes_directory) + return "\n".join(lines) - @classmethod - def print_lines(self, filename): - with open(filename, 'r') as file: - for line in file: - self.code += line - @classmethod - def merge_js_files(self, path): - self.find_require() - if len(self.reqfiles) == 0: - s = os.path.join('./', self.searchfile) - sfiles = glob.glob(s) - for fname in sfiles: - self.print_lines(fname) - else: - js = '*.js' - p = os.path.join(path, js) - filenames = glob.glob(p) - for fname in self.reqfiles: - fname = path + '/' + fname - if fname in filenames: - self.print_lines(fname) +if __name__ == '__main__': + opt_parser = argparse.ArgumentParser() + opt_parser.add_argument("--file", "-f", type=str, help="input file name") + opt_parser.add_argument("--path", "-p", type=str, default="js", help="path to the includes directory") - @classmethod - def minize_code(self): - self.code = js_minimize.minimize(self.code) + opts = opt_parser.parse_args() + code = process_file(opts.file, opts.path) + code = slimit.minify(code) - @classmethod - def main(self, argv): - path = 'js' - try: - opts, args = getopt.getopt(argv,"hf:p:",["file=", "path="]) - except getopt.GetoptError: - print __file__ + ' -h' - sys.exit() - if len(argv) > 0: - for opt, arg in opts: - if opt in ("-h"): - print 'Help:' - print '' - print __file__ + '-f -p ' - print '' - print ' \t \t\t ' - print '-f \t --file \t Name of the file where script searching for require files:' - print '\t \t \t ' + self.startwith + 'file_name.js' + self.endwith - print '-p \t --path \t Path to "' + path + '" directory' - print '' - sys.exit() - elif opt in ("-f", "--file"): - self.searchfile = arg - elif opt in ("-p", "--path"): - path = arg - self.merge_js_files(path) - self.minize_code() - print self.code + print(code) -if Utils.__module__ == "__main__": - Utils.main(sys.argv[1:]) -- 2.7.4