-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkmd5.py
182 lines (163 loc) · 5.56 KB
/
checkmd5.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""\
Run check and update of md5sum identical files.
Example.
%(prog)s --username ACCESS_KEY_ID --password SECRET_ACCESS_KEY \\
--out experiments_statuses.log https://www.encodeproject.org
"""
import datetime
import json
import sys
import subprocess
from collections import defaultdict
from urllib.parse import urljoin
import requests
from slackclient import SlackClient
EPILOG = __doc__
def run(out, url, username, password, bot_token=None, dry_run=False):
session = requests.Session()
session.auth = (username, password)
session.headers['Accept'] = 'application/json'
dr = ""
if dry_run:
dr = "-- Dry Run"
version = '0.01'
try:
ip_output = subprocess.check_output(
['hostname'], stderr=subprocess.STDOUT
).strip()
ip = ip_output.decode(errors='replace').rstrip('\n')
except subprocess.CalledProcessError:
ip = ''
initiating_run = (
'STARTING matching md5sum files detection, version {} ({}) ({}): {} at {}'
).format(
version,
url,
dr,
ip,
datetime.datetime.now()
)
out.write(initiating_run + '\nFile uuid\tmd5sum\tMatching md5sum files\n')
out.flush()
if bot_token:
sc = SlackClient(bot_token)
sc.api_call(
"chat.postMessage",
channel="#bot-reporting",
text=initiating_run,
as_user=True,
)
graph = []
r = session.get(
urljoin(
url,
'/search/?type=File&field=external_accession&field=accession&field=uuid&field=status&field=md5sum&field=matching_md5sum&limit=all&format=json'
)
)
try:
r.raise_for_status()
except requests.HTTPError:
return
else:
graph = r.json()['@graph']
accession_to_uuid = {}
for f in graph:
if (f.get('accession')):
accession_to_uuid[f.get('accession')] = f.get('uuid')
else:
accession_to_uuid[f.get('external_accession')] = f.get('uuid')
excluded_statuses = ['uploading', 'upload failed', 'content error']
md5dictionary = defaultdict(set)
clashing_dictionary = defaultdict(list)
for f in graph:
if f.get('status') not in excluded_statuses:
md5 = f.get('md5sum')
if md5:
md5dictionary[md5].add(f.get('uuid'))
matching_md5sum = f.get('matching_md5sum')
if matching_md5sum:
matching_md5sum_uuids = [
entry.split('/')[2]
if (len(entry.split('/')[2]) == 36
and (entry.split('/')[2]).find('-') != -1)
else accession_to_uuid.get(entry.split('/')[2]) for entry in matching_md5sum
]
clashing_dictionary[f.get('uuid')] = sorted(matching_md5sum_uuids)
for key, value in md5dictionary.items():
if len(value) > 1:
uuids_list = sorted(list(value))
for uuid in uuids_list:
identical_files_list = [
entry for entry in uuids_list if entry != uuid
]
if uuid not in clashing_dictionary or sorted(clashing_dictionary[uuid]) != sorted(identical_files_list):
item_url = urljoin(url, uuid)
data = {
"matching_md5sum": identical_files_list,
}
if not dry_run:
r = session.patch(
item_url,
data=json.dumps(data),
headers={
'content-type': 'application/json',
'accept': 'application/json',
},
)
if not r.ok:
print('{} {}\n{}'.format(r.status_code, r.reason, r.text))
else:
out.write(
'{}\tmd5:{}\t{}\n'.format(
uuid,
key,
identical_files_list,
)
)
out.flush()
finishing_run = 'FINISHED matching md5sum files detection at {}'.format(
datetime.datetime.now()
)
out.write(finishing_run + '\n')
out.flush()
out.close()
if bot_token:
sc.api_call(
"chat.postMessage",
channel="#bot-reporting",
text=finishing_run,
as_user=True,
)
def main():
import argparse
parser = argparse.ArgumentParser(
description="Update files with identical MD5 values",
epilog=EPILOG,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
'--username', '-u', default='', help="HTTP username (access_key_id)"
)
parser.add_argument(
'--bot-token', default='', help="Slack bot token"
)
parser.add_argument(
'--password', '-p',
default='',
help="HTTP password (secret_access_key)",
)
parser.add_argument(
'--out', '-o', type=argparse.FileType('w'),
default=sys.stdout,
help="file to write json lines of results",
)
parser.add_argument(
'--dry-run',
action='store_true',
help="Don't update status, just check",
)
parser.add_argument('url', help="server to post to")
args = parser.parse_args()
run(**vars(args))
if __name__ == '__main__':
main()