Mercurial > public > sg101
comparison downloads/management/commands/dlorphan.py @ 667:264b08bce8b8
Improvements to dlorphan management command.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 25 May 2013 14:21:46 -0500 |
parents | 86d04190ff4e |
children |
comparison
equal
deleted
inserted
replaced
666:ddc189ff96bb | 667:264b08bce8b8 |
---|---|
25 delete = options.get('delete', False) | 25 delete = options.get('delete', False) |
26 | 26 |
27 orphans = set() | 27 orphans = set() |
28 missing_pending = [] | 28 missing_pending = [] |
29 missing_dls = [] | 29 missing_dls = [] |
30 empty_dirs = [] | |
31 | 30 |
32 dls_dir = os.path.join(settings.MEDIA_ROOT, 'downloads') | 31 dls_dir = os.path.join(settings.MEDIA_ROOT, 'downloads') |
33 # find the set of all files in the downloads area | 32 # find the set of all files in the downloads area |
34 for root, dirs, files in os.walk(dls_dir, followlinks=True): | 33 for root, dirs, files in os.walk(dls_dir, followlinks=True): |
35 for name in files: | 34 for name in files: |
36 orphans.add(unicode(os.path.join(root, name), 'utf-8')) | 35 orphans.add(unicode(os.path.join(root, name), 'utf-8')) |
37 | |
38 # check for empty directories | |
39 if not len(dirs) and not len(files): | |
40 empty_dirs.append(root) | |
41 | 36 |
42 # examine the pending downloads: | 37 # examine the pending downloads: |
43 for dl in PendingDownload.objects.iterator(): | 38 for dl in PendingDownload.objects.iterator(): |
44 try: | 39 try: |
45 orphans.remove(dl.file.path) | 40 orphans.remove(dl.file.path) |
51 try: | 46 try: |
52 orphans.remove(dl.file.path) | 47 orphans.remove(dl.file.path) |
53 except KeyError: | 48 except KeyError: |
54 missing_dls.append(dl) | 49 missing_dls.append(dl) |
55 | 50 |
56 if orphans: | 51 if orphans and delete: |
57 print "Orphan files:" | 52 for path in orphans: |
53 self.stdout.write("Deleting: {}\n".format(path)) | |
54 os.remove(path) | |
55 elif orphans: | |
56 self.stdout.write("Orphan files:\n") | |
58 for orphan in orphans: | 57 for orphan in orphans: |
59 print orphan | 58 self.stdout.write("{}\n".format(orphan)) |
60 | |
61 if empty_dirs: | |
62 print "Empty directories:" | |
63 for d in empty_dirs: | |
64 print d | |
65 | 59 |
66 if missing_pending: | 60 if missing_pending: |
67 print "PendingDownloads with missing files:" | 61 self.stdout.write("PendingDownloads with missing files:\n") |
68 for dl in missing_pending: | 62 for dl in missing_pending: |
69 print dl | 63 self.stdout.write("{}\n".format(dl)) |
70 | 64 |
71 if missing_dls: | 65 if missing_dls: |
72 print "Downloads with missing files:" | 66 self.stdout.write("Downloads with missing files:\n") |
73 for dl in missing_dls: | 67 for dl in missing_dls: |
74 print dl | 68 self.stdout.write("{}\n".format(dl)) |
75 | 69 |
76 if delete: | 70 empty_dirs = [] |
77 for path in orphans: | 71 # check for empty directories after deletions |
78 print "Deleting: {}".format(path) | 72 for root, dirs, files in os.walk(dls_dir, followlinks=True): |
79 os.remove(path) | 73 if not len(dirs) and not len(files): |
74 empty_dirs.append(root) | |
80 | 75 |
76 if empty_dirs and delete: | |
81 for path in empty_dirs: | 77 for path in empty_dirs: |
82 print "Deleting: {}".format(path) | 78 self.stdout.write("Deleting empty dir: {}\n".format(path)) |
83 os.removedirs(path) | 79 os.removedirs(path) |
80 elif empty_dirs: | |
81 self.stdout.write("Empty directories:\n") | |
82 for d in empty_dirs: | |
83 self.stdout.write("{}\n".format(d)) |