]>
Commit | Line | Data |
---|---|---|
94e9b4e3 | 1 | #!/usr/bin/python3 |
1859b517 WF |
2 | |
3 | """ | |
4 | This script tries to set ordering in which packages ought to be sent to builders. | |
5 | Input: file with names of packages (without the .spec suffix). One package name per line. | |
6 | Output: sorted packages on stdout. | |
7 | ||
8 | If the script goes in a infinite loop, that means there is a cycle or other bug. | |
9 | """ | |
10 | ||
11 | import os | |
12 | import re | |
13 | import sys | |
14 | ||
94e9b4e3 WF |
15 | BR_PATTERN = re.compile('BuildRequires:\s+(.*?)(\s|$)') |
16 | PACKAGE_PATTERN_WITH_N = re.compile('%package\s+-n\s+(.*)') | |
17 | PACKAGE_PATTERN = re.compile('%package\s+(.*)') | |
18 | ||
19 | DIR = os.getenv("HOME") + '/rpm/packages' | |
20 | ||
21 | BUILD_REQUIRES = {} | |
22 | PACKAGES = {} | |
23 | SPECS = {} | |
24 | VISITED = {} | |
25 | ||
26 | ||
27 | def parse_spec(name): | |
28 | global PACKAGES, SPECS, BUILD_REQUIRES, VISITED | |
29 | res = [] | |
30 | try: | |
31 | with open(os.path.join(DIR, name, name + '.spec'), 'r') as f: | |
32 | for line in f: | |
33 | br = BR_PATTERN.match(line) | |
34 | if br: | |
35 | p = br.group(1) | |
36 | res.append(p) | |
37 | if line.startswith('%package'): | |
38 | pn = PACKAGE_PATTERN_WITH_N.match(line) | |
39 | if pn: | |
40 | package = pn.group(1) | |
41 | PACKAGES[package] = name | |
42 | else: | |
43 | pn = PACKAGE_PATTERN.match(line) | |
44 | if pn: | |
45 | ext = pn.group(1) | |
46 | if ext: | |
47 | package = name + '-' + ext | |
48 | PACKAGES[package] = name | |
49 | BUILD_REQUIRES[name] = res[:] | |
50 | PACKAGES[name] = name | |
51 | SPECS[name] = True | |
52 | VISITED[name] = False | |
53 | except: | |
54 | pass | |
55 | ||
56 | ||
57 | def print_spec(spec): | |
58 | global PACKAGES, SPECS, BUILD_REQUIRES, VISITED | |
59 | ||
60 | if not VISITED[spec]: | |
61 | VISITED[spec] = True | |
62 | for br in BUILD_REQUIRES[spec]: | |
63 | name = PACKAGES.get(br, '') | |
64 | if name in SPECS: | |
65 | if not VISITED[name]: | |
66 | print_spec(name) | |
67 | print(spec) | |
68 | ||
1859b517 WF |
69 | |
70 | if __name__ == "__main__": | |
94e9b4e3 WF |
71 | if len(sys.argv) < 2: |
72 | print("Usage: %s filename" % sys.argv[0]) | |
73 | sys.exit(1) | |
74 | with open(sys.argv[1], 'r') as f: | |
75 | for line in f: | |
76 | spec = line.rstrip() | |
77 | parse_spec(spec) | |
78 | ||
79 | for spec in SPECS: | |
80 | print_spec(spec) |