2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
10 DESCRIPTION
= '''This tools reads in a GYP file, parses it as JSON, grabs the
11 requested object(s) using the passed in |key_name| and |key_value| and then
12 builds up a space delimited string using the |return_key| given. The built
13 up string is then output to STDOUT which can be read and tokenized in
14 bash or python scripts.'''
15 FILE_HELP
= '''The GYP file to parse'''
16 APP_KEY_NAME_HELP
= '''Key name used to identify relevant webapp details'''
17 APP_KEY_VALUE_HELP
= '''Key value used to identify relevant webapp details,
18 multiple values can be passed in'''
19 TARGET_KEY_VALUE_HELP
= '''The key name used to output the targeted value'''
20 DEBUG_HELP
= '''Turns on verbose debugging output'''
22 # Cleans up the text in the passed in GYP file, updates it to make it valid JSON
23 # and returns the valid json string.
24 def gyp_file_to_json_string(gyp_file
):
25 # First, read in each line and discard comments and whitespace.
28 lines
= line
.split("#")
29 line_data
+= lines
[0].strip()
31 # Trailing commas are valid in GYP files but invalid in JSON, so remove them
32 # here. Also convert double quotes to single quotes and those throw off the
34 line_data
= line_data
.replace(',}', '}')
35 line_data
= line_data
.replace(',]', ']')
36 line_data
= line_data
.replace('\'', '\"')
41 # Finds the matching app detail sections in |data| and generates a space
42 # delimited string with the |return_key|'s value. If we found at least one
43 # matching app and created a string, we output it to STDOUT.
44 def print_details(data
, key_name
, key_values
, target_key
):
46 for target
in data
['targets']:
47 if target
[key_name
] in key_values
:
49 output_string
+= " " + target
[target_key
]
51 output_string
+= target
[target_key
]
58 parser
= argparse
.ArgumentParser(description
= DESCRIPTION
)
59 parser
.add_argument('file', nargs
= '+', help = FILE_HELP
)
60 parser
.add_argument('-k', '--key_name', help = APP_KEY_NAME_HELP
,
61 nargs
= '?', required
=True)
62 parser
.add_argument('-v', '--key_value', help = APP_KEY_VALUE_HELP
,
63 nargs
= '+', required
=True)
64 parser
.add_argument('-t', '--target_key', help = TARGET_KEY_VALUE_HELP
,
65 nargs
= '?', required
=True)
66 parser
.add_argument('-d', '--debug', help = DEBUG_HELP
,
68 options
= parser
.parse_args()
71 print 'Reading from file \"' + options
.file[0] + '\"'
73 gyp_file
= open(options
.file[0])
74 json_string
= gyp_file_to_json_string(gyp_file
)
76 json_object
= json
.loads(json_string
)
79 print 'The following app details were found:'
80 for target
in json_object
['targets']:
81 print target
['target_name'], target
['app_id'], target
['app_name']
83 print_details(json_object
,
90 if __name__
== '__main__':