forked from vdt/deepdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
159 lines (124 loc) · 5.48 KB
/
run.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
import argparse
import json
import logging
import os
import subprocess
import sys
import csv
import traceback
import urllib
import urllib2
import zipfile
from sys import platform as _platform
import time
import utils
from constants import *
from enforce_version import enforce_version
# make AMI's for every region
# remove steam password
# reset system password on restart
from tail_logs import tail_caffe_logs
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
logger = logging.getLogger(__name__)
ALL_PROCESS_NAMES = [CAFFE_PROCESS_NAME, AUTOIT_PROCESS_NAME, GTAV_PROCESS_NAME]
def _start_gtav_command(_install_dir):
if IS_STEAM:
start_command = r'start steam://rungameid/271590'
else:
start_command = r'"{GTAV_DIR}/PlayGTAV.exe"'.format(GTAV_DIR=GTAV_DIR)
return start_command
def _start_obs_command(install_dir):
return r'"{install_dir}/OBS-new/rundir/OBS.exe"'.format(install_dir=install_dir)
def _start_autoit_command(install_dir):
return r'"{install_dir}/AutoIt3/AutoItX/AutoItX.exe"'.format(install_dir=install_dir)
def _gtav_is_running():
p_tasklist = subprocess.Popen('tasklist.exe /fo csv',
stdout=subprocess.PIPE,
universal_newlines=True)
pythons_tasklist = []
tasks = list(csv.DictReader(p_tasklist.stdout))
for p in tasks:
if p['Image Name'] == GTAV_PROCESS_NAME:
pythons_tasklist.append(p)
if len(pythons_tasklist) > 0:
return True
else:
return False
class GTAVRunner(object):
def __init__(self, install_dir, weights_path):
self.install_dir = install_dir
self.weights_path = weights_path
self.gtav_process = None
self.obs_process = None
self.caffe_process = None
self.autoit_process = None
self.processes = []
def _configure(self):
self.popen()
def _start_process(self, proc_name, get_cmd_fn, shell=True, cwd=None):
cmd = get_cmd_fn(self.install_dir)
logger.info(r'Starting {proc_name} with the following command: {cmd}'.format(proc_name=proc_name, cmd=cmd))
process = subprocess.Popen(cmd, shell=shell, cwd=cwd)
self.processes.append(process)
return process
def popen(self):
self._kill_competing_procs()
self.obs_process = self._start_process('OBS', _start_obs_command)
input('Press any key after ensuring GTAV is not open and hitting the "Preview Stream" button in OBS (you should see a black preview pane)')
self.gtav_process = self._start_process('GTAV', _start_gtav_command)
if IS_STEAM:
steam_message = ' (you may need to login to steam as well)'
else:
steam_message = ''
input('Start in normal mode if asked, then press any key after entering story mode %s, loading Franlkin and Lamar saved game from 5/14/16, and making sure the camera is on the hood (see the setup readme for more details)...' % steam_message)
caffe_work_dir = self.install_dir + '/caffe'
self.caffe_process = self._start_process('Caffe', self._start_caffe_command, shell=False, cwd=caffe_work_dir)
self.autoit_process = self._start_process('AutoIt', _start_autoit_command, shell=False)
pass
def _start_caffe_command(self, install_dir):
return [r'{install_dir}/caffe/build/x64/Release/caffeout.exe'.format(install_dir=install_dir), self.weights_path]
@staticmethod
def _kill_competing_procs():
subprocess.call("taskkill /IM %s /F" % OBS_PROCESS_NAME)
subprocess.call("taskkill /IM %s /F" % AUTOIT_PROCESS_NAME)
subprocess.call("taskkill /IM %s /F" % CAFFE_PROCESS_NAME)
subprocess.call("taskkill /IM %s /F" % GTAV_PROCESS_NAME)
subprocess.call("taskkill /IM GTAV* /F")
subprocess.call("taskkill /IM Steam* /F")
def popen_cleanup(self):
for proc in self.processes:
proc.kill()
proc.wait()
# Kill procs that we might have missed
self._kill_competing_procs()
def main():
default_weights_path = 'caffe_deep_drive_train_iter_35352.caffemodel'
parser = argparse.ArgumentParser(description=None)
parser.add_argument('-v', '--verbose', action='count', dest='verbosity', default=0, help='Set verbosity.')
parser.add_argument('-w', '--weights', default=default_weights_path, help='Path to caffemodel weights file - default is ' + default_weights_path)
args = parser.parse_args()
logging.basicConfig()
if args.verbosity == 0:
logger.setLevel(logging.INFO)
elif args.verbosity >= 1:
logger.setLevel(logging.DEBUG)
GTAVRunner._kill_competing_procs()
enforce_version(GTAV_DIR)
install_dir = utils.get_config()['install_dir']
runner = GTAVRunner(install_dir, args.weights)
runner.popen()
time.sleep(10) # Give some time for caffe to create a new log file
tail_caffe_logs()
while True:
if utils.processes_are_running(ALL_PROCESS_NAMES):
if 'GTAV_DEAD_MANS_SNITCH_URL' in os.environ: # i.e. https://nosnch.in/a69389848a
# Send heartbeat (for monitoring long running environments)
logging.info('Sending heartbeat')
try:
urllib2.urlopen(os.environ['GTAV_DEAD_MANS_SNITCH_URL']).read()
except Exception as e:
logging.error('Error sending heartbeat \n' + traceback.format_exc())
time.sleep(15 * 60)
if __name__ == '__main__':
sys.exit(main())