Merge pull request #110 from franciscod/playaudio-script-spaces
[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
4import os, sys
5
8906fc18 6def die(msg): sys.exit('ERROR: ' + msg)
59f0d218 7
0c6bde0a 8if len(sys.argv) != 1: die('buildorder.py takes no arguments')
59f0d218
FF
9
10class DebianPackage:
11 def __init__(self, name):
12 self.name = name
13 self.remaining_dependencies = set() # String
14 self.sub_packages = set() # String
15 self.prerequisite_for = set() # Packages that needs this package
16
17all_packages = [] # List of all DebianPackage:s
18packages_map = {} # Mapping from package name to DebianPackage (if subpackage, mapping from subpackage name to parent package)
19
8906fc18 20packages_dir = 'packages'
59f0d218
FF
21for subdir_name in sorted(os.listdir(packages_dir)):
22 subdir_path = packages_dir + '/' + subdir_name
23 if os.path.exists(subdir_path + '/BROKEN.txt'): continue
24 build_sh_path = subdir_path + '/build.sh'
25
26 this_package = DebianPackage(subdir_name)
27 all_packages.append(this_package)
28 packages_map[this_package.name] = this_package
29
30 if not os.path.isfile(build_sh_path): die('The directory ' + subdir_name + ' does not contain build.sh')
31 with open(build_sh_path) as build_sh_file:
32 for line in build_sh_file:
33 if line.startswith('TERMUX_PKG_DEPENDS='):
34 deps_comma_separated = line[(line.index('=')+2):(len(line)-2)]
35 for dep in deps_comma_separated.split(','):
36 dep = dep.strip()
fc28d8f4 37 if not dep.endswith('libandroid-support-dev'): this_package.remaining_dependencies.add(dep)
59f0d218
FF
38 for file_in_subdir_name in sorted(os.listdir(subdir_path)):
39 if file_in_subdir_name.endswith('.subpackage.sh'):
40 subpackage_name = file_in_subdir_name[0:-len(".subpackage.sh"):]
41 this_package.sub_packages.add(subpackage_name)
42 packages_map[subpackage_name] = this_package
43 with open(subdir_path + '/' + file_in_subdir_name) as subpackage_sh_file:
44 for line in subpackage_sh_file:
45 if line.startswith('TERMUX_SUBPKG_DEPENDS='):
46 deps_comma_separated = line[(line.index('=')+2):(len(line)-2)]
47 for dep in deps_comma_separated.split(','):
48 dep = dep.strip()
49 this_package.remaining_dependencies.add(dep)
50 this_package.remaining_dependencies.discard(this_package.name) # Do not depend on itself
51 this_package.remaining_dependencies.difference_update(this_package.sub_packages) # Do not depend on any sub package
52
53for package in all_packages:
54 for remaining in package.remaining_dependencies:
55 if not remaining in packages_map: die('Package ' + package.name + ' depends on non-existing package "' + remaining + '"')
56 packages_map[remaining].prerequisite_for.add(package)
57
58# List of all DebianPackage:s without dependencies
59packages_without_deps = [p for p in all_packages if not p.remaining_dependencies]
60if not packages_without_deps: die('No package without dependency - where to start?')
61
62# Sort alphabetically, but with libandroid-support first (since dependency on libandroid-support
63# does not need to be declared explicitly, so anything might in theory depend on it to build):
64packages_without_deps.sort(key=lambda p: 'aaaa' if p.name == 'libandroid-support' else p.name, reverse=True)
65
66# Topological sorting
67build_order = []
68while packages_without_deps:
69 pkg = packages_without_deps.pop()
70 build_order.append(pkg)
71 for other_package in pkg.prerequisite_for:
72 other_package.remaining_dependencies.discard(pkg.name) # Remove this package
73 other_package.remaining_dependencies.difference_update(pkg.sub_packages) # .. and all its subpackages
74 if not other_package.remaining_dependencies:
75 # Check if the other package is ready to build now
76 packages_without_deps.append(other_package)
77
78if len(all_packages) != len(build_order):
79 print("ERROR: Cycle exists. Remaining: ");
80 for pkg in all_packages:
81 if pkg not in build_order: print(pkg.name)
82 sys.exit(1)
83
84for pkg in build_order: print(pkg.name)
85