-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
95 lines (75 loc) · 2.05 KB
/
install.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
pi433 setup
Installs pi433 on a pi zero
Needs to be run with sudo.
'''
import os
import pip
import shutil
import subprocess
try:
from setuptools import setup, Extension
from setuptools.command import install_lib, sdist, build_ext
except ImportError:
from distutils.core import setup, Extension
from distutils.command import install_lib, sdist, build_ext
LOCAL_DIR = os.path.dirname(os.path.realpath(__file__))
INSTALL_DIR = os.path.expanduser('~/.pi433')
# Check for admin priv
if not os.getuid() == 0:
raise OSError('Must run setup script with sudo!')
# Create ~/.pi433 directory
if not os.path.isdir(INSTALL_DIR):
os.makedirs(INSTALL_DIR)
# Copy `pi433` directory to ~/.py433
print('Copying pi433 module...')
module_dir = os.path.join(INSTALL_DIR, 'pi433')
if os.path.isdir(module_dir):
shutil.rmtree(module_dir)
shutil.copytree(
os.path.join(LOCAL_DIR, 'pi433'),
os.path.join(module_dir)
)
# Copy cli.py to ~/.py433
print('Copying "cli.py"...')
shutil.copy(
os.path.join(LOCAL_DIR, 'cli.py'),
os.path.join(INSTALL_DIR, 'cli.py')
)
# Copy "config.json" to ~/.py433
print('Copying "config.json"...')
shutil.copy(
os.path.join(LOCAL_DIR, 'config.json'),
os.path.join(INSTALL_DIR, 'config.json')
)
# Write pi433 service file
service_txt = '''[Unit]
Description=pi433 service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python %s/cli.py
[Install]
WantedBy=multi-user.target''' % INSTALL_DIR
print('Writing pi433.service...')
SERVICE_PATH = '/lib/systemd/system/' + 'pi433.service'
with open(SERVICE_PATH, 'w') as fh:
fh.write(service_txt)
print('Setting permissions for pi433.service...')
subprocess.call(
['sudo', 'chmod', '644', SERVICE_PATH]
)
print('Restarting systemd...')
subprocess.call(
['sudo', 'systemctl', 'daemon-reload']
)
print('Enabling "pi433.service"...')
subprocess.call(
['sudo', 'systemctl', 'enable', 'pi433.service']
)
print('Starting "pi433.service"...')
subprocess.call(
['sudo', 'systemctl', 'start', 'pi433.service']
)