-
Notifications
You must be signed in to change notification settings - Fork 222
/
setup.py
174 lines (147 loc) · 5.72 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
# -*- coding: utf-8 -*-
from distutils.command.build import build
from setuptools import setup, find_packages
from codecs import open
from os import path, getcwd
import urllib.request
import os
import stat
import platform
import subprocess
def parse_requirements(filename):
"""
Helper which parses requirement_?.*.txt files
:param filename: relative path, e.g. `./requirements.txt`
:returns: List of requirements
"""
# Get absolute filepath
filepath = path.join(getcwd(), filename)
# Check if file exists
if not path.exists(filepath):
print("[!] File {} not found".format(filename))
return []
# Parse install requirements
with open(filepath, encoding="utf-8") as f:
return [requires.strip() for requires in f.readlines()]
def parse_long_description():
"""
Helper function which parses the readme
:returns: Content of the Readme
"""
with open(path.join(getcwd(), "README.md"), encoding="utf-8") as f:
return f.read()
class custom_build(build):
"""
Class that overrides the build step to add a custom pre-build step
"""
@property
def platform_suffix(self):
"""
Trying to detect the platform to know which `mavsdk_server` executable
to download
"""
if platform.system() == 'Linux' and 'MAVSDK_SERVER_ARCH' in os.environ:
if os.environ['MAVSDK_SERVER_ARCH'] == "armv6l":
return 'linux-armv6-musl'
elif os.environ['MAVSDK_SERVER_ARCH'] == "armv7l":
return 'linux-armv7l-musl'
elif os.environ['MAVSDK_SERVER_ARCH'] == "aarch64":
return 'linux-arm64-musl'
else:
raise NotImplementedError(
"Error: unknown MAVSDK_SERVER_ARCH: "
f"{os.environ['MAVSDK_SERVER_ARCH']}")
elif platform.system() == 'Linux':
return 'musl_x86_64'
elif platform.system() == 'Darwin':
if platform.processor() == 'i386':
return 'macos_x64'
elif platform.processor() == 'arm':
return 'macos_arm64'
raise NotImplementedError(
f"Error: unknown macOS processor: {platform.processor()}")
elif platform.system() == 'Windows' \
and platform.processor().startswith('AMD64'):
return 'win32.exe'
else:
raise NotImplementedError(
"Error: mavsdk_server is not distributed for platform "
f"{platform.system()} ({platform.processor()}) (yet)!\n\n"
"You should set the 'MAVSDK_BUILD_PURE=ON' environment "
"variable and get mavsdk_server manually.")
@property
def mavsdk_server_filepath(self):
"""
The location of the downloaded `mavsdk_server` binary
For Windows this needs to be a .exe file
"""
if platform.system() == 'Windows':
return 'mavsdk/bin/mavsdk_server.exe'
else:
return 'mavsdk/bin/mavsdk_server'
@property
def mavsdk_server_tag(self):
"""
The release tag of the `mavsdk_server` binary is defined in the file
"MAVSDK_SERVER_VERSION", and used to download the corresponding release
"""
with open("MAVSDK_SERVER_VERSION") as mavsdk_server_version_file:
return mavsdk_server_version_file.read().rstrip()
@property
def mavsdk_server_url(self):
"""
Build the url of the `mavsdk_server` binary
"""
return "https://github.com/mavlink/MAVSDK/releases/download/" \
f"{self.mavsdk_server_tag}/mavsdk_server_{self.platform_suffix}"
def run(self):
if 'MAVSDK_BUILD_PURE' not in os.environ:
self.download_mavsdk_server()
build.run(self)
def download_mavsdk_server(self):
print(
f"downloading {self.mavsdk_server_url} into "
f"{self.mavsdk_server_filepath}")
urllib.request.urlretrieve(
self.mavsdk_server_url,
filename=self.mavsdk_server_filepath)
print(f"adding execution permission to {self.mavsdk_server_filepath}")
st = os.stat(self.mavsdk_server_filepath)
os.chmod(self.mavsdk_server_filepath, st.st_mode | stat.S_IEXEC)
def version():
process = subprocess.Popen(["git", "describe", "--tags"],
stdout=subprocess.PIPE)
output, _ = process.communicate()
exit_code = process.wait()
if exit_code != 0:
raise RuntimeError(f"git describe command exited with: {exit_code}")
git_describe_str = output.decode("utf-8").strip()
git_describe_str = git_describe_str.replace("-g", "+g")
print(git_describe_str)
return git_describe_str
setup(
name="mavsdk",
version=version(),
description="Python wrapper for MAVSDK",
long_description=parse_long_description(),
long_description_content_type="text/markdown",
url="https://github.com/mavlink/MAVSDK-Python",
maintainer="Jonas Vautherin, Julian Oes",
maintainer_email="[email protected], [email protected]",
python_requires='>=3.7',
include_package_data=True,
cmdclass={'build': custom_build},
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python :: 3.7",
],
packages=find_packages(exclude=["other", "docs", "tests", "examples",
"proto"]),
install_requires=parse_requirements("requirements.txt"),
project_urls={
"Bug Reports": "https://github.com/mavlink/MAVSDK-Python/issues",
"Source": "https://github.com/mavlink/MAVSDK-Python/",
},
)