|
11 | 11 | RHEL_reg = r'(x?RHEL)_(\d{1,2})_(\d{1,2}).*\.json' |
12 | 12 | Ubuntu_reg = r'(x?Ubuntu)_(\d{1,2})_(\d{1,2}).*\.json' |
13 | 13 | regexes = [ |
14 | | - re.compile(SLES_reg), |
15 | | - re.compile(RHEL_reg), |
16 | | - re.compile(Ubuntu_reg) |
| 14 | + re.compile(SLES_reg), |
| 15 | + re.compile(RHEL_reg), |
| 16 | + re.compile(Ubuntu_reg) |
17 | 17 | ] |
18 | 18 |
|
| 19 | + |
19 | 20 | def scan(): |
20 | | - return os.listdir(DATA_FILE_LOCATION) |
| 21 | + return os.listdir(DATA_FILE_LOCATION) |
| 22 | + |
21 | 23 |
|
22 | 24 | def packagetype(file): |
23 | | - global SLES_reg, RHEL_reg, Ubuntu_reg, regexes |
24 | | - for ind,reg in enumerate(regexes): |
25 | | - if reg.match(file): |
26 | | - addfile(file, ind) |
| 25 | + for ind, reg in enumerate(regexes): |
| 26 | + if reg.match(file): |
| 27 | + addfile(file, ind) |
| 28 | + |
27 | 29 |
|
28 | 30 | def addfile(file, ind): |
29 | | - global SLES_reg, RHEL_reg, Ubuntu_reg, regexes |
30 | | - flag = False |
31 | | - with open(SUPPORTED_DISTRO_FILE) as DATA: |
32 | | - data = DATA.read() |
33 | | - groups = regexes[ind].search(file) |
34 | | - name = re.sub(r'^x', '', groups.group(1).replace('_', ' ')) |
35 | | - if not ind == 0: |
36 | | - if name in data and not file in data: |
37 | | - print(f"Found new file: {file}") |
38 | | - start = data.index(name) + len(name)+7 |
39 | | - new_data = f"\t'{groups.group(1)} {groups.group(2)}.{groups.group(3)}': '{file}',\n" |
40 | | - flag = True |
41 | | - else: |
42 | | - if name in data and not file in data: |
43 | | - print(f"Found new file: {file}") |
44 | | - start = data.index(name) + len(name)+7 |
45 | | - new_data = f"\t'SLES {groups.group(4)} {'' if groups.group(6)==None else groups.group(6)}': '{file}',\n" |
46 | | - flag = True |
47 | | - |
48 | | - if flag: |
49 | | - with open(SUPPORTED_DISTRO_FILE, 'r+') as DATA: |
50 | | - DATA.seek(start) |
51 | | - next_data = DATA.read() |
52 | | - DATA.seek(start) |
53 | | - DATA.write(new_data) |
54 | | - DATA.write(next_data) |
| 31 | + flag = False |
| 32 | + with open(SUPPORTED_DISTRO_FILE) as DATA: |
| 33 | + data = DATA.read() |
| 34 | + groups = regexes[ind].search(file) |
| 35 | + name = re.sub(r'^x', '', groups.group(1).replace('_', ' ')) |
| 36 | + if ind != 0: |
| 37 | + if name in data and file not in data: |
| 38 | + print(f"Found new file: {file}") |
| 39 | + start = data.index(name) + len(name) + 7 |
| 40 | + new_data = f"\t'{groups.group(1)} {groups.group(2)}.{groups.group(3)}': '{file}',\n" |
| 41 | + flag = True |
| 42 | + else: |
| 43 | + if name in data and file not in data: |
| 44 | + print(f"Found new file: {file}") |
| 45 | + start = data.index(name) + len(name) + 7 |
| 46 | + new_data = ( |
| 47 | + f"\t'SLES {groups.group(4)} " |
| 48 | + f"{'': if groups.group(6) is None else groups.group(6)}': '{file}',\n" |
| 49 | + ) |
| 50 | + flag = True |
55 | 51 |
|
56 | | -def format_data(): |
57 | | - duplicate = [] |
| 52 | + if flag: |
58 | 53 | with open(SUPPORTED_DISTRO_FILE, 'r+') as DATA: |
59 | | - old_format = DATA.read() |
60 | | - DATA.truncate(0) |
61 | | - new_format = re.sub(r"(json',\n})", "json'\n}", old_format) |
62 | | - DATA.seek(0) |
63 | | - DATA.write(new_format) |
| 54 | + DATA.seek(start) |
| 55 | + next_data = DATA.read() |
| 56 | + DATA.seek(start) |
| 57 | + DATA.write(new_data) |
| 58 | + DATA.write(next_data) |
| 59 | + |
| 60 | + |
| 61 | +def format_data(): |
| 62 | + with open(SUPPORTED_DISTRO_FILE, 'r+') as DATA: |
| 63 | + old_format = DATA.read() |
| 64 | + DATA.truncate(0) |
| 65 | + new_format = re.sub(r"(json',\n})", "json'\n}", old_format) |
| 66 | + DATA.seek(0) |
| 67 | + DATA.write(new_format) |
| 68 | + |
64 | 69 |
|
65 | 70 | def del_cache(): |
66 | | - try: |
67 | | - print("Attempting to delete cached_data.json...") |
68 | | - os.remove(f'{DATA_FILE_LOCATION}/cached_data.json') |
69 | | - except: |
70 | | - print("File not found in directory.") |
| 71 | + try: |
| 72 | + print("Attempting to delete cached_data.json...") |
| 73 | + os.remove(f'{DATA_FILE_LOCATION}/cached_data.json') |
| 74 | + except OSError: |
| 75 | + print("File not found in directory.") |
| 76 | + |
71 | 77 |
|
72 | 78 | def pull_new(files): |
73 | | - global regexes |
74 | | - print("Attempting to update PDS data sources...") |
75 | | - for file in files: |
76 | | - if any(regex.match(file) for regex in regexes): |
77 | | - print(f'Updating {file}...') |
78 | | - subprocess.run([f'{SDT_BASE}/bin/package_build.py', file], stdout=subprocess.DEVNULL) |
| 79 | + print("Attempting to update PDS data sources...") |
| 80 | + for file in files: |
| 81 | + if any(regex.match(file) for regex in regexes): |
| 82 | + print(f'Updating {file}...') |
| 83 | + subprocess.run([f'{SDT_BASE}/bin/package_build.py', file], stdout=subprocess.DEVNULL) |
| 84 | + |
79 | 85 |
|
80 | 86 | if __name__ == "__main__": |
81 | | - print("Scanning distro_data directory...") |
82 | | - files = scan() |
83 | | - for file in files: |
84 | | - packagetype(file) |
85 | | - format_data() |
86 | | - pull_new(files) |
87 | | - del_cache() |
88 | | - print('Done.') |
| 87 | + print("Scanning distro_data directory...") |
| 88 | + files = scan() |
| 89 | + for file in files: |
| 90 | + packagetype(file) |
| 91 | + format_data() |
| 92 | + pull_new(files) |
| 93 | + del_cache() |
| 94 | + print('Done.') |
0 commit comments