forked from wbond/package_control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_installer.py
242 lines (204 loc) · 8.75 KB
/
package_installer.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
import sublime
import os
import re
import threading
from .preferences_filename import preferences_filename
from .thread_progress import ThreadProgress
from .package_manager import PackageManager
from .upgraders.git_upgrader import GitUpgrader
from .upgraders.hg_upgrader import HgUpgrader
class PackageInstaller():
"""
Provides helper functionality related to installing packages
"""
def __init__(self):
self.manager = PackageManager()
def make_package_list(self, ignore_actions=[], override_action=None,
ignore_packages=[]):
"""
Creates a list of packages and what operation would be performed for
each. Allows filtering by the applicable action or package name.
Returns the information in a format suitable for displaying in the
quick panel.
:param ignore_actions:
A list of actions to ignore packages by. Valid actions include:
`install`, `upgrade`, `downgrade`, `reinstall`, `overwrite`,
`pull` and `none`. `pull` andd `none` are for Git and Hg
repositories. `pull` is present when incoming changes are detected,
where as `none` is selected if no commits are available. `overwrite`
is for packages that do not include version information via the
`package-metadata.json` file.
:param override_action:
A string action name to override the displayed action for all listed
packages.
:param ignore_packages:
A list of packages names that should not be returned in the list
:return:
A list of lists, each containing three strings:
0 - package name
1 - package description
2 - action; [extra info;] package url
"""
packages = self.manager.list_available_packages()
installed_packages = self.manager.list_packages()
package_list = []
for package in sorted(packages.iterkeys(), key=lambda s: s.lower()):
if ignore_packages and package in ignore_packages:
continue
package_entry = [package]
info = packages[package]
download = info['downloads'][0]
if package in installed_packages:
installed = True
metadata = self.manager.get_metadata(package)
if metadata.get('version'):
installed_version = metadata['version']
else:
installed_version = None
else:
installed = False
installed_version_name = 'v' + installed_version if \
installed and installed_version else 'unknown version'
new_version = 'v' + download['version']
vcs = None
package_dir = self.manager.get_package_dir(package)
settings = self.manager.settings
if override_action:
action = override_action
extra = ''
else:
if os.path.exists(os.path.join(sublime.packages_path(),
package, '.git')):
if settings.get('ignore_vcs_packages'):
continue
vcs = 'git'
incoming = GitUpgrader(settings.get('git_binary'),
settings.get('git_update_command'), package_dir,
settings.get('cache_length'), settings.get('debug')
).incoming()
elif os.path.exists(os.path.join(sublime.packages_path(),
package, '.hg')):
if settings.get('ignore_vcs_packages'):
continue
vcs = 'hg'
incoming = HgUpgrader(settings.get('hg_binary'),
settings.get('hg_update_command'), package_dir,
settings.get('cache_length'), settings.get('debug')
).incoming()
if installed:
if not installed_version:
if vcs:
if incoming:
action = 'pull'
extra = ' with ' + vcs
else:
action = 'none'
extra = ''
else:
action = 'overwrite'
extra = ' %s with %s' % (installed_version_name,
new_version)
else:
res = self.manager.compare_versions(
installed_version, download['version'])
if res < 0:
action = 'upgrade'
extra = ' to %s from %s' % (new_version,
installed_version_name)
elif res > 0:
action = 'downgrade'
extra = ' to %s from %s' % (new_version,
installed_version_name)
else:
action = 'reinstall'
extra = ' %s' % new_version
else:
action = 'install'
extra = ' %s' % new_version
extra += ';'
if action in ignore_actions:
continue
description = info.get('description')
if not description:
description = 'No description provided'
package_entry.append(description)
package_entry.append(action + extra + ' ' +
re.sub('^https?://', '', info['url']))
package_list.append(package_entry)
return package_list
def disable_package(self, package):
"""
Disables a package before installing or upgrading to prevent errors
where Sublime Text tries to read files that no longer exist, or read a
half-written file.
:param package: The string package name
"""
# Don't disable Package Control so it does not get stuck disabled
if package == 'Package Control':
return False
settings = sublime.load_settings(preferences_filename())
ignored = settings.get('ignored_packages')
if not ignored:
ignored = []
if not package in ignored:
ignored.append(package)
settings.set('ignored_packages', ignored)
sublime.save_settings(preferences_filename())
return True
return False
def reenable_package(self, package):
"""
Re-enables a package after it has been installed or upgraded
:param package: The string package name
"""
settings = sublime.load_settings(preferences_filename())
ignored = settings.get('ignored_packages')
if not ignored:
return
if package in ignored:
settings.set('ignored_packages',
list(set(ignored) - set([package])))
sublime.save_settings(preferences_filename())
def on_done(self, picked):
"""
Quick panel user selection handler - disables a package, installs or
upgrades it, then re-enables the package
:param picked:
An integer of the 0-based package name index from the presented
list. -1 means the user cancelled.
"""
if picked == -1:
return
name = self.package_list[picked][0]
if self.disable_package(name):
on_complete = lambda: self.reenable_package(name)
else:
on_complete = None
thread = PackageInstallerThread(self.manager, name, on_complete)
thread.start()
ThreadProgress(thread, 'Installing package %s' % name,
'Package %s successfully %s' % (name, self.completion_type))
class PackageInstallerThread(threading.Thread):
"""
A thread to run package install/upgrade operations in so that the main
Sublime Text thread does not get blocked and freeze the UI
"""
def __init__(self, manager, package, on_complete):
"""
:param manager:
An instance of :class:`PackageManager`
:param package:
The string package name to install/upgrade
:param on_complete:
A callback to run after installing/upgrading the package
"""
self.package = package
self.manager = manager
self.on_complete = on_complete
threading.Thread.__init__(self)
def run(self):
try:
self.result = self.manager.install_package(self.package)
finally:
if self.on_complete:
sublime.set_timeout(self.on_complete, 1)