|
1 | 1 | import sys
|
2 | 2 |
|
3 |
| -patch_script = """ |
| 3 | +patch_script_tmp = """ |
4 | 4 |
|
5 | 5 | def run_patch_scripts(patch_script_path):
|
6 |
| - import os |
7 |
| - for patch in os.listdir(patch_script_path): |
8 |
| - full_patch_file = os.path.join(patch_script_path, patch) |
9 |
| - if full_patch_file.endswith('.py') and os.path.isfile(full_patch_file): |
10 |
| - with open(full_patch_file, 'r') as f: |
11 |
| - try: |
12 |
| - exec(f.read()) |
13 |
| - except: |
14 |
| - pass |
15 |
| -run_patch_scripts('/patcher-script.d') |
| 6 | + with open(patch_script_path, 'r') as f: |
| 7 | + try: |
| 8 | + exec(f.read()) |
| 9 | + except: |
| 10 | + pass |
| 11 | +run_patch_scripts("%s") |
16 | 12 |
|
17 | 13 | """
|
18 | 14 |
|
19 | 15 |
|
20 |
| -def main(patch_file): |
| 16 | +def main(patch_file, patch_script_file): |
21 | 17 | result_list = []
|
| 18 | + patch_script = patch_script_tmp % patch_script_file |
22 | 19 | with open(patch_file, "r") as f:
|
23 | 20 | has_patched = False
|
24 | 21 | for line in f:
|
25 |
| - if not has_patched and line.startswith('import'): |
| 22 | + if (line.startswith('import') or line.startswith('from')) \ |
| 23 | + and not has_patched: |
26 | 24 | result_list.append(patch_script)
|
27 | 25 | has_patched = True
|
28 | 26 | result_list.append(line)
|
| 27 | + if not has_patched: result_list.append(patch_script) |
29 | 28 | with open(patch_file, "w") as f:
|
30 | 29 | for line in result_list:
|
31 | 30 | f.write(line)
|
32 | 31 |
|
33 | 32 | if __name__ == '__main__':
|
34 |
| - main(sys.argv[1]) |
| 33 | + patch_type = sys.argv[1] |
| 34 | + if patch_type == 'file': |
| 35 | + patch_file = sys.argv[2] |
| 36 | + elif patch_type == 'module': |
| 37 | + module = __import__(sys.argv[2], fromlist=True) |
| 38 | + patch_file = module.__file__ |
| 39 | + patch_script_file = sys.argv[3] |
| 40 | + main(patch_file, patch_script_file) |
| 41 | + |
0 commit comments