Skip to content

Instantly share code, notes, and snippets.

@lf-
Created April 17, 2017 00:51
Show Gist options
  • Save lf-/984c5e9a4ef6a2be3af0f6fb5aa96c74 to your computer and use it in GitHub Desktop.
Save lf-/984c5e9a4ef6a2be3af0f6fb5aa96c74 to your computer and use it in GitHub Desktop.
import json
import os
import os.path
import platform
import requests
import shutil
import subprocess
import time
SCREEPS_UPLOAD_URL = 'https://screeps.com/api/user/code'
def screeps_name(filename):
return os.path.splitext(os.path.basename(filename))[0]
def upload_code(code_files, branch, auth):
"""
Upload some code to the Screeps mothership
Parameters:
code_files -- array of files to upload
branch -- branch to upload to
auth -- (username, password) tuple
"""
data = {
'branch': branch
}
data['modules'] = {screeps_name(f): open(f, encoding='utf-8').read() for f in code_files}
requests.post(SCREEPS_UPLOAD_URL, json=data, auth=auth)
def task_upload(outdir, branch, auth_file):
with open(auth_file) as f:
auth = tuple(f.read().split('\n'))
files = [os.path.join(outdir, f) for f in os.listdir(outdir)]
upload_code(files, branch, auth)
def task_clean(outdir):
shutil.rmtree(outdir, ignore_errors=True)
def task_compile(projdir, outdir):
tsc = 'tsc'
tslint = 'tslint'
if platform.system() == 'Windows':
tsc += '.cmd'
tslint += '.cmd'
children = [subprocess.Popen(p, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) for p in [
[tsc, '-p', projdir, '--outDir', outdir],
[tslint, '--project', projdir]
]]
while children:
for child in children:
retcode = child.poll()
if retcode is not None:
# process done
if retcode:
print(child.stdout.read().decode())
raise RuntimeError('Compile or lint error')
children.remove(child)
# nothing done yet, wait a bit
time.sleep(0.1)
def cli_upload(args):
task_upload(args.outdir, args.branch, args.auth_file)
def cli_clean(args):
task_clean(args.outdir)
def cli_compile(args):
cli_clean(args)
task_compile(args.projdir, args.outdir)
def cli_build(args):
cli_compile(args)
cli_upload(args)
def load_config(config_file):
with open(config_file) as f:
return json.load(f)
def main():
def fail(*args):
raise ValueError('Subcommand not specified')
import argparse
ap = argparse.ArgumentParser()
ap.set_defaults(func=fail)
ap.add_argument('--config')
sps = ap.add_subparsers()
upload_parser = sps.add_parser('upload')
upload_parser.add_argument('--outdir')
upload_parser.add_argument('--branch')
upload_parser.add_argument('--auth-file')
upload_parser.set_defaults(func=cli_upload)
compile_parser = sps.add_parser('compile')
compile_parser.add_argument('--outdir')
compile_parser.add_argument('--projdir')
compile_parser.set_defaults(func=cli_compile)
build_parser = sps.add_parser('build')
build_parser.add_argument('--outdir')
build_parser.add_argument('--branch')
build_parser.add_argument('--auth-file')
build_parser.add_argument('--projdir')
build_parser.set_defaults(func=cli_build)
args = ap.parse_args()
config = load_config(args.config)
for (name, arg) in vars(args).items():
if not arg and config[name]:
setattr(args, name, config[name])
print('Start task', flush=True)
args.func(args)
print('Finish task', flush=True)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment