Bug #8805 ยป 8805walk.py
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
from __future__ import print_function |
4 |
|
5 |
import os |
6 |
import sys |
7 |
|
8 |
import arvados |
9 |
|
10 |
implementation = arvados.getjobparam('implementation') |
11 |
if implementation == 'shutil': |
12 |
print("Using shutil copy implementation", file=sys.stderr) |
13 |
import shutil |
14 |
def mkdir(path): |
15 |
if not os.path.isdir(path): |
16 |
os.mkdir(path) |
17 |
copyfile = shutil.copyfile |
18 |
else: |
19 |
print("Using builtin copy implementation", file=sys.stderr) |
20 |
import errno |
21 |
def mkdir(path): |
22 |
try: |
23 |
os.mkdir(out_dir) |
24 |
except OSError as error: |
25 |
if error.errno != errno.EEXIST: |
26 |
raise
|
27 |
def copyfile(in_path, out_path): |
28 |
with open(in_path) as in_file, open(out_path, 'w') as out_file: |
29 |
for file_data in iter(lambda: in_file.read(1024), ''): |
30 |
out_file.write(file_data) |
31 |
|
32 |
task = arvados.current_task() |
33 |
out_root = task.tmpdir |
34 |
|
35 |
os.chdir(os.environ['CRUNCH_SRC']) |
36 |
for subdir, _, files in os.walk('.'): |
37 |
print("Traversing {!r}".format(subdir), file=sys.stderr) |
38 |
out_dir = os.path.join(out_root, subdir) |
39 |
mkdir(out_dir) |
40 |
for basename in files: |
41 |
print("Writing {!r}".format(basename), file=sys.stderr) |
42 |
copyfile(os.path.join(subdir, basename), os.path.join(out_dir, basename)) |
43 |
|
44 |
task.set_output('') |