forked from canonical/core-base
-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate-connections.py
executable file
·53 lines (45 loc) · 1.16 KB
/
generate-connections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import sys
import os
import stat
if len(sys.argv) != 3:
print("Usage: generate-connections.py origin-file destination-file")
sys.exit(1)
origin = sys.argv[1]
destination = sys.argv[2]
if not os.path.isfile(origin):
sys.exit(0)
destination_dir = os.path.dirname(destination)
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
with open(origin, "rt") as origin_data:
with open(destination, "wt") as destination_data:
destination_data.write("""#!/bin/sh
while : ; do
retval=0
""")
for line in origin_data.readlines():
line = line.strip()
if line == "":
continue
if line[0] == '#':
continue
snap_name = line[:line.find(":")]
destination_data.write(f"""
if /usr/bin/snap list {snap_name}; then
if ! /usr/bin/snap connect {line}; then
retval=1
fi
else
retval=1
fi
""")
destination_data.write("""
if [ "$retval" = "0" ] ; then
systemctl disable manual-plug-connection.service
exit 0
fi
sleep 1
done
""")
os.chmod(destination, 0o755)