Skip to content

Commit 608c1c2

Browse files
committed
feat: Add callable for Twine
Issue-19: #19
1 parent d43f275 commit 608c1c2

File tree

1 file changed

+284
-0
lines changed

1 file changed

+284
-0
lines changed

Diff for: src/duty/callables/twine.py

+284
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
"""Callable for [Twine](https://github.com/pypa/twine)."""
2+
3+
from __future__ import annotations
4+
5+
from failprint.lazy import lazy
6+
7+
8+
def run(*args: str, version: bool = False, no_color: bool = False) -> None:
9+
"""Run `twine`.
10+
11+
Parameters:
12+
*args: CLI arguments.
13+
version: Show program's version number and exit.
14+
no_color: Disable colored output.
15+
"""
16+
from twine.cli import dispatch as twine
17+
18+
cli_args = []
19+
20+
# --version and --no-color must appear first.
21+
if version:
22+
cli_args.append("--version")
23+
24+
if no_color:
25+
cli_args.append("--no-color")
26+
27+
cli_args.extend(args)
28+
29+
twine(cli_args)
30+
31+
32+
@lazy(name="twine.check")
33+
def check(
34+
*dists: str,
35+
strict: bool = False,
36+
version: bool = False,
37+
no_color: bool = False,
38+
) -> None:
39+
"""Checks whether your distribution's long description will render correctly on PyPI.
40+
41+
Parameters:
42+
dists: The distribution files to check, usually `dist/*`.
43+
strict: Fail on warnings.
44+
version: Show program's version number and exit.
45+
no_color: Disable colored output.
46+
"""
47+
cli_args = list(dists)
48+
49+
if strict is True:
50+
cli_args.append("--strict")
51+
52+
run("check", *cli_args, version=version, no_color=no_color)
53+
54+
55+
@lazy(name="twine.register")
56+
def register(
57+
package: str,
58+
*,
59+
repository: str = "pypi",
60+
repository_url: str | None = None,
61+
attestations: bool = False,
62+
sign: bool = False,
63+
sign_with: str | None = None,
64+
identity: str | None = None,
65+
username: str | None = None,
66+
password: str | None = None,
67+
non_interactive: bool = False,
68+
comment: str | None = None,
69+
config_file: str | None = None,
70+
skip_existing: bool = False,
71+
cert: str | None = None,
72+
client_cert: str | None = None,
73+
verbose: bool = False,
74+
disable_progress_bar: bool = False,
75+
version: bool = False,
76+
no_color: bool = False,
77+
) -> None:
78+
"""Pre-register a name with a repository before uploading a distribution.
79+
80+
Pre-registration is not supported on PyPI, so the register command
81+
is only necessary if you are using a different repository that requires it.
82+
83+
Parameters:
84+
package: File from which we read the package metadata.
85+
repository: The repository (package index) to upload the package to.
86+
Should be a section in the config file (default: `pypi`).
87+
Can also be set via `TWINE_REPOSITORY` environment variable.
88+
repository_url: The repository (package index) URL to upload the package to. This overrides `--repository`.
89+
Can also be set via `TWINE_REPOSITORY_URL` environment variable.
90+
attestations: Upload each file's associated attestations.
91+
sign: Sign files to upload using GPG.
92+
sign_with: GPG program used to sign uploads (default: `gpg`).
93+
identity: GPG identity used to sign files.
94+
username: The username to authenticate to the repository (package index) as.
95+
Can also be set via `TWINE_USERNAME` environment variable.
96+
password: The password to authenticate to the repository (package index) with.
97+
Can also be set via `TWINE_PASSWORD` environment variable.
98+
non_interactive: Do not interactively prompt for username/password if the required credentials are missing.
99+
Can also be set via `TWINE_NON_INTERACTIVE` environment variable.
100+
comment: The comment to include with the distribution file.
101+
config_file: The `.pypirc` config file to use.
102+
skip_existing: Continue uploading files if one already exists.
103+
Only valid when uploading to PyPI. Other implementations may not support this.
104+
cert: Path to alternate CA bundle (can also be set via `TWINE_CERT` environment variable).
105+
client_cert: Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
106+
verbose: Show verbose output.
107+
disable_progress_bar: Disable the progress bar.
108+
version: Show program's version number and exit.
109+
no_color: Disable colored output.
110+
"""
111+
cli_args = [package]
112+
113+
if repository:
114+
cli_args.append("--repository")
115+
cli_args.append(repository)
116+
117+
if repository_url:
118+
cli_args.append("--repository-url")
119+
cli_args.append(repository_url)
120+
121+
if attestations:
122+
cli_args.append("--attestations")
123+
124+
if sign:
125+
cli_args.append("--sign")
126+
127+
if sign_with:
128+
cli_args.append("--sign-with")
129+
cli_args.append(sign_with)
130+
131+
if identity:
132+
cli_args.append("--identity")
133+
cli_args.append(identity)
134+
135+
if username:
136+
cli_args.append("--username")
137+
cli_args.append(username)
138+
139+
if password:
140+
cli_args.append("--password")
141+
cli_args.append(password)
142+
143+
if non_interactive:
144+
cli_args.append("--non-interactive")
145+
146+
if comment:
147+
cli_args.append("--repository")
148+
149+
if config_file:
150+
cli_args.append("--config-file")
151+
cli_args.append(config_file)
152+
153+
if skip_existing:
154+
cli_args.append("--skip-existing")
155+
156+
if cert:
157+
cli_args.append("--cert")
158+
cli_args.append(cert)
159+
160+
if client_cert:
161+
cli_args.append("--client-cert")
162+
cli_args.append(client_cert)
163+
164+
if verbose:
165+
cli_args.append("--verbose")
166+
167+
if disable_progress_bar:
168+
cli_args.append("--disable-progress-bar")
169+
170+
run("check", *cli_args, version=version, no_color=no_color)
171+
172+
173+
@lazy(name="twine.upload")
174+
def upload(
175+
*dists: str,
176+
repository: str = "pypi",
177+
repository_url: str | None = None,
178+
attestations: bool = False,
179+
sign: bool = False,
180+
sign_with: str | None = None,
181+
identity: str | None = None,
182+
username: str | None = None,
183+
password: str | None = None,
184+
non_interactive: bool = False,
185+
comment: str | None = None,
186+
config_file: str | None = None,
187+
skip_existing: bool = False,
188+
cert: str | None = None,
189+
client_cert: str | None = None,
190+
verbose: bool = False,
191+
disable_progress_bar: bool = False,
192+
version: bool = False,
193+
no_color: bool = False,
194+
) -> None:
195+
"""Uploads one or more distributions to a repository.
196+
197+
Parameters:
198+
dists: The distribution files to check, usually `dist/*`.
199+
repository: The repository (package index) to upload the package to.
200+
Should be a section in the config file (default: `pypi`).
201+
Can also be set via `TWINE_REPOSITORY` environment variable.
202+
repository_url: The repository (package index) URL to upload the package to. This overrides `--repository`.
203+
Can also be set via `TWINE_REPOSITORY_URL` environment variable.
204+
attestations: Upload each file's associated attestations.
205+
sign: Sign files to upload using GPG.
206+
sign_with: GPG program used to sign uploads (default: `gpg`).
207+
identity: GPG identity used to sign files.
208+
username: The username to authenticate to the repository (package index) as.
209+
Can also be set via `TWINE_USERNAME` environment variable.
210+
password: The password to authenticate to the repository (package index) with.
211+
Can also be set via `TWINE_PASSWORD` environment variable.
212+
non_interactive: Do not interactively prompt for username/password if the required credentials are missing.
213+
Can also be set via `TWINE_NON_INTERACTIVE` environment variable.
214+
comment: The comment to include with the distribution file.
215+
config_file: The `.pypirc` config file to use.
216+
skip_existing: Continue uploading files if one already exists.
217+
Only valid when uploading to PyPI. Other implementations may not support this.
218+
cert: Path to alternate CA bundle (can also be set via `TWINE_CERT` environment variable).
219+
client_cert: Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
220+
verbose: Show verbose output.
221+
disable_progress_bar: Disable the progress bar.
222+
version: Show program's version number and exit.
223+
no_color: Disable colored output.
224+
"""
225+
cli_args = list(dists)
226+
227+
if repository:
228+
cli_args.append("--repository")
229+
cli_args.append(repository)
230+
231+
if repository_url:
232+
cli_args.append("--repository-url")
233+
cli_args.append(repository_url)
234+
235+
if attestations:
236+
cli_args.append("--attestations")
237+
238+
if sign:
239+
cli_args.append("--sign")
240+
241+
if sign_with:
242+
cli_args.append("--sign-with")
243+
cli_args.append(sign_with)
244+
245+
if identity:
246+
cli_args.append("--identity")
247+
cli_args.append(identity)
248+
249+
if username:
250+
cli_args.append("--username")
251+
cli_args.append(username)
252+
253+
if password:
254+
cli_args.append("--password")
255+
cli_args.append(password)
256+
257+
if non_interactive:
258+
cli_args.append("--non-interactive")
259+
260+
if comment:
261+
cli_args.append("--repository")
262+
263+
if config_file:
264+
cli_args.append("--config-file")
265+
cli_args.append(config_file)
266+
267+
if skip_existing:
268+
cli_args.append("--skip-existing")
269+
270+
if cert:
271+
cli_args.append("--cert")
272+
cli_args.append(cert)
273+
274+
if client_cert:
275+
cli_args.append("--client-cert")
276+
cli_args.append(client_cert)
277+
278+
if verbose:
279+
cli_args.append("--verbose")
280+
281+
if disable_progress_bar:
282+
cli_args.append("--disable-progress-bar")
283+
284+
run("upload", *cli_args, version=version, no_color=no_color)

0 commit comments

Comments
 (0)