Add 'XW_MessagingInterface_2' for exchanging binary messages
[platform/framework/web/xwalk-extensions-common.git] / tools / build / mergejs.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import fileinput
8 import sys
9 import getopt
10 import glob
11 import os
12
13 class Utils:
14     reqfiles = []
15     searchfile = '*_api.js'
16     startwith = "//= require('"
17     endwith = "')"
18     code = ""
19
20     @classmethod
21     def get_require(self, s):
22         try:
23             start = s.index(self.startwith) + len(self.startwith)
24             end = s.index(self.endwith, start)
25             filename = s[start:end]
26             self.reqfiles.append(filename)
27         except ValueError:
28            return ""
29
30     @classmethod
31     def find_require(self):
32         p = os.path.join('./', self.searchfile)
33         filenames = glob.glob(self.searchfile)
34         for fname in filenames:
35             with open(fname, 'r') as myfile:
36                 for line in myfile:
37                     self.get_require(line)
38
39     @classmethod
40     def print_lines(self, filename):
41         with open(filename, 'r') as file:
42             for line in file:
43                 self.code += line
44
45     @classmethod
46     def merge_js_files(self, path):
47         self.find_require()
48         if len(self.reqfiles) == 0:
49             s = os.path.join('./', self.searchfile)
50             sfiles = glob.glob(s)
51             for fname in sfiles:
52                 self.print_lines(fname)
53         else:
54             js = '*.js'
55             p = os.path.join(path, js)
56             filenames = glob.glob(p)
57             for fname in self.reqfiles:
58                 fname = path + '/' + fname
59                 if fname in filenames:
60                     self.print_lines(fname)
61
62     @classmethod
63     def main(self, argv):
64         path = 'js'
65         try:
66             opts, args = getopt.getopt(argv,"hf:p:",["file=", "path="])
67         except getopt.GetoptError:
68             print __file__ + ' -h'
69             sys.exit()
70         if len(argv) > 0:
71           for opt, arg in opts:
72               if opt in ("-h"):
73                   print 'Help:'
74                   print ''
75                   print __file__ + '-f <file> -p <path>'
76                   print ''
77                   print '<opt> \t <opt> \t\t <description>'
78                   print '-f \t --file \t Name of the file where script searching for require files:'
79                   print '\t \t \t ' + self.startwith + 'file_name.js' + self.endwith
80                   print '-p \t --path \t Path to "' + path + '" directory'
81                   print ''
82                   sys.exit()
83               elif opt in ("-f", "--file"):
84                   self.searchfile = arg
85               elif opt in ("-p", "--path"):
86                   path = arg
87         self.merge_js_files(path)
88         print self.code
89
90 if Utils.__module__ == "__main__":
91     Utils.main(sys.argv[1:])