Skip to content

Commit

Permalink
Use pyproject.toml (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed May 15, 2023
1 parent cbd894c commit 89c1e0f
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 261 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
.tox/
build/
dist/
MySQLdb/release.py
.coverage
5 changes: 0 additions & 5 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
recursive-include doc *.rst
recursive-include tests *.py
include doc/conf.py
include MANIFEST.in
include HISTORY.rst
include README.md
include LICENSE
include metadata.cfg
include site.cfg
include setup_common.py
include setup_posix.py
include setup_windows.py
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ clean:
find . -name '*.pyc' -delete
find . -name '__pycache__' -delete
rm -rf build

.PHONY: check
check:
ruff .
black *.py MySQLdb
9 changes: 4 additions & 5 deletions MySQLdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
MySQLdb.converters module.
"""

# Check if the version of _mysql matches the version of MySQLdb.
from MySQLdb.release import version_info
from .release import version_info
from . import _mysql

if version_info != _mysql.version_info:
raise ImportError(
"this is MySQLdb version {}, but _mysql is version {!r}\n_mysql: {!r}".format(
version_info, _mysql.version_info, _mysql.__file__
)
f"this is MySQLdb version {version_info}, "
f"but _mysql is version {_mysql.version_info!r}\n"
f"_mysql: {_mysql.__file__!r}"
)


Expand Down
3 changes: 3 additions & 0 deletions MySQLdb/release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__author__ = "Inada Naoki <songofacandy@gmail.com>"
version_info = (2, 2, 0, "dev", 0)
__version__ = "2.2.0.dev0"
41 changes: 0 additions & 41 deletions metadata.cfg

This file was deleted.

48 changes: 48 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[project]
name = "mysqlclient"
# version = "2.2.0dev0"
description = "Python interface to MySQL"
readme = "README.md"
requires-python = ">=3.8"
authors = [
{name = "Inada Naoki", email = "songofacandy@gmail.com"}
]
license = {text = "GNU General Public License v2 (GPLv2)"}
keywords = ["MySQL"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Environment :: Other Environment",
"License :: OSI Approved :: GNU General Public License (GPL)",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows :: Windows NT/2000",
"Operating System :: OS Independent",
"Operating System :: POSIX",
"Operating System :: POSIX :: Linux",
"Operating System :: Unix",
"Programming Language :: C",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Database",
"Topic :: Database :: Database Engines/Servers",
]
dynamic = ["version"]

[project.urls]
Project = "https://github.com/PyMySQL/mysqlclient"
Documentation = "https://mysqlclient.readthedocs.io/"

[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"

[tool.setuptools.packages.find]
namespaces = false
include = ["MySQLdb*"]
exclude = ["tests*", "pymysql.tests*"]

[tool.setuptools.dynamic]
version = {attr = "MySQLdb.release.__version__"}
177 changes: 163 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,171 @@
#!/usr/bin/env python

import os
import subprocess
import sys

import setuptools
from configparser import ConfigParser


release_info = {}
with open("MySQLdb/release.py", encoding="utf-8") as f:
exec(f.read(), None, release_info)


def find_package_name():
"""Get available pkg-config package name"""
packages = ["mysqlclient", "mariadb"]
for pkg in packages:
try:
cmd = f"pkg-config --exists {pkg}"
print(f"Trying {cmd}")
subprocess.check_call(cmd, shell=True)
except subprocess.CalledProcessError as err:
print(err)
else:
return pkg
raise Exception(
"Can not find valid pkg-config name.\n"
"Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually"
)


def get_config_posix(options=None):
# allow a command-line option to override the base config file to permit
# a static build to be created via requirements.txt
# TODO: find a better way for
static = False
if "--static" in sys.argv:
static = True
sys.argv.remove("--static")

ldflags = os.environ.get("MYSQLCLIENT_LDFLAGS")
cflags = os.environ.get("MYSQLCLIENT_CFLAGS")

pkg_name = None
static_opt = " --static" if static else ""
if not (cflags and ldflags):
pkg_name = find_package_name()
if not cflags:
cflags = subprocess.check_output(
f"pkg-config{static_opt} --cflags {pkg_name}", encoding="utf-8", shell=True
)
if not ldflags:
ldflags = subprocess.check_output(
f"pkg-config{static_opt} --libs {pkg_name}", encoding="utf-8", shell=True
)

cflags = cflags.split()
for f in cflags:
if f.startswith("-std="):
break
else:
cflags += ["-std=c99"]

ldflags = ldflags.split()

define_macros = [
("version_info", release_info["version_info"]),
("__version__", release_info["__version__"]),
]

ext_options = dict(
extra_compile_args=cflags,
extra_link_args=ldflags,
define_macros=define_macros,
)
# newer versions of gcc require libstdc++ if doing a static build
if static:
ext_options["language"] = "c++"

print("Options for building extention module:")
for k, v in ext_options.items():
print(f" {k}: {v}")

return ext_options


def get_config_win32(options):
client = "mariadbclient"
connector = os.environ.get("MYSQLCLIENT_CONNECTOR", options.get("connector"))
if not connector:
connector = os.path.join(
os.environ["ProgramFiles"], "MariaDB", "MariaDB Connector C"
)

extra_objects = []

library_dirs = [
os.path.join(connector, "lib", "mariadb"),
os.path.join(connector, "lib"),
]
libraries = [
"kernel32",
"advapi32",
"wsock32",
"shlwapi",
"Ws2_32",
"crypt32",
"secur32",
"bcrypt",
client,
]
include_dirs = [
os.path.join(connector, "include", "mariadb"),
os.path.join(connector, "include"),
]

extra_link_args = ["/MANIFEST"]

define_macros = [
("version_info", release_info["version_info"]),
("__version__", release_info["__version__"]),
]

ext_options = dict(
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
include_dirs=include_dirs,
extra_objects=extra_objects,
define_macros=define_macros,
)
return ext_options


def enabled(options, option):
value = options[option]
s = value.lower()
if s in ("yes", "true", "1", "y"):
return True
elif s in ("no", "false", "0", "n"):
return False
else:
raise ValueError(f"Unknown value {value} for option {option}")


def get_options():
config = ConfigParser()
config.read(["site.cfg"])
options = dict(config.items("options"))
options["static"] = enabled(options, "static")
return options


if os.name == "posix":
from setup_posix import get_config
else: # assume windows
from setup_windows import get_config
if sys.platform == "win32":
ext_options = get_config_win32(get_options())
else:
ext_options = get_config_posix(get_options())

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

metadata, options = get_config()
metadata["ext_modules"] = [
setuptools.Extension("MySQLdb._mysql", sources=["MySQLdb/_mysql.c"], **options)
ext_modules = [
setuptools.Extension(
"MySQLdb._mysql",
sources=["MySQLdb/_mysql.c"],
**ext_options,
)
]
metadata["long_description"] = readme
metadata["long_description_content_type"] = "text/markdown"
metadata["python_requires"] = ">=3.7"
setuptools.setup(**metadata)
setuptools.setup(ext_modules=ext_modules)
37 changes: 0 additions & 37 deletions setup_common.py

This file was deleted.

0 comments on commit 89c1e0f

Please sign in to comment.