Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / tools / swarming_client / isolate_merge.py
1 #!/usr/bin/env python
2 # Copyright 2012 The Swarming Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 that
4 # can be found in the LICENSE file.
5
6 """Merges multiple OS-specific gyp dependency lists into one that works on all
7 of them.
8
9 The logic is relatively simple. Takes the current conditions, add more
10 condition, find the strict subset. Done.
11 """
12
13 import logging
14 import os
15 import sys
16
17 import isolate_format
18
19 from utils import tools
20
21
22 def load_isolates(items):
23   """Parses each .isolate file and returns the merged results.
24
25   It only loads what load_isolate_as_config() can process.
26
27   Return values:
28     files: dict(filename, set(OS where this filename is a dependency))
29     dirs:  dict(dirame, set(OS where this dirname is a dependency))
30     oses:  set(all the OSes referenced)
31   """
32   # pylint: disable=W0212
33   configs = isolate_format.Configs(None, ())
34   for item in items:
35     item = os.path.abspath(item)
36     logging.debug('loading %s' % item)
37     if item == '-':
38       content = sys.stdin.read()
39     else:
40       with open(item, 'r') as f:
41         content = f.read()
42     new_config = isolate_format.load_isolate_as_config(
43         os.path.dirname(item),
44         isolate_format.eval_content(content),
45         isolate_format.extract_comment(content))
46     logging.debug('has configs: ' + ','.join(map(repr, new_config._by_config)))
47     configs = configs.union(new_config)
48   logging.debug('Total configs: ' + ','.join(map(repr, configs._by_config)))
49   return configs
50
51
52 def main(args=None):
53   tools.disable_buffering()
54   parser = tools.OptionParserWithLogging(
55       usage='%prog <options> [file1] [file2] ...')
56   parser.add_option(
57       '-o', '--output', help='Output to file instead of stdout')
58
59   options, args = parser.parse_args(args)
60
61   configs = load_isolates(args)
62   data = configs.make_isolate_file()
63   if options.output:
64     with open(options.output, 'wb') as f:
65       isolate_format.print_all(configs.file_comment, data, f)
66   else:
67     isolate_format.print_all(configs.file_comment, data, sys.stdout)
68   return 0
69
70
71 if __name__ == '__main__':
72   sys.exit(main())