Total Complexity | 3 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import os |
||
2 | |||
3 | from django.conf import settings |
||
4 | from django.core.management import BaseCommand, call_command |
||
5 | |||
6 | from uid.models import Submission |
||
7 | |||
8 | |||
9 | class Command(BaseCommand): |
||
10 | help = """ |
||
11 | Deletes orphaned backup files: files in filesystem without any |
||
12 | correspondence in the backup table are deleted. Their existance is due to |
||
13 | a behaviour of django: files are deleted from the database but not |
||
14 | fisically from the filesystem. This command calls also mycheck after its |
||
15 | function. |
||
16 | """ |
||
17 | |||
18 | def handle(self, *args, **options): |
||
19 | # get the file list from the filesystem |
||
20 | data_source_dir = os.path.join( |
||
21 | settings.PROTECTED_MEDIA_ROOT, |
||
22 | Submission.upload_dir) |
||
23 | |||
24 | data_source_files = os.listdir(data_source_dir) |
||
25 | |||
26 | # prepend DataSource.upload_dir to file list |
||
27 | data_source_files = [ |
||
28 | os.path.join( |
||
29 | Submission.upload_dir, |
||
30 | uploaded_file) |
||
31 | for uploaded_file in data_source_files] |
||
32 | |||
33 | # get the file list from the db |
||
34 | queryset = Submission.objects.all() |
||
35 | database_files = [ |
||
36 | str(datasource.uploaded_file) for datasource in queryset] |
||
37 | |||
38 | for datasource_file in data_source_files: |
||
39 | if datasource_file not in database_files: |
||
40 | to_remove = os.path.join( |
||
41 | settings.PROTECTED_MEDIA_ROOT, |
||
42 | datasource_file) |
||
43 | |||
44 | # debug |
||
45 | print("Removing %s" % (to_remove)) |
||
46 | os.remove(to_remove) |
||
47 | |||
48 | call_command('mycheck') |
||
49 |