|
6 | 6 |
|
7 | 7 |
|
8 | 8 | def run_equery_depgraph(pkg):
|
9 |
| - pkg = pkg.replace('_','/',1) |
10 |
| - command = f"emerge -pv {pkg}" |
| 9 | + pkg = pkg.replace('_', '/', 1) |
| 10 | + command = f"emerge -pv {pkg}" |
| 11 | + try: |
| 12 | + result = subprocess.run( |
| 13 | + command, shell=True, check=True, capture_output=True, text=True) |
| 14 | + return result.stdout |
| 15 | + except subprocess.CalledProcessError as e: |
| 16 | + if "The following USE changes are necessary to proceed" in e.stderr: |
| 17 | + print(f"Package {pkg} needs USE flag") |
| 18 | + use_changes = parse_emerge_output(e.stderr) |
| 19 | + update_package_use_custom(use_changes) |
11 | 20 | try:
|
12 |
| - result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) |
13 |
| - return result.stdout |
| 21 | + result = subprocess.run( |
| 22 | + command, shell=True, check=True, capture_output=True, text=True) |
| 23 | + return result.stdout |
14 | 24 | except subprocess.CalledProcessError as e:
|
15 |
| - if "The following USE changes are necessary to proceed" in e.stderr: |
16 |
| - print(f"Package {pkg} needs USE flag") |
17 |
| - use_changes = parse_emerge_output(e.stderr) |
18 |
| - update_package_use_custom(use_changes) |
19 |
| - try: |
20 |
| - result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) |
21 |
| - return result.stdout |
22 |
| - except subprocess.CalledProcessError as e: |
23 |
| - if "have been masked." in e.stderr: |
24 |
| - return 1 |
25 |
| - if "The following USE changes are necessary to proceed" in e.stderr: |
26 |
| - return 2 |
27 |
| - return None |
| 25 | + if "have been masked." in e.stderr: |
| 26 | + return 1 |
| 27 | + if "The following USE changes are necessary to proceed" in e.stderr: |
| 28 | + return 2 |
| 29 | + return None |
| 30 | + |
28 | 31 |
|
29 | 32 | def parse_emerge_output(output):
|
30 |
| - use_changes = re.findall(r'>=([^\s]+)\s+([^\n]+)', output) |
31 |
| - use_changes = [(re.sub(r'-[0-9.]+(?:-r[0-9]+)?$', '', package), flags) for package, flags in use_changes] |
32 |
| - return use_changes |
| 33 | + use_changes = re.findall(r'>=([^\s]+)\s+([^\n]+)', output) |
| 34 | + use_changes = [(re.sub(r'-[0-9.]+(?:-r[0-9]+)?$', '', package), flags) |
| 35 | + for package, flags in use_changes] |
| 36 | + return use_changes |
33 | 37 |
|
34 | 38 |
|
35 | 39 | def update_package_use_custom(use_changes):
|
36 |
| - package_use_dir = '/etc/portage/package.use' |
37 |
| - custom_file_path = os.path.join(package_use_dir, 'custom') |
38 |
| - |
39 |
| - if not os.path.exists(package_use_dir): |
40 |
| - os.makedirs(package_use_dir) |
41 |
| - |
42 |
| - if not os.path.exists(custom_file_path): |
43 |
| - open(custom_file_path, 'a').close() |
44 |
| - |
45 |
| - with open(custom_file_path, 'r+') as f: |
46 |
| - content = f.read() |
47 |
| - for package, flags in use_changes: |
48 |
| - if package not in content: |
49 |
| - f.write(f"{package} {flags}\n") |
50 |
| - print(f"Added to {custom_file_path}: {package} {flags}") |
51 |
| - else: |
52 |
| - print(f"Package {package} already exists in {custom_file_path}") |
| 40 | + package_use_dir = '/etc/portage/package.use' |
| 41 | + custom_file_path = os.path.join(package_use_dir, 'custom') |
| 42 | + |
| 43 | + if not os.path.exists(package_use_dir): |
| 44 | + os.makedirs(package_use_dir) |
| 45 | + |
| 46 | + if not os.path.exists(custom_file_path): |
| 47 | + open(custom_file_path, 'a').close() |
| 48 | + |
| 49 | + with open(custom_file_path, 'r+') as f: |
| 50 | + content = f.read() |
| 51 | + for package, flags in use_changes: |
| 52 | + if package not in content: |
| 53 | + f.write(f"{package} {flags}\n") |
| 54 | + print(f"Added to {custom_file_path}: {package} {flags}") |
| 55 | + else: |
| 56 | + print(f"Package {package} already exists in {custom_file_path}") |
53 | 57 |
|
54 | 58 |
|
55 | 59 | def parse_depgraph(content):
|
56 |
| - lines = content.split('\n') |
57 |
| - dependencies = [] |
58 |
| - for line in lines[1:]: |
59 |
| - match = re.search(r"(\w+-[\w+/-]+?)(?:-\d[\w\._-]*)", line) |
60 |
| - if match: |
61 |
| - dependencies.append(match.group(1)) |
| 60 | + lines = content.split('\n') |
| 61 | + dependencies = [] |
| 62 | + for line in lines[1:]: |
| 63 | + match = re.search(r"(\w+-[\w+/-]+?)(?:-\d[\w\._-]*)", line) |
| 64 | + if match: |
| 65 | + dependencies.append(match.group(1)) |
| 66 | + |
| 67 | + return dependencies |
62 | 68 |
|
63 |
| - return dependencies |
64 | 69 |
|
65 | 70 | def analyse_neither():
|
66 |
| - failed = [] |
67 |
| - successed = [] |
68 |
| - # neither.json is a log analyzing the results of a package whose installation result type is neither. |
69 |
| - # 'Y' means it has been built successfully. |
70 |
| - # 'N' means its build failed. |
71 |
| - with open('./portage-lists/neither.json') as neither_pkgs_files: |
72 |
| - neither_pkgs = json.load(neither_pkgs_files) |
73 |
| - for pkg in neither_pkgs: |
74 |
| - if neither_pkgs[pkg] == 'N': |
75 |
| - failed.append(pkg) |
76 |
| - if neither_pkgs[pkg] == 'Y': |
77 |
| - successed.append(pkg) |
78 |
| - |
79 |
| - return failed, successed |
| 71 | + failed = [] |
| 72 | + succeeded = [] |
| 73 | + # neither.json is a log analyzing the results of a package whose installation result type is neither. |
| 74 | + # 'Y' means it has been built successfully. |
| 75 | + # 'N' means its build failed. |
| 76 | + with open('./portage-lists/neither.json') as neither_pkgs_files: |
| 77 | + neither_pkgs = json.load(neither_pkgs_files) |
| 78 | + for pkg in neither_pkgs: |
| 79 | + if neither_pkgs[pkg] == 'N': |
| 80 | + failed.append(pkg) |
| 81 | + if neither_pkgs[pkg] == 'Y': |
| 82 | + succeeded.append(pkg) |
| 83 | + |
| 84 | + return failed, succeeded |
| 85 | + |
80 | 86 |
|
81 | 87 | def preprocessed_notinstalled_pkgs():
|
82 |
| - notinstalled = [] |
83 |
| - with open('./portage-lists/notinstalled.list', 'r') as notinstalled_file: |
84 |
| - for i in notinstalled_file: |
85 |
| - notinstalled.append(i[:-1]) |
86 |
| - return notinstalled |
87 |
| - |
| 88 | + notinstalled = [] |
| 89 | + with open('./portage-lists/notinstalled.list', 'r') as notinstalled_file: |
| 90 | + for i in notinstalled_file: |
| 91 | + notinstalled.append(i[:-1]) |
| 92 | + return notinstalled |
| 93 | + |
| 94 | + |
88 | 95 | def main():
|
89 |
| - failed_pkgs, successed_pkgs = analyse_neither() |
90 |
| - |
91 |
| - notinstalled_pkgs = preprocessed_notinstalled_pkgs() |
92 |
| - installed_pkgs = preprocessed_notinstalled_pkgs() |
93 |
| - |
94 |
| - failed_pkgs.extend(notinstalled_pkgs) |
95 |
| - successed_pkgs.extend(installed_pkgs) |
96 |
| - |
97 |
| - error_dict = {} |
98 |
| - error_list = [] |
99 |
| - mask_list = [] |
100 |
| - use_list = [] |
101 |
| - for pkg in failed_pkgs: |
102 |
| - content = run_equery_depgraph(pkg) |
103 |
| - if content == 1: |
104 |
| - mask_list.append(pkg) |
105 |
| - continue |
106 |
| - if content == 2: |
107 |
| - use_list.append(pkg) |
108 |
| - continue |
109 |
| - if content is None: |
110 |
| - error_list.append(pkg) |
111 |
| - print(f"Package {pkg} cannot be merged.") |
112 |
| - else: |
113 |
| - parsed_data = parse_depgraph(content) |
114 |
| - for i in parsed_data: |
115 |
| - if i.replace('/','_') not in successed_pkgs: |
116 |
| - if i in error_dict: |
117 |
| - error_dict[i] += 1 |
118 |
| - else: |
119 |
| - error_dict[i] = 1 |
120 |
| - |
121 |
| - sorted_dict = OrderedDict(sorted(error_dict.items(), key=lambda item: item[1], reverse=True)) |
122 |
| - |
123 |
| - with open('./portage-lists/error.list', 'w') as file: |
124 |
| - for i in error_list: |
125 |
| - file.write(i+'\n') |
126 |
| - with open('./portage-lists/mask.list', 'w') as file: |
127 |
| - for i in mask_list: |
128 |
| - file.write(i+'\n') |
129 |
| - with open('./portage-lists/use.list', 'w') as file: |
130 |
| - for i in use_list: |
131 |
| - file.write(i+'\n') |
132 |
| - with open('./portage-lists/depedencies_pkgs.json', 'w') as file: |
133 |
| - json.dump(sorted_dict, file, indent=4) |
134 |
| - |
| 96 | + failed_pkgs, succeeded_pkgs = analyse_neither() |
| 97 | + |
| 98 | + notinstalled_pkgs = preprocessed_notinstalled_pkgs() |
| 99 | + installed_pkgs = preprocessed_notinstalled_pkgs() |
| 100 | + |
| 101 | + failed_pkgs.extend(notinstalled_pkgs) |
| 102 | + succeeded_pkgs.extend(installed_pkgs) |
| 103 | + |
| 104 | + error_dict = {} |
| 105 | + error_list = [] |
| 106 | + mask_list = [] |
| 107 | + use_list = [] |
| 108 | + for pkg in failed_pkgs: |
| 109 | + content = run_equery_depgraph(pkg) |
| 110 | + if content == 1: |
| 111 | + mask_list.append(pkg) |
| 112 | + continue |
| 113 | + if content == 2: |
| 114 | + use_list.append(pkg) |
| 115 | + continue |
| 116 | + if content is None: |
| 117 | + error_list.append(pkg) |
| 118 | + print(f"Package {pkg} cannot be merged.") |
| 119 | + else: |
| 120 | + parsed_data = parse_depgraph(content) |
| 121 | + for i in parsed_data: |
| 122 | + if i.replace('/', '_') not in succeeded_pkgs: |
| 123 | + if i in error_dict: |
| 124 | + error_dict[i] += 1 |
| 125 | + else: |
| 126 | + error_dict[i] = 1 |
| 127 | + |
| 128 | + sorted_dict = OrderedDict( |
| 129 | + sorted(error_dict.items(), key=lambda item: item[1], reverse=True)) |
| 130 | + |
| 131 | + with open('./portage-lists/error.list', 'w') as file: |
| 132 | + for i in error_list: |
| 133 | + file.write(i + '\n') |
| 134 | + with open('./portage-lists/mask.list', 'w') as file: |
| 135 | + for i in mask_list: |
| 136 | + file.write(i + '\n') |
| 137 | + with open('./portage-lists/use.list', 'w') as file: |
| 138 | + for i in use_list: |
| 139 | + file.write(i + '\n') |
| 140 | + with open('./portage-lists/depedencies_pkgs.json', 'w') as file: |
| 141 | + json.dump(sorted_dict, file, indent=4) |
| 142 | + |
| 143 | + |
135 | 144 | if __name__ == "__main__":
|
136 |
| - main() |
| 145 | + main() |
0 commit comments