|
| 1 | +from sys import argv |
| 2 | +from os.path import isfile |
| 3 | +from shutil import copy2 |
| 4 | + |
| 5 | +# Validate arguments |
| 6 | +if len(argv) != 2: |
| 7 | + print('Invalid number of arguments, you should pass the location of \'node.gyp\'.') |
| 8 | + print('Usage: python3 NodeJSGYPPatch.py <path_to_node_gyp>') |
| 9 | + exit(1) |
| 10 | + |
| 11 | +# Validate the project file |
| 12 | +if not isfile(argv[1]) or not argv[1].endswith('node.gyp'): |
| 13 | + print('The file \'' + argv[1] + '\' does not exist or is not a valid gyp file, it must be named as \'node.gyp\'.') |
| 14 | + exit(2) |
| 15 | + |
| 16 | +# Read the file |
| 17 | +f = open(argv[1]) |
| 18 | +nodegyp = eval(f.read()) |
| 19 | +f.close() |
| 20 | + |
| 21 | +# Validate that the target is present and it is correct |
| 22 | +target = next((x for x in nodegyp['targets'] if 'target_name' in x and x['target_name'] == '<(node_lib_target_name)'), None) |
| 23 | + |
| 24 | +if target is None: |
| 25 | + print('Invalid node.gyp configuration, the target \'node_lib_target_name\' is not present.') |
| 26 | + exit(3) |
| 27 | + |
| 28 | +condition = next((x for x in target['conditions'] if x[0] == 'OS=="win"'), None) |
| 29 | + |
| 30 | +if condition is None or (condition is not None and len(condition) != 2): |
| 31 | + print('Invalid node.gyp configuration, the condition \'OS=="win"\' is not present in target \'node_lib_target_name\'.') |
| 32 | + exit(4) |
| 33 | + |
| 34 | +if not 'libraries' in condition[1]: |
| 35 | + print('Invalid node.gyp configuration, \'libraries\' field is not present in the condition \'OS=="win"\' of the target target \'node_lib_target_name\'.') |
| 36 | + exit(5) |
| 37 | + |
| 38 | +# Get the libraries |
| 39 | +libraries = condition[1]['libraries'] |
| 40 | + |
| 41 | +# Check if the library to patch is present |
| 42 | +if not 'Winmm' in libraries: |
| 43 | + # Copy file as backup |
| 44 | + copy2(argv[1], argv[1] + '.backup') |
| 45 | + |
| 46 | + # Apply the patch to the libraries |
| 47 | + libraries.append('Winmm') |
| 48 | + |
| 49 | + # Overwrite the node.gyp project |
| 50 | + f = open(argv[1], 'w') |
| 51 | + f.write(repr(nodegyp)) |
| 52 | + f.close() |
| 53 | + |
| 54 | +print('Build project node.gyp patched correctly') |
0 commit comments