2019-01-04 23:43:37 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
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
|
|
|
|
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-05 00:52:21 +01:00
|
|
|
if sys.version_info[0] == 3 and sys.version_info[1] < 7:
|
|
|
|
from backports.datetime_fromisoformat import MonkeyPatch
|
2019-08-25 00:27:31 +02:00
|
|
|
|
2019-01-05 00:52:21 +01:00
|
|
|
MonkeyPatch.patch_fromisoformat()
|
|
|
|
|
2019-01-04 23:43:37 +01:00
|
|
|
|
|
|
|
root = os.path.normpath(os.path.join(os.path.dirname(__file__), ".."))
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger("cibuild")
|
|
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(prog="cibuild")
|
|
|
|
general = parser.add_argument_group()
|
|
|
|
general.add_argument("-s", "--snapshots", action="store_true", dest="snapshot", default=False)
|
|
|
|
general.add_argument("-r", "--release", action="store_true", dest="release", default=False)
|
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2019-01-05 00:36:24 +01:00
|
|
|
travis = os.environ.get("TRAVIS", "")
|
2019-01-04 23:43:37 +01:00
|
|
|
travis_tag = os.environ.get("TRAVIS_TAG", "")
|
|
|
|
travis_branch = os.environ.get("TRAVIS_BRANCH", "")
|
|
|
|
appveyor_tag = os.environ.get("APPVEYOR_REPO_TAG_NAME", "")
|
|
|
|
appveyor_branch = os.environ.get("APPVEYOR_REPO_BRANCH", "")
|
|
|
|
|
|
|
|
|
|
|
|
def tag():
|
|
|
|
return travis_tag or appveyor_tag
|
|
|
|
|
|
|
|
|
|
|
|
def branch():
|
|
|
|
return travis_branch or appveyor_branch
|
|
|
|
|
|
|
|
|
2019-09-06 18:53:05 +02:00
|
|
|
def docker_name(version):
|
2019-08-22 08:01:25 +02:00
|
|
|
return "spaam/svtplay-dl:{}".format(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 build_package():
|
|
|
|
logger.info("Building python package")
|
2019-08-25 00:27:31 +02:00
|
|
|
|
|
|
|
subprocess.check_output(["python", "setup.py", "sdist", "bdist_wheel"])
|
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:
|
|
|
|
logger.error("Error: {}".format(e))
|
|
|
|
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()
|
|
|
|
logger.info("Upload to aws {}/{}".format(folder, version))
|
|
|
|
for file in ["svtplay-dl", "svtplay-dl.zip"]:
|
|
|
|
if os.path.isfile(file):
|
2019-08-25 00:27:31 +02:00
|
|
|
subprocess.check_call(["aws", "s3", "cp", "{}".format(file), "s3://svtplay-dl/{}/{}/{}".format(folder, version, file)])
|
2019-01-04 23:43:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
def pypi_upload():
|
|
|
|
logger.info("Uploading to pypi")
|
2019-09-17 23:09:07 +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
|
|
|
|
2019-09-03 20:18:21 +02:00
|
|
|
logger.info("Branch: {}".format(branch()))
|
|
|
|
logger.info("Tag: {}".format(tag()))
|
2019-01-04 23:43:37 +01:00
|
|
|
|
2019-09-03 20:18:21 +02:00
|
|
|
if not tag() and branch() != "master":
|
2019-01-04 23:43:37 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2019-09-06 19:15:31 +02:00
|
|
|
build_package()
|
2019-01-05 00:36:24 +01:00
|
|
|
if travis:
|
|
|
|
build_docker()
|
2019-01-04 23:43:37 +01:00
|
|
|
aws_upload()
|
|
|
|
|
2019-09-06 18:22:16 +02:00
|
|
|
if tag() and travis:
|
2019-01-04 23:43:37 +01:00
|
|
|
pypi_upload()
|