Backlog/tools/scripts/automation.py

131 lines
4.5 KiB
Python

import os
import argparse
import shutil
import sys
import subprocess
orig_dir = os.path.abspath(os.path.dirname(__file__))
def getCommitHash():
commit_hash = subprocess.check_output(
['git', 'rev-parse', '--short', 'HEAD'],
text=True
).strip()
print(commit_hash)
dryRun = False
def run(cmd, cwd=None):
global dryRun
print(" ".join(cmd))
c = os.getcwd()
if cwd is not None:
c = cwd
if not dryRun:
print(subprocess.check_output(" ".join(cmd), shell=True, cwd=c).decode(), file=sys.stderr)
def buildAndPackage(branch, target, deploy):
run(['zig', 'env'])
run(['zig', 'build', f'-Dtarget={target}', '--prefix', 'zig-out-debug', 'install'])
run(['zig', 'build', f'-Dtarget={target}', '-Dstatic_build', '-Doptimize=ReleaseSafe', '--prefix', 'zig-out-release', 'install'])
if deploy is not None:
host = '$BACKLOG_INFRA_DEPLOY_HOST'
path = '$BACKLOG_INFRA_DEPLOY_PATH'
if os.name == 'nt':
host = '%BACKLOG_INFRA_DEPLOY_HOST%'
path = '%BACKLOG_INFRA_DEPLOY_PATH%'
sampleGameDeploy = f'{path}/sampleGame/{branch}/{deploy}'
binariesDeploy = f'{path}/binaries/{branch}/{deploy}'
def makePackage(target, branch, deploy, tag):
run(['ssh', host, f'mkdir -p {binariesDeploy}'])
run(['ssh', host, f'mkdir -p {sampleGameDeploy}'])
if os.name == 'nt':
packageFile = f'binaries-{deploy}-{tag}-{target}.zip'
run(['zip', '-r', '-7', packageFile, f'zig-out-{tag}'])
run(['scp', packageFile, f'{host}:{binariesDeploy}/{packageFile}'])
else:
packageFile = f'binaries-{deploy}-{tag}-{target}.tar.xz'
run(['tar', '-cvzf', os.path.abspath(packageFile), '-C', f'zig-out-{tag}'])
run(['scp', packageFile, f'{host}:{binariesDeploy}/{packageFile}'])
os.makedirs(f'staging/sampleGame/{tag}', exist_ok=True)
excludeList = [
'headless.exe',
'headless.pdb',
'newProject.exe',
'newProject.pdb',
'toolbox.exe',
'toolbox.pdb'
]
for f in os.listdir(f'zig-out-{tag}/bin'):
shutil.copy(os.path.join(f'zig-out-{tag}/bin', f), f"staging/sampleGame/{tag}/")
if os.path.exists(f'zig-out-{tag}/modules'):
os.makedirs('staging/sampleGame/zig-out/modules', exist_ok=True)
for f in os.listdir(f'zig-out-{tag}/modules'):
print("module: ", f, os.path.join(f'zig-out-{tag}/modules', f))
shutil.copy(os.path.join(f'zig-out-{tag}/modules', f), f"staging/sampleGame/zig-out/modules/" + f)
makePackage(target, branch, deploy, 'debug')
makePackage(target, branch, deploy, 'release')
# stage content
if os.path.exists('staging/sampleGame/content'):
shutil.rmtree('staging/sampleGame/content', ignore_errors=False, onerror=None)
shutil.copytree('content', 'staging/sampleGame/content')
if os.name == 'nt':
packageFile = f'sampleGame-{deploy}-{target}.zip'
run(['zip', '-r', '-7', os.path.abspath(packageFile), f'.'], cwd=os.path.join(os.getcwd(), 'staging/sampleGame'))
run(['scp', packageFile, f'{host}:{sampleGameDeploy}/{packageFile}'])
else:
packageFile = f'sampleGame-{deploy}-{target}.tar.xz'
run(['tar', '-cvzf', os.path.abspath(packageFile), '-C', f'staging/sampleGame'])
run(['scp', packageFile, f'{host}:{sampleGameDeploy}/{packageFile}'])
run(['zig', 'build', f'-Dtarget={target}', 'run-headless'])
run(['zig', 'build', f'-Dtarget={target}', '-Dstatic_build','run-headless'])
run(['zig', 'build', f'-Dtarget={target}', '-Dslow_logging','run-headless'])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='run automation tasks for the repo')
# Positional argument (required)
parser.add_argument('--deploy', nargs="?", help='tag for the deployment')
parser.add_argument('--branch', default="manual", help='branch name')
parser.add_argument('--dry', action="store_true", help='dont run anything')
parser.add_argument('target', help='required')
args = parser.parse_args()
if args.dry:
dryRun = True
def cleanBranchName(branch):
return branch.replace('/', '_')
b = cleanBranchName(args.branch)
buildAndPackage(b, args.target, args.deploy)