-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.py
146 lines (117 loc) · 4.57 KB
/
tasks.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
# coding: utf-8
import os
import re
import subprocess
import sys
import time
import zipfile
from functools import wraps
if sys.version_info < (3,):
print("Are you using Python 2?")
time.sleep(1.4)
print("Do you know what year it is?")
time.sleep(1.4)
print("—okay.")
time.sleep(1.4)
from urllib import urlretrieve
else:
from urllib.request import urlretrieve
from invoke import task, run
from furniture import tablemaker
# TODO: current-working-directory robustness
def not_if_files_exist(*filenames):
def derived_decorator(func):
@wraps(func)
def core(*args, **kwargs):
inventory = {f: os.path.exists(f) for f in filenames}
if all(inventory.values()):
print("skipping {}: {} already present".format(
func.__name__, ', '.join(filenames)))
else:
print("running {}".format(func.__name__))
return func(*args, **kwargs)
return core
return derived_decorator
CHESSBOARDJS_ZIPBALL_DOWNLOAD_PATH = os.path.join('web_client', 'resources',
'chessboardjs-0.3.0.zip')
UNDERSCORE_PATH = os.path.join('web_client', 'resources', 'public', 'js',
"underscore-min.js")
@not_if_files_exist(CHESSBOARDJS_ZIPBALL_DOWNLOAD_PATH)
def download_chessboard_js():
urlretrieve("http://chessboardjs.com/releases/0.3.0/chessboardjs-0.3.0.zip",
CHESSBOARDJS_ZIPBALL_DOWNLOAD_PATH)
@not_if_files_exist(*[
os.path.join('web_client', 'resources', 'public', subpath)
for subpath in
(os.path.join('css', "chessboard-0.3.0.min.css"),
os.path.join('js', "chessboard-0.3.0.min.js"))
])
def unpack_chessboard_js():
boardzip = zipfile.ZipFile(CHESSBOARDJS_ZIPBALL_DOWNLOAD_PATH)
for name in boardzip.namelist():
if name.startswith("js") or name.startswith("css"):
boardzip.extract(name, path=os.path.join('web_client', 'resources', 'public'))
@task
def install_chessboard_js(ctx):
download_chessboard_js()
unpack_chessboard_js()
@task
def download_underscore(ctx):
urlretrieve("http://underscorejs.org/underscore-min.js", UNDERSCORE_PATH)
ctx.run("mv {0} {0}.gz".format(UNDERSCORE_PATH))
ctx.run("gunzip {}.gz".format(UNDERSCORE_PATH))
@task
def download_statics(ctx):
install_chessboard_js(ctx)
download_underscore(ctx)
@task
def build_furniture(ctx):
tablemaker.main()
@task
def build_release(ctx):
ctx.run("cargo build --release")
ctx.run("cd web_client && lein uberjar")
ctx.run("cp target/release/leafline provisioning/leafline")
ctx.run("cp web_client/target/leafline-web-client.jar "
"provisioning/leafline-web-client.jar")
@task
def sed(ctx, pattern, replacement):
for subtree in ('src', "web_client"):
for fortress, _subsubtrees, deëdgers in os.walk(subtree):
for deëdger in deëdgers:
with open(os.path.join(fortress, deëdger), 'r+') as d:
try:
prior = d.read()
except UnicodeDecodeError:
...
else:
posterior = re.sub(pattern, replacement, prior)
if prior != posterior:
d.seek(0)
d.write(posterior)
d.truncate()
@task
def new_methodize(ctx, struct_name, src_file=None):
field_subliteral_pattern = "(\w+): ([^\s,]+),?\s+"
field_subliteral_regex = re.compile(field_subliteral_pattern)
literal_pattern = r"{} ?{{ ({})+}}".format(
struct_name, field_subliteral_pattern)
literal_regex = re.compile(literal_pattern)
if src_file is None:
filepaths = [os.path.join('src', name) for name in os.listdir('src')]
else:
filepaths = [os.path.join('src', src_file)]
for filepath in filepaths:
if not filepath.endswith(".rs"):
continue
with open(filepath) as source_file:
source = source_file.read()
for struct_literal_match in literal_regex.finditer(source):
new_args = ', '.join(subliteral_match.group(2)
for subliteral_match
in field_subliteral_regex.finditer(
struct_literal_match.group(0)))
new_call = "{}::new({})".format(struct_name, new_args)
source = source.replace(struct_literal_match.group(0), new_call)
with open(filepath, 'w') as source_file:
source_file.write(source)