Repository navigation
Expand file tree
/
Copy pathgenerate_workflow_list.py
More file actions
executable file
·342 lines (274 loc) · 11.1 KB
/
Copy pathgenerate_workflow_list.py
File metadata and controls
executable file
·342 lines (274 loc) · 11.1 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
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright © 2014 deanishe@deanishe.net
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2014-10-02
#
"""
Generate a list of workflows on Packal that use Alfred-Workflow.
"""
from __future__ import print_function, unicode_literals, absolute_import
import argparse
import csv
import json
import logging
import os
import re
import subprocess
from time import time
from xml.etree import ElementTree as ET
from zipfile import ZipFile
match_zip = re.compile(r'alfred-workflow-.*\.zip').search
is_github_url = re.compile(r'^https?://github.com/.+$').match
parse_github_url = re.compile(r'https?://github.com/(.+?)/(.+)').match
WORKFLOW_LIST = os.path.join(os.path.dirname(__file__),
'library_workflows.tsv')
PACKAL_REPO_URL = 'https://github.com/packal/repository'
PACKAL_REPO_DIR = os.path.expanduser('~/Temp/Packal-Repository')
CACHE_PATH = os.path.expanduser('~/.packal_repo_cache.json')
COOKIE_PATH = os.path.expanduser('~/.packal_repo_cookies.txt')
USER_AGENT = 'Alfred-Workflow/1.12 (www.deanishe.net/alfred-workflow/)'
MAX_CACHE_AGE = 7 # days
logging.basicConfig(
level=logging.INFO,
format='%(filename)s:%(lineno)d %(levelname)-8s %(message)s',
datefmt='%H:%M:%S')
log = logging.getLogger()
def fetch_url(url):
"""Return contents of ``url``."""
cmd = [
'curl',
'--referer', 'http://www.packal.org/workflow-list;auto',
'--location',
'--user-agent', USER_AGENT,
'--silent', '--show-error',
'--cookie-jar', COOKIE_PATH,
'--cookie', COOKIE_PATH,
url]
data = subprocess.check_output(cmd).decode('utf-8')
return data
class Cache(object):
find_github_url = re.compile(r"""field-github-url
.*?
href="(.+?)"
.*?
</div>""",
re.DOTALL | re.VERBOSE).search
def __init__(self, filepath):
self.filepath = filepath
self._data = {}
self._load()
def _load(self):
if os.path.exists(self.filepath):
with open(self.filepath) as file_obj:
self._data = json.load(file_obj)
def save(self):
with open(self.filepath, 'wb') as file_obj:
json.dump(self._data, file_obj, indent=2, sort_keys=True)
def add_github_info(self, workflow):
"""Add GitHub info to workflow dict
{
'github_user': 'github username',
'github_repo': 'github repo',
'github_repo_url': 'https://github.com/user/repo',
'github_user_url': 'https://github.com/user',
}
"""
update = True
data = {}
if workflow['bundle'] in self._data:
data = self._data[workflow['bundle']]
cache_age = time() - data['cached']
log.debug('Cache age of {} : {:0.3f} days'.format(
workflow['bundle'], cache_age / 86400))
if cache_age < (MAX_CACHE_AGE * 86400):
update = False
if not update:
workflow.update(data)
return workflow
if update: # Fetch workflow pages from Packal and grab GitHub info
log.info('Retrieving {} ...'.format(workflow['url']))
# fp = urlopen(workflow['url'])
# html = fp.read()
html = fetch_url(workflow['url'])
# log.debug(html)
m = self.find_github_url(html)
if not m:
log.warning('No Github info for `{}`'.format(
workflow['bundle']))
else:
github_url = m.group(1)
m = parse_github_url(github_url)
if m:
username, repo = m.groups()
data['github_user'] = username
data['github_repo'] = repo
data['github_repo_url'] = github_url
data['github_user_url'] = 'https://github.com/{}/'.format(
username)
data['cached'] = time()
self._data[workflow['bundle']] = data
self.save()
workflow.update(data)
return workflow
def workflow_link(workflow, rest=False, github_links=True):
"""Return a link for ``workflow`` in Markdown or ReST."""
output = []
if rest:
output.append('`{name} <{url}>`__')
if (github_links and workflow.get('github_repo_url') and
workflow['github_repo_url'] != workflow['url']):
output.append('(`GitHub repo <{github_repo_url}>`__)')
if workflow.get('author_url'):
if (github_links and workflow.get('github_user_url') and
workflow['github_user_url'] != workflow['author_url']):
output.append('by `{author} <{author_url}>`__')
output.append('(`on GitHub <{github_user_url}>`__).')
else:
output.append('by `{author} <{author_url}>`__.')
else:
output.append('by {author}.')
output.append('{description}')
return '\n '.join(output).format(**workflow)
else: # Markdown
output.append('[{name}]({url})')
if (github_links and workflow.get('github_repo_url') and
workflow['github_repo_url'] != workflow['url']):
output.append('([GitHub repo]({github_repo_url}))')
if workflow.get('author_url'):
if (github_links and workflow.get('github_user_url') and
workflow['github_user_url'] != workflow['author_url']):
output.append('by [{author}]({author_url})')
output.append('([on GitHub]({github_user_url})).')
else:
output.append('by [{author}]({author_url}).')
else:
output.append(' by {author}.')
output.append('\n {description}')
if rest:
return '\n '.join(output).format(**workflow)
else:
return ' '.join(output).format(**workflow)
def update_repo():
"""Ensure Packal repo is present and up-to-date."""
if not os.path.exists(PACKAL_REPO_DIR): # Clone repo
cmd = ['git', 'clone', PACKAL_REPO_URL, PACKAL_REPO_DIR]
subprocess.call(cmd)
else: # Update repo
cwd = os.getcwd()
os.chdir(PACKAL_REPO_DIR)
cmd = ['git', 'pull']
subprocess.call(cmd)
os.chdir(cwd)
def read_list(path):
"""Read list of workflows from a TSV file."""
workflows = []
with open(path) as file_obj:
reader = csv.DictReader(file_obj, delimiter=b'\t')
for workflow in reader:
# Decode text
for k in workflow:
workflow[k] = workflow[k].decode('utf-8')
if is_github_url(workflow['url']):
m = parse_github_url(workflow['url'])
if m:
username, repo = m.groups()
workflow['github_user'] = username
workflow['github_repo'] = repo
workflow['github_repo_url'] = workflow['url']
workflow['github_user_url'] = ('https://github.com/{}/'
.format(username))
if not workflow.get('author_url'):
workflow['author_url'] = workflow['github_user_url']
workflows.append(workflow)
return workflows
def read_manifest(path):
"""Read dictionary of workflows from the Packal manifest.xml file."""
workflows = {}
tree = ET.parse(path)
root = tree.getroot()
for workflow in root:
data = {'packal': True}
for child in workflow:
if child.tag == 'short':
data['description'] = child.text
else:
data[child.tag] = child.text
# print(child.tag, ':', child.text)
data['author_url'] = packal_user_url(data['author'])
if 'bundle' in data:
workflows[data['bundle']] = data
return workflows
def workflows_using_aw(dirpath):
for root, _, filenames in os.walk(dirpath):
for filename in filenames:
if not filename.endswith('.alfredworkflow'):
continue
path = os.path.join(root, filename)
with ZipFile(path) as z:
uses_alfred_workflow = False
for name in z.namelist():
if name in (b'workflow/workflow.py', b'workflow.zip'):
uses_alfred_workflow = True
elif match_zip(name):
uses_alfred_workflow = True
if uses_alfred_workflow:
bundle = os.path.basename(os.path.dirname(path))
# print(bundle)
yield bundle
def packal_username(author):
user = author.lower()
user = user.replace(' ', '-')
return user
def packal_user_url(author):
return 'http://www.packal.org/users/{}'.format(packal_username(author))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-b', '--bundleid', action='store_true', default=False,
help="Print a list of bundle IDs.")
parser.add_argument('-r', '--rest', action='store_true', default=False,
help='Generate ReST list (default is Markdown)')
parser.add_argument('-g', '--github', action='store_true',
default=False,
help='Add GitHub links to repo/user profile.'
' Default is no extra links.')
args = parser.parse_args()
cache = Cache(CACHE_PATH)
update_repo()
workflows = read_list(WORKFLOW_LIST)
packal_workflows = read_manifest(os.path.join(PACKAL_REPO_DIR,
'manifest.xml'))
bundles = []
log.info('Searching {}...'.format(PACKAL_REPO_DIR))
for bundle in workflows_using_aw(PACKAL_REPO_DIR):
if bundle in packal_workflows:
bundles.append(bundle)
# print('\n\n')
log.info('{} Packal workflows using Alfred-Workflow'.format(len(bundles)))
output = []
for bundle in bundles:
workflow = packal_workflows[bundle]
workflow['username'] = packal_username(workflow['author'])
workflow = cache.add_github_info(workflow)
workflows.append(workflow)
log.info('{} workflows using Alfred-Workflow'.format(len(workflows)))
for workflow in workflows:
if args.bundleid:
# print(workflow.keys())
# return
output.append((workflow['name'], workflow['bundle']))
else:
# msg = '[{name}]({url}) by [{author}](http://www.packal.org/users/{username}). {short}'.format(**workflow)
msg = workflow_link(workflow, rest=args.rest, github_links=args.github)
if not msg.endswith('.'):
msg += '.'
output.append((workflow['name'], msg))
output.sort(key=lambda s: s[0].lower())
output = [t[1] for t in output]
for line in output:
print('- {}'.format(line).encode('utf-8'))
if __name__ == '__main__':
main()