buildorder.py: wrap main()
[termux-packages] / buildorder.py
CommitLineData
59f0d218
FF
1#!/usr/bin/env python3
2# buildorder.py - script to generate a build order respecting package dependencies
3
6521bf5c
FD
4import os
5import sys
59f0d218 6
59f0d218 7
6521bf5c
FD
8def die(msg):
9 sys.exit('ERROR: ' + msg)
59f0d218 10
59f0d218 11
6521bf5c
FD
12if len(sys.argv) != 1:
13 die('buildorder.py takes no arguments')
14
15
16class DebianPackage(object):
17 def __init__(self, name):
18 self.name = name
19 self.remaining_dependencies = set() # String
20 self.sub_packages = set() # String
21 self.prerequisite_for = set() # Packages that needs this package
22
23# List of all DebianPackage:s
24all_packages = []
25
26# Mapping from package name to DebianPackage
27# (if subpackage, mapping from subpackage name to parent package)
28packages_map = {}
59f0d218 29
8906fc18 30packages_dir = 'packages'
6521bf5c
FD
31
32
15ea0fef
FD
33def main():
34 for subdir_name in sorted(os.listdir(packages_dir)):
35 subdir_path = packages_dir + '/' + subdir_name
36
37 if os.path.exists(subdir_path + '/BROKEN.txt'):
38 continue
39
40 build_sh_path = subdir_path + '/build.sh'
41
42 this_package = DebianPackage(subdir_name)
43 all_packages.append(this_package)
44 packages_map[this_package.name] = this_package
45
46 if not os.path.isfile(build_sh_path):
47 die('The directory ' + subdir_name + ' does not contain build.sh')
48
49 with open(build_sh_path) as build_sh_file:
50 for line in build_sh_file:
51 if line.startswith('TERMUX_PKG_DEPENDS='):
52 deps_comma_separated = line[(line.index('=')+2):(len(line)-2)]
53 for dep in deps_comma_separated.split(','):
54 dep = dep.strip()
55 if not dep.endswith('libandroid-support-dev'):
6521bf5c 56 this_package.remaining_dependencies.add(dep)
15ea0fef
FD
57
58 for file_in_subdir_name in sorted(os.listdir(subdir_path)):
59 if file_in_subdir_name.endswith('.subpackage.sh'):
60 subpackage_name = file_in_subdir_name[0:-len(".subpackage.sh"):]
61 this_package.sub_packages.add(subpackage_name)
62 packages_map[subpackage_name] = this_package
63 with open(subdir_path + '/' + file_in_subdir_name) as subpackage_sh_file:
64 for line in subpackage_sh_file:
65 if line.startswith('TERMUX_SUBPKG_DEPENDS='):
66 deps_comma_separated = line[(line.index('=')+2):(len(line)-2)]
67 for dep in deps_comma_separated.split(','):
68 dep = dep.strip()
69 this_package.remaining_dependencies.add(dep)
70 # Do not depend on itself
71 this_package.remaining_dependencies.discard(this_package.name)
72 # Do not depend on any sub package
73 this_package.remaining_dependencies.difference_update(this_package.sub_packages)
74
75 for package in all_packages:
76 for remaining in package.remaining_dependencies:
77 if remaining not in packages_map:
78 die('Package %s depends on non-existing package "%s"' % (
79 package.name, remaining
80 ))
81 packages_map[remaining].prerequisite_for.add(package)
82
83 # List of all DebianPackage:s without dependencies
84 packages_without_deps = [p for p in all_packages if not p.remaining_dependencies]
85 if not packages_without_deps:
86 die('No package without dependency - where to start?')
87
88 # Sort alphabetically, but with libandroid-support first (since dependency on libandroid-support
89 # does not need to be declared explicitly, so anything might in theory depend on it to build):
90
91 packages_without_deps.sort(
92 key=lambda p: '' if p.name == 'libandroid-support' else p.name,
93 reverse=True)
94
95 # Topological sorting
96 build_order = []
97 while packages_without_deps:
98 pkg = packages_without_deps.pop()
99 build_order.append(pkg)
100 for other_package in pkg.prerequisite_for:
101 # Remove this package
102 other_package.remaining_dependencies.discard(pkg.name)
103 # .. and all its subpackages
104 other_package.remaining_dependencies.difference_update(pkg.sub_packages)
105 if not other_package.remaining_dependencies:
106 # Check if the other package is ready to build now
107 packages_without_deps.append(other_package)
108
109 if len(all_packages) != len(build_order):
110 print("ERROR: Cycle exists. Remaining: ")
111 for pkg in all_packages:
112 if pkg not in build_order:
113 print(pkg.name)
114 sys.exit(1)
115
116 for pkg in build_order:
117 print(pkg.name)
118
119if __name__ == '__main__':
120 main()