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