forked from wbond/package_control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_reloader.py
306 lines (273 loc) · 9.99 KB
/
1_reloader.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import os
import sys
import sublime
import sublime_plugin
if sys.version_info >= (3,):
import importlib
import zipimport
st_build = int(sublime.version())
mod_prefix = 'package_control'
# ST3 loads each package as a module, so it needs an extra prefix
if sys.version_info >= (3,):
bare_mod_prefix = mod_prefix
mod_prefix = 'Package Control.' + mod_prefix
from imp import reload
# When reloading the package, we also need to reload the base "package_control"
# module in ST3. This flag inidicates we should re-add the PC package path
# to the beginning of sys.path before we try to reload.
do_insert = False
is_zipped = False
commands_name = mod_prefix + '.commands'
if commands_name in sys.modules and sys.version_info >= (3,) and st_build < 3112:
# Unfortunately with ST3, the ZipLoader does not "properly"
# implement load_module(), instead loading the code from the zip
# file when the object is instantiated. This means that calling
# reload() by itself does nothing. Instead we have to refresh the
# actual source code and then call reload().
pc_package_path = os.path.dirname(__file__)
if pc_package_path.endswith('.sublime-package'):
refreshing_zip_loader = sublime_plugin.ZipLoader(pc_package_path)
pc_zip_loader = sys.modules[commands_name].__loader__
if hasattr(pc_zip_loader, 'contents') and hasattr(pc_zip_loader, 'packages'):
pc_zip_loader.contents = refreshing_zip_loader.contents
pc_zip_loader.packages = refreshing_zip_loader.packages
if pc_package_path in zipimport._zip_directory_cache:
del zipimport._zip_directory_cache[pc_package_path]
is_zipped = True
importlib.invalidate_caches()
do_insert = True
# Python allows reloading modules on the fly, which allows us to do live upgrades.
# The only caveat to this is that you have to reload in the dependency order.
#
# Thus is module A depends on B and we don't reload B before A, when A is reloaded
# it will still have a reference to the old B. Thus we hard-code the dependency
# order of the various Package Control modules so they get reloaded properly.
#
# There are solutions for doing this all programatically, but this is much easier
# to understand.
reload_mods = []
for mod in sys.modules:
if mod[0:15] in set(['package_control', 'Package Control']) and sys.modules[mod] is not None:
reload_mods.append(mod)
mods_load_order = [
'',
'.sys_path',
'.text',
'.cache',
'.file_not_found_error',
'.open_compat',
'.http_cache',
'.console_write',
'.unicode',
'.clear_directory',
'.show_error',
'.cmd',
'.processes',
'.settings',
'.show_quick_panel',
'.thread_progress',
'.package_io',
'.semver',
'.versions',
'.deps.asn1crypto._errors',
'.deps.asn1crypto._int',
'.deps.asn1crypto._ordereddict',
'.deps.asn1crypto._teletex_codec',
'.deps.asn1crypto._types',
'.deps.asn1crypto._inet',
'.deps.asn1crypto._iri',
'.deps.asn1crypto.version',
'.deps.asn1crypto.pem',
'.deps.asn1crypto.util',
'.deps.asn1crypto.parser',
'.deps.asn1crypto.core',
'.deps.asn1crypto.algos',
'.deps.asn1crypto.keys',
'.deps.asn1crypto.x509',
'.deps.asn1crypto.crl',
'.deps.asn1crypto.csr',
'.deps.asn1crypto.ocsp',
'.deps.asn1crypto.cms',
'.deps.asn1crypto.pdf',
'.deps.asn1crypto.pkcs12',
'.deps.asn1crypto.tsp',
'.deps.asn1crypto',
'.deps.oscrypto._asn1',
'.deps.oscrypto._cipher_suites',
'.deps.oscrypto._errors',
'.deps.oscrypto._int',
'.deps.oscrypto._types',
'.deps.oscrypto.errors',
'.deps.oscrypto.version',
'.deps.oscrypto',
'.deps.oscrypto._ffi',
'.deps.oscrypto._pkcs12',
'.deps.oscrypto._pkcs5',
'.deps.oscrypto._rand',
'.deps.oscrypto._tls',
'.deps.oscrypto._linux_bsd.trust_list',
'.deps.oscrypto._mac._common_crypto_ctypes',
'.deps.oscrypto._mac._common_crypto',
'.deps.oscrypto._mac._core_foundation_ctypes',
'.deps.oscrypto._mac._core_foundation',
'.deps.oscrypto._mac._security_ctypes',
'.deps.oscrypto._mac._security',
'.deps.oscrypto._mac.trust_list',
'.deps.oscrypto._mac.util',
'.deps.oscrypto._openssl._libcrypto_ctypes',
'.deps.oscrypto._openssl._libcrypto',
'.deps.oscrypto._openssl._libssl_ctypes',
'.deps.oscrypto._openssl._libssl',
'.deps.oscrypto._openssl.util',
'.deps.oscrypto._win._cng_ctypes',
'.deps.oscrypto._win._cng',
'.deps.oscrypto._win._decode',
'.deps.oscrypto._win._advapi32_ctypes',
'.deps.oscrypto._win._advapi32',
'.deps.oscrypto._win._kernel32_ctypes',
'.deps.oscrypto._win._kernel32',
'.deps.oscrypto._win._secur32_ctypes',
'.deps.oscrypto._win._secur32',
'.deps.oscrypto._win._crypt32_ctypes',
'.deps.oscrypto._win._crypt32',
'.deps.oscrypto._win.trust_list',
'.deps.oscrypto._win.util',
'.deps.oscrypto.trust_list',
'.deps.oscrypto.util',
'.deps.oscrypto.kdf',
'.deps.oscrypto._mac.symmetric',
'.deps.oscrypto._openssl.symmetric',
'.deps.oscrypto._win.symmetric',
'.deps.oscrypto.symmetric',
'.deps.oscrypto._asymmetric',
'.deps.oscrypto._ecdsa',
'.deps.oscrypto._pkcs1',
'.deps.oscrypto._mac.asymmetric',
'.deps.oscrypto._openssl.asymmetric',
'.deps.oscrypto._win.asymmetric',
'.deps.oscrypto.asymmetric',
'.deps.oscrypto.keys',
'.deps.oscrypto._mac.tls',
'.deps.oscrypto._openssl.tls',
'.deps.oscrypto._win.tls',
'.deps.oscrypto.tls',
'.http',
'.http.invalid_certificate_exception',
'.http.debuggable_http_response',
'.http.debuggable_https_response',
'.http.debuggable_http_connection',
'.http.persistent_handler',
'.http.debuggable_http_handler',
'.http.validating_https_connection',
'.http.validating_https_handler',
'.ca_certs',
'.downloaders.downloader_exception',
'.downloaders.rate_limit_exception',
'.downloaders.binary_not_found_error',
'.downloaders.non_clean_exit_error',
'.downloaders.non_http_error',
'.downloaders.caching_downloader',
'.downloaders.decoding_downloader',
'.downloaders.limiting_downloader',
'.downloaders.urllib_downloader',
'.downloaders.cli_downloader',
'.downloaders.curl_downloader',
'.downloaders.wget_downloader',
'.downloaders.win_downloader_exception',
'.downloaders.wininet_downloader',
'.downloaders.oscrypto_downloader_exception',
'.downloaders.oscrypto_downloader',
'.downloaders.background_downloader',
'.downloaders',
'.download_manager',
'.clients',
'.clients.client_exception',
'.clients.bitbucket_client',
'.clients.github_client',
'.clients.readme_client',
'.clients.json_api_client',
'.providers.provider_exception',
'.providers.bitbucket_repository_provider',
'.providers.github_repository_provider',
'.providers.github_user_provider',
'.providers.schema_compat',
'.providers.release_selector',
'.providers.channel_provider',
'.providers.repository_provider',
'.providers',
'.upgraders',
'.upgraders.vcs_upgrader',
'.upgraders.git_upgrader',
'.upgraders.hg_upgrader',
'.package_manager',
'.package_creator',
'.package_disabler',
'.package_installer',
'.package_renamer',
'.loader',
'.bootstrap',
'.tests',
'.tests.clients',
'.tests.providers',
'.commands',
'.commands.add_channel_command',
'.commands.add_repository_command',
'.commands.create_package_command',
'.commands.disable_package_command',
'.commands.discover_packages_command',
'.commands.enable_package_command',
'.commands.existing_packages_command',
'.commands.install_local_dependency_command',
'.commands.install_package_command',
'.commands.list_packages_command',
'.commands.list_unmanaged_packages_command',
'.commands.remove_channel_command',
'.commands.remove_package_command',
'.commands.remove_repository_command',
'.commands.upgrade_all_packages_command',
'.commands.upgrade_package_command',
'.commands.package_control_insert_command',
'.commands.satisfy_dependencies_command',
'.commands.package_control_tests_command',
'.automatic_upgrader',
'.package_cleanup'
]
if do_insert:
if is_zipped:
# When we run into modules imports from a .sublime-package, the
# in memory modules reference a zipimport.zipimporter object that
# has an out-dated reference to the .sublime-package file, which
# means when we call reload(), we get a zipimport.ZipImportError
# of "bad local file header". To work around this, we construct
# new zipimporter instances and attach them to the in-memory
# modules using the .__loader__ attribute.
loaders = {}
loaders[''] = zipimport.zipimporter(pc_package_path)
else:
sys.path.insert(0, pc_package_path)
for suffix in mods_load_order:
mod = mod_prefix + suffix
if mod in reload_mods:
try:
reload(sys.modules[mod])
except (ImportError):
pass # Upgrade issues from PC 2.0 -> 3.0
if sys.version_info >= (3,) and st_build < 3112:
bare_mod = bare_mod_prefix + suffix
if bare_mod in reload_mods:
bare_module = sys.modules[bare_mod]
if is_zipped:
# See the command above near "if is_zipped:" to understand why
# we are replacing the .__loader__ attribute of the modules with
# a fresh zipimporter object.
if bare_mod.find('.') == -1:
loader_lookup = ''
else:
loader_lookup = os.sep.join(bare_mod.split('.')[0:-1])
if loader_lookup not in loaders:
zi_path = os.path.join(pc_package_path, loader_lookup) + os.sep
loaders[loader_lookup] = zipimport.zipimporter(zi_path)
bare_module.__loader__ = loaders[loader_lookup]
reload(bare_module)
if do_insert and not is_zipped:
sys.path.remove(pc_package_path)