nexus-pkg-push 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nexus_pkg_push/__init__.py +4 -0
- nexus_pkg_push/__main__.py +529 -0
- nexus_pkg_push/tests/test_main.py +187 -0
- nexus_pkg_push-1.1.0.dist-info/METADATA +40 -0
- nexus_pkg_push-1.1.0.dist-info/RECORD +9 -0
- nexus_pkg_push-1.1.0.dist-info/WHEEL +5 -0
- nexus_pkg_push-1.1.0.dist-info/entry_points.txt +3 -0
- nexus_pkg_push-1.1.0.dist-info/licenses/LICENSE +19 -0
- nexus_pkg_push-1.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# nexus-pkg-push: Publish packages to a Sonatype Nexus repository.
|
|
3
|
+
# Copyright (C) 2026 Patrik Dufresne <patrik@ikus-soft.com>
|
|
4
|
+
#
|
|
5
|
+
# SPDX-License-Identifier: MIT
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import functools
|
|
9
|
+
import getpass
|
|
10
|
+
import gzip
|
|
11
|
+
import io
|
|
12
|
+
import os
|
|
13
|
+
import re
|
|
14
|
+
import sys
|
|
15
|
+
import time
|
|
16
|
+
from collections import namedtuple
|
|
17
|
+
from importlib.metadata import distribution
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from urllib.parse import urlsplit, urlunsplit
|
|
20
|
+
|
|
21
|
+
import requests
|
|
22
|
+
from requests.adapters import HTTPAdapter
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
__version__ = distribution("nexus_pkg_push").version
|
|
26
|
+
except Exception:
|
|
27
|
+
__version__ = 'DEV'
|
|
28
|
+
|
|
29
|
+
EXIT_OK = 0
|
|
30
|
+
EXIT_UPLOAD_FAILED = 1
|
|
31
|
+
EXIT_USAGE_ERROR = 2
|
|
32
|
+
EXIT_INDEX_TIMEOUT = 3
|
|
33
|
+
|
|
34
|
+
FileToUpload = namedtuple('FileToUpload', 'local_path,remote_path')
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class PublishError(Exception):
|
|
38
|
+
"""Raised for fatal, non-recoverable errors (usage, repo not found, etc.)."""
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class NexusRepo:
|
|
42
|
+
"""Represents repository info discovered from the Nexus REST API."""
|
|
43
|
+
|
|
44
|
+
def __init__(self, name, fmt, base_url, path_within_repo):
|
|
45
|
+
self.name = name
|
|
46
|
+
self.format = fmt # 'apt', 'yum', 'raw', etc.
|
|
47
|
+
self.base_url = base_url
|
|
48
|
+
self.path_within_repo = path_within_repo
|
|
49
|
+
|
|
50
|
+
def __repr__(self):
|
|
51
|
+
return f"NexusRepo(name={self.name!r}, format={self.format!r})"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class TimeoutHTTPAdapter(HTTPAdapter):
|
|
55
|
+
def __init__(self, *args, timeout=None, **kwargs):
|
|
56
|
+
self.timeout = timeout
|
|
57
|
+
super().__init__(*args, **kwargs)
|
|
58
|
+
|
|
59
|
+
def send(self, request, **kwargs):
|
|
60
|
+
if kwargs.get("timeout") is None:
|
|
61
|
+
kwargs["timeout"] = self.timeout
|
|
62
|
+
return super().send(request, **kwargs)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def parse_args(argv=None):
|
|
66
|
+
parser = argparse.ArgumentParser(
|
|
67
|
+
prog="nexus-pkg-push",
|
|
68
|
+
description="Publish deb, yum, or raw packages to a Sonatype Nexus "
|
|
69
|
+
"repository and wait for index availability.",
|
|
70
|
+
)
|
|
71
|
+
parser.add_argument(
|
|
72
|
+
"-u",
|
|
73
|
+
"--user",
|
|
74
|
+
dest="user",
|
|
75
|
+
metavar="USER[:PASS]",
|
|
76
|
+
default=os.environ.get("NEXUS_USERNAME", os.environ.get("NEXUS_USR")),
|
|
77
|
+
help="Nexus username, or user:pass pair (like curl -U). Env: NEXUS_USERNAME, NEXUS_USR",
|
|
78
|
+
)
|
|
79
|
+
parser.add_argument(
|
|
80
|
+
"--password",
|
|
81
|
+
dest="password",
|
|
82
|
+
default=os.environ.get("NEXUS_PASSWORD", os.environ.get("NEXUS_PWD")),
|
|
83
|
+
help="Nexus password. Env: NEXUS_PASSWORD, NEXUS_PWD",
|
|
84
|
+
)
|
|
85
|
+
parser.add_argument(
|
|
86
|
+
"--token",
|
|
87
|
+
dest="token",
|
|
88
|
+
default=os.environ.get("NEXUS_TOKEN"),
|
|
89
|
+
help="Bearer token, alternative to username/password. Env: NEXUS_TOKEN",
|
|
90
|
+
)
|
|
91
|
+
parser.add_argument(
|
|
92
|
+
"--timeout",
|
|
93
|
+
type=int,
|
|
94
|
+
default=10,
|
|
95
|
+
help="HTTP request timeout in seconds. Default: 10",
|
|
96
|
+
)
|
|
97
|
+
parser.add_argument(
|
|
98
|
+
"--wait-timeout",
|
|
99
|
+
type=int,
|
|
100
|
+
default=60,
|
|
101
|
+
help="Max time to wait for index confirmation. Default: 60. Use 0 to skip waiting.",
|
|
102
|
+
)
|
|
103
|
+
parser.add_argument(
|
|
104
|
+
"--wait-interval",
|
|
105
|
+
type=int,
|
|
106
|
+
default=1,
|
|
107
|
+
help="Polling interval while waiting for index. Default: 1",
|
|
108
|
+
)
|
|
109
|
+
parser.add_argument(
|
|
110
|
+
"--retries",
|
|
111
|
+
type=int,
|
|
112
|
+
default=3,
|
|
113
|
+
help="Number of upload retry attempts on transient failure. Default: 3",
|
|
114
|
+
)
|
|
115
|
+
parser.add_argument(
|
|
116
|
+
"--continue-on-error",
|
|
117
|
+
action="store_true",
|
|
118
|
+
help="Attempt all uploads and report a summary instead of stopping at first failure.",
|
|
119
|
+
)
|
|
120
|
+
parser.add_argument(
|
|
121
|
+
"--distribution",
|
|
122
|
+
dest="distribution",
|
|
123
|
+
default=None,
|
|
124
|
+
help="(deb only) Comma-separated list of target distributions.",
|
|
125
|
+
)
|
|
126
|
+
parser.add_argument(
|
|
127
|
+
"--component",
|
|
128
|
+
dest="component",
|
|
129
|
+
default=None,
|
|
130
|
+
help="(deb only) Component/section.",
|
|
131
|
+
)
|
|
132
|
+
parser.add_argument(
|
|
133
|
+
"-R",
|
|
134
|
+
"--recursive",
|
|
135
|
+
action="store_true",
|
|
136
|
+
help="(raw only) Upload directory contents recursively.",
|
|
137
|
+
)
|
|
138
|
+
parser.add_argument(
|
|
139
|
+
"-d",
|
|
140
|
+
"--debug",
|
|
141
|
+
action="store_true",
|
|
142
|
+
help="Increase verbosity.",
|
|
143
|
+
)
|
|
144
|
+
parser.add_argument(
|
|
145
|
+
"--version",
|
|
146
|
+
action="version",
|
|
147
|
+
version=f"%(prog)s {__version__}",
|
|
148
|
+
)
|
|
149
|
+
parser.add_argument("files", metavar="FILE", nargs="+", help="File(s) or directory (raw + --recursive) to upload.")
|
|
150
|
+
parser.add_argument("repourl", metavar="REPOURL", help="Full URL to the target Nexus repository path.")
|
|
151
|
+
|
|
152
|
+
cfg = parser.parse_args(argv)
|
|
153
|
+
|
|
154
|
+
# Parse -U USER[:PASS]
|
|
155
|
+
if cfg.user and ":" in cfg.user and not cfg.password:
|
|
156
|
+
cfg.user, cfg.password = cfg.user.split(":", 1)
|
|
157
|
+
|
|
158
|
+
if not cfg.token and cfg.user and not cfg.password:
|
|
159
|
+
if sys.stdin.isatty():
|
|
160
|
+
cfg.password = getpass.getpass(f"Nexus password for {cfg.user}: ")
|
|
161
|
+
else:
|
|
162
|
+
parser.error("password required: provide --password, NEXUS_PASSWORD/NEXUS_PWD, or run interactively")
|
|
163
|
+
|
|
164
|
+
if not cfg.token and not cfg.user:
|
|
165
|
+
parser.error("authentication required: use -U/--user or --token")
|
|
166
|
+
|
|
167
|
+
return cfg
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def log(debug, *msg):
|
|
171
|
+
if debug:
|
|
172
|
+
print("[debug]", *msg, file=sys.stderr)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def build_session(cfg):
|
|
176
|
+
|
|
177
|
+
session = requests.Session()
|
|
178
|
+
if cfg.token:
|
|
179
|
+
session.headers["Authorization"] = f"Bearer {cfg.token}"
|
|
180
|
+
elif cfg.user:
|
|
181
|
+
session.auth = (cfg.user, cfg.password)
|
|
182
|
+
|
|
183
|
+
# Enforce timeout at session level.
|
|
184
|
+
adapter = TimeoutHTTPAdapter(timeout=cfg.timeout)
|
|
185
|
+
session.mount("http://", adapter)
|
|
186
|
+
session.mount("https://", adapter)
|
|
187
|
+
|
|
188
|
+
return session
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def split_repourl(repourl):
|
|
192
|
+
"""
|
|
193
|
+
Split a REPOURL into (base_url, repo_name, path_within_repo).
|
|
194
|
+
|
|
195
|
+
Assumes convention: {scheme}://{host}/repository/{repo_name}/{path...}
|
|
196
|
+
"""
|
|
197
|
+
parts = urlsplit(repourl)
|
|
198
|
+
segments = [s for s in parts.path.split("/") if s]
|
|
199
|
+
|
|
200
|
+
try:
|
|
201
|
+
idx = segments.index("repository")
|
|
202
|
+
except ValueError:
|
|
203
|
+
raise PublishError(f"Cannot locate '/repository/' segment in REPOURL: {repourl}")
|
|
204
|
+
|
|
205
|
+
if idx + 1 >= len(segments):
|
|
206
|
+
raise PublishError(f"REPOURL is missing a repository name: {repourl}")
|
|
207
|
+
|
|
208
|
+
repo_name = segments[idx + 1]
|
|
209
|
+
path_within_repo = "/".join(segments[idx + 2 :])
|
|
210
|
+
|
|
211
|
+
base_url = urlunsplit((parts.scheme, parts.netloc, "", "", ""))
|
|
212
|
+
return base_url, repo_name, path_within_repo
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def discover_repo_format(session, repourl, debug=False):
|
|
216
|
+
"""Query Nexus REST API to determine the repository's format (apt/yum/raw/...)."""
|
|
217
|
+
base_url, repo_name, path_within_repo = split_repourl(repourl)
|
|
218
|
+
|
|
219
|
+
api_url = f"{base_url}/service/rest/v1/repositories/{repo_name}"
|
|
220
|
+
log(debug, f"GET {api_url}")
|
|
221
|
+
try:
|
|
222
|
+
resp = session.get(api_url)
|
|
223
|
+
resp.raise_for_status()
|
|
224
|
+
except requests.RequestException as e:
|
|
225
|
+
raise PublishError(f"Failed to query Nexus repositories API: {e}")
|
|
226
|
+
|
|
227
|
+
repo = resp.json()
|
|
228
|
+
if repo.get("name") == repo_name:
|
|
229
|
+
fmt = repo.get("format")
|
|
230
|
+
log(debug, f"Resolved repo '{repo_name}' -> format={fmt}")
|
|
231
|
+
return NexusRepo(name=repo_name, fmt=fmt, base_url=base_url, path_within_repo=path_within_repo)
|
|
232
|
+
|
|
233
|
+
raise PublishError(f"Repository '{repo_name}' not found via Nexus API (check name, permissions, or NEXUS URL)")
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _collect_files(files, recursive, repo):
|
|
237
|
+
"""
|
|
238
|
+
Expand file/directory args into a flat list of (local_path, remote_rel_path) tuples.
|
|
239
|
+
|
|
240
|
+
remote_rel_path is the path (relative, POSIX-style, no leading slash) that should
|
|
241
|
+
be appended to repo.path_within_repo (for raw uploads) to preserve directory structure.
|
|
242
|
+
|
|
243
|
+
Applies cp-like logic:
|
|
244
|
+
- Single file + path_within_repo with filename → rename
|
|
245
|
+
- Multiple files + path_within_repo with filename → error
|
|
246
|
+
- Otherwise → preserve basenames under path_within_repo
|
|
247
|
+
"""
|
|
248
|
+
result = []
|
|
249
|
+
for f in files:
|
|
250
|
+
p = Path(f)
|
|
251
|
+
if p.is_dir():
|
|
252
|
+
if not recursive:
|
|
253
|
+
raise PublishError(f"'{f}' is a directory; use -R/--recursive to upload its contents")
|
|
254
|
+
base = p
|
|
255
|
+
for sub in sorted(base.rglob("*")):
|
|
256
|
+
if sub.is_file():
|
|
257
|
+
rel = sub.relative_to(base).as_posix()
|
|
258
|
+
result.append(FileToUpload(sub, rel))
|
|
259
|
+
elif p.is_file():
|
|
260
|
+
result.append(FileToUpload(p, p.name))
|
|
261
|
+
else:
|
|
262
|
+
raise PublishError(f"File not found: {f}")
|
|
263
|
+
|
|
264
|
+
if not result:
|
|
265
|
+
raise PublishError("No files to upload")
|
|
266
|
+
|
|
267
|
+
# Apply cp-like logic for path_within_repo
|
|
268
|
+
if repo.path_within_repo and not repo.path_within_repo.endswith("/"):
|
|
269
|
+
# path_within_repo looks like a filename (rename case)
|
|
270
|
+
if len(files) > 1 or len(result) > 1:
|
|
271
|
+
raise PublishError(
|
|
272
|
+
f"cannot specify target path when uploading multiple sources "
|
|
273
|
+
f"(got {len(result)} files, target path is '{repo.path_within_repo}')"
|
|
274
|
+
)
|
|
275
|
+
# Single file: rename it
|
|
276
|
+
local_path, _ = result[0]
|
|
277
|
+
result[0] = FileToUpload(local_path, repo.path_within_repo)
|
|
278
|
+
repo.path_within_repo = ''
|
|
279
|
+
|
|
280
|
+
return result
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _upload(session, repo, filepath, cfg):
|
|
284
|
+
# Build the upload function.
|
|
285
|
+
if repo.format == "apt":
|
|
286
|
+
_upload_func = functools.partial(
|
|
287
|
+
_upload_deb,
|
|
288
|
+
session,
|
|
289
|
+
repo,
|
|
290
|
+
filepath,
|
|
291
|
+
distribution=cfg.distribution,
|
|
292
|
+
component=cfg.component,
|
|
293
|
+
debug=cfg.debug,
|
|
294
|
+
)
|
|
295
|
+
elif repo.format in ("yum", "raw"):
|
|
296
|
+
_upload_func = functools.partial(
|
|
297
|
+
_upload_raw,
|
|
298
|
+
session,
|
|
299
|
+
repo,
|
|
300
|
+
filepath,
|
|
301
|
+
debug=cfg.debug,
|
|
302
|
+
)
|
|
303
|
+
else:
|
|
304
|
+
raise PublishError(f"Unsupported repository format: {repo.format}")
|
|
305
|
+
|
|
306
|
+
# Upload with retrial
|
|
307
|
+
last_exc = None
|
|
308
|
+
for attempt in range(1, cfg.retries + 1):
|
|
309
|
+
try:
|
|
310
|
+
_upload_func()
|
|
311
|
+
return
|
|
312
|
+
except requests.RequestException as e:
|
|
313
|
+
last_exc = e
|
|
314
|
+
log(cfg.debug, f"Upload attempt {attempt}/{cfg.retries} failed: {e}")
|
|
315
|
+
if attempt < cfg.retries:
|
|
316
|
+
time.sleep(min(2**attempt, 10))
|
|
317
|
+
|
|
318
|
+
raise PublishError(f"Upload failed for {filepath.local_path} after {cfg.retries} attempts: {last_exc}")
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def _upload_deb(session, repo, filepath, distribution, component, debug=False):
|
|
322
|
+
url = f"{repo.base_url}/repository/{repo.name}/"
|
|
323
|
+
if repo.path_within_repo:
|
|
324
|
+
raise PublishError(
|
|
325
|
+
"Uploading .deb packages does not support a sub-path within the repository "
|
|
326
|
+
"(Nexus apt-hosted repositories only accept uploads at the repository root)."
|
|
327
|
+
)
|
|
328
|
+
params = {}
|
|
329
|
+
if distribution:
|
|
330
|
+
params["distribution"] = distribution
|
|
331
|
+
if component:
|
|
332
|
+
params["component"] = component
|
|
333
|
+
log(debug, f"Uploading {filepath.local_path} to {url} distribution={distribution} component={component}")
|
|
334
|
+
# curl -u "admin:admin123" -H "Content-Type: multipart/form-data" --data-binary "@./test.deb" "http://localhost:8081/repository/apt-hosted/?distribution=trixie,bionic&component=non-free"
|
|
335
|
+
with open(filepath.local_path, "rb") as f:
|
|
336
|
+
resp = session.post(url, data=f, params=params, headers={'Content-Type': 'multipart/form-data'})
|
|
337
|
+
resp.raise_for_status()
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def _upload_raw(session, repo, filepath, debug=False):
|
|
341
|
+
# Preserve relative path structure under path_within_repo when recursive.
|
|
342
|
+
dest_path = (
|
|
343
|
+
f"{repo.path_within_repo}/{filepath.remote_path}".strip("/") if repo.path_within_repo else filepath.remote_path
|
|
344
|
+
)
|
|
345
|
+
url = f"{repo.base_url}/repository/{repo.name}/{dest_path}"
|
|
346
|
+
log(debug, f"Uploading {filepath.local_path} to {url}")
|
|
347
|
+
|
|
348
|
+
# curl -v --user 'admin:admin123' --upload-file ./test.txt http://localhost:8081/repository/raw-hosted/test.txt
|
|
349
|
+
with open(filepath.local_path, "rb") as f:
|
|
350
|
+
resp = session.put(url, data=f)
|
|
351
|
+
resp.raise_for_status()
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def _wait_for_index(session, repo, filepath, cfg):
|
|
355
|
+
# Skip if timeout is disabled.
|
|
356
|
+
if cfg.wait_timeout <= 0:
|
|
357
|
+
return True
|
|
358
|
+
|
|
359
|
+
# Build the check function.
|
|
360
|
+
if repo.format == "apt":
|
|
361
|
+
_check_func = functools.partial(
|
|
362
|
+
_check_deb,
|
|
363
|
+
session,
|
|
364
|
+
repo,
|
|
365
|
+
filepath,
|
|
366
|
+
distribution=cfg.distribution,
|
|
367
|
+
component=cfg.component,
|
|
368
|
+
debug=cfg.debug,
|
|
369
|
+
)
|
|
370
|
+
elif repo.format == "yum":
|
|
371
|
+
_check_func = functools.partial(
|
|
372
|
+
_check_yum,
|
|
373
|
+
session,
|
|
374
|
+
repo,
|
|
375
|
+
filepath,
|
|
376
|
+
debug=cfg.debug,
|
|
377
|
+
)
|
|
378
|
+
elif repo.format == "raw":
|
|
379
|
+
# Nothing to check for raw: PUT uploads are immediately available.
|
|
380
|
+
return True
|
|
381
|
+
else:
|
|
382
|
+
raise PublishError(f"Unsupported repository format: {repo.format}")
|
|
383
|
+
|
|
384
|
+
deadline = time.time() + cfg.wait_timeout
|
|
385
|
+
while time.time() < deadline:
|
|
386
|
+
try:
|
|
387
|
+
if _check_func():
|
|
388
|
+
log(cfg.debug, f"Index confirmed for {filepath}")
|
|
389
|
+
return True
|
|
390
|
+
except requests.RequestException as e:
|
|
391
|
+
log(cfg.debug, f"Index check error (will retry): {e}")
|
|
392
|
+
time.sleep(cfg.wait_interval)
|
|
393
|
+
|
|
394
|
+
return False
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def _list_href(session, url, debug=False):
|
|
398
|
+
"""GET a directory-listing-style HTML/XML page and return the list of href values."""
|
|
399
|
+
log(debug, f"GET {url}")
|
|
400
|
+
resp = session.get(url)
|
|
401
|
+
resp.raise_for_status()
|
|
402
|
+
return re.findall(r'href="([^"]+)"', resp.text)
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def _check_deb(session, repo, filepath, distribution, component, debug=False):
|
|
406
|
+
local_path, _rel = filepath
|
|
407
|
+
basename = local_path.name
|
|
408
|
+
|
|
409
|
+
metadata_url = f"{repo.base_url}/service/rest/repository/browse/{repo.name}/metadata/dists/"
|
|
410
|
+
|
|
411
|
+
if distribution:
|
|
412
|
+
distributions = [d.strip() for d in distribution.split(",") if d.strip()]
|
|
413
|
+
else:
|
|
414
|
+
# Discover all available distributions under dists/
|
|
415
|
+
hrefs = _list_href(session, metadata_url, debug=debug)
|
|
416
|
+
distributions = [h.strip("/") for h in hrefs if not (h.startswith("/") or h.startswith("../"))]
|
|
417
|
+
|
|
418
|
+
for dist in distributions:
|
|
419
|
+
comp_url = f"{metadata_url}{dist}/{component or 'main'}/"
|
|
420
|
+
try:
|
|
421
|
+
hrefs = _list_href(session, comp_url, debug=debug)
|
|
422
|
+
except requests.RequestException as e:
|
|
423
|
+
log(debug, f"Cannot list {comp_url}: {e}")
|
|
424
|
+
continue
|
|
425
|
+
|
|
426
|
+
binary_dirs = [h.strip("/") for h in hrefs if h.strip("/").startswith("binary-")]
|
|
427
|
+
|
|
428
|
+
for binary_dir in binary_dirs:
|
|
429
|
+
packages_url = (
|
|
430
|
+
f"{repo.base_url}/repository/{repo.name}/dists/{dist}/{component or 'main'}/{binary_dir}/Packages"
|
|
431
|
+
)
|
|
432
|
+
log(debug, f"Checking {packages_url} for {basename}")
|
|
433
|
+
try:
|
|
434
|
+
resp = session.get(packages_url)
|
|
435
|
+
resp.raise_for_status()
|
|
436
|
+
except requests.RequestException as e:
|
|
437
|
+
log(debug, f"Cannot fetch {packages_url}: {e}")
|
|
438
|
+
continue
|
|
439
|
+
if basename in resp.text:
|
|
440
|
+
return True
|
|
441
|
+
|
|
442
|
+
return False
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def _check_yum(session, repo, filepath, debug=False):
|
|
446
|
+
local_path, _rel = filepath
|
|
447
|
+
basename = local_path.name
|
|
448
|
+
|
|
449
|
+
repomd_url = f"{repo.base_url}/repository/{repo.name}/"
|
|
450
|
+
if repo.path_within_repo:
|
|
451
|
+
repomd_url = f"{repomd_url}{repo.path_within_repo}/"
|
|
452
|
+
repomd_url = f"{repomd_url}repodata/repomd.xml"
|
|
453
|
+
|
|
454
|
+
log(debug, f"GET {repomd_url}")
|
|
455
|
+
resp = session.get(repomd_url)
|
|
456
|
+
resp.raise_for_status()
|
|
457
|
+
|
|
458
|
+
match = re.search(r'<location href="([^"]*primary\.xml\.gz)"\s*/?>', resp.text)
|
|
459
|
+
if not match:
|
|
460
|
+
log(debug, "Could not find primary.xml.gz location in repomd.xml")
|
|
461
|
+
return False
|
|
462
|
+
|
|
463
|
+
primary_href = match.group(1)
|
|
464
|
+
# location href is relative to the repo root (e.g. "repodata/xxxx-primary.xml.gz")
|
|
465
|
+
base = f"{repo.base_url}/repository/{repo.name}/"
|
|
466
|
+
if repo.path_within_repo:
|
|
467
|
+
base = f"{base}{repo.path_within_repo}/"
|
|
468
|
+
primary_url = f"{base}{primary_href}"
|
|
469
|
+
|
|
470
|
+
log(debug, f"GET {primary_url}")
|
|
471
|
+
resp = session.get(primary_url)
|
|
472
|
+
resp.raise_for_status()
|
|
473
|
+
|
|
474
|
+
with gzip.GzipFile(fileobj=io.BytesIO(resp.content)) as gz:
|
|
475
|
+
data = gz.read().decode("utf-8", errors="replace")
|
|
476
|
+
|
|
477
|
+
return basename in data
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
def main(argv=None):
|
|
481
|
+
cfg = parse_args(argv)
|
|
482
|
+
session = build_session(cfg)
|
|
483
|
+
|
|
484
|
+
# Get information
|
|
485
|
+
try:
|
|
486
|
+
repo = discover_repo_format(session, cfg.repourl, cfg.debug)
|
|
487
|
+
except PublishError as e:
|
|
488
|
+
print(f"error: {e}", file=sys.stderr)
|
|
489
|
+
return EXIT_USAGE_ERROR
|
|
490
|
+
|
|
491
|
+
# List files recursively
|
|
492
|
+
try:
|
|
493
|
+
filelist = _collect_files(cfg.files, cfg.recursive, repo)
|
|
494
|
+
except PublishError as e:
|
|
495
|
+
print(f"error: {e}", file=sys.stderr)
|
|
496
|
+
return EXIT_USAGE_ERROR
|
|
497
|
+
|
|
498
|
+
upload_failed = False
|
|
499
|
+
index_timeout = False
|
|
500
|
+
|
|
501
|
+
# Upload file one by one.
|
|
502
|
+
for filepath in filelist:
|
|
503
|
+
try:
|
|
504
|
+
_upload(session, repo, filepath, cfg)
|
|
505
|
+
except PublishError as e:
|
|
506
|
+
print(f"error: {e}", file=sys.stderr)
|
|
507
|
+
upload_failed = True
|
|
508
|
+
if not cfg.continue_on_error:
|
|
509
|
+
return EXIT_UPLOAD_FAILED
|
|
510
|
+
continue
|
|
511
|
+
|
|
512
|
+
# After uploading all files, then check for index.
|
|
513
|
+
for filepath in filelist:
|
|
514
|
+
confirmed = _wait_for_index(session, repo, filepath, cfg)
|
|
515
|
+
if not confirmed:
|
|
516
|
+
print(f"warning: index confirmation timed out for {filepath[0]}", file=sys.stderr)
|
|
517
|
+
index_timeout = True
|
|
518
|
+
if not cfg.continue_on_error:
|
|
519
|
+
return EXIT_INDEX_TIMEOUT
|
|
520
|
+
|
|
521
|
+
if upload_failed:
|
|
522
|
+
return EXIT_UPLOAD_FAILED
|
|
523
|
+
if index_timeout:
|
|
524
|
+
return EXIT_INDEX_TIMEOUT
|
|
525
|
+
return EXIT_OK
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
if __name__ == "__main__":
|
|
529
|
+
sys.exit(main())
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# nexus-pkg-push: Publish packages to a Sonatype Nexus repository.
|
|
2
|
+
# Copyright (C) 2026 Patrik Dufresne <patrik@ikus-soft.com>
|
|
3
|
+
#
|
|
4
|
+
# SPDX-License-Identifier: MIT
|
|
5
|
+
|
|
6
|
+
import tempfile
|
|
7
|
+
import unittest
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from nexus_pkg_push.__main__ import (
|
|
11
|
+
NexusRepo,
|
|
12
|
+
PublishError,
|
|
13
|
+
_collect_files,
|
|
14
|
+
parse_args,
|
|
15
|
+
split_repourl,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class TestSplitRepourl(unittest.TestCase):
|
|
20
|
+
"""Test URL parsing."""
|
|
21
|
+
|
|
22
|
+
def test_split_repourl_simple(self):
|
|
23
|
+
"""Parse a simple repo URL."""
|
|
24
|
+
base_url, repo_name, path_within_repo = split_repourl("https://nexus.example.com/repository/yum-release/")
|
|
25
|
+
self.assertEqual(base_url, "https://nexus.example.com")
|
|
26
|
+
self.assertEqual(repo_name, "yum-release")
|
|
27
|
+
self.assertEqual(path_within_repo, "")
|
|
28
|
+
|
|
29
|
+
def test_split_repourl_with_path(self):
|
|
30
|
+
"""Parse a repo URL with a sub-path."""
|
|
31
|
+
base_url, repo_name, path_within_repo = split_repourl(
|
|
32
|
+
"https://nexus.example.com/repository/raw-release/archive/v1.0/"
|
|
33
|
+
)
|
|
34
|
+
self.assertEqual(base_url, "https://nexus.example.com")
|
|
35
|
+
self.assertEqual(repo_name, "raw-release")
|
|
36
|
+
self.assertEqual(path_within_repo, "archive/v1.0")
|
|
37
|
+
|
|
38
|
+
def test_split_repourl_missing_repository(self):
|
|
39
|
+
"""Raise error if /repository/ is missing."""
|
|
40
|
+
with self.assertRaises(PublishError) as cm:
|
|
41
|
+
split_repourl("https://nexus.example.com/invalid/yum-release/")
|
|
42
|
+
self.assertIn("Cannot locate '/repository/' segment", str(cm.exception))
|
|
43
|
+
|
|
44
|
+
def test_split_repourl_missing_repo_name(self):
|
|
45
|
+
"""Raise error if repo name is missing."""
|
|
46
|
+
with self.assertRaises(PublishError) as cm:
|
|
47
|
+
split_repourl("https://nexus.example.com/repository/")
|
|
48
|
+
self.assertIn("missing a repository name", str(cm.exception))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class TestCollectFiles(unittest.TestCase):
|
|
52
|
+
"""Test file collection logic."""
|
|
53
|
+
|
|
54
|
+
def setUp(self):
|
|
55
|
+
"""Create a temporary directory with test files."""
|
|
56
|
+
self.tmpdir = tempfile.TemporaryDirectory()
|
|
57
|
+
self.tmppath = Path(self.tmpdir.name)
|
|
58
|
+
|
|
59
|
+
# Create test files
|
|
60
|
+
(self.tmppath / "file1.deb").touch()
|
|
61
|
+
(self.tmppath / "file2.rpm").touch()
|
|
62
|
+
|
|
63
|
+
# Create a subdirectory with files
|
|
64
|
+
subdir = self.tmppath / "subdir"
|
|
65
|
+
subdir.mkdir()
|
|
66
|
+
(subdir / "nested.tar.gz").touch()
|
|
67
|
+
|
|
68
|
+
def tearDown(self):
|
|
69
|
+
self.tmpdir.cleanup()
|
|
70
|
+
|
|
71
|
+
def test_collect_single_file(self):
|
|
72
|
+
"""Collect a single file."""
|
|
73
|
+
repo = NexusRepo(name="test", fmt="raw", base_url="http://localhost", path_within_repo="")
|
|
74
|
+
files = [str(self.tmppath / "file1.deb")]
|
|
75
|
+
result = _collect_files(files, recursive=False, repo=repo)
|
|
76
|
+
|
|
77
|
+
self.assertEqual(len(result), 1)
|
|
78
|
+
self.assertEqual(result[0].local_path, self.tmppath / "file1.deb")
|
|
79
|
+
self.assertEqual(result[0].remote_path, "file1.deb")
|
|
80
|
+
|
|
81
|
+
def test_collect_multiple_files(self):
|
|
82
|
+
"""Collect multiple files."""
|
|
83
|
+
repo = NexusRepo(name="test", fmt="raw", base_url="http://localhost", path_within_repo="")
|
|
84
|
+
files = [
|
|
85
|
+
str(self.tmppath / "file1.deb"),
|
|
86
|
+
str(self.tmppath / "file2.rpm"),
|
|
87
|
+
]
|
|
88
|
+
result = _collect_files(files, recursive=False, repo=repo)
|
|
89
|
+
|
|
90
|
+
self.assertEqual(len(result), 2)
|
|
91
|
+
self.assertEqual(result[0].remote_path, "file1.deb")
|
|
92
|
+
self.assertEqual(result[1].remote_path, "file2.rpm")
|
|
93
|
+
|
|
94
|
+
def test_collect_directory_without_recursive(self):
|
|
95
|
+
"""Raise error when collecting a directory without --recursive."""
|
|
96
|
+
repo = NexusRepo(name="test", fmt="raw", base_url="http://localhost", path_within_repo="")
|
|
97
|
+
files = [str(self.tmppath / "subdir")]
|
|
98
|
+
|
|
99
|
+
with self.assertRaises(PublishError) as cm:
|
|
100
|
+
_collect_files(files, recursive=False, repo=repo)
|
|
101
|
+
self.assertIn("use -R/--recursive", str(cm.exception))
|
|
102
|
+
|
|
103
|
+
def test_collect_directory_with_recursive(self):
|
|
104
|
+
"""Collect files recursively from a directory."""
|
|
105
|
+
repo = NexusRepo(name="test", fmt="raw", base_url="http://localhost", path_within_repo="")
|
|
106
|
+
files = [str(self.tmppath / "subdir")]
|
|
107
|
+
result = _collect_files(files, recursive=True, repo=repo)
|
|
108
|
+
|
|
109
|
+
self.assertEqual(len(result), 1)
|
|
110
|
+
self.assertEqual(result[0].local_path, self.tmppath / "subdir" / "nested.tar.gz")
|
|
111
|
+
self.assertEqual(result[0].remote_path, "nested.tar.gz")
|
|
112
|
+
|
|
113
|
+
def test_collect_single_file_with_rename(self):
|
|
114
|
+
"""Rename a single file (cp-like behavior)."""
|
|
115
|
+
repo = NexusRepo(name="test", fmt="raw", base_url="http://localhost", path_within_repo="archive/renamed.deb")
|
|
116
|
+
files = [str(self.tmppath / "file1.deb")]
|
|
117
|
+
result = _collect_files(files, recursive=False, repo=repo)
|
|
118
|
+
|
|
119
|
+
self.assertEqual(len(result), 1)
|
|
120
|
+
self.assertEqual(result[0].remote_path, "archive/renamed.deb")
|
|
121
|
+
|
|
122
|
+
def test_collect_multiple_files_with_path_error(self):
|
|
123
|
+
"""Error when multiple files with a specific target path."""
|
|
124
|
+
repo = NexusRepo(name="test", fmt="raw", base_url="http://localhost", path_within_repo="archive/renamed.deb")
|
|
125
|
+
files = [
|
|
126
|
+
str(self.tmppath / "file1.deb"),
|
|
127
|
+
str(self.tmppath / "file2.rpm"),
|
|
128
|
+
]
|
|
129
|
+
|
|
130
|
+
with self.assertRaises(PublishError) as cm:
|
|
131
|
+
_collect_files(files, recursive=False, repo=repo)
|
|
132
|
+
self.assertIn("cannot specify target path when uploading multiple sources", str(cm.exception))
|
|
133
|
+
|
|
134
|
+
def test_collect_file_not_found(self):
|
|
135
|
+
"""Raise error for non-existent file."""
|
|
136
|
+
repo = NexusRepo(name="test", fmt="raw", base_url="http://localhost", path_within_repo="")
|
|
137
|
+
files = ["/nonexistent/file.deb"]
|
|
138
|
+
|
|
139
|
+
with self.assertRaises(PublishError) as cm:
|
|
140
|
+
_collect_files(files, recursive=False, repo=repo)
|
|
141
|
+
self.assertIn("File not found", str(cm.exception))
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class TestParseArgs(unittest.TestCase):
|
|
145
|
+
"""Test argument parsing."""
|
|
146
|
+
|
|
147
|
+
def test_parse_args_basic(self):
|
|
148
|
+
"""Parse basic arguments."""
|
|
149
|
+
argv = ["-u", "admin:password", "file.deb", "https://nexus.example.com/repository/apt-dev/"]
|
|
150
|
+
cfg = parse_args(argv)
|
|
151
|
+
|
|
152
|
+
self.assertEqual(cfg.user, "admin")
|
|
153
|
+
self.assertEqual(cfg.password, "password")
|
|
154
|
+
self.assertEqual(cfg.files, ["file.deb"])
|
|
155
|
+
self.assertEqual(cfg.repourl, "https://nexus.example.com/repository/apt-dev/")
|
|
156
|
+
|
|
157
|
+
def test_parse_args_missing_auth(self):
|
|
158
|
+
"""Raise error when authentication is missing."""
|
|
159
|
+
argv = ["file.deb", "https://nexus.example.com/repository/apt-dev/"]
|
|
160
|
+
|
|
161
|
+
with self.assertRaises(SystemExit):
|
|
162
|
+
parse_args(argv)
|
|
163
|
+
|
|
164
|
+
def test_parse_args_distribution(self):
|
|
165
|
+
"""Parse --distribution option."""
|
|
166
|
+
argv = [
|
|
167
|
+
"-u",
|
|
168
|
+
"admin:password",
|
|
169
|
+
"--distribution",
|
|
170
|
+
"bullseye,bookworm",
|
|
171
|
+
"file.deb",
|
|
172
|
+
"https://nexus.example.com/repository/apt-dev/",
|
|
173
|
+
]
|
|
174
|
+
cfg = parse_args(argv)
|
|
175
|
+
|
|
176
|
+
self.assertEqual(cfg.distribution, "bullseye,bookworm")
|
|
177
|
+
|
|
178
|
+
def test_parse_args_recursive(self):
|
|
179
|
+
"""Parse -R/--recursive option."""
|
|
180
|
+
argv = ["-u", "admin:password", "-R", "dir/", "https://nexus.example.com/repository/raw-release/"]
|
|
181
|
+
cfg = parse_args(argv)
|
|
182
|
+
|
|
183
|
+
self.assertTrue(cfg.recursive)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
if __name__ == "__main__":
|
|
187
|
+
unittest.main()
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nexus-pkg-push
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: Publish deb, rpm, and raw packages to a Sonatype Nexus repository, with automatic format detection and index-availability polling.
|
|
5
|
+
Author-email: Patrik Dufresne <patrik@ikus-soft.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
Project-URL: Homepage, https://gitlab.com/ikus-soft/nexus-pkg-push
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Requires-Python: <4,>=3.11
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: requests
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: black==26.3.0; extra == "dev"
|
|
19
|
+
Requires-Dist: flake8-copyright; extra == "dev"
|
|
20
|
+
Requires-Dist: flake8==7.3.0; extra == "dev"
|
|
21
|
+
Requires-Dist: isort==8.0.1; extra == "dev"
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
<p align="center">
|
|
26
|
+
<a href="LICENSE"><img alt="License" src="https://img.shields.io/pypi/l/debbuild"></a>
|
|
27
|
+
<a href="https://gitlab.com/ikus-soft/nexus-pkg-push/pipelines"><img alt="Build" src="https://gitlab.com/ikus-soft/nexus-pkg-push/badges/main/pipeline.svg"></a>
|
|
28
|
+
<a href="https://sonar.ikus-soft.com/dashboard?id=nexus-pkg-push"><img alt="Quality Gate Minarca Client" src="https://sonar.ikus-soft.com/api/project_badges/measure?project=nexus-pkg-push&metric=alert_status"></a>
|
|
29
|
+
<a href="https://sonar.ikus-soft.com/dashboard?id=nexus-pkg-push"><img alt="Coverage" src="https://sonar.ikus-soft.com/api/project_badges/measure?project=nexus-pkg-push&metric=coverage"></a>
|
|
30
|
+
</p>
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
## Support
|
|
35
|
+
|
|
36
|
+
If you encounter any issues or need assistance, feel free to open a ticket on [GitLab](https://gitlab.com/ikus-soft/nexus-pkg-push/-/issues/new) or GitHub.
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
This project is released under the MIT License.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
nexus_pkg_push/__init__.py,sha256=79i49Fh5D-e_8W5nb1t5ctioAV6RK_YWl5WyHe8GRRQ,160
|
|
2
|
+
nexus_pkg_push/__main__.py,sha256=fY60tGhSa0kmajFcTyhcmVEDzaOSMi4dOBemusmCeZc,17491
|
|
3
|
+
nexus_pkg_push/tests/test_main.py,sha256=W5fK-MU-NEWnvIxrGxztQiM9a6csajAHUHQRD3t9_KU,7307
|
|
4
|
+
nexus_pkg_push-1.1.0.dist-info/licenses/LICENSE,sha256=zMPIAcyWaEbFvnIAUAU44Bl7rQhOWIwgEOiF7DglswM,1078
|
|
5
|
+
nexus_pkg_push-1.1.0.dist-info/METADATA,sha256=714iVE5NQLhxFH4rLVdF733JN4yYaK76c0whbTEn5QM,1854
|
|
6
|
+
nexus_pkg_push-1.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
7
|
+
nexus_pkg_push-1.1.0.dist-info/entry_points.txt,sha256=HWUp-X65PT5W3YoN31mmxOLcQPPFXhjwkteFLup3Ogk,99
|
|
8
|
+
nexus_pkg_push-1.1.0.dist-info/top_level.txt,sha256=_Si60Q3LZnJ8Cyk1qcI2zO0FaqXm0X0XB-wPfvx-M-Y,15
|
|
9
|
+
nexus_pkg_push-1.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright 2026 Patrik Dufresne <patrik@ikus-soft.com>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nexus_pkg_push
|