-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·200 lines (178 loc) · 6.6 KB
/
setup.py
File metadata and controls
executable file
·200 lines (178 loc) · 6.6 KB
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
#!/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 library."""
import os
import platform
import subprocess # noqa: S404
import sys
from pathlib import Path
from typing import cast
from packaging.version import parse
from setuptools import Extension, setup
# some defines
VERSION = "3.2.6"
GAMMU_REQUIRED = "1.43.0"
class GammuConfig:
def __init__(self) -> None:
self.on_windows = platform.system() == "Windows"
self.has_pkgconfig = self.check_pkconfig()
self.has_env = "GAMMU_PATH" in os.environ
self.path: Path = cast("Path", self.lookup_path())
self.use_pkgconfig = self.has_pkgconfig and not self.has_env
def check_pkconfig(self) -> bool:
try:
subprocess.check_output(["pkg-config", "--help"])
return True
except (subprocess.CalledProcessError, OSError):
return False
def config_path(self, base: Path) -> Path:
return base / "include" / "gammu" / "gammu-config.h"
def lookup_path(self) -> Path | None:
paths: list[Path]
if self.has_env:
paths = [Path(os.environ["GAMMU_PATH"])]
elif self.on_windows:
paths = [
Path("C:\\Gammu"),
Path("C:\\Program Files\\Gammu"),
Path("C:\\Program Files (x86)\\Gammu"),
]
paths += Path("C:\\Program Files").glob("Gammu*")
paths += Path("C:\\Program Files (x86)").glob("Gammu*")
else:
paths = [Path("/usr/local/"), Path("/usr/")]
paths += Path("/opt").glob("gammu*")
for path in paths:
include = self.config_path(path)
if include.exists():
return Path(path)
return None
def check_version(self) -> None:
if self.use_pkgconfig:
try:
subprocess.check_output( # noqa: S603
[
"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 not self.path:
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 self.config_path(self.path).open(encoding="utf-8") 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) -> list[str]:
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.extend(("Advapi32", "shfolder", "shell32"))
else:
libs.append("m")
return libs
def get_cflags(self) -> str:
if self.use_pkgconfig:
return (
subprocess.check_output(
["pkg-config", "--cflags", "gammu", "gammu-smsd"]
)
.decode("utf-8")
.strip()
)
return "-I{}".format((self.path / "include" / "gammu").as_posix())
def get_ldflags(self) -> str:
if self.use_pkgconfig:
return (
subprocess.check_output(
["pkg-config", "--libs-only-L", "gammu", "gammu-smsd"]
)
.decode("utf-8")
.strip()
)
if self.on_windows:
return "/LIBPATH:{}".format(self.path / "lib")
return "-L{}".format((self.path / "lib").as_posix())
def get_module():
config = GammuConfig()
config.check_version()
version_parts = VERSION.split(".")
module = Extension(
"gammu._gammu",
define_macros=[
("PYTHON_GAMMU_MAJOR_VERSION", version_parts[0]),
("PYTHON_GAMMU_MINOR_VERSION", version_parts[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(
ext_modules=[get_module()],
)