Skip to content

Commit 6f38981

Browse files
authored
fix local context with uploading files in the subdirectory (#300)
1 parent e4ac4f5 commit 6f38981

File tree

5 files changed

+7
-129
lines changed

5 files changed

+7
-129
lines changed

dpdispatcher/local_context.py

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
from dpdispatcher import dlog
66
from subprocess import TimeoutExpired
77

8-
# class LocalSession (object) :
9-
# def __init__ (self, jdata) :
10-
# self.work_path = os.path.abspath(jdata['work_path'])
11-
# assert(os.path.exists(self.work_path))
12-
13-
# def get_work_root(self) :
14-
# return self.work_path
158

169
class SPRetObj(object) :
1710
def __init__ (self,
@@ -60,18 +53,6 @@ def __init__(self,
6053
self.temp_local_root = os.path.abspath(local_root)
6154
self.temp_remote_root = os.path.abspath(remote_root)
6255
self.remote_profile = remote_profile
63-
# self.work_profile = work_profile
64-
# self.job_uuid = job_uuid
65-
# self.submission = None
66-
# if job_uuid:
67-
# self.job_uuid = job_uuid
68-
# else:
69-
# self.job_uuid = str(uuid.uuid4())
70-
71-
# self.remote_root = os.path.join(work_profile.get_work_root(), self.job_uuid)
72-
# dlog.debug("local_root is %s"% local_root)
73-
74-
# os.makedirs(self.remote_root, exist_ok = True)
7556

7657
@classmethod
7758
def load_from_dict(cls, context_dict):
@@ -92,34 +73,9 @@ def bind_submission(self, submission):
9273
self.submission = submission
9374
self.local_root = os.path.join(self.temp_local_root, submission.work_base)
9475
self.remote_root = os.path.join(self.temp_remote_root, submission.submission_hash)
95-
# print('debug:LocalContext.bind_submission', submission.submission_hash,
96-
# self.local_root, self.remote_root)
97-
98-
# os.makedirs(self.remote_root, exist_ok = True)
99-
# self.job_uuid = submission.submission_hash
100-
# self.remote_root = os.path.join(self.work_profile.get_work_root(), self.job_uuid)
101-
# os.makedirs(self.remote_root, exist_ok = True)
102-
# print('local_context.bind_submission:self.remote_root', self.remote_root)
103-
# dlog.debug("remote_root is %s"% self.remote_root)
104-
105-
# @property
106-
# def remote_root(self):
107-
# print('local_context.remote_root:self.submission.submission_hash', self.submission.submission_hash)
108-
# print('local_context.remote_root self.submission', self.submission)
109-
# self._remote_root = os.path.join(self.work_profile.get_work_root(), self.submission.submission_hash, self.submission.work_base)
110-
# os.makedirs(self._remote_root, exist_ok = True)
111-
# return self._remote_root
112-
113-
# @property
114-
# def local_root(self):
115-
# # self.local_root = os.path.abspath(local_root)
116-
# self._local_root = os.path.join(, self.submission.submission_hash, self.submission.work_base)
117-
# return self._local_root
11876

11977
def upload(self, submission):
12078
os.makedirs(self.remote_root, exist_ok = True)
121-
# os.makedirs(self.remote_root, exist_ok = True)
122-
# job_dirs = [ ii.task_work_path for ii in submission.belonging_tasks]
12379
for ii in submission.belonging_tasks:
12480
local_job = os.path.join(self.local_root, ii.task_work_path)
12581
remote_job = os.path.join(self.remote_root, ii.task_work_path)
@@ -138,13 +94,12 @@ def upload(self, submission):
13894
raise RuntimeError('cannot find upload file ' + os.path.join(local_job, jj))
13995
if os.path.exists(os.path.join(remote_job, jj)) :
14096
os.remove(os.path.join(remote_job, jj))
141-
_check_file_path(jj)
97+
_check_file_path(os.path.join(remote_job, jj))
14298
os.symlink(os.path.join(local_job, jj),
14399
os.path.join(remote_job, jj))
144100

145101
local_job = self.local_root
146102
remote_job = self.remote_root
147-
# os.makedirs(remote_job, exist_ok = True)
148103

149104
file_list = []
150105
for kk in submission.forward_common_files:
@@ -159,39 +114,19 @@ def upload(self, submission):
159114
raise RuntimeError('cannot find upload file ' + os.path.join(local_job, jj))
160115
if os.path.exists(os.path.join(remote_job, jj)) :
161116
os.remove(os.path.join(remote_job, jj))
162-
_check_file_path(jj)
117+
_check_file_path(os.path.join(remote_job, jj))
163118
os.symlink(os.path.join(local_job, jj),
164119
os.path.join(remote_job, jj))
165120

166-
def upload_(self,
167-
job_dirs,
168-
local_up_files,
169-
dereference = True) :
170-
for ii in job_dirs :
171-
local_job = os.path.join(self.local_root, ii)
172-
remote_job = os.path.join(self.remote_root, ii)
173-
os.makedirs(remote_job, exist_ok = True)
174-
for jj in local_up_files :
175-
if not os.path.exists(os.path.join(local_job, jj)):
176-
raise RuntimeError('cannot find upload file ' + os.path.join(local_job, jj))
177-
if os.path.exists(os.path.join(remote_job, jj)) :
178-
os.remove(os.path.join(remote_job, jj))
179-
_check_file_path(jj)
180-
os.symlink(os.path.join(local_job, jj),
181-
os.path.join(remote_job, jj))
182-
183-
184121
def download(self,
185122
submission,
186123
check_exists = False,
187124
mark_failure = True,
188125
back_error=False) :
189126

190127
for ii in submission.belonging_tasks:
191-
# for ii in job_dirs :
192128
local_job = os.path.join(self.local_root, ii.task_work_path)
193129
remote_job = os.path.join(self.remote_root, ii.task_work_path)
194-
# flist = remote_down_files
195130
flist = ii.backward_files
196131
if back_error :
197132
flist += glob(os.path.join(remote_job, 'error*'))
@@ -227,18 +162,11 @@ def download(self,
227162
elif os.path.isfile(lfile) or os.path.islink(lfile):
228163
os.remove(lfile)
229164
shutil.copyfile(rfile, lfile)
230-
# shutil.move(rfile, lfile)
231165
else :
232166
raise RuntimeError('should not reach here!')
233167
else :
234168
# no nothing in the case of linked files
235169
pass
236-
# for ii in submission.belonging_tasks:
237-
# for ii in job_dirs :
238-
# local_job = os.path.join(self.local_root, ii.task_work_path)
239-
# remote_job = os.path.join(self.remote_root, ii.task_work_path)
240-
# flist = remote_down_files
241-
# flist = ii.backward_files
242170
local_job = self.local_root
243171
remote_job = self.remote_root
244172
flist = submission.backward_common_files
@@ -274,61 +202,12 @@ def download(self,
274202
elif os.path.isfile(lfile) or os.path.islink(lfile):
275203
os.remove(lfile)
276204
shutil.copyfile(rfile, lfile)
277-
# shutil.move(rfile, lfile)
278205
else :
279206
raise RuntimeError('should not reach here!')
280207
else :
281208
# no nothing in the case of linked files
282209
pass
283210

284-
285-
286-
def download_(self,
287-
job_dirs,
288-
remote_down_files,
289-
check_exists = False,
290-
mark_failure = True,
291-
back_error=False) :
292-
for ii in job_dirs :
293-
local_job = os.path.join(self.local_root, ii)
294-
remote_job = os.path.join(self.remote_root, ii)
295-
flist = remote_down_files
296-
if back_error :
297-
flist += glob(os.path.join(remote_job, 'error*'))
298-
for jj in flist :
299-
rfile = os.path.join(remote_job, jj)
300-
lfile = os.path.join(local_job, jj)
301-
if not os.path.realpath(rfile) == os.path.realpath(lfile) :
302-
if (not os.path.exists(rfile)) and (not os.path.exists(lfile)):
303-
if check_exists:
304-
if mark_failure:
305-
with open(os.path.join(self.local_root, ii, 'tag_failure_download_%s' % jj), 'w') as fp:
306-
pass
307-
else :
308-
pass
309-
else :
310-
raise RuntimeError('do not find download file ' + rfile)
311-
elif (not os.path.exists(rfile)) and (os.path.exists(lfile)) :
312-
# already downloaded
313-
pass
314-
elif (os.path.exists(rfile)) and (not os.path.exists(lfile)) :
315-
# trivial case, download happily
316-
shutil.move(rfile, lfile)
317-
elif (os.path.exists(rfile)) and (os.path.exists(lfile)) :
318-
# both exists, replace!
319-
dlog.info('find existing %s, replacing by %s' % (lfile, rfile))
320-
if os.path.isdir(lfile):
321-
shutil.rmtree(lfile, ignore_errors=True)
322-
elif os.path.isfile(lfile) or os.path.islink(lfile):
323-
os.remove(lfile)
324-
shutil.move(rfile, lfile)
325-
else :
326-
raise RuntimeError('should not reach here!')
327-
else :
328-
# no nothing in the case of linked files
329-
pass
330-
331-
332211
def block_checkcall(self,
333212
cmd) :
334213
proc = sp.Popen(cmd, cwd=self.remote_root, shell=True, stdout = sp.PIPE, stderr = sp.PIPE)
@@ -351,9 +230,6 @@ def block_call(self, cmd) :
351230
def clean(self):
352231
shutil.rmtree(self.remote_root, ignore_errors=True)
353232

354-
# def _clean(self) :
355-
# shutil.rmtree(self.remote_root, ignore_errors=True)
356-
357233
def write_file(self, fname, write_str):
358234
os.makedirs(self.remote_root, exist_ok = True)
359235
with open(os.path.join(self.remote_root, fname), 'w') as fp :

tests/test_context_dir/0_md/bct-1/some_dir/some_file

Whitespace-only changes.

tests/test_context_dir/0_md/some_dir/some_file

Whitespace-only changes.

tests/test_lazy_local_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_download(self):
3636
def test_block_call(self):
3737
code, stdin, stdout, stderr = self.lazy_local_context.block_call('ls')
3838
self.assertEqual(stdout.readlines(), ['bct-1\n',
39-
'bct-2\n', 'bct-3\n', 'bct-4\n', 'dir with space\n', 'graph.pb\n'])
39+
'bct-2\n', 'bct-3\n', 'bct-4\n', 'dir with space\n', 'graph.pb\n', 'some_dir\n'])
4040
self.assertEqual(code, 0)
4141

4242
code, stdin, stdout, stderr = self.lazy_local_context.block_call('ls a')

tests/test_local_context.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ def test_upload(self):
6767
submission_hash = 'mock_hash_2'
6868
task1 = MagicMock(
6969
task_work_path='bct-1/',
70-
forward_files=['input.lammps', 'conf.lmp']
70+
forward_files=['input.lammps', 'conf.lmp', 'some_dir/some_file']
7171
)
7272
task2 = MagicMock(
7373
task_work_path='bct-2/',
7474
forward_files=['input.lammps', 'conf.lmp']
7575
)
7676
submission = MagicMock(work_base='0_md/',
7777
belonging_tasks=[task1, task2],
78-
forward_common_files=['graph.pb'],
78+
forward_common_files=['graph.pb', 'some_dir/some_file'],
7979
submission_hash=submission_hash)
8080

8181
self.local_context.bind_submission(submission)
@@ -84,9 +84,11 @@ def test_upload(self):
8484
check_file_list = [
8585
'bct-1/input.lammps',
8686
'bct-1/conf.lmp',
87+
'bct-1/some_dir/some_file',
8788
'bct-2/input.lammps',
8889
'bct-2/conf.lmp',
8990
'graph.pb',
91+
'some_dir/some_file',
9092
]
9193
for file in check_file_list:
9294
f1 = os.path.join(self.tmp_local_root, '0_md/', file)

0 commit comments

Comments
 (0)