-
Notifications
You must be signed in to change notification settings - Fork 11
/
setup_battery_for_webserver.py
102 lines (83 loc) · 3.35 KB
/
setup_battery_for_webserver.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
from expfactory.battery import *
from expfactory.utils import copy_directory
from expfactory.vm import download_repo
import argparse
from glob import glob
import os
import sys
import shutil
import tempfile
# Get CLI parameters
parser = argparse.ArgumentParser()
parser.add_argument('--output', dest="output", help='Battery output directory.')
parser.add_argument('--experiments', dest="experiments", help='Experiment(s) to use in the battery, separated by commas.')
parser.add_argument('--workingdir', dest="workingdir", help='Working directory, existing GIT repos are used if present and are not removed after execution.',default=None)
try:
args = parser.parse_args()
except:
parser.print_help()
sys.exit(0)
if args.output == None or args.experiments == None:
print("Output and experiments parameters are mandatory.")
parser.print_help()
sys.exit(1)
if os.path.exists(args.output):
print("Output directory should not exist, remove and run again.")
sys.exit(1)
battery_dest = args.output
working_dir = args.workingdir
is_temp_working_dir = False
script_dirname=os.path.dirname(__file__)
if script_dirname == "":
script_dirname="."
if working_dir != None:
if not (os.path.isdir(working_dir)):
print("Working directory does not exist!")
sys.exit(1)
else:
is_temp_working_dir = True
working_dir = tempfile.mkdtemp() # We will set up the repo downloads, etc, in a temporary place
experiments = args.experiments.split(",")
# Download the missing repos to working directory
battery_repo='battery'
experiment_repo='experiments'
survey_repo='surveys'
game_repo='games'
print('Downloading expfactory repos...')
for repo in (battery_repo, experiment_repo, survey_repo, game_repo):
folder = "%s/%s" %(working_dir,repo)
if not (os.path.isdir(folder)):
download_repo(repo_type=repo,
destination=folder)
# Generate battery with the experiment(s)
print('Generating base...')
base = generate_base(battery_dest=battery_dest,
tasks=experiments,
experiment_repo="%s/%s" %(working_dir,experiment_repo),
survey_repo="%s/%s" %(working_dir,survey_repo),
game_repo="%s/%s" %(working_dir,game_repo),
add_experiments=True,
add_surveys=False,
add_games=False,
battery_repo="%s/%s" %(working_dir,battery_repo))
# Customize the battery
custom_variables = dict()
custom_variables["load"] = [("[SUB_TOTALTIME_SUB]", 30)]
template_exp = ("%s/templates/webserver-battery-template.html" %(script_dirname))
template_exp_output = "%s/index.html" %(battery_dest)
print('Generating experiment and battery templates...')
template_experiments(battery_dest=battery_dest,
battery_repo=base["battery_repo"],
valid_experiments=base["experiments"],
custom_variables=custom_variables,
template_exp=template_exp,
template_exp_output=template_exp_output)
# Copy needed php files there
add_files = glob("%s/*.php" %(script_dirname))
for add_file in add_files:
shutil.copy(add_file, battery_dest)
# Cleanup
shutil.rmtree("%s/.git" %(battery_dest)) # Remove git data from the battery
if is_temp_working_dir:
shutil.rmtree(working_dir)
print('Battery generated in %s' %(battery_dest))