forked from SickGear/SickGear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cleaner.py
142 lines (126 loc) · 5.93 KB
/
_cleaner.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# remove this file when no longer needed
import os
import io
import shutil
import sys
if 2 == sys.version_info[0]:
# noinspection PyDeprecation
import imp
# noinspection PyDeprecation
magic_number = imp.get_magic().encode('hex').decode('utf-8')
else:
import importlib.util
magic_number = importlib.util.MAGIC_NUMBER.hex()
parent_dir = os.path.abspath(os.path.dirname(__file__))
magic_number_file = os.path.join(parent_dir, '.python_magic.tmp')
old_magic = ''
try:
if os.path.isfile(magic_number_file):
with io.open(magic_number_file, 'r', encoding='utf-8') as mf:
old_magic = mf.read()
except (BaseException, Exception):
pass
if old_magic != magic_number:
# print('Python magic changed: removing all .pyc, .pyo files')
for pc in ['sickbeard', 'lib']:
search_dir = os.path.join(parent_dir, pc)
for dpath, dnames, fnames in os.walk(search_dir):
for filename in [fn for fn in fnames if os.path.splitext(fn)[-1].lower() in ('.pyc', '.pyo')]:
try:
os.remove(os.path.abspath(os.path.join(dpath, filename)))
except (BaseException, Exception):
pass
try:
with io.open(magic_number_file, 'w+') as mf:
mf.write(magic_number)
except (BaseException, Exception):
pass
# print('finished')
# skip cleaned005 as used during dev by testers
cleanups = [
['.cleaned006.tmp', ('lib', 'boto'), [
('lib', 'boto'), ('lib', 'growl'),
('lib', 'hachoir', 'core'), ('lib', 'hachoir', 'field'), ('lib', 'hachoir', 'metadata'),
('lib', 'hachoir', 'parser', 'archive'), ('lib', 'hachoir', 'parser', 'audio'),
('lib', 'hachoir', 'parser', 'common'), ('lib', 'hachoir', 'parser', 'container'),
('lib', 'hachoir', 'parser', 'image'), ('lib', 'hachoir', 'parser', 'misc'),
('lib', 'hachoir', 'parser', 'network'), ('lib', 'hachoir', 'parser', 'program'),
('lib', 'hachoir', 'parser', 'video'), ('lib', 'hachoir', 'parser'), ('lib', 'hachoir', 'stream'),
('lib', 'httplib2'), ('lib', 'oauth2'), ('lib', 'pythontwitter'), ('lib', 'tmdb_api')]],
['.cleaned004.tmp', ('lib', 'requests', 'packages'), [
('lib', 'requests', 'packages'), ('lib', 'pynma')]],
['.cleaned003.tmp', ('lib', 'imdb'), [
('lib', 'imdb')]],
['.cleaned002.tmp', ('lib', 'hachoir_core'), [
('.cleaned.tmp',), ('tornado',),
('lib', 'feedcache'), ('lib', 'hachoir_core'), ('lib', 'hachoir_metadata'), ('lib', 'hachoir_parser'),
('lib', 'jsonrpclib'), ('lib', 'shove'), ('lib', 'trakt'), ('lib', 'tvrage_api'), ('lib', 'unrar2')]],
]
for cleaned_path, test_path, dir_list in cleanups:
cleaned_file = os.path.abspath(os.path.join(parent_dir, cleaned_path))
test = os.path.abspath(os.path.join(parent_dir, *test_path))
if not os.path.isfile(cleaned_file) or os.path.exists(test):
dead_dirs = [os.path.abspath(os.path.join(parent_dir, *d)) for d in dir_list]
for dpath, dnames, fnames in os.walk(parent_dir):
for dead_dir in filter(lambda x: x in dead_dirs, [os.path.abspath(os.path.join(dpath, d)) for d in dnames]):
try:
shutil.rmtree(dead_dir)
except (BaseException, Exception):
pass
for filename in [fn for fn in fnames if os.path.splitext(fn)[-1].lower() in ('.pyc', '.pyo')]:
try:
os.remove(os.path.abspath(os.path.join(dpath, filename)))
except (BaseException, Exception):
pass
with io.open(cleaned_file, 'w+', encoding='utf-8') as fp:
fp.write(u'This file exists to prevent a rerun delete of *.pyc, *.pyo files')
fp.flush()
os.fsync(fp.fileno())
cleaned_file = os.path.abspath(os.path.join(parent_dir, '.cleaned_html5lib.tmp'))
test = os.path.abspath(os.path.join(parent_dir, 'lib', 'html5lib', 'treebuilders', '_base.pyc'))
danger_output = os.path.abspath(os.path.join(parent_dir, '__README-DANGER.txt'))
bad_files = []
if not os.path.isfile(cleaned_file) or os.path.exists(test):
for dead_path in [os.path.abspath(os.path.join(parent_dir, *d)) for d in [
('lib', 'html5lib', 'trie'),
('lib', 'html5lib', 'serializer')
]]:
try:
shutil.rmtree(dead_path)
except (BaseException, Exception):
pass
for dead_file in [os.path.abspath(os.path.join(parent_dir, *d)) for d in [
('lib', 'html5lib', 'ihatexml.py'),
('lib', 'html5lib', 'inputstream.py'),
('lib', 'html5lib', 'tokenizer.py'),
('lib', 'html5lib', 'utils.py'),
('lib', 'html5lib', 'filters', '_base.py'),
('lib', 'html5lib', 'sanitizer.py'),
('lib', 'html5lib', 'treebuilders', '_base.py'),
('lib', 'html5lib', 'treewalkers', '_base.py'),
('lib', 'html5lib', 'treewalkers', 'lxmletree.py'),
('lib', 'html5lib', 'treewalkers', 'genshistream.py'),
]]:
for ext in ['', 'c', 'o']:
name = '%s.py%s' % (os.path.splitext(dead_file)[:-1][0], ext)
if os.path.exists(name):
try:
os.remove(name)
except (BaseException, Exception):
bad_files += [name]
if any(bad_files):
swap_name = cleaned_file
cleaned_file = danger_output
danger_output = swap_name
msg = u'Failed (permissions?) to delete file(s). You must manually delete:\r\n%s' % '\r\n'.join(bad_files)
print(msg)
else:
msg = u'This file exists to prevent a rerun delete of dead lib/html5lib files'
with io.open(cleaned_file, 'w+', encoding='utf-8') as fp:
fp.write(msg)
fp.flush()
os.fsync(fp.fileno())
try:
os.remove(danger_output)
except (BaseException, Exception):
pass