-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdircheck.py
83 lines (63 loc) · 2.16 KB
/
dircheck.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
import os
import re
import shutil
import psycopg2
from clean import files_are_old, get_path
import config
UUID_REGEX = re.compile(
r"[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}"
)
def connect():
"""Connect to PostgreSQL database"""
print("Connecting to the PostgreSQL database...")
return psycopg2.connect(
host=config.host,
user=config.user,
password=config.password,
dbname=config.dbname,
port=config.port,
)
def is_uuid(string):
"""retun True if string is a UUID"""
if UUID_REGEX.match(string):
return True
return False
def make_stage_list(path):
"""walk staging dir and create a list of all directories"""
stages = []
for root, dirs, files in os.walk(path):
for dir in dirs:
# only include directory names that are UUIDs
if is_uuid(dir):
stages.append(dir)
return stages
def check_db(dirs, db):
"""test a list of staging directories for presence in the database_is_safe
args:
dirs: list of directories to test
db: database connection
no return value, issues printed to stdout
"""
cursor = db.cursor()
for stage in dirs:
cursor.execute("SELECT * FROM staging WHERE stagingid = %s;", (stage,))
result = cursor.fetchone()
if result is None:
print("ATTN: folder {} is not in staging database table.".format(stage))
if not config.debug and files_are_old(stage):
print("Deleting {}".format(get_path(stage)))
# while these directories should all exist I still see
# "no such file or directory" errors when deleting them
shutil.rmtree(get_path(stage), ignore_errors=True)
else:
print("{} is present in the database.".format(stage))
cursor.close()
def main():
"""check that all staging directories are present in openEQUELLA database"""
stages = make_stage_list(config.filestore)
print("Testing {} directories...".format(len(stages)))
db = connect()
check_db(stages, db)
db.close()
if __name__ == "__main__":
main()