-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_files.py
executable file
·41 lines (36 loc) · 1.77 KB
/
copy_files.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
#!/usr/bin/env python3
import os, argparse, csv, shutil
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Copy files by the CSV output of a collector, where name, exe, sim_files are required')
parser.add_argument('dst', help='destination directory')
parser.add_argument('--csv', required=True, help='input CSV file describing the mappings')
parser.add_argument('-o', '--output', required=True, help='simplified CSV file by removing the dir name of files')
args = parser.parse_args()
with open(args.csv, 'r') as csv_file, open(args.output, 'w') as output_file:
csv_reader = csv.DictReader(csv_file)
for f in ['name', 'exe', 'sim_files']:
assert f in csv_reader.fieldnames, f'cannot find field {f}'
csv_writer = csv.DictWriter(output_file, fieldnames=csv_reader.fieldnames)
csv_writer.writeheader()
exe_from_to = {}
for row in csv_reader:
new_row = row.copy()
sim_files = row['sim_files'].split(',')
exe = row['exe']
new_row['exe'] = os.path.basename(exe)
new_row['sim_files'] = ','.join([os.path.basename(sim_file) for sim_file in sim_files])
sub_dir = os.path.join(args.dst, new_row['name'])
if os.path.isdir(sub_dir):
shutil.rmtree(sub_dir)
os.mkdir(sub_dir)
to_exe = os.path.join(sub_dir, os.path.basename(exe))
# Avoid duplicated copies
if exe in exe_from_to:
os.link(exe_from_to[exe], to_exe)
else:
shutil.copyfile(exe, to_exe)
exe_from_to[exe] = to_exe
for sim_file in sim_files:
shutil.copy(sim_file, sub_dir)
csv_writer.writerow(new_row)