Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 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. | |
| 5 | |
| 6 import argparse | |
| 7 import json | |
| 8 import sys | |
| 9 | |
| 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''' | |
| 21 | |
| 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. | |
| 26 line_data = '' | |
| 27 for line in gyp_file: | |
| 28 lines = line.split("#") | |
| 29 line_data += lines[0].strip() | |
| 30 | |
| 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 | |
| 33 # python json parser. | |
| 34 line_data = line_data.replace(',}', '}') | |
| 35 line_data = line_data.replace(',]', ']') | |
| 36 line_data = line_data.replace('\'', '\"') | |
| 37 | |
| 38 return line_data | |
| 39 | |
| 40 | |
| 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): | |
| 45 output_string = '' | |
|
garykac
2015/02/19 01:24:17
FYI: Most of this function can be replaced with so
| |
| 46 for target in data['targets']: | |
| 47 if target[key_name] in key_values: | |
| 48 if output_string: | |
|
garykac
2015/02/19 01:24:17
Another way to get rid of this "add a space if we
| |
| 49 output_string += " " + target[target_key] | |
| 50 else: | |
| 51 output_string += target[target_key] | |
| 52 | |
| 53 if output_string: | |
| 54 print output_string | |
| 55 | |
| 56 | |
| 57 def main(): | |
| 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, | |
| 67 action='store_true') | |
| 68 options = parser.parse_args() | |
| 69 | |
| 70 if options.debug: | |
| 71 print 'Reading from file \"' + options.file[0] + '\"' | |
| 72 | |
| 73 gyp_file = open(options.file[0]) | |
| 74 json_string = gyp_file_to_json_string(gyp_file) | |
| 75 | |
| 76 json_object = json.loads(json_string) | |
| 77 | |
| 78 if options.debug: | |
| 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'] | |
| 82 | |
| 83 print_details(json_object, | |
| 84 options.key_name, | |
| 85 options.key_value, | |
| 86 options.target_key) | |
| 87 | |
| 88 return 0 | |
| 89 | |
| 90 if __name__ == '__main__': | |
| 91 sys.exit(main()) | |
| OLD | NEW |