69 lines
3.2 KiB
Python
Executable File
69 lines
3.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Upload the package built for the newest CHANGELOG.md entry to the Gitea Debian repository: publish.py.
|
|
|
|
The package is out/PACKAGE_VERSION_all.deb, its name the Source of debian/control and its version
|
|
the Debian version of the newest entry of CHANGELOG.md (bin/changelog.py), which build.py builds.
|
|
It is uploaded to the component main of each distribution of DISTRIBUTIONS (resolute by default,
|
|
separated by spaces) of the owner OWNER (koszewscy by default) at SERVER_URL
|
|
(https://gitea.koszewscy.waw.pl by default), signed in as OWNER with the token PACKAGES_TOKEN, else
|
|
the content of ~/.gitea-packages-token. A distribution that has the version already (Gitea's 409)
|
|
is skipped. A failure is a message on standard error and the exit status 1.
|
|
"""
|
|
|
|
import base64
|
|
import os
|
|
import pathlib
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parent
|
|
sys.path.insert(0, str(ROOT / 'bin'))
|
|
|
|
import changelog # noqa: E402
|
|
|
|
CHANGELOG = ROOT / 'CHANGELOG.md'
|
|
CONTROL = ROOT / 'debian-package' / 'debian' / 'control'
|
|
TOKEN_FILE = pathlib.Path.home() / '.gitea-packages-token'
|
|
|
|
|
|
def main():
|
|
owner = os.environ.get('OWNER') or 'koszewscy'
|
|
server = (os.environ.get('SERVER_URL') or 'https://gitea.koszewscy.waw.pl').rstrip('/')
|
|
distributions = (os.environ.get('DISTRIBUTIONS') or 'resolute').split()
|
|
try:
|
|
found = changelog.entries(CHANGELOG.read_text(encoding='utf-8'))
|
|
package = changelog.control_field(CONTROL.read_text(encoding='utf-8'), 'Source')
|
|
if not found:
|
|
raise ValueError('no entry')
|
|
token = os.environ.get('PACKAGES_TOKEN') or TOKEN_FILE.read_text(encoding='utf-8').strip()
|
|
except (OSError, ValueError) as error:
|
|
sys.exit(f'publish.py: {error}')
|
|
version = changelog.debian_version(found[0]['version'])
|
|
deb = ROOT / 'out' / f'{package}_{version}_all.deb'
|
|
if not deb.is_file():
|
|
sys.exit(f'publish.py: {deb.relative_to(ROOT)} does not exist; run ./build.py first')
|
|
content = deb.read_bytes()
|
|
credentials = base64.b64encode(f'{owner}:{token}'.encode()).decode()
|
|
|
|
for distribution in distributions:
|
|
request = urllib.request.Request(f'{server}/api/packages/{owner}/debian/pool/{distribution}/main/upload',
|
|
data=content, method='PUT',
|
|
headers={'Authorization': f'Basic {credentials}',
|
|
'Content-Type': 'application/octet-stream'})
|
|
try:
|
|
with urllib.request.urlopen(request, timeout=300):
|
|
print(f'{package} {version} is published in {distribution}/main.')
|
|
except urllib.error.HTTPError as error:
|
|
if error.code != 409:
|
|
sys.exit(f'publish.py: the upload to {distribution}/main returned HTTP {error.code}: '
|
|
f'{error.read().decode(errors="replace").strip()}')
|
|
print(f'{package} {version} is already in {distribution}/main, skipping.')
|
|
except urllib.error.URLError as error:
|
|
sys.exit(f'publish.py: {server} cannot be reached: {error.reason}')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|