-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
245 lines (214 loc) Β· 8.31 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# -*- coding: UTF-8 -*-
# GNU General Public License 3.0
#
# This file is part of the ros2_system_manager
# package (https://github.com/rbonghi/ros2_system_manager or http://rnext.it).
# Copyright (c) 2021 Raffaello Bonghi.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Always prefer setuptools over distutils
import grp
from glob import glob
from os import path
from shutil import copyfile
import logging
import os
import re
import sys
from setuptools import find_packages, setup
from setuptools.command.develop import develop
from setuptools.command.install import install
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
log = logging.getLogger()
# https://gist.github.com/anantkamath/623ce7f5432680749e087cf8cfba9b69
def runningInDocker():
with open('/proc/self/cgroup', 'r') as procfile:
for line in procfile:
fields = line.strip().split('/')
if 'docker' in fields:
return True
return False
def check_systemctl():
return os.path.exists('/bin/systemctl')
def is_superuser():
return os.getuid() == 0
def list_scripts():
# Load scripts to install
scripts = []
return scripts
def list_services():
return ['services/{file}'.format(file=f) for f in os.listdir('services')
if os.path.isfile(os.path.join('services', f))]
package_name = 'ros2_system_manager'
here = os.path.abspath(os.path.dirname(__file__))
project_homepage = 'https://github.com/rbonghi/ros2_system_manager'
documentation_homepage = 'https://github.com/rbonghi/ros2_system_manager'
with open(path.join(here, 'requirements.txt'), encoding='utf-8') as f:
requirements = f.read().splitlines()
# Get the long description from the README file
with open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
# Load version package
with open(os.path.join(here, package_name, '__init__.py')) as fp:
VERSION = (
re.compile(
r""".*__version__ = ["'](.*?)['"]""", re.S).match(fp.read()).group(1)
)
# Store version package
version = VERSION
def install_services(copy):
print(f'System prefix {sys.prefix}')
# Make jetson stats folder
root = sys.prefix + f'/lib/{package_name}/'
if not os.path.exists(root):
os.makedirs(root)
# Copy all files
for f_service in list_services():
name_service = os.path.basename(f_service)
print(f'Service name: {name_service}')
folder, _ = os.path.split(__file__)
path = root + name_service
# Check if service is active
if os.system('systemctl is-active --quiet {name_service}') == 0:
# Stop service
os.system(f'systemctl stop {name_service}')
# Disable ros2_system_manager at startup
os.system(f'systemctl disable {name_service}')
# remove if exist file
if os.path.exists(path):
os.remove(path)
# Copy or link file
if copy:
type_service = 'Copying'
copyfile(folder + '/' + f_service, path)
else:
type_service = 'Linking'
os.symlink(folder + '/' + f_service, path)
# Prompt message
print(f'{type_service} {name_service} -> {path}')
# Reload all services
os.system('systemctl daemon-reload')
# Enable and start all services
for f_service in list_services():
name_service = os.path.basename(f_service)
path = root + name_service
# Link service
copyfile(path, f'/etc/systemd/system/{name_service}')
print(f'make {name_service} as a service')
# Enable ros2_system_manager at startup
os.system(f'systemctl enable {name_service}')
# Start service
os.system(f'systemctl start {name_service}')
def pre_installer(installer, obj, copy):
group = 'ros2sm'
# Get user
user = os.getenv('SUDO_USER', os.getenv('USER'))
if user is None:
user = os.getenv('USER')
# Install services
if not runningInDocker() and is_superuser() and check_systemctl() and not os.environ.get('DOCKER_CONTAINER', False):
print('Install services')
# Install services
install_services(copy)
# Create group
try:
grp.getgrnam(group)
except KeyError:
print(f'Group {group} does not exist.')
os.system(f'groupadd {group}')
# Add system_manager to group
os.system(f'usermod -a -G {group} {user}')
# Run the default installation script
installer.run(obj)
class PostInstallCommand(install):
def run(self):
# Run the uninstaller before to copy all scripts
pre_installer(install, self, True)
class PostDevelopCommand(develop):
def run(self):
# Run the uninstaller before to copy all scripts
# Install services (linking)
pre_installer(develop, self, False)
# Configuration setup module
setup(
name=package_name,
version=version,
author='Raffaello Bonghi',
author_email='[email protected]',
maintainer='Raffaello Bonghi',
maintainer_email='[email protected]',
description='ros system manager',
license='GPLv3',
long_description=long_description,
long_description_content_type='text/markdown',
url=project_homepage,
download_url=(project_homepage + '/archive/master.zip'),
project_urls={
'How To': documentation_homepage,
'Examples': (project_homepage + '/tree/master/examples'),
'Bug Reports': (project_homepage + '/issues'),
'Source': (project_homepage + '/tree/master')
},
install_requires=requirements,
packages=find_packages(
exclude=['examples', 'scripts', 'tests']), # Required
# Load jetson_variables
package_data={},
# Define research keywords
keywords=('ros2 foxy system manager'),
tests_require=['pytest'],
classifiers=['Development Status :: 4 - Beta',
# Audiencence and topics
'Intended Audience :: Developers',
# License
'License :: OSI Approved :: MIT License',
# Programming and Operative system
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Operating System :: POSIX :: Linux',
],
# Requisites
# https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires
python_requires='>=3, <4',
platforms=['linux', 'linux2', 'darwin'],
# Zip safe configuration
# https://setuptools.readthedocs.io/en/latest/setuptools.html#setting-the-zip-safe-flag
zip_safe=True,
# Add jetson_variables in /opt/jetson_stats
# http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
('share/' + package_name, ['requirements.txt']),
('share/' + package_name, ['README.md']),
('share/' + package_name, ['LICENSE']),
('share/' + package_name, list_services()),
(path.join('share', package_name), glob('launch/*.py'))
],
# Install extra scripts
scripts=list_scripts(),
cmdclass={'develop': PostDevelopCommand,
'install': PostInstallCommand},
# The following provide a command called `ros2_system_manager`
entry_points={'console_scripts': [
'system_manager_server=ros2_system_manager.__main__:main',
'system_manager=ros2_system_manager.ros2_system_manager:main',
'joy2sm=ros2_system_manager.joy2sm:main',
]},
)
# EOF