]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/90-rearrange-path.py
verify: use manager_load_startable_unit_or_warn() to load units for verification
[thirdparty/systemd.git] / man / 90-rearrange-path.py
1 #!/usr/bin/env python3
2
3 """
4
5 Proof-of-concept systemd environment generator that makes sure that bin dirs
6 are always after matching sbin dirs in the path.
7 (Changes /sbin:/bin:/foo/bar to /bin:/sbin:/foo/bar.)
8
9 This generator shows how to override the configuration possibly created by
10 earlier generators. It would be easier to write in bash, but let's have it
11 in Python just to prove that we can, and to serve as a template for more
12 interesting generators.
13
14 """
15
16 import os
17 import pathlib
18
19 def rearrange_bin_sbin(path):
20 """Make sure any pair of …/bin, …/sbin directories is in this order
21
22 >>> rearrange_bin_sbin('/bin:/sbin:/usr/sbin:/usr/bin')
23 '/bin:/sbin:/usr/bin:/usr/sbin'
24 """
25 items = [pathlib.Path(p) for p in path.split(':')]
26 for i in range(len(items)):
27 if 'sbin' in items[i].parts:
28 ind = items[i].parts.index('sbin')
29 bin = pathlib.Path(*items[i].parts[:ind], 'bin', *items[i].parts[ind+1:])
30 if bin in items[i+1:]:
31 j = i + 1 + items[i+1:].index(bin)
32 items[i], items[j] = items[j], items[i]
33 return ':'.join(p.as_posix() for p in items)
34
35 if __name__ == '__main__':
36 path = os.environ['PATH'] # This should be always set.
37 # If it's not, we'll just crash, we is OK too.
38 new = rearrange_bin_sbin(path)
39 if new != path:
40 print('PATH={}'.format(new))