-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
133 lines (114 loc) · 4.55 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
# -*- coding: utf-8 -*-
#
# This file is part of the VecNet OpenMalaria Portal.
# For copyright and licensing information about this package, see the
# NOTICE.txt and LICENSE.txt files in its top-level directory; they are
# available at https://github.com/vecnet/om
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License (MPL), version 2.0. If a copy of the MPL was not distributed
# with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import subprocess
import uuid
import sys
import shutil
import logging
if __name__ == "__main__": # pragma: no cover
# This is a code to use this script as a standalone python program
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings.production")
import django
django.setup()
# Ignore PEP8 warning, the code above is necessary to use Django models in a standalone script
from website.apps.ts_om.models import Simulation
from django.conf import settings
def run(simulation):
assert isinstance(simulation, Simulation)
# Prepare input files
base_dir = os.path.dirname(os.path.abspath(__file__))
input_dir = "%s-%s" %(simulation.id, str(uuid.uuid4()))
if hasattr(settings, "SIM_SERVICE_LOCAL_INPUT_DIR"):
input_dir = os.path.join(settings.SIM_SERVICE_LOCAL_INPUT_DIR,input_dir)
os.makedirs(input_dir)
logging.basicConfig(filename=os.path.join(input_dir, 'error.log'), level=logging.DEBUG)
logging.debug("Working directory: %s" % input_dir)
xml = simulation.input_file.read().decode("utf-8")
fp = open(os.path.join(input_dir, "scenario.xml"),"w+")
fp.write(xml)
fp.close()
shutil.copy2(os.path.join(base_dir, "sim_services_local", "files", "scenario_32.xsd"), os.path.join(input_dir, "scenario_32.xsd"))
shutil.copy2(os.path.join(base_dir, "sim_services_local", "files", "densities.csv"), os.path.join(input_dir, "densities.csv"))
# Run OpenMalaria model
simulation.status = Simulation.RUNNING
simulation.cwd = input_dir
simulation.save(update_fields=["status", "cwd"])
input_file_path = os.path.join(input_dir, "scenario.xml")
stdout = open(os.path.join(input_dir, "stdout.txt"), 'w')
try:
logging.debug("Running openmalaria.exe")
exitcode = subprocess.call(
"%s -s scenario.xml" % settings.OM_EXECUTABLE,
shell=True,
cwd=input_dir,
stdout=stdout,
stderr=stdout
)
logging.debug("Openmalaria execution complete")
stdout.flush()
stdout.close()
if exitcode != 0:
logging.debug("Exit code: %s" % exitcode)
try:
fp = open(os.path.join(input_dir, "stdout.txt"))
stdout_contents = fp.read()
fp.close()
except IOError:
logging.warn("No stdout.txt file")
else:
logging.debug("Saving stdout.txt")
simulation.set_model_stdout(stdout_contents)
logging.debug("stdout.txt saved successfully")
simulation.status = Simulation.FAILED
simulation.last_error_message = "Exit code: %s" % exitcode
simulation.save()
return
except Exception as e:
logging.debug(e)
simulation.status = Simulation.FAILED
simulation.last_error_message = "%s" % e
simulation.save()
return
try:
try:
fp = open(os.path.join(input_dir, "ctsout.txt"))
ctsout_contents = fp.read()
fp.close()
simulation.set_ctsout_file(ctsout_contents)
except IOError:
logging.warn("No ctsout.txt file")
try:
with open(os.path.join(input_dir, "output.txt")) as fp:
simulation.set_output_file(fp)
except IOError:
logging.warn("No output.txt file")
try:
fp = open(os.path.join(input_dir, "stdout.txt"))
stdout_contents = fp.read()
fp.close()
simulation.set_model_stdout(stdout_contents)
except IOError:
logging.warn("No stdout.txt file")
simulation.status = Simulation.COMPLETE
simulation.last_error_message = ""
simulation.save()
except Exception as e:
logging.debug(str(e))
simulation.status = Simulation.FAILED
simulation.last_error_message = "I/O Error"
simulation.save()
def main(sim_id):
simulation = Simulation.objects.get(id=sim_id)
run(simulation)
if __name__ == "__main__": # pragma: no cover
sim_id = int(sys.argv[1])
main(sim_id)