-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert-pickle-db.py
executable file
·56 lines (42 loc) · 1.47 KB
/
convert-pickle-db.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
#!/usr/bin/env python
""" Convert metdata saved as .pickle (old format) to json (the new format).
Use this if there are .pickle files in your db/ folder
.. codeauthor:: Intra2net AG <[email protected]>
"""
import sys
import os
from os.path import isfile
import pickle
from pyfile.db import set_stored_metadata, get_stored_files
def main():
"""
Main function, called when running file as script
see module doc for more info
"""
# get all names of files (except .json, .source.txt) in db
# (luckily, there is no .pickle nor .json as entry in db)
entries = get_stored_files("db")
# loop over all entries
n_errors = 0
n_converted = 0
for entry in entries:
# continue if there is no pickle with saved metadata
old_filename = entry + '.pickle'
if not isfile(old_filename):
continue
try:
# unpickle stored metadata, save as json
with open(old_filename, 'r') as file_handle:
metadata = pickle.load(file_handle)
set_stored_metadata(entry, metadata)
# remove pickle
os.unlink(old_filename)
n_converted += 1
except Exception as exc:
print('Could not convert {}: {}'.format(entry, exc))
n_errors += 1
print('Converted {} entries, {} conversion attempts failed'
.format(n_converted, n_errors))
return 1 if n_errors else 0
if __name__ == '__main__':
sys.exit(main())