-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
setup.py
executable file
·243 lines (220 loc) · 8.12 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
#!/usr/bin/env python
# vim: expandtab sw=4 ts=4 sts=4:
#
# Copyright © 2003 - 2018 Michal Čihař <michal@cihar.com>
#
# This file is part of python-gammu <https://wammu.eu/python-gammu/>
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""
python-gammu - Phone communication libary
"""
import codecs
import glob
import os
import platform
import subprocess
import sys
from packaging.version import parse
from setuptools import Extension, setup
# some defines
VERSION = "3.2.4"
GAMMU_REQUIRED = "1.37.90"
# readme
THIS_DIRECTORY = os.path.abspath(os.path.dirname(__file__))
README_FILE = os.path.join(THIS_DIRECTORY, "README.rst")
with open(README_FILE, encoding="utf-8") as readme:
LONG_DESCRIPTION = readme.read()
class GammuConfig:
def __init__(self):
self.on_windows = platform.system() == "Windows"
self.has_pkgconfig = self.check_pkconfig()
self.has_env = "GAMMU_PATH" in os.environ
self.path = self.lookup_path()
self.use_pkgconfig = self.has_pkgconfig and not self.has_env
def check_pkconfig(self):
try:
subprocess.check_output(["pkg-config", "--help"])
return True
except (subprocess.CalledProcessError, OSError):
return False
def config_path(self, base):
return os.path.join(base, "include", "gammu", "gammu-config.h")
def lookup_path(self):
if self.has_env:
paths = [os.environ["GAMMU_PATH"]]
elif self.on_windows:
paths = [
"C:\\Gammu",
"C:\\Program Files\\Gammu",
"C:\\Program Files (x86)\\Gammu",
]
paths += glob.glob("C:\\Program Files\\Gammu*")
paths += glob.glob("C:\\Program Files (x86)\\Gammu*")
else:
paths = ["/usr/local/", "/usr/"]
paths += glob.glob("/opt/gammu*")
for path in paths:
include = self.config_path(path)
if os.path.exists(include):
return path
def check_version(self):
if self.use_pkgconfig:
try:
subprocess.check_output(
[
"pkg-config",
"--print-errors",
f"--atleast-version={GAMMU_REQUIRED}",
"gammu",
"gammu-smsd",
]
)
return
except subprocess.CalledProcessError:
print("Can not find supported Gammu version using pkg-config!")
sys.exit(100)
if self.path is None:
print("Failed to find Gammu!")
print("Either it is not installed or not found.")
print("After install Gammu ensure that setup finds it by any of:")
print(" * Specify path to it using GAMMU_PATH in environment.")
print(" * Install pkg-config.")
sys.exit(101)
version = None
with open(self.config_path(self.path)) as handle:
for line in handle:
if line.startswith("#define GAMMU_VERSION "):
version = parse(line.split('"')[1])
if version is None or version < parse(GAMMU_REQUIRED):
print("Too old Gammu version, please upgrade!")
sys.exit(100)
def get_libs(self):
if self.use_pkgconfig:
output = subprocess.check_output(
["pkg-config", "--libs-only-l", "gammu", "gammu-smsd"]
).decode("utf-8")
return output.replace("-l", "").strip().split()
libs = ["Gammu", "gsmsd"]
if self.on_windows:
libs.append("Advapi32")
libs.append("shfolder")
libs.append("shell32")
else:
libs.append("m")
return libs
def get_cflags(self):
if self.use_pkgconfig:
return (
subprocess.check_output(
["pkg-config", "--cflags", "gammu", "gammu-smsd"]
)
.decode("utf-8")
.strip()
)
return "-I{}".format(os.path.join(self.path, "include", "gammu"))
def get_ldflags(self):
if self.use_pkgconfig:
return (
subprocess.check_output(
["pkg-config", "--libs-only-L", "gammu", "gammu-smsd"]
)
.decode("utf-8")
.strip()
)
elif self.on_windows:
return "/LIBPATH:{}".format(os.path.join(self.path, "lib"))
return "-L{}".format(os.path.join(self.path, "lib"))
def get_module():
config = GammuConfig()
config.check_version()
module = Extension(
"gammu._gammu",
define_macros=[
("PYTHON_GAMMU_MAJOR_VERSION", VERSION.split(".")[0]),
("PYTHON_GAMMU_MINOR_VERSION", VERSION.split(".")[1]),
],
libraries=config.get_libs(),
include_dirs=["include/"],
sources=[
"gammu/src/errors.c",
"gammu/src/data.c",
"gammu/src/misc.c",
"gammu/src/convertors/misc.c",
"gammu/src/convertors/string.c",
"gammu/src/convertors/time.c",
"gammu/src/convertors/base.c",
"gammu/src/convertors/sms.c",
"gammu/src/convertors/memory.c",
"gammu/src/convertors/todo.c",
"gammu/src/convertors/calendar.c",
"gammu/src/convertors/bitmap.c",
"gammu/src/convertors/ringtone.c",
"gammu/src/convertors/backup.c",
"gammu/src/convertors/file.c",
"gammu/src/convertors/call.c",
"gammu/src/convertors/wap.c",
"gammu/src/convertors/diverts.c",
"gammu/src/gammu.c",
"gammu/src/smsd.c",
],
)
flags = config.get_cflags()
if flags:
module.extra_compile_args.append(flags)
flags = config.get_ldflags()
if flags:
module.extra_link_args.append(flags)
return module
setup(
name="python-gammu",
version=VERSION,
description="Gammu bindings",
long_description=LONG_DESCRIPTION,
long_description_content_type="text/x-rst",
author="Michal Cihar",
author_email="michal@cihar.com",
platforms=["Linux", "Mac OSX", "Windows XP/2000/NT", "Windows 95/98/ME"],
keywords=["mobile", "phone", "SMS", "contact", "gammu", "calendar", "todo"],
license="GPLv2+",
url="https://wammu.eu/python-gammu/",
download_url="https://wammu.eu/download/python-gammu/",
classifiers=[
"Development Status :: 6 - Mature",
"Intended Audience :: Developers",
"Intended Audience :: Telecommunications Industry",
"License :: OSI Approved :: " "GNU General Public License v2 or later (GPLv2+)",
"Operating System :: MacOS",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX",
"Operating System :: Unix",
"Programming Language :: C",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Communications :: Telephony",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Hardware",
],
python_requires=">=3.7",
test_suite="test",
packages=["gammu"],
ext_modules=[get_module()],
)