forked from Wuestengecko/DoorPi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
executable file
·138 lines (118 loc) · 4.17 KB
/
setup.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
"""DoorPi Setup"""
from pathlib import Path
from os.path import basename, join, exists, expanduser
from os import chmod, makedirs, geteuid, getegid, execv
import sys
import subprocess
import importlib
from shutil import rmtree
from argparse import ArgumentParser
ap = ArgumentParser()
ap.add_argument("--prefix", required=False, help="prefix for setuptools setup")
ap.add_argument("install", help="build and install the package")
args = vars(ap.parse_args())
SYSTEM_REQUIREMENTS = ["python3-pip", "ffmpeg"]
# base path of the cloned git
BASE_PATH = Path(__file__).resolve().parent
PACKAGE = basename(BASE_PATH)
PROJECT = PACKAGE.lower()
DEFAULT_PREFIX = join(expanduser("~"), ".local", "share")
PREFIX = args["prefix"] or DEFAULT_PREFIX
if PREFIX in ("/usr", "/usr/local"):
WORKING_DIR = join(PREFIX, "etc", PROJECT)
else:
WORKING_DIR = join(PREFIX, PROJECT)
CONFIG_DIR = join(WORKING_DIR, "conf")
CONFIG_FILE = join(CONFIG_DIR, f"{PROJECT}.ini")
LOG_FILE = join("/var", "log", PROJECT, f"{PROJECT}.log")
LOG_DIR = join("/var", "log", PROJECT)
# DAEMON_DIR = "/etc/init.d"
def pako_installed():
def cleanup():
if exists("/tmp/pako"):
rmtree("/tmp/pako")
proc_clone = subprocess.Popen(["git", "clone", "https://github.com/MycroftAI/pako"], cwd="/tmp")
proc_clone.wait()
if proc_clone.returncode != 0:
cleanup()
return False
proc_setup = subprocess.Popen(["python3", "setup.py", "install"], cwd="/tmp/pako")
proc_setup.wait()
cleanup()
if proc_setup.returncode != 0:
return False
return True
# Check for pip, setuptools and wheel
try:
import pip
import setuptools
import wheel
except ImportError as exp:
print("install missing pip now (%s)" % exp)
from get_pip import main as check_for_pip
old_args = sys.argv
sys.argv = [sys.argv[0]]
try:
check_for_pip()
except SystemExit as e:
if e.code == 0:
# Thus additional system packages are required, install a os independent packet manager (python package)
if not pako_installed():
print(f'''Exiting. Can't install the required packages.
System Requirements: {SYSTEM_REQUIREMENTS}
Please install above packages manually before installing DoorPi''')
sys.exit()
execv(sys.executable, [sys.executable] + old_args)
else:
print("install pip failed with error code %s" % e.code)
sys.exit(e.code)
if importlib.util.find_spec("pako"):
from pako import PakoManager
manager = PakoManager()
manager.update()
manager.install(SYSTEM_REQUIREMENTS, flags=['no-confirm'])
datapath = BASE_PATH / "data"
substkeys = {
"package": PACKAGE,
"project": PROJECT,
"prefix": PREFIX,
"cfgfile": CONFIG_FILE,
"cfgdir": CONFIG_DIR,
"logfile": LOG_FILE,
"user": geteuid(),
"group": getegid()
}
for file in datapath.iterdir():
if file.suffix != ".in":
continue
content = file.read_text()
for key, val in substkeys.items():
content = content.replace(f"!!{key}!!", str(val))
file.with_suffix("").write_text(content)
# make the resulting file executable
if file.stem.endswith(".sh"):
chmod(join(datapath, file.stem), 0o755)
# create relevant folders
for folder in (CONFIG_DIR, LOG_DIR):
if not exists(folder):
makedirs(folder)
Path(CONFIG_FILE).touch()
if PREFIX != DEFAULT_PREFIX:
with open(CONFIG_FILE, "w") as f:
f.write(f'base_path = "{WORKING_DIR}"')
# system.d service used only until there's a need for it
# (DAEMON_DIR, ["data/doorpi.sh"]),
setuptools.setup(
package_data={
# include
'': ['*.yml', '*.cfg', '*.txt', '*.toml', '*.rst', '*.wav', '*.ico', '*.md', '*.json', '*.html', '*.js',
'*.css', '*.png', '*.tab', '*.sh', '*.gif', '*.jpg', '*.coffee', '*.less', '*.psd', '*.swf', '*.svg',
'*.otf', '*.eot', '*.woff', '*.ttf', '*.scss', '*.db', '*.map', '*.lang', '*.xml', '*.pack', '*.idx',
'*.sample'],
},
data_files=[
# systemd service
(join(PREFIX, "lib/systemd/system"), ["data/doorpi.service", "data/doorpi.socket"]),
],
)