-
Notifications
You must be signed in to change notification settings - Fork 6
/
workflow-build.py
332 lines (267 loc) · 9.68 KB
/
workflow-build.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
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
#!/usr/bin/python
# encoding: utf-8
#
# Copyright (c) 2013 deanishe@deanishe.net.
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2013-11-01
#
"""workflow-build [options] <workflow-dir>
Build Alfred Workflows.
Compile contents of <workflow-dir> to a ZIP file (with extension
`.alfredworkflow`).
The name of the output file is generated from the workflow name,
which is extracted from the workflow's `info.plist`. If a `version`
file is contained within the workflow directory, it's contents
will be appended to the compiled workflow's filename.
Usage:
workflow-build [-v|-q|-d] [-f] [-n] [-o <outputdir>] <workflow-dir>...
workflow-build (-h|--version)
Options:
-o, --output=<outputdir> Directory to save workflow(s) to.
Default is current working directory.
-f, --force Overwrite existing files.
-n, --dry-run Only show files that would be included
in workflow. Don't build anything.
-h, --help Show this message and exit.
-V, --version Show version number and exit.
-q, --quiet Only show errors and above.
-v, --verbose Show info messages and above.
-d, --debug Show debug messages.
"""
from __future__ import print_function
from contextlib import contextmanager
from fnmatch import fnmatch
import logging
import os
import plistlib
import re
from subprocess import check_call, CalledProcessError
import string
import sys
from unicodedata import normalize
__version__ = "0.5"
__author__ = "Dean Jackson <deanishe@deanishe.net>"
DEFAULT_LOG_LEVEL = logging.WARNING
# Characters permitted in workflow filenames
OK_CHARS = set(string.ascii_letters + string.digits + '-.')
EXCLUDE_PATTERNS = [
'.DS_Store',
'.*',
'*.pyc',
'*.log',
'*.acorn',
'*.swp',
'*.bak',
'*.sublime-project',
'*.sublime-workflow',
'*.git',
'*.dist-info',
'*.egg-info',
'workflow-build.py',
'art',
'README.md'
]
log = logging.getLogger('')
@contextmanager
def chdir(dirpath):
"""Context-manager to change working directory."""
startdir = os.path.abspath(os.curdir)
os.chdir(dirpath)
log.debug('cwd=%s', dirpath)
yield
os.chdir(startdir)
log.debug('cwd=%s', startdir)
class TechnicolorFormatter(logging.Formatter):
"""Intelligent and pretty log formatting.
Colourise output to a TTY and prepend logging level name to
levels other than INFO.
"""
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
RESET = '\033[0m'
COLOUR_BASE = '\033[1;{:d}m'
BOLD = '\033[1m'
LEVEL_COLOURS = {
logging.DEBUG: BLUE,
logging.INFO: WHITE,
logging.WARNING: YELLOW,
logging.ERROR: MAGENTA,
logging.CRITICAL: RED
}
def __init__(self, fmt=None, datefmt=None, technicolor=True):
"""Create new Formatter.
Args:
fmt (str): A `logging.Formatter` format string.
datefmt (str): `strftime` format string.
technicolor (bool): Colourise TTY output?
"""
logging.Formatter.__init__(self, fmt, datefmt)
self.technicolor = technicolor
self._isatty = sys.stderr.isatty()
def format(self, record):
"""Format (and colourise) log record.
Prepend log level for levels other than INFO.
Colourise level names for TTY output.
"""
# Output `INFO` messages without level name.
# The idea is to treat them as normal status messages.
if record.levelno == logging.INFO:
msg = logging.Formatter.format(self, record)
return msg
# Other levels have their level name colourised if
# the destination is a TTY.
if self.technicolor and self._isatty:
colour = self.LEVEL_COLOURS[record.levelno]
bold = (False, True)[record.levelno > logging.INFO]
levelname = self.colourise('{:9s}'.format(record.levelname),
colour, bold)
else:
levelname = '{:9s}'.format(record.levelname)
return (levelname + logging.Formatter.format(self, record))
def colourise(self, text, colour, bold=False):
"""Surround `text` with terminal colours."""
colour = self.COLOUR_BASE.format(colour + 30)
output = []
if bold:
output.append(self.BOLD)
output.append(colour)
output.append(text)
output.append(self.RESET)
return ''.join(output)
def init_logging():
"""Set up logging handlers, add and configure global `log`."""
# console output
console = logging.StreamHandler()
formatter = TechnicolorFormatter('%(message)s')
console.setFormatter(formatter)
console.setLevel(logging.DEBUG)
log.addHandler(console)
def safename(name):
"""Make name filesystem and web-safe."""
if isinstance(name, str):
name = unicode(name, 'utf-8')
# remove non-ASCII
s = normalize('NFD', name)
b = s.encode('us-ascii', 'ignore')
clean = []
for c in b:
if c in OK_CHARS:
clean.append(c)
else:
clean.append('-')
return re.sub(r'-+', '-', ''.join(clean)).strip('-')
def get_workflow_files(dirpath):
"""Return all files to be included in the workflow."""
paths = []
for root, dirnames, filenames in os.walk('.'):
# Remove directories that match EXCLUDE_PATTERNS.
# Iterate through them in reverse, so as not to mess with
# the indexing
for i in range(len(dirnames) - 1, -1, -1):
dn = dirnames[i]
for pat in EXCLUDE_PATTERNS:
if fnmatch(dn, pat):
log.debug(' DEL - [%s] %s', pat, dn)
del dirnames[i]
break
# Process filenames within accepted directories
for fn in filenames:
p = os.path.join(root, fn)
for pat in EXCLUDE_PATTERNS:
if fnmatch(fn, pat):
log.debug('NOT INCLUDED - [%s] %s', pat, fn)
break
else: # didn't match any patterns
paths.append(p)
return paths
def build_workflow(workflow_dir, outputdir, overwrite=False, verbose=False,
dry_run=False):
"""Create an .alfredworkflow file from the contents of `workflow_dir`."""
with chdir(workflow_dir):
# ------------------------------------------------------------
# Read workflow metadata from info.plist
info = plistlib.readPlist(u'info.plist')
version = None
if not os.path.exists(u'info.plist'):
log.error(u'info.plist not found')
return False
if 'version' in info and info.get('version'):
version = info['version']
elif os.path.exists('version'):
with open('version') as fp:
version = fp.read().strip().decode('utf-8')
if 'variablesdontexport' in info and info.get('variablesdontexport'):
variablesdontexport = info['variablesdontexport']
if 'variables' in info and info.get('variables'):
variables = info['variables']
for key in variablesdontexport:
log.debug("Remove key " + key)
if key in variables:
log.debug("Remove key " + key)
variables[key] = ""
plistlib.writePlist(info, u'info.plist')
name = safename(info['name'])
zippath = os.path.join(outputdir, name)
if version:
zippath = '{}-v{}'.format(zippath, version)
zippath += '.alfredworkflow'
# ------------------------------------------------------------
# Workflow files
wffiles = get_workflow_files('.')
if dry_run:
print('workflow directory: ' + os.path.abspath(os.curdir))
print('workflow file: ' + zippath)
print('contents:')
for p in wffiles:
print(p)
return
# ------------------------------------------------------------
# Build workflow
if os.path.exists(zippath):
if overwrite:
log.info('overwriting existing workflow')
os.unlink(zippath)
else:
log.error('File "%s" exists. Use -f to overwrite', zippath)
return False
# build workflow
command = ['zip']
if not verbose:
command.append(u'-q')
command.append(zippath)
for p in wffiles:
command.append(p)
log.debug('command=%r', command)
try:
check_call(command)
except CalledProcessError as err:
log.error('zip exited with %d', err.returncode)
return False
log.info('wrote %s', zippath)
return True
def main(args=None):
"""Run CLI."""
# ------------------------------------------------------------
# CLI flags
init_logging()
log.setLevel(logging.DEBUG)
log.debug('log level=%s', logging.getLevelName(log.level))
# Build options
force = True
outputdir = os.path.abspath(os.curdir)
workflow_dirs = [os.path.abspath('./')]
verbose = log.level == logging.DEBUG
log.debug(u'outputdir=%r, workflow_dirs=%r', outputdir, workflow_dirs)
# ------------------------------------------------------------
# Build workflow(s)
errors = False
for path in workflow_dirs:
ok = build_workflow(path, outputdir, force, verbose)
if not ok:
errors = True
if errors:
return 1
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))