Skip to content

Commit f41e333

Browse files
authored
build: replace setup.py with pyproject.toml (canonical#1455)
1 parent fd5bd6b commit f41e333

File tree

6 files changed

+121
-142
lines changed

6 files changed

+121
-142
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,4 @@ dmypy.json
133133

134134
# backups
135135
*~
136+
/charmcraft/_version.py

charmcraft/__init__.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@
1313
# limitations under the License.
1414
#
1515
# For further info, check https://github.com/canonical/charmcraft
16-
1716
"""Expose needed names at main package level."""
1817

19-
from importlib.metadata import version, PackageNotFoundError
20-
import os
21-
18+
try:
19+
from ._version import __version__
20+
except ImportError: # pragma: no cover
21+
from importlib.metadata import version, PackageNotFoundError
2222

23-
def _get_version() -> str:
24-
if os.getenv("SNAP_NAME") == "charmcraft":
25-
return os.getenv("SNAP_VERSION", "")
2623
try:
27-
return version("charmcraft")
24+
__version__ = version("craft-archives")
2825
except PackageNotFoundError:
29-
return "devel"
30-
31-
32-
__version__ = _get_version()
26+
__version__ = "dev"

pyproject.toml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,108 @@
1+
[project]
2+
name = "charmcraft"
3+
description = "The main tool to build, upload, and develop in general the Juju charms."
4+
dynamic = ["version", "readme"]
5+
dependencies = [
6+
"craft-application~=1.1",
7+
"craft-cli>=2.3.0",
8+
"craft-parts>=1.18",
9+
"craft-providers",
10+
"craft-store>=2.4",
11+
"distro>=1.3.0",
12+
"humanize>=2.6.0",
13+
"jsonschema",
14+
"jinja2",
15+
# Pydantic will need to be updated all at once with our libraries.
16+
# When you update it, remove the pydantic constraints from renovate.
17+
"pydantic>=1.10,<2.0",
18+
"python-dateutil",
19+
"pyyaml",
20+
"requests",
21+
"requests-toolbelt",
22+
"requests-unixsocket",
23+
"snap-helpers",
24+
"tabulate",
25+
# Needed until requests-unixsocket supports urllib3 v2
26+
# https://github.com/msabramo/requests-unixsocket/pull/69
27+
"urllib3<2.0",
28+
]
29+
classifiers = [
30+
"Development Status :: 5 - Production/Stable",
31+
"Environment :: Console",
32+
"Intended Audience :: Developers",
33+
"License :: OSI Approved :: Apache Software License",
34+
"Operating System :: POSIX :: Linux",
35+
"Programming Language :: Python :: 3",
36+
"Programming Language :: Python :: 3.10",
37+
]
38+
requires-python = ">=3.10"
39+
40+
[project.scripts]
41+
charmcraft = "charmcraft.application.main:main"
42+
43+
[project.optional-dependencies]
44+
dev = [
45+
"coverage",
46+
"flake8",
47+
"freezegun",
48+
"hypothesis~=6.0",
49+
"pydocstyle",
50+
"pyfakefs",
51+
"pytest",
52+
"pytest-cov",
53+
"pytest-mock",
54+
"pytest-check",
55+
"pytest-subprocess",
56+
"responses",
57+
]
58+
lint = [
59+
"black>=23.10.1,<24.0.0",
60+
"codespell[tomli]>=2.2.6,<3.0.0",
61+
"yamllint>=1.32.0,<2.0.0",
62+
]
63+
types = [
64+
"mypy[reports]~=1.5",
65+
"pyright==1.1.332",
66+
"types-python-dateutil",
67+
"types-requests<2.31.0.7", # Frozen until we can get urllib3 v2
68+
"types-setuptools",
69+
"types-tabulate",
70+
"types-urllib3",
71+
]
72+
apt = [
73+
"python-apt>=2.4.0;sys_platform=='linux'"
74+
]
75+
76+
[build-system]
77+
requires = [
78+
"setuptools>=67.7.2",
79+
"setuptools_scm[toml]>=7.1"
80+
]
81+
build-backend = "setuptools.build_meta"
82+
83+
[tool.setuptools.dynamic]
84+
readme = {file = "README.md"}
85+
86+
[tool.setuptools_scm]
87+
write_to = "charmcraft/_version.py"
88+
# the version comes from the latest annotated git tag formatted as 'X.Y.Z'
89+
# standard version scheme:
90+
# 'X.Y.Z.post<commits since tag>+g<hash>'
91+
# scheme when no tags exist:
92+
# '0.0.post<total commits>+g<hash>
93+
# scheme when no tags exist and working dir is dirty:
94+
# '0.0.post<total commits>+g<hash>.d<date formatted as %Y%m%d>'
95+
version_scheme = "post-release"
96+
# deviations from the default 'git describe' command:
97+
# - only match annotated tags
98+
# - only match tags formatted as 'X.Y.Z'
99+
# - exclude '+dirty<hash>' suffix
100+
git_describe_command = "git describe --long --match '[0-9]*.[0-9]*.[0-9]*' --exclude '*[^0-9.]*'"
101+
102+
[tool.setuptools.packages.find]
103+
include = ["*craft*"]
104+
namespaces = false
105+
1106
[tool.black]
2107
target-version = ["py310", "py311"]
3108
line-length = 99
@@ -97,6 +202,7 @@ extend-exclude = [
97202
"__pycache__",
98203
"tools",
99204
"snap/local",
205+
"charmcraft/_version.py", # setuptools_scm generates old-style type annotations.
100206
]
101207
# Follow ST063 - Maintaining and updating linting specifications for updating these.
102208
select = [ # Base linting rule selections.

setup.cfg

Lines changed: 0 additions & 18 deletions
This file was deleted.

setup.py

Lines changed: 3 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
# Copyright 2020-2023 Canonical Ltd.
3+
# Copyright 2020-2024 Canonical Ltd.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -15,105 +15,8 @@
1515
# limitations under the License.
1616
#
1717
# For further info, check https://github.com/canonical/charmcraft
18-
1918
"""Setup script for Charmcraft."""
2019

21-
from setuptools import find_packages, setup
22-
23-
from tools.version import determine_version
24-
25-
with open("README.md", encoding="utf8") as fh:
26-
long_description = fh.read()
27-
28-
install_requires = [
29-
"craft-application~=1.1",
30-
"craft-cli>=2.3.0",
31-
"craft-parts>=1.18",
32-
"craft-providers",
33-
"craft-store>=2.4",
34-
"distro>=1.3.0",
35-
"humanize>=2.6.0",
36-
"jsonschema",
37-
"jinja2",
38-
# Pydantic will need to be updated all at once with our libraries.
39-
# When you update it, remove the pydantic constraints from renovate.
40-
"pydantic>=1.10,<2.0",
41-
"pydantic-yaml<1.0",
42-
"python-dateutil",
43-
"pyyaml",
44-
"requests",
45-
"requests-toolbelt",
46-
"requests-unixsocket",
47-
"snap-helpers",
48-
"tabulate",
49-
# Needed until requests-unixsocket supports urllib3 v2
50-
# https://github.com/msabramo/requests-unixsocket/pull/69
51-
"urllib3<2.0",
52-
]
53-
54-
lint_requires = [
55-
"black>=23.10.1,<24.0.0",
56-
"codespell[tomli]>=2.2.6,<3.0.0",
57-
"yamllint>=1.32.0,<2.0.0",
58-
]
59-
60-
type_requires = [
61-
"mypy[reports]~=1.5",
62-
"pyright==1.1.332",
63-
"types-python-dateutil",
64-
"types-requests<2.31.0.7", # Frozen until we can get urllib3 v2
65-
"types-setuptools",
66-
"types-tabulate",
67-
"types-urllib3",
68-
]
69-
70-
dev_requires = [
71-
"coverage",
72-
"flake8",
73-
"freezegun",
74-
"hypothesis~=6.0",
75-
"pydocstyle",
76-
"pyfakefs",
77-
"pytest",
78-
"pytest-cov",
79-
"pytest-mock",
80-
"pytest-check",
81-
"pytest-subprocess",
82-
"responses",
83-
"tox",
84-
]
85-
dev_requires += lint_requires + type_requires
86-
87-
extras_require = {
88-
"dev": dev_requires,
89-
"lint": lint_requires,
90-
"type": type_requires,
91-
}
92-
20+
from setuptools import setup
9321

94-
setup(
95-
name="charmcraft",
96-
version=determine_version(),
97-
author="Facundo Batista",
98-
author_email="[email protected]",
99-
description="The main tool to build, upload, and develop in general the Juju charms.",
100-
long_description=long_description,
101-
long_description_content_type="text/markdown",
102-
url="https://github.com/canonical/charmcraft",
103-
license="Apache-2.0",
104-
packages=find_packages(include=["charmcraft", "charmcraft.*"]),
105-
classifiers=[
106-
"Environment :: Console",
107-
"License :: OSI Approved :: Apache Software License",
108-
"Operating System :: OS Independent",
109-
"Programming Language :: Python :: 3",
110-
"Programming Language :: Python :: 3.10",
111-
],
112-
entry_points={
113-
"console_scripts": ["charmcraft = charmcraft.application.main:main"],
114-
},
115-
python_requires=">=3",
116-
install_requires=install_requires,
117-
extras_require=extras_require,
118-
include_package_data=True, # so we get templates in the wheel
119-
)
22+
setup()

tests/test_infra.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
import itertools
1818
import os
1919
import re
20-
import subprocess
21-
import sys
2220

2321
import pytest
2422

25-
from charmcraft import __version__, main
23+
from charmcraft import main
2624

2725

2826
def get_python_filepaths(*, roots=None, python_paths=None):
@@ -46,6 +44,10 @@ def test_ensure_copyright():
4644
for filepath in get_python_filepaths():
4745
if os.stat(filepath).st_size == 0:
4846
continue
47+
if filepath.endswith("charmcraft/_version.py") or filepath.endswith(
48+
"charmcraft\\_version.py"
49+
):
50+
continue
4951

5052
with open(filepath, encoding="utf8") as fh:
5153
for line in itertools.islice(fh, 5):
@@ -58,15 +60,6 @@ def test_ensure_copyright():
5860
pytest.fail(msg, pytrace=False)
5961

6062

61-
@pytest.mark.skipif(sys.platform == "win32", reason="Windows not [yet] supported")
62-
def test_setup_version():
63-
"""Verify that setup.py is picking up the version correctly."""
64-
cmd = [os.path.abspath("setup.py"), "--version"]
65-
proc = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
66-
output = proc.stdout.decode("utf8")
67-
assert output.strip() == __version__
68-
69-
7063
def test_bashcompletion_all_commands():
7164
"""Verify that all commands are represented in the bash completion file."""
7265
# get the line where all commands are specified in the completion file; this is custom

0 commit comments

Comments
 (0)