-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall-sysdata.py
executable file
·114 lines (93 loc) · 3.41 KB
/
install-sysdata.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018-2024 Matthias Klumpp <[email protected]>
#
# SPDX-License-Identifier: LGPL-3.0-or-later
#
# This is a helper script to install additional configuration and documentation into
# system locations, which Python's setuptools and pip will not usually let us install.
#
import os
import sys
import shutil
from pathlib import Path
from argparse import ArgumentParser
from tempfile import TemporaryDirectory
try:
import pkgconfig
except ImportError:
print()
print(
(
'Unable to import pkgconfig. Please install the module '
'(apt install python3-pkgconfig or pip install pkgconfig) '
'to continue.'
)
)
print()
sys.exit(4)
class Installer:
def __init__(self, root: str = None, prefix: str = None):
if not root:
root = os.environ.get('DESTDIR')
if not root:
root = '/'
self.root = root
if not prefix:
prefix = '/usr/local' if self.root == '/' else '/usr'
if prefix.startswith('/'):
prefix = prefix[1:]
self.prefix = prefix
def install(self, src, dst, replace_vars=False):
if dst.startswith('/'):
dst = dst[1:]
dst_full = os.path.join(self.root, dst, os.path.basename(src))
else:
dst_full = os.path.join(self.root, self.prefix, dst, os.path.basename(src))
if dst_full.endswith('.in'):
dst_full = dst_full[:-3]
Path(os.path.dirname(dst_full)).mkdir(mode=0o755, parents=True, exist_ok=True)
if replace_vars:
with open(src, 'r') as f_src:
with open(dst_full, 'w') as f_dst:
for line in f_src:
f_dst.write(line.replace('@PREFIX@', '/' + self.prefix))
else:
shutil.copy(src, dst_full)
os.chmod(dst_full, 0o644)
print('{}\t\t{}'.format(os.path.basename(src), dst_full))
def chdir_to_source_root():
thisfile = __file__
if not os.path.isabs(thisfile):
thisfile = os.path.normpath(os.path.join(os.getcwd(), thisfile))
os.chdir(os.path.dirname(thisfile))
def install_data(temp_dir: str, root_dir: str, prefix_dir: str):
chdir_to_source_root()
print('Checking dependencies')
if not pkgconfig.installed('systemd', '>= 240'):
print('Systemd is not installed on this system. Please make systemd available to continue.')
sys.exit(4)
print('Installing data')
inst = Installer(root_dir, prefix_dir)
sd_system_unit_dir = pkgconfig.variables('systemd')['systemdsystemunitdir']
inst.install('data/systemd/laniakea-spark.service.in', sd_system_unit_dir, replace_vars=True)
inst.install('data/sudo/10laniakea-spark', '/etc/sudoers.d/', replace_vars=True)
def main():
parser = ArgumentParser(description='Debspawn system data installer')
parser.add_argument(
'--root', action='store', dest='root', default=None, help='Root directory to install into.'
)
parser.add_argument(
'--prefix',
action='store',
dest='prefix',
default=None,
help='Directory prefix (usually `/usr` or `/usr/local`).',
)
options = parser.parse_args(sys.argv[1:])
with TemporaryDirectory(prefix='dsinstall-') as temp_dir:
install_data(temp_dir, options.root, options.prefix)
return 0
if __name__ == '__main__':
sys.exit(main())