-# 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)
+
-#!/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 <file> -p <path>'
- print ''
- print '<opt> \t <opt> \t\t <description>'
- 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:])