forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_artifact_paths.py
45 lines (33 loc) · 1.28 KB
/
sync_artifact_paths.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
#!/usr/bin/python
import argparse
import os
import yaml
parser = argparse.ArgumentParser(
description='Reorganize an artifact directory so paths match with names.')
parser.add_argument('directory', type=str,
help="The base path to inspect.")
def main():
args = parser.parse_args()
root = args.directory
file_info = dict()
for root, dirs, files in os.walk(root, topdown=False):
for name in files:
path_name = os.path.join(root, name)
if path_name.endswith(".yaml") or path_name.endswith(".yml"):
with open(path_name) as fd:
definition = yaml.safe_load(fd)
name = definition['name']
if name in file_info:
raise TypeError("Files %s and %s contain artifact named %s" % (
path_name, file_info[name], name))
file_info[name] = path_name
for name, path_name in file_info.items():
correct_path = os.path.join(root, *name.split(".")) + ".yaml"
print (correct_path)
directory = os.path.dirname(correct_path)
try:
os.makedirs(directory)
except: pass
os.rename(path_name, correct_path)
if __name__ == '__main__':
main()