]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - sort-pkgs
use cargo-vendor-filterer if available
[packages/rpm-build-tools.git] / sort-pkgs
CommitLineData
94e9b4e3 1#!/usr/bin/python3
1859b517
WF
2
3"""
4This script tries to set ordering in which packages ought to be sent to builders.
1481cd8e 5Input: file with names of packages. If not given packages are read from stdin. One package name per line.
1859b517
WF
6Output: sorted packages on stdout.
7
8If the script goes in a infinite loop, that means there is a cycle or other bug.
9"""
10
11import os
12import re
13import sys
14
94e9b4e3
WF
15BR_PATTERN = re.compile('BuildRequires:\s+(.*?)(\s|$)')
16PACKAGE_PATTERN_WITH_N = re.compile('%package\s+-n\s+(.*)')
17PACKAGE_PATTERN = re.compile('%package\s+(.*)')
18
3fc1a823
JP
19try:
20 import rpm
21 DIR = rpm.expandMacro('%_topdir')
22except ModuleNotFoundError:
23 try:
24 import subprocess
25 DIR = subprocess.check_output(['rpm', '-E', '%_topdir']).decode('utf-8').strip()
26 except:
27 DIR = os.getenv("HOME") + '/rpm/packages'
94e9b4e3
WF
28
29BUILD_REQUIRES = {}
30PACKAGES = {}
31SPECS = {}
32VISITED = {}
33
34
35def parse_spec(name):
36 global PACKAGES, SPECS, BUILD_REQUIRES, VISITED
37 res = []
38 try:
39 with open(os.path.join(DIR, name, name + '.spec'), 'r') as f:
40 for line in f:
41 br = BR_PATTERN.match(line)
42 if br:
43 p = br.group(1)
44 res.append(p)
45 if line.startswith('%package'):
46 pn = PACKAGE_PATTERN_WITH_N.match(line)
47 if pn:
48 package = pn.group(1)
49 PACKAGES[package] = name
50 else:
51 pn = PACKAGE_PATTERN.match(line)
52 if pn:
53 ext = pn.group(1)
54 if ext:
55 package = name + '-' + ext
56 PACKAGES[package] = name
57 BUILD_REQUIRES[name] = res[:]
58 PACKAGES[name] = name
59 SPECS[name] = True
60 VISITED[name] = False
61 except:
62 pass
63
64
65def print_spec(spec):
66 global PACKAGES, SPECS, BUILD_REQUIRES, VISITED
67
68 if not VISITED[spec]:
69 VISITED[spec] = True
70 for br in BUILD_REQUIRES[spec]:
71 name = PACKAGES.get(br, '')
72 if name in SPECS:
73 if not VISITED[name]:
74 print_spec(name)
75 print(spec)
76
1859b517
WF
77
78if __name__ == "__main__":
1481cd8e 79 with (len(sys.argv) > 1 and open(sys.argv[1], 'r') or sys.stdin) as f:
94e9b4e3 80 for line in f:
258a4bea 81 spec = os.path.basename(os.path.normpath(line.rstrip())).removesuffix('.spec')
94e9b4e3
WF
82 parse_spec(spec)
83
84 for spec in SPECS:
85 print_spec(spec)
This page took 0.074619 seconds and 4 git commands to generate.