-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrument_source.py
67 lines (59 loc) · 2.24 KB
/
instrument_source.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
# coding: utf-8
import logging
import os
import pathlib
import argparse
import subprocess
import shutil
logging.basicConfig(level=logging.INFO)
ROOT_DIR = pathlib.Path(__file__).parent
INSTRUMENT_DIR = ROOT_DIR / 'instruments'
MXNET_DIR = ROOT_DIR / 'mxnet'
def _mkdir(newdir):
"""
works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
"""
if type(newdir) is not str:
newdir = str(newdir)
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired " \
"dir, '%s', already exists." % newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
_mkdir(head)
if tail:
os.mkdir(newdir)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--base-dir', type=str, default='common',
help='directory containing base instrumentation source')
parser.add_argument('-e', '--extra-dir', type=str,
help='directory containing extra instrumentation source')
args, unknown = parser.parse_known_args()
return args
def copy_files(instrument_dir):
for dir_path, dirs, files in os.walk(instrument_dir):
for file in files:
full_path = f'{dir_path}/{file}'
try:
target_dir = dir_path.replace(instrument_dir, str(MXNET_DIR))
_mkdir(target_dir) # ensure target dir exists
shutil.copyfile(full_path, f'{target_dir}/{file}')
logging.info(f'Copied {dir_path.replace(instrument_dir, "")}/{file}.')
except Exception as e:
logging.warning(f'Error copying {dir_path.replace(instrument_dir, "")}/{file}.')
logging.warning('{}'.format(e))
def instrument_source():
args = get_args()
logging.info('Start walking in instrumentation directory.')
copy_files(f'{INSTRUMENT_DIR}/{args.base_dir}')
if args.extra_dir:
copy_files(f'{INSTRUMENT_DIR}/{args.extra_dir}')
if __name__ == '__main__':
instrument_source()