-
Notifications
You must be signed in to change notification settings - Fork 3
/
deleter.py
78 lines (64 loc) · 2.81 KB
/
deleter.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
import shutil
import os
import codecs
import json
"""
Using the list of active pages generated by compile_active from the API,
deleter removes whatever isn't accounted for online.
Warning: anything in a category folder that isn't in the compile_active list will be deleted.
"""
MIRROR = 'archive'
class Deleter:
"""
A Deleter class for deleting folders in the archive of pages that have been deleted on the OSF.
A CLI is designed to work with this deleter.
Basic Workflow:
Init -> compare archive to mirror -> delete folders in archive
"""
def __init__(self, json_filename):
"""
Constructor for Deleter class
Generates three attributes to the deleter
1. A list of all active projects
2. A list of all active registrations
3. A list of all active users
:param json_filename: Name of the json file generated from compile_active
"""
with codecs.open(json_filename, mode='r', encoding='utf-8') as file:
active_lists = json.load(file)
self.active_node_guids = active_lists["list_of_active_nodes"]
self.active_registration_guids = active_lists["list_of_active_registrations"]
self.active_user_guids = active_lists["list_of_active_users"]
def compare_to_mirror(self, osf_type, active_list):
"""
Compares the archive to the active nodes from the OSF and deletes the folders of guids that are
present in the archive but no longer active on the OSF.
:param osf_type: Whether the list is of registrations, projects, or user profiles
:param active_list: The list of active guids
"""
mirror_list = os.listdir(MIRROR + '/' + osf_type)
print("OSF type: " + osf_type)
for subdir in mirror_list:
print("Checking", subdir)
if subdir not in active_list:
print(subdir, "inactive. Deleting")
subdir_path = '/'.join([MIRROR, osf_type, subdir])
self.delete_directory(subdir_path)
def delete_directory(self, directory_path):
"""
Deletes a given directory
:param directory_path: The path to the directory to be deleted
"""
print(directory_path)
if os.path.isdir(directory_path):
shutil.rmtree(directory_path)
print("Deleted", directory_path)
def run(self):
"""
CLI Endpoint for deleter
Controls workflow
runs deleter on projects, registrations, and user profiles
"""
self.compare_to_mirror('project', self.active_node_guids)
self.compare_to_mirror('registration', self.active_registration_guids)
self.compare_to_mirror('profile', self.active_user_guids)