-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDMP_tool_v2.py
More file actions
62 lines (44 loc) · 1.29 KB
/
Copy pathDMP_tool_v2.py
File metadata and controls
62 lines (44 loc) · 1.29 KB
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
"""
A data management plan tool to take stock of all files in a directory
tree.
Douglas McCulloch, October 2023
"""
import json
import os
import sys
import hashlib
# Setup parameters
ignore_hidden = 1
conda_override = 0 # sort of pointless because PATH varies in conda env's
json_filename = "DMP_list_test.json"
if os.environ["PATH"].find("Anaconda") > -1 and not conda_override:
dir_name = os.getcwd()
else: # assume we have executed from command line (input path)
dir_name = str(sys.argv[1])
# Hash files and add to dict
print("Hashing", dir_name, end="...")
file_d = { }
def hash_dir(cwd: str):
file_list = os.listdir(cwd)
file_d[cwd] = { }
for file in file_list:
if file[0] == '.' and ignore_hidden:
continue
filepath = cwd + os.sep + file
if os.path.isdir(filepath):
hash_dir(filepath)
continue
with open(filepath, "rb") as f:
file_d[cwd][file] = hashlib.file_digest(f, "md5").hexdigest()
hash_dir(dir_name)
print("done")
# Dump JSON to file
print("Writing to file", json_filename, end="...")
try:
json_f = open(json_filename, "wt")
except FileNotFoundError:
json_f = open(json_filename, "xt")
json.dump(file_d, json_f)
print("done")
# Clean up
json_f.close()