|
1 | 1 | import hashlib |
2 | | -import os |
3 | 2 | from pathlib import Path |
4 | 3 |
|
5 | | -from . import builder, trusted_publishing |
| 4 | +from . import builder |
| 5 | +from .creds import Credentials |
6 | 6 | from .filesystem import find_files, wheel_file_info |
7 | 7 | from .log import logger |
8 | 8 | from .http import post |
@@ -37,14 +37,6 @@ def __init__(self, files, repository=None): |
37 | 37 | self.files = files |
38 | 38 | self.repository = repository |
39 | 39 |
|
40 | | - self.username = os.environ.get("BORK_PYPI_USERNAME", None) |
41 | | - self.password = os.environ.get("BORK_PYPI_PASSWORD", None) |
42 | | - |
43 | | - token = os.environ.get("BORK_PYPI_TOKEN", None) |
44 | | - if self.username is None and token is not None: |
45 | | - self.username = "__token__" |
46 | | - self.password = token |
47 | | - |
48 | 40 | def _upload_file(self, url, file, metadata): |
49 | 41 | file_contents = Path(file).read_bytes() |
50 | 42 | file_digest = hashlib.sha256(file_contents).hexdigest() |
@@ -119,20 +111,15 @@ def _upload_file(self, url, file, metadata): |
119 | 111 | *other_fields |
120 | 112 | ] |
121 | 113 |
|
122 | | - # If we've still have no credentials, try Trusted Publishing. |
123 | | - if self.username is None and self.password is None: |
124 | | - token = trusted_publishing.get_token(self.repository) |
125 | | - if token is not None: |
126 | | - self.username = "__token__" |
127 | | - self.password = token |
| 114 | + username, password = self._get_credentials() |
128 | 115 |
|
129 | | - if self.username is None and self.password is None: |
| 116 | + if username is None and password is None: |
130 | 117 | raise RuntimeError( |
131 | 118 | "BORK_PYPI_USERNAME and BORK_PYPI_PASSWORD environment variables are undefined.\n\n" |
132 | 119 | "If you used Bork prior to v9.0.0, these variables used to be TWINE_USERNAME and " |
133 | 120 | "TWINE_PASSWORD. You can use the same values.") |
134 | 121 |
|
135 | | - response = post(url, form, auth=(self.username, self.password)) |
| 122 | + response = post(url, form, auth=(username, password)) |
136 | 123 | return response |
137 | 124 |
|
138 | 125 | def upload(self, *, dry_run = True, metadata = None): |
@@ -162,6 +149,17 @@ def upload(self, *, dry_run = True, metadata = None): |
162 | 149 | log.info("FAILED - %s couldn't be uploaded to %s", filename, self.repository) |
163 | 150 | log.info(response.data.decode().strip()) |
164 | 151 |
|
| 152 | + def _get_credentials(self): |
| 153 | + username = None |
| 154 | + password = None |
| 155 | + |
| 156 | + credentials = Credentials.from_env(self.repository) |
| 157 | + if credentials.pypi: |
| 158 | + username = credentials.pypi.username |
| 159 | + password = credentials.pypi.password |
| 160 | + |
| 161 | + return (username, password) |
| 162 | + |
165 | 163 |
|
166 | 164 | def upload(repository_name, *globs, **kwargs): |
167 | 165 | files = find_files(globs) |
|
0 commit comments