Skip to content

Commit 89c1e0f

Browse files
authored
Use pyproject.toml (#598)
1 parent cbd894c commit 89c1e0f

11 files changed

+223
-261
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@
1010
.tox/
1111
build/
1212
dist/
13-
MySQLdb/release.py
1413
.coverage

MANIFEST.in

-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
recursive-include doc *.rst
22
recursive-include tests *.py
33
include doc/conf.py
4-
include MANIFEST.in
54
include HISTORY.rst
65
include README.md
76
include LICENSE
8-
include metadata.cfg
97
include site.cfg
10-
include setup_common.py
11-
include setup_posix.py
12-
include setup_windows.py

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ clean:
1414
find . -name '*.pyc' -delete
1515
find . -name '__pycache__' -delete
1616
rm -rf build
17+
18+
.PHONY: check
19+
check:
20+
ruff .
21+
black *.py MySQLdb

MySQLdb/__init__.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
MySQLdb.converters module.
1414
"""
1515

16-
# Check if the version of _mysql matches the version of MySQLdb.
17-
from MySQLdb.release import version_info
16+
from .release import version_info
1817
from . import _mysql
1918

2019
if version_info != _mysql.version_info:
2120
raise ImportError(
22-
"this is MySQLdb version {}, but _mysql is version {!r}\n_mysql: {!r}".format(
23-
version_info, _mysql.version_info, _mysql.__file__
24-
)
21+
f"this is MySQLdb version {version_info}, "
22+
f"but _mysql is version {_mysql.version_info!r}\n"
23+
f"_mysql: {_mysql.__file__!r}"
2524
)
2625

2726

MySQLdb/release.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__author__ = "Inada Naoki <[email protected]>"
2+
version_info = (2, 2, 0, "dev", 0)
3+
__version__ = "2.2.0.dev0"

metadata.cfg

-41
This file was deleted.

pyproject.toml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[project]
2+
name = "mysqlclient"
3+
# version = "2.2.0dev0"
4+
description = "Python interface to MySQL"
5+
readme = "README.md"
6+
requires-python = ">=3.8"
7+
authors = [
8+
{name = "Inada Naoki", email = "[email protected]"}
9+
]
10+
license = {text = "GNU General Public License v2 (GPLv2)"}
11+
keywords = ["MySQL"]
12+
classifiers = [
13+
"Development Status :: 5 - Production/Stable",
14+
"Environment :: Other Environment",
15+
"License :: OSI Approved :: GNU General Public License (GPL)",
16+
"Operating System :: MacOS :: MacOS X",
17+
"Operating System :: Microsoft :: Windows :: Windows NT/2000",
18+
"Operating System :: OS Independent",
19+
"Operating System :: POSIX",
20+
"Operating System :: POSIX :: Linux",
21+
"Operating System :: Unix",
22+
"Programming Language :: C",
23+
"Programming Language :: Python",
24+
"Programming Language :: Python :: 3",
25+
"Programming Language :: Python :: 3.8",
26+
"Programming Language :: Python :: 3.9",
27+
"Programming Language :: Python :: 3.10",
28+
"Programming Language :: Python :: 3.11",
29+
"Topic :: Database",
30+
"Topic :: Database :: Database Engines/Servers",
31+
]
32+
dynamic = ["version"]
33+
34+
[project.urls]
35+
Project = "https://github.com/PyMySQL/mysqlclient"
36+
Documentation = "https://mysqlclient.readthedocs.io/"
37+
38+
[build-system]
39+
requires = ["setuptools>=61"]
40+
build-backend = "setuptools.build_meta"
41+
42+
[tool.setuptools.packages.find]
43+
namespaces = false
44+
include = ["MySQLdb*"]
45+
exclude = ["tests*", "pymysql.tests*"]
46+
47+
[tool.setuptools.dynamic]
48+
version = {attr = "MySQLdb.release.__version__"}

setup.py

+163-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,171 @@
11
#!/usr/bin/env python
2-
32
import os
3+
import subprocess
4+
import sys
45

56
import setuptools
7+
from configparser import ConfigParser
8+
9+
10+
release_info = {}
11+
with open("MySQLdb/release.py", encoding="utf-8") as f:
12+
exec(f.read(), None, release_info)
13+
14+
15+
def find_package_name():
16+
"""Get available pkg-config package name"""
17+
packages = ["mysqlclient", "mariadb"]
18+
for pkg in packages:
19+
try:
20+
cmd = f"pkg-config --exists {pkg}"
21+
print(f"Trying {cmd}")
22+
subprocess.check_call(cmd, shell=True)
23+
except subprocess.CalledProcessError as err:
24+
print(err)
25+
else:
26+
return pkg
27+
raise Exception(
28+
"Can not find valid pkg-config name.\n"
29+
"Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually"
30+
)
31+
32+
33+
def get_config_posix(options=None):
34+
# allow a command-line option to override the base config file to permit
35+
# a static build to be created via requirements.txt
36+
# TODO: find a better way for
37+
static = False
38+
if "--static" in sys.argv:
39+
static = True
40+
sys.argv.remove("--static")
41+
42+
ldflags = os.environ.get("MYSQLCLIENT_LDFLAGS")
43+
cflags = os.environ.get("MYSQLCLIENT_CFLAGS")
44+
45+
pkg_name = None
46+
static_opt = " --static" if static else ""
47+
if not (cflags and ldflags):
48+
pkg_name = find_package_name()
49+
if not cflags:
50+
cflags = subprocess.check_output(
51+
f"pkg-config{static_opt} --cflags {pkg_name}", encoding="utf-8", shell=True
52+
)
53+
if not ldflags:
54+
ldflags = subprocess.check_output(
55+
f"pkg-config{static_opt} --libs {pkg_name}", encoding="utf-8", shell=True
56+
)
57+
58+
cflags = cflags.split()
59+
for f in cflags:
60+
if f.startswith("-std="):
61+
break
62+
else:
63+
cflags += ["-std=c99"]
64+
65+
ldflags = ldflags.split()
66+
67+
define_macros = [
68+
("version_info", release_info["version_info"]),
69+
("__version__", release_info["__version__"]),
70+
]
71+
72+
ext_options = dict(
73+
extra_compile_args=cflags,
74+
extra_link_args=ldflags,
75+
define_macros=define_macros,
76+
)
77+
# newer versions of gcc require libstdc++ if doing a static build
78+
if static:
79+
ext_options["language"] = "c++"
80+
81+
print("Options for building extention module:")
82+
for k, v in ext_options.items():
83+
print(f" {k}: {v}")
84+
85+
return ext_options
86+
87+
88+
def get_config_win32(options):
89+
client = "mariadbclient"
90+
connector = os.environ.get("MYSQLCLIENT_CONNECTOR", options.get("connector"))
91+
if not connector:
92+
connector = os.path.join(
93+
os.environ["ProgramFiles"], "MariaDB", "MariaDB Connector C"
94+
)
95+
96+
extra_objects = []
97+
98+
library_dirs = [
99+
os.path.join(connector, "lib", "mariadb"),
100+
os.path.join(connector, "lib"),
101+
]
102+
libraries = [
103+
"kernel32",
104+
"advapi32",
105+
"wsock32",
106+
"shlwapi",
107+
"Ws2_32",
108+
"crypt32",
109+
"secur32",
110+
"bcrypt",
111+
client,
112+
]
113+
include_dirs = [
114+
os.path.join(connector, "include", "mariadb"),
115+
os.path.join(connector, "include"),
116+
]
117+
118+
extra_link_args = ["/MANIFEST"]
119+
120+
define_macros = [
121+
("version_info", release_info["version_info"]),
122+
("__version__", release_info["__version__"]),
123+
]
124+
125+
ext_options = dict(
126+
library_dirs=library_dirs,
127+
libraries=libraries,
128+
extra_link_args=extra_link_args,
129+
include_dirs=include_dirs,
130+
extra_objects=extra_objects,
131+
define_macros=define_macros,
132+
)
133+
return ext_options
134+
135+
136+
def enabled(options, option):
137+
value = options[option]
138+
s = value.lower()
139+
if s in ("yes", "true", "1", "y"):
140+
return True
141+
elif s in ("no", "false", "0", "n"):
142+
return False
143+
else:
144+
raise ValueError(f"Unknown value {value} for option {option}")
145+
146+
147+
def get_options():
148+
config = ConfigParser()
149+
config.read(["site.cfg"])
150+
options = dict(config.items("options"))
151+
options["static"] = enabled(options, "static")
152+
return options
153+
6154

7-
if os.name == "posix":
8-
from setup_posix import get_config
9-
else: # assume windows
10-
from setup_windows import get_config
155+
if sys.platform == "win32":
156+
ext_options = get_config_win32(get_options())
157+
else:
158+
ext_options = get_config_posix(get_options())
11159

12-
with open("README.md", encoding="utf-8") as f:
13-
readme = f.read()
160+
print("# Extention options")
161+
for k, v in ext_options.items():
162+
print(f" {k}: {v}")
14163

15-
metadata, options = get_config()
16-
metadata["ext_modules"] = [
17-
setuptools.Extension("MySQLdb._mysql", sources=["MySQLdb/_mysql.c"], **options)
164+
ext_modules = [
165+
setuptools.Extension(
166+
"MySQLdb._mysql",
167+
sources=["MySQLdb/_mysql.c"],
168+
**ext_options,
169+
)
18170
]
19-
metadata["long_description"] = readme
20-
metadata["long_description_content_type"] = "text/markdown"
21-
metadata["python_requires"] = ">=3.7"
22-
setuptools.setup(**metadata)
171+
setuptools.setup(ext_modules=ext_modules)

setup_common.py

-37
This file was deleted.

0 commit comments

Comments
 (0)