-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
181 lines (155 loc) · 3.47 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
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
"""
this module is used via the
`invoke` CLI utility.
suggested use:
pip install invoke
. <(inv --print-completion-script=zsh)
invoke --list
and so on according to self-documenting
interface.
see www.pyinvoke.org for more.
### todo
* invoke recognizes task.py when
cwd contains task.py.
running invoke from other directories
may make some of these utils more
useful.
"""
import os
import os.path as pth
from functools import (
reduce,
)
from operator import (
add,
)
from warnings import warn
from invoke import task
from invoke import exceptions as exc
from fabric import Connection
def _ensure_clojure(c: Connection):
pass
def _stop_extant_todo_list_deploy(c: Connection):
pass
def _ensure_git_repo(c: Connection):
pass
def _start_todo_list_deploy(c: Connection):
pass
@task
def deploy_todo_list(ctx, host='161.35.58.205'):
con = Connection(host)
class ProgrammingError(Exception):
pass
def _parse_ifconfig_output(
ifconfig_str):
ifconfig_lines = ifconfig_str.split('\n')
ifname = None
ifname_to_config_lines = {}
for line in ifconfig_lines:
if line and line[0] != ' ':
ifname = line.split(':')[0]
ifname_to_config_lines[ifname] = [line]
continue
if ifname is None:
raise ProgrammingError()
ifname_to_config_lines[ifname].append(line)
return {}
@task
def find_router(
c,
):
ifconfig_res = c.run(
'/sbin/ifconfig',
hide=True,
shell='zsh',
)
ifconfig_out_parsed = _parse_ifconfig_output(ifconfig_res.stdout)
ip_addr = '192.168.1.65'
ip_addr_range = '.'.join(ip_addr.split('.')[:3]+['0-255'])
# ping scan
c.run(
'nmap -sn '+ip_addr_range,
shell='zsh',
)
@task
def dir_diff(
c,
dir_a,
dir_b,
):
if not pth.isdir(dir_a):
raise exc.Exit((
dir_a +
" is not a directory"
))
if not pth.isdir(dir_b):
raise exc.Exit((
dir_b +
" is not a directory"
))
raise NotImplementedError()
def _in_place_replace_string(
search,
replacement,
filepath,
):
with open(filepath) as f:
try:
contents = f.read()
except UnicodeDecodeError as ex:
warn(("skipping " +
filepath +
" on unicode decode "
"error" +
str(ex)))
return
update = contents.replace(
search,
replacement,
)
# bp()
with open(filepath, 'w') as f:
f.write(update)
@task(
help={
'loc': (
"location could be "
"directory or file"
),
},
)
def replace_string(
c,
search,
replacement,
loc,
verbose=False,
):
for filepath in (
[loc]
if
pth.isfile(loc)
else
reduce(
add,
[[pth.join(dirpath,
filename)
for filename in filenames]
for (dirpath, _, filenames)
in os.walk(loc)])):
if verbose:
print("replacing in " +
filepath)
_in_place_replace_string(
search,
replacement,
filepath,
)
@task
def build_docs(c):
"""
build the ReStructuredText docs in doc/
"""
with c.cd('doc'):
c.run('sphinx-build . _build/',
shell='zsh',)