forked from nfreader/renderbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
71 lines (53 loc) · 2.64 KB
/
render.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
#
# render.py - Renders maps
import os, sys, zipfile, re, json
import subprocess
from subprocess import call
from sqlalchemy import create_engine
from sqlalchemy.sql import text
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
ALT_DB_NAME = os.environ.get("ALT_DB_NAME")
ALT_DB_USER = os.environ.get("ALT_DB_USER")
ALT_DB_PASS = os.environ.get("ALT_DB_PASS")
ALT_DB_HOST = os.environ.get("ALT_DB_HOST")
ALT_DB_PORT = os.environ.get("ALT_DB_PORT")
codebase = os.environ.get("CODEBASE_PATH")
print("Updating codebase repository...")
call("git pull".split(), shell=False, cwd=codebase)
repohash = subprocess.run(["git", "rev-parse", "--verify", "HEAD"],stdout=subprocess.PIPE, cwd=codebase)
repohash = repohash.stdout.decode('UTF-8')
# I'd like to do a hard reset and pull, but IN THEORY no changes should ever be
# made to the local repository
#
maps = []
connstr = "mysql+pymysql://{0}:{1}@{2}:{3}/{4}".format(ALT_DB_USER, ALT_DB_PASS, ALT_DB_HOST, ALT_DB_PORT, ALT_DB_NAME)
db = create_engine(connstr, pool_recycle=3600)
with db.connect() as con:
json_files = [pos_json for pos_json in os.listdir(codebase+'/_maps') if pos_json.endswith('.json')]
for j in json_files:
maps.append(json.load(open(codebase+'/_maps/'+j)))
query = text('INSERT INTO jobs (mapcount, maplist, repohash) VALUES (:mapcount, :maplist, :hash)')
result = con.execute(query, mapcount=len(json_files), maplist=json.dumps(maps), hash=repohash)
jobid = result.lastrowid
for j in json_files:
map = json.load(open(codebase+'/_maps/'+j))
command = "../SpacemanDMM/target/debug/dmm-tools minimap -o ../renders --pngcrush {0}".format('_maps/'+map['map_path']+'/'+map['map_file'])
print("Rendering {0}...".format(map['map_name']))
query = text('INSERT INTO renders (job, mapname, mapfile) VALUES (:job, :mapname, :mapfile)')
result = con.execute(query, job=jobid, mapname=map['map_name'], mapfile=map['map_file'])
renderid = result.lastrowid
print(command)
call(command.split(), shell=False, cwd=codebase)
tilecommand = "./tile.sh renders/{0}-1.png {1} 256 5".format(map['map_file'].replace('.dmm',''),map['map_name'].replace(' ',''))
print("Tiling {0}...".format(map['map_name']))
query = text('UPDATE renders SET ended = NOW() WHERE id = :renderid')
con.execute(query, renderid=renderid)
query = text('UPDATE jobs SET completed = completed+1 WHERE id = :jobid')
con.execute(query, jobid=jobid)
call(tilecommand.split(), shell=False)
query = text('UPDATE jobs SET finished = NOW() WHERE id = :jobid')
con.execute(query, jobid=jobid)