Repository navigation
Expand file tree
/
Copy pathupdate.py
More file actions
471 lines (348 loc) · 13.5 KB
/
Copy pathupdate.py
File metadata and controls
471 lines (348 loc) · 13.5 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2014 Fabio Niephaus <fabio.niephaus@gmail.com>,
# Dean Jackson <deanishe@deanishe.net>
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2014-08-16
#
"""
Self-updating from GitHub
.. versionadded:: 1.9
.. note::
This module is not intended to be used directly. Automatic updates
are controlled by the ``update_settings`` :class:`dict` passed to
:class:`~workflow.workflow.Workflow` objects.
"""
from __future__ import print_function, unicode_literals
import os
import tempfile
import re
import subprocess
import workflow
import web
# __all__ = []
RELEASES_BASE = 'https://api.github.com/repos/{0}/releases'
_wf = None
def wf():
global _wf
if _wf is None:
_wf = workflow.Workflow()
return _wf
class Version(object):
"""Mostly semantic versioning.
The main difference to proper :ref:`semantic versioning <semver>`
is that this implementation doesn't require a minor or patch version.
Version strings may also be prefixed with "v", e.g.:
>>> v = Version('v1.1.1')
>>> v.tuple
(1, 1, 1, '')
>>> v = Version('2.0')
>>> v.tuple
(2, 0, 0, '')
>>> Version('3.1-beta').tuple
(3, 1, 0, 'beta')
>>> Version('1.0.1') > Version('0.0.1')
True
"""
#: Match version and pre-release/build information in version strings
match_version = re.compile(r'([0-9\.]+)(.+)?').match
def __init__(self, vstr):
"""Create new `Version` object.
Args:
vstr (basestring): Semantic version string.
"""
self.vstr = vstr
self.major = 0
self.minor = 0
self.patch = 0
self.suffix = ''
self.build = ''
self._parse(vstr)
def _parse(self, vstr):
if vstr.startswith('v'):
m = self.match_version(vstr[1:])
else:
m = self.match_version(vstr)
if not m:
raise ValueError('Invalid version number: {0}'.format(vstr))
version, suffix = m.groups()
parts = self._parse_dotted_string(version)
self.major = parts.pop(0)
if len(parts):
self.minor = parts.pop(0)
if len(parts):
self.patch = parts.pop(0)
if not len(parts) == 0:
raise ValueError('Invalid version (too long) : {0}'.format(vstr))
if suffix:
# Build info
idx = suffix.find('+')
if idx > -1:
self.build = suffix[idx+1:]
suffix = suffix[:idx]
if suffix:
if not suffix.startswith('-'):
raise ValueError(
'Invalid suffix : `{0}`. Must start with `-`'.format(
suffix))
self.suffix = suffix[1:]
# wf().logger.debug('version str `{}` -> {}'.format(vstr, repr(self)))
def _parse_dotted_string(self, s):
"""Parse string ``s`` into list of ints and strings."""
parsed = []
parts = s.split('.')
for p in parts:
if p.isdigit():
p = int(p)
parsed.append(p)
return parsed
@property
def tuple(self):
"""Version number as a tuple of major, minor, patch, pre-release."""
return (self.major, self.minor, self.patch, self.suffix)
def __lt__(self, other):
if not isinstance(other, Version):
raise ValueError('Not a Version instance: {0!r}'.format(other))
t = self.tuple[:3]
o = other.tuple[:3]
if t < o:
return True
if t == o: # We need to compare suffixes
if self.suffix and not other.suffix:
return True
if other.suffix and not self.suffix:
return False
return (self._parse_dotted_string(self.suffix) <
self._parse_dotted_string(other.suffix))
# t > o
return False
def __eq__(self, other):
if not isinstance(other, Version):
raise ValueError('Not a Version instance: {0!r}'.format(other))
return self.tuple == other.tuple
def __ne__(self, other):
return not self.__eq__(other)
def __gt__(self, other):
if not isinstance(other, Version):
raise ValueError('Not a Version instance: {0!r}'.format(other))
return other.__lt__(self)
def __le__(self, other):
if not isinstance(other, Version):
raise ValueError('Not a Version instance: {0!r}'.format(other))
return not other.__lt__(self)
def __ge__(self, other):
return not self.__lt__(other)
def __str__(self):
vstr = '{0}.{1}.{2}'.format(self.major, self.minor, self.patch)
if self.suffix:
vstr += '-{0}'.format(self.suffix)
if self.build:
vstr += '+{0}'.format(self.build)
return vstr
def __repr__(self):
return "Version('{0}')".format(str(self))
def download_workflow(url):
"""Download workflow at ``url`` to a local temporary file
:param url: URL to .alfredworkflow file in GitHub repo
:returns: path to downloaded file
"""
filename = url.split("/")[-1]
if (not url.endswith('.alfredworkflow') or
not filename.endswith('.alfredworkflow')):
raise ValueError('Attachment `{0}` not a workflow'.format(filename))
local_path = os.path.join(tempfile.gettempdir(), filename)
wf().logger.debug(
'Downloading updated workflow from `{0}` to `{1}` ...'.format(
url, local_path))
response = web.get(url)
with open(local_path, 'wb') as output:
output.write(response.content)
return local_path
def build_api_url(slug):
"""Generate releases URL from GitHub slug.
:param slug: Repo name in form ``username/repo``
:returns: URL to the API endpoint for the repo's releases
"""
if len(slug.split('/')) != 2:
raise ValueError('Invalid GitHub slug : {0}'.format(slug))
return RELEASES_BASE.format(slug)
def _validate_release(release):
"""Return release for running version of Alfred."""
alf3 = wf().alfred_version.major == 3
downloads = {'.alfredworkflow': [], '.alfred3workflow': []}
dl_count = 0
version = release['tag_name']
for asset in release.get('assets', []):
url = asset.get('browser_download_url')
if not url: # pragma: nocover
continue
ext = os.path.splitext(url)[1].lower()
if ext not in downloads:
continue
# Ignore Alfred 3-only files if Alfred 2 is running
if ext == '.alfred3workflow' and not alf3:
continue
downloads[ext].append(url)
dl_count += 1
# download_urls.append(url)
if dl_count == 0:
wf().logger.warning(
'Invalid release {0} : No workflow file'.format(version))
return None
for k in downloads:
if len(downloads[k]) > 1:
wf().logger.warning(
'Invalid release %s : multiple %s files'.format(version, k))
return None
# Prefer .alfred3workflow file if there is one and Alfred 3 is
# running.
if alf3 and len(downloads['.alfred3workflow']):
download_url = downloads['.alfred3workflow'][0]
else:
download_url = downloads['.alfredworkflow'][0]
wf().logger.debug('Release `{0}` : {1}'.format(version, download_url))
return {
'version': version,
'download_url': download_url,
'prerelease': release['prerelease']
}
def get_valid_releases(github_slug, prereleases=False):
"""Return list of all valid releases.
:param github_slug: ``username/repo`` for workflow's GitHub repo
:param prereleases: Whether to include pre-releases.
:returns: list of dicts. Each :class:`dict` has the form
``{'version': '1.1', 'download_url': 'http://github.com/...',
'prerelease': False }``
A valid release is one that contains one ``.alfredworkflow`` file.
If the GitHub version (i.e. tag) is of the form ``v1.1``, the leading
``v`` will be stripped.
"""
api_url = build_api_url(github_slug)
releases = []
wf().logger.debug('Retrieving releases list from `{0}` ...'.format(
api_url))
def retrieve_releases():
wf().logger.info(
'Retrieving releases for `{0}` ...'.format(github_slug))
return web.get(api_url).json()
slug = github_slug.replace('/', '-')
for release in wf().cached_data('gh-releases-{0}'.format(slug),
retrieve_releases):
wf().logger.debug('Release : %r', release)
release = _validate_release(release)
if release is None:
wf().logger.debug('Invalid release')
continue
elif release['prerelease'] and not prereleases:
wf().logger.debug('Ignoring prerelease : %s', release['version'])
continue
releases.append(release)
# else:
# try:
# valid = _validate_release(release)
# except Exception as err:
# wf().logger.exception(err)
# raise err
# else:
# wf().logger.debug('valid=%r', valid)
# version = release['tag_name']
# download_urls = []
# for asset in release.get('assets', []):
# url = asset.get('browser_download_url')
# if not url or not url.endswith('.alfredworkflow'):
# continue
# download_urls.append(url)
# # Validate release
# if release['prerelease'] and not prereleases:
# wf().logger.warning(
# 'Invalid release {0} : pre-release detected'.format(version))
# continue
# if not download_urls:
# wf().logger.warning(
# 'Invalid release {0} : No workflow file'.format(version))
# continue
# if len(download_urls) > 1:
# wf().logger.warning(
# 'Invalid release {0} : multiple workflow files'.format(version))
# continue
# wf().logger.debug('Release `{0}` : {1}'.format(version, url))
# releases.append({
# 'version': version,
# 'download_url': download_urls[0],
# 'prerelease': release['prerelease']
# })
return releases
def check_update(github_slug, current_version, prereleases=False):
"""Check whether a newer release is available on GitHub
:param github_slug: ``username/repo`` for workflow's GitHub repo
:param current_version: the currently installed version of the
workflow. :ref:`Semantic versioning <semver>` is required.
:param prereleases: Whether to include pre-releases.
:type current_version: ``unicode``
:returns: ``True`` if an update is available, else ``False``
If an update is available, its version number and download URL will
be cached.
"""
releases = get_valid_releases(github_slug, prereleases)
wf().logger.info('{0} releases for {1}'.format(len(releases),
github_slug))
if not len(releases):
raise ValueError('No valid releases for {0}'.format(github_slug))
# GitHub returns releases newest-first
latest_release = releases[0]
# (latest_version, download_url) = get_latest_release(releases)
vr = Version(latest_release['version'])
vl = Version(current_version)
wf().logger.debug('Latest : {0!r} Installed : {1!r}'.format(vr, vl))
if vr > vl:
wf().cache_data('__workflow_update_status', {
'version': latest_release['version'],
'download_url': latest_release['download_url'],
'available': True
})
return True
wf().cache_data('__workflow_update_status', {
'available': False
})
return False
def install_update(github_slug, current_version):
"""If a newer release is available, download and install it
:param github_slug: ``username/repo`` for workflow's GitHub repo
:param current_version: the currently installed version of the
workflow. :ref:`Semantic versioning <semver>` is required.
:type current_version: ``unicode``
If an update is available, it will be downloaded and installed.
:returns: ``True`` if an update is installed, else ``False``
"""
# TODO: `github_slug` and `current_version` are both unusued.
update_data = wf().cached_data('__workflow_update_status', max_age=0)
if not update_data or not update_data.get('available'):
wf().logger.info('No update available')
return False
local_file = download_workflow(update_data['download_url'])
wf().logger.info('Installing updated workflow ...')
subprocess.call(['open', local_file])
update_data['available'] = False
wf().cache_data('__workflow_update_status', update_data)
return True
if __name__ == '__main__': # pragma: nocover
import sys
def show_help():
print('Usage : update.py (check|install) github_slug version [--prereleases]')
sys.exit(1)
argv = sys.argv[:]
prereleases = '--prereleases' in argv
if prereleases:
argv.remove('--prereleases')
if len(argv) != 4:
show_help()
action, github_slug, version = argv[1:]
if action not in ('check', 'install'):
show_help()
if action == 'check':
check_update(github_slug, version, prereleases)
elif action == 'install':
install_update(github_slug, version)