-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup.py
103 lines (93 loc) · 4.23 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
from setuptools import setup, Extension
import os
import pwd
import grp
from myDevices import __version__
from myDevices.system.hardware import Hardware
classifiers = ['Development Status :: 5 - Production/Stable',
'Operating System :: POSIX :: Linux',
'License :: OSI Approved :: MIT License',
'Intended Audience :: Developers',
'Programming Language :: Python :: 3',
'Topic :: Software Development',
'Topic :: Home Automation',
'Topic :: System :: Hardware',
'Topic :: System :: Monitoring']
try:
# Try to install under the cayenne user, if it exists.
username = 'cayenne'
user = pwd.getpwnam(username)
user_id = user.pw_uid
group_id = user.pw_gid
except KeyError:
# Otherwise install under the user that ran sudo.
user_id = int(os.environ['SUDO_UID'])
group_id = int(os.environ['SUDO_GID'])
username = pwd.getpwuid(user_id).pw_name
directories = ('/etc/myDevices', '/etc/myDevices/scripts', '/etc/myDevices/plugins', '/var/log/myDevices', '/var/run/myDevices')
for directory in directories:
try:
os.makedirs(directory)
except FileExistsError:
pass
os.chown(directory, user_id, group_id)
# Touch config file so it overwrites older versions
os.utime('scripts/config.sh', None)
setup(name = 'myDevices',
version = __version__,
author = 'myDevices',
author_email = 'N/A',
description = 'myDevices Cayenne agent',
long_description = 'N/A',
license = 'N/A',
keywords = 'myDevices Cayenne IoT',
url = 'https://www.mydevices.com/',
classifiers = classifiers,
packages = ["myDevices", "myDevices.cloud", "myDevices.utils", "myDevices.system", "myDevices.sensors" , "myDevices.schedule", "myDevices.requests_futures", "myDevices.devices", "myDevices.devices.analog", "myDevices.devices.digital", "myDevices.devices.sensor", "myDevices.decorators", "myDevices.plugins"],
install_requires = ['enum34', 'netifaces >= 0.10.5', 'psutil >= 0.7.0', 'requests', 'paho-mqtt'],
data_files = [('/etc/myDevices/scripts', ['scripts/config.sh'])]
)
os.chmod('/etc/myDevices/scripts/config.sh', 0o0755)
# Add conf file to create /var/run/myDevices at boot
with open('/usr/lib/tmpfiles.d/cayenne.conf', 'w') as tmpfile:
tmpfile.write('d /run/myDevices 0744 {0} {0} -\n'.format(username))
relogin = False
# Add user to the i2c group if it isn't already a member
user_groups = [g.gr_name for g in grp.getgrall() if username in g.gr_mem]
if not 'i2c' in user_groups:
os.system('adduser {} i2c'.format(username))
relogin = True
if Hardware().isTinkerBoard():
# Add spi group if it doesn't exist
all_groups = [g.gr_name for g in grp.getgrall()]
if not 'spi' in all_groups:
os.system('groupadd -f -f spi')
os.system('adduser {} spi'.format(username))
with open('/etc/udev/rules.d/99-com.rules', 'w') as spirules:
spirules.write('SUBSYSTEM=="spidev", GROUP="spi", MODE="0660"\n')
os.system('udevadm control --reload-rules && udevadm trigger')
relogin = True
# Install GPIO library if it doesn't exist
try:
import ASUS.GPIO
except:
current_dir = os.getcwd()
try:
TEMP_FOLDER = '/tmp/GPIO_API_for_Python'
GPIO_API_ZIP = TEMP_FOLDER + '.zip'
import urllib.request
print('Downloading ASUS.GPIO library')
urllib.request.urlretrieve('http://dlcdnet.asus.com/pub/ASUS/mb/Linux/Tinker_Board_2GB/GPIO_API_for_Python.zip', GPIO_API_ZIP)
import zipfile
with zipfile.ZipFile(GPIO_API_ZIP, 'r') as lib_zip:
lib_zip.extractall(TEMP_FOLDER)
os.chdir(TEMP_FOLDER)
import distutils.core
print('Installing ASUS.GPIO library')
distutils.core.run_setup(TEMP_FOLDER + '/setup.py', ['install'])
except Exception as ex:
print('Error installing ASUS.GPIO library: {}'.format(ex))
finally:
os.chdir(current_dir)
if relogin:
print('\nYou may need to re-login in order to use I2C or SPI devices')