2019-01-04 23:43:37 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-08-25 00:40:39 +02:00
|
|
|
import glob
|
2019-01-04 23:43:37 +01:00
|
|
|
import logging
|
2019-08-25 00:40:39 +02:00
|
|
|
import os
|
2020-09-06 15:08:16 +02:00
|
|
|
import re
|
2019-08-25 00:40:39 +02:00
|
|
|
import subprocess
|
2019-01-04 23:43:37 +01:00
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
2019-08-25 00:27:31 +02:00
|
|
|
|
|
|
|
|
2019-01-04 23:43:37 +01:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger("cibuild")
|
|
|
|
|
|
|
|
|
|
|
|
twine_username = os.environ.get("TWINE_USERNAME")
|
|
|
|
twine_password = os.environ.get("TWINE_PASSWORD")
|
|
|
|
docker_username = os.environ.get("DOCKER_USERNAME")
|
|
|
|
docker_password = os.environ.get("DOCKER_PASSWORD")
|
|
|
|
aws_creds = os.environ.get("AWS_ACCESS_KEY_ID")
|
|
|
|
|
|
|
|
|
|
|
|
def tag():
|
2020-09-06 15:08:16 +02:00
|
|
|
match = re.search("refs/tags/(.*)", os.environ.get("GITHUB_REF"))
|
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
return None
|
2019-01-04 23:43:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
def branch():
|
2020-09-06 15:08:16 +02:00
|
|
|
match = re.search("refs/heads/(.*)", os.environ.get("GITHUB_REF"))
|
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
return None
|
2019-01-04 23:43:37 +01:00
|
|
|
|
|
|
|
|
2019-09-06 18:53:05 +02:00
|
|
|
def docker_name(version):
|
2021-02-28 22:05:15 +01:00
|
|
|
return f"spaam/svtplay-dl:{version}"
|
2019-01-04 23:43:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
def build_docker():
|
|
|
|
logger.info("Building docker")
|
2019-09-06 18:53:05 +02:00
|
|
|
if tag():
|
|
|
|
version = tag()
|
|
|
|
else:
|
|
|
|
version = "dev"
|
|
|
|
|
2019-08-25 00:27:31 +02:00
|
|
|
subprocess.check_output(["docker", "build", "-f", "dockerfile/Dockerfile", "-t", docker_name(version), "."])
|
|
|
|
subprocess.check_call(["docker", "login", "-u", docker_username, "-p", docker_password])
|
|
|
|
subprocess.check_call(["docker", "push", docker_name(version)])
|
2019-01-04 23:43:37 +01:00
|
|
|
|
2019-08-22 08:01:25 +02:00
|
|
|
if tag():
|
2019-08-25 00:27:31 +02:00
|
|
|
subprocess.check_output(["docker", "tag", docker_name(version), docker_name("latest")])
|
|
|
|
subprocess.check_call(["docker", "push", docker_name("latest")])
|
2019-08-22 08:01:25 +02:00
|
|
|
|
2019-01-04 23:43:37 +01:00
|
|
|
|
|
|
|
def snapshot_folder():
|
|
|
|
"""
|
|
|
|
Use the commit date in UTC as folder name
|
|
|
|
"""
|
|
|
|
logger.info("Snapshot folder")
|
|
|
|
try:
|
|
|
|
stdout = subprocess.check_output(["git", "show", "-s", "--format=%cI", "HEAD"])
|
|
|
|
except subprocess.CalledProcessError as e:
|
2019-08-25 00:27:31 +02:00
|
|
|
logger.error("Error: {}".format(e.output.decode("ascii", "ignore").strip()))
|
2019-01-04 23:43:37 +01:00
|
|
|
sys.exit(2)
|
|
|
|
except FileNotFoundError as e:
|
2021-02-28 22:05:15 +01:00
|
|
|
logger.error(f"Error: {e}")
|
2019-01-04 23:43:37 +01:00
|
|
|
sys.exit(2)
|
2019-08-25 00:27:31 +02:00
|
|
|
ds = stdout.decode("ascii", "ignore").strip()
|
2019-01-04 23:43:37 +01:00
|
|
|
dt = datetime.fromisoformat(ds)
|
|
|
|
utc = dt - dt.utcoffset()
|
|
|
|
return utc.strftime("%Y%m%d_%H%M%S")
|
|
|
|
|
|
|
|
|
|
|
|
def aws_upload():
|
|
|
|
if tag():
|
|
|
|
folder = "release"
|
|
|
|
version = tag()
|
|
|
|
else:
|
|
|
|
folder = "snapshots"
|
|
|
|
version = snapshot_folder()
|
2021-02-28 22:05:15 +01:00
|
|
|
logger.info(f"Upload to aws {folder}/{version}")
|
2020-09-06 15:08:16 +02:00
|
|
|
for file in ["svtplay-dl", "svtplay-dl-amd64.zip", "svtplay-dl-win32.zip"]:
|
2019-01-04 23:43:37 +01:00
|
|
|
if os.path.isfile(file):
|
2020-09-13 20:13:31 +02:00
|
|
|
subprocess.check_call(
|
2021-02-28 22:05:15 +01:00
|
|
|
["aws", "--region", "us-east-1", "s3", "cp", f"{file}", f"s3://svtplay-dl/{folder}/{version}/{file}"],
|
2020-09-13 20:13:31 +02:00
|
|
|
)
|
2019-01-04 23:43:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
def pypi_upload():
|
|
|
|
logger.info("Uploading to pypi")
|
2020-09-20 10:16:15 +02:00
|
|
|
sdist = glob.glob("dist/svtplay-dl-*.tar.gz")
|
2019-09-06 18:22:16 +02:00
|
|
|
if sdist:
|
|
|
|
subprocess.check_call(["twine", "upload", sdist[0]])
|
|
|
|
else:
|
|
|
|
logging.warning("Can't find file for pypi..")
|
2019-01-04 23:43:37 +01:00
|
|
|
|
2019-09-06 19:16:47 +02:00
|
|
|
|
2021-02-28 22:05:15 +01:00
|
|
|
logger.info(f"Branch: {branch()}")
|
|
|
|
logger.info(f"Tag: {tag()}")
|
2019-01-04 23:43:37 +01:00
|
|
|
|
2020-09-10 20:21:44 +02:00
|
|
|
if not tag() and branch() != "master":
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if os.environ.get("CIBUILD") != "yes":
|
2019-01-04 23:43:37 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2020-11-19 22:06:47 +01:00
|
|
|
if os.environ.get("OS").startswith("ubuntu") and os.environ.get("BUILD_DOCKER") == "yes":
|
2020-09-10 20:21:44 +02:00
|
|
|
build_docker()
|
|
|
|
|
2020-09-10 11:55:58 +02:00
|
|
|
aws_upload()
|
2019-01-04 23:43:37 +01:00
|
|
|
|
2020-09-10 20:13:54 +02:00
|
|
|
if tag() and os.environ.get("OS").startswith("ubuntu"):
|
2019-01-04 23:43:37 +01:00
|
|
|
pypi_upload()
|