-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript_tools.py
267 lines (218 loc) · 6.61 KB
/
script_tools.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
import tempfile
from contextlib import contextmanager
from os.path import join
import time
from fabric.api import local
from gtd.io import shell
from gtd.utils import Config
def bash_string(s):
"""Wrap a string in double quotes and escape things inside."""
s = s.replace('\\', '\\\\') # \ -> \\
s = s.replace('\"', '\\\"') # " -> \"
return '\"{}\"'.format(s) # s -> "s"
def check_cwd_is_repo_root():
"""Check that script is being called from root of Git repo."""
cwd = os.getcwd()
cwd_name = os.path.split(cwd)[1]
if not os.path.exists(join(cwd, '.git')) or cwd_name != 'wge':
raise RuntimeError('Must run script from root of the Git repository.')
def upload_code(worksheet, debug=False):
"""Upload source code."""
check_cwd_is_repo_root()
import git
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
with open('git-metadata-codalab.txt', 'w') as f:
f.write(sha + '\n')
if repo.is_dirty():
f.write('dirty repo\n')
print '\nUploading new source code:'
up_cmd = ("cl upload -w {} -n wge -x .git docs downloads "
"launch_jobs.sh LICENSE README.md requirements.txt "
"script_tools.py venv third-party/miniwob-demos -- .").format(worksheet)
if debug:
print up_cmd
else:
local(up_cmd)
def upload_demos(worksheet, debug=False):
"""Upload demonstrations and make a new rl_data bundle."""
check_cwd_is_repo_root()
up_cmd = 'cl upload -w {} -n demonstrations third-party/miniwob-demos'.format(
worksheet)
make_cmd = 'cl make -w {} -n rl_data glove:glove nltk:nltk demonstrations:demonstrations'.format(
worksheet)
print '\nUploading new demonstrations:'
if debug:
print up_cmd
print make_cmd
else:
local(up_cmd)
local(make_cmd)
time.sleep(5)
def create_worksheet(worksheet):
"""Create worksheet if it doesn't already exist."""
try:
local('cl work {}'.format(worksheet), capture=True)
except BaseException:
# create new worksheet
local('cl new {}'.format(worksheet))
# give it a title
local('cl wedit -t {w} {w}'.format(w=worksheet))
# link to worksheet from home page
local('cl add worksheet {} wge'.format(worksheet))
# grant permissions
local('cl wperm {} wge all'.format(worksheet))
print 'Created worksheet: {}'.format(worksheet)
miniwob_easy = [
"choose-list",
"click-button",
"click-button-sequence",
"click-collapsible-nodelay",
"click-collapsible-2-nodelay",
"click-dialog",
"click-dialog-2",
"click-link",
"click-option",
"click-pie-nodelay",
"click-tab",
"click-test",
"click-test-2",
"click-widget",
"enter-date",
"enter-password",
"enter-text",
"enter-text-dynamic",
"focus-text",
"focus-text-2",
"grid-coordinate",
"login-user",
"navigate-tree",
"use-autocomplete-nodelay",
"click-color",
"click-shape",
"count-shape",
"identify-shape"
]
miniwob_hard = [
'book-flight-nodelay',
'choose-date-nodelay',
'search-engine',
'guess-number',
'click-shades',
'tic-tac-toe',
'enter-time',
'click-checkboxes',
'social-media',
'email-inbox',
'click-tab-2',
'use-spinner',
]
miniwob_stochastic = [
'click-checkboxes-large',
'click-checkboxes-soft',
'click-checkboxes-transfer',
'click-tab-2-hard',
'social-media-all',
'social-media-some',
'login-user-popup',
'multi-layouts',
'multi-orderings',
]
miniwob_nl = [
'email-inbox-nl-turk',
]
formwob = [
'flight.Alaska',
'flight.Alaska-auto',
#'flight.Alaska-auto-medium',
]
few_shot = [
'flight.Alaska-auto',
'enter-time',
'social-media',
'email-inbox-nl-turk',
'click-tab-2-hard',
'click-checkboxes-soft',
'click-checkboxes-large',
]
all_unique = lambda s: len(s) == len(set(s))
assert all_unique(miniwob_easy)
assert all_unique(miniwob_hard)
assert len(set(miniwob_easy) & set(miniwob_hard)) == 0 # no overlap
assert len(miniwob_hard) + len(miniwob_easy) == 40 # total tasks
task_lists = {
'miniwob-easy': miniwob_easy,
'miniwob-hard': miniwob_hard,
'miniwob-stoch': miniwob_stochastic,
'miniwob-nl': miniwob_nl,
'formwob': formwob,
'few-shot': few_shot,
}
# marker class
class BundleSpec(object):
pass
class UUIDBundleSpec(BundleSpec):
def __init__(self, uuid):
self.uuid = uuid
class NameBundleSpec(BundleSpec):
def __init__(self, worksheet, name):
self.worksheet = worksheet
self.name = name
@contextmanager
def open_cl_file(bundle_spec, bundle_path):
"""Get the raw file content within a particular bundle at a particular path.
Args:
bundle_spec (BundleSpec)
bundle_path (str): path inside the bundle. Has no leading slash.
"""
def save_file(path):
if isinstance(bundle_spec, UUIDBundleSpec):
cmd = 'cl down -o {} {}/{}'.format(path,
bundle_spec.uuid,
bundle_path)
elif isinstance(bundle_spec, NameBundleSpec):
cmd = 'cl down -o {} -w {} {}/{}'.format(path,
bundle_spec.worksheet,
bundle_spec.name,
bundle_path)
shell(cmd)
with open_in_temp_file(save_file) as f:
yield f
@contextmanager
def open_in_temp_file(save_file):
"""Open a file at a temporary path.
Args:
save_file (Callable[str]): takes a file path, and saves a file
at that path.
"""
# create temporary file just so we can get an unused file path
f = tempfile.NamedTemporaryFile()
f.close() # close and delete right away
fname = f.name
# try to save file at the temporary path
try:
save_file(fname)
except RuntimeError:
try:
os.remove(fname) # if file was created, remove it
except OSError:
pass
raise IOError('Failed to open file')
# open the temporary file
f = open(fname, 'r')
yield f
f.close()
os.remove(fname) # delete temp file
def get_metadata(bundle_spec):
"""
Args:
bundle_spec (BundleSpec)
Returns:
Config
"""
path = 'data/experiments/0_unnamed/metadata.txt'
with open_cl_file(bundle_spec, path) as f:
f_path = os.path.realpath(f.name)
metadata = Config.from_file(f_path)
return metadata