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