Publish

Application for publishing packages to the gitea package server.

Hardcoded my credentials and URLS's. It's a kinda note for myself but if others can learn from it, it's fine by me.

I've copied this to my /usr/local/bin/ and can just execute publish [filename] where ever i want.

Usage

Will overwrite version 1.0.0 from the server, else will create a new one with version 1.0.0.

publish [filename]

Or with version parameter:

publish [filename] --version=1.33.7 

Source code

#!/usr/bin/env python

import logging
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s',
)
log = logging.getLogger("publish")

import env
import sys
import pathlib 
import os
import argparse
import subprocess
parser = argparse.ArgumentParser(description="Application for deploying packages at molodetz.nl.")
parser.add_argument("file", type=str, help="File you want to publish.")
parser.add_argument("--version", type=str, required=False, default="1.0.0", help="Version of your package")
args = parser.parse_args()

if not len(args.version.split(".")) == 3:
    raise Exception("{} is an invalid version number. It shoud be in '\\d.\\d.\\d.' format.".format(args.version))

version = args.version

path = pathlib.Path(args.file)
if not path.exists():
    log.error("Path does not exist: '{}'.".format(path))
    exit(1)

file_name = path.name

username = "retoor"
password = env.secret
package_name = path.name

command_delete = " ".join([
     f"curl --user {username}:{password}",
     f"-X DELETE",
     f"https://retoor.molodetz.nl/api/packages/{username}/generic/{package_name}/{version}/{file_name}",
     "--silent"
])

command = " ".join([
     f"curl --user {username}:{password}",
     f"--upload-file {path}",
     f"https://retoor.molodetz.nl/api/packages/{username}/generic/{package_name}/{version}/{file_name}",
     "--silent"
])
log.info("Publishing package {} {}.".format(package_name, version))
log.warning("Deleting original package with version {} if exists.".format(version))
subprocess.check_output(command_delete, shell=True)
subprocess.check_output(command,shell=True)
log.info("Publishing package {} {} success.".format(package_name, version))
log.info("Download package using curl -OJ https://retoor.molodetz.nl/api/packages/retoor/generic/{}/1.0.0/{} --silent.".format(package_name, package_name))
log.info("or download package using wget https://retoor.molodetz.nl/api/packages/retoor/generic/{}/1.0.0/{} --quiet.".format(package_name, package_name))
log.info("Don't forget to chmod +x after downloading the package if it is executable.")