-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtasks.py
259 lines (216 loc) · 7.34 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
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
"""
Source: https://gist.github.com/mfekadu/ceaa65dd158bd45dcfadbbda17b83b03
"""
from invoke import task
import os
import webbrowser
try:
from StringIO import StringIO ## for Python 2
except ImportError:
from io import StringIO ## for Python 3
"""
HELPERS
"""
def go_to_website(URL, verbose=True):
"""
given a URL, opens the browser
"""
print("Opening...", URL) if verbose else None
webbrowser.open(URL)
"""
TASKS
"""
@task(aliases=("list", "lsit", "ist", "-list", "lis", "li", "slit", "slist"))
def _dash_dash_list(c):
"""
because i forget --list often and fixz my ttypos
"""
try: # because pyinvoke issue #704
c.run("invoke --list", hide="err")
except Exception as e:
print("uh oh, https://github.com/pyinvoke/invoke/issues/704")
print("but here, try this...\n")
cmd = 'cat tasks.py | grep def | grep "(\c"' # \c avoid self-reference
print(f"$ {cmd}\n")
c.run(cmd)
@task(aliases=("gh", "repo", "remote", "origin"))
def github(c, username="calpoly-csai", repo="api"):
"""
opens the GitHub website for this project in default browser
"""
# optionally just hard code this
# TODO: look into how to read the .git/ folder to redirect based on that.
SITE = f"https://github.com/{username}/{repo}"
go_to_website(SITE)
@task(aliases=("gsit", "gst", "sgit", "gis", "gsi", "giat", "gisr", "gsot", "gost"))
def gist(
c, edit=False, username="mfekadu", gist_hash="ceaa65dd158bd45dcfadbbda17b83b03"
):
"""
opens the gist.GitHub.com website for this task.py source code
"""
SITE = f"https://gist.github.com/{username}/{gist_hash}"
SITE = f"{SITE}/edit" if edit else SITE
go_to_website(SITE)
@task(aliases=("ghd", "desktop"))
def github_desktop(c):
"""
opens the GitHub Desktop app <yes i am *that* lazy>. macOS only.
"""
c.run("open -a 'GitHub Desktop'")
@task(aliases=("invoke", "wtf", "huh", "what", "umm", "uhh", "idk"))
def go_to_invoke_docs(c):
"""
opens the docs for the PyInvoke project in default browser
"""
SITE = "https://www.pyinvoke.org"
go_to_website(SITE)
@task(help={"name": "Name of the person to say hi to."})
def hi(c, name, help=False):
"""
Say hi to someone.
"""
print("Hi {}!".format(name))
@task(aliases=("format", "black", "lint"))
def black_auto_format(c, verbose=True):
"""
Make the code look nice.
"""
print("Formatting!")
cwd = os.getcwd()
# move up to the directory that contains ".git"
# which often is the root of a repository
print("current directory: ", cwd)
while cwd != "/" and ".git" not in os.listdir(cwd):
if ".git" not in os.listdir(cwd):
os.chdir("..")
cwd = os.getcwd()
print("current directory: ", cwd)
else:
break
cmd = "black ."
print("running command: {}".format(cmd))
c.run("black .")
@task(aliases=("sc", "scala", "hi-scala", "hiscala", "helloscala"))
def hello_scala(c, verbose=True, name="hello_scala"):
"""
create a hello_world scala file
"""
filename = f"{name}.sc"
print(f"Creating {filename}")
file_content = """import scala.io._
object HelloApp {
def main(args: Array[String]): Unit = {
val coder = "Python"
val num = 21
println(s"Hello Scala from ${coder}!");
println(s"${num + num} is a cool num");
}
}
"""
with open(filename, "w") as f:
f.write(file_content)
cmd = f"cat {filename}"
print(f"$ {cmd}\n")
c.run(cmd)
cmd = f"scala {filename}"
print(f"$ {cmd}\n")
c.run(cmd)
@task(aliases=("copy", "pbcopy"))
def copy_tasks_py_to_clipboard(c):
"""
"""
cmd = "cat tasks.py | pbcopy"
print(f"$ {cmd}\n")
c.run(cmd)
@task(aliases=("ssh",))
def copy_ssh(c):
"""
"""
# https://askubuntu.com/a/811236
cmd = "ls -p ~/.ssh/ | grep -v /"
print(f"$ {cmd}\n")
c.run(cmd)
choice = input("\n\nWhich one? (enter the name): ")
print("\n\n")
cmd = f"cat ~/.ssh/{choice} | pbcopy"
print(f"$ {cmd}\n")
c.run(cmd)
@task
def docker(c, username=None, app_name="nimbus"):
"""
Locally, docker build && docker run
"""
ENV_KEY = "TASKS_DOCKER_USERNAME"
if username is not None:
print("hey run this to make life easier...")
print(f"export {ENV_KEY}={username}")
else:
try:
username = os.environ[ENV_KEY]
except Exception as e:
username = input("docker username? ")
print("hey run this to make life easier...")
print(f"export {ENV_KEY}={username}")
print("make first sure to run...")
print("source .export_env_vars")
print("\n\n")
try:
DATABASE_HOSTNAME = os.environ["DATABASE_HOSTNAME"]
DATABASE_PASSWORD = os.environ["DATABASE_PASSWORD"]
DATABASE_USERNAME = os.environ["DATABASE_USERNAME"]
DATABASE_NAME = os.environ["DATABASE_NAME"]
PYDRIVE_CLIENT_ID = os.environ["PYDRIVE_CLIENT_ID"]
PYDRIVE_CLIENT_SECRET = os.environ["PYDRIVE_CLIENT_SECRET"]
GOOGLE_DRIVE_CREDENTIALS = os.environ["GOOGLE_DRIVE_CREDENTIALS"]
GOOGLE_DRIVE_FOLDER_ID = os.environ["GOOGLE_DRIVE_FOLDER_ID"]
GOOGLE_CLOUD_NLP_CREDENTIALS = os.environ["GOOGLE_CLOUD_NLP_CREDENTIALS"]
GOOGLE_CLOUD_NLP_MODEL_NAME = os.environ["GOOGLE_CLOUD_NLP_MODEL_NAME"]
except Exception:
print("make first sure to run...")
print("source .export_env_vars")
print("\n\n")
exit()
# automatically pass in local environment variables into the docker thing
cmd = "docker build"
cmd += " --build-arg DATABASE_HOSTNAME"
cmd += " --build-arg DATABASE_PASSWORD"
cmd += " --build-arg DATABASE_USERNAME"
cmd += " --build-arg DATABASE_NAME"
cmd += " --build-arg PYDRIVE_CLIENT_ID"
cmd += " --build-arg PYDRIVE_CLIENT_SECRET"
cmd += " --build-arg GOOGLE_DRIVE_CREDENTIALS"
cmd += " --build-arg GOOGLE_DRIVE_FOLDER_ID"
cmd += " --build-arg GOOGLE_CLOUD_NLP_CREDENTIALS"
cmd += " --build-arg GOOGLE_CLOUD_NLP_MODEL_NAME"
cmd += f' -t "{username}/{app_name}" .'
print(f"$ {cmd}\n")
c.run(cmd, pty=True) # run the docker build
# http://www.pyinvoke.org/faq.html#running-local-shell-commands-run
# --rm will make sure to remove the container on exit of shell
# otherwise docker containers will eat up your storage space
cmd = f"docker run -it --rm -p 8080:8080 {username}/{app_name}"
print(f"$ {cmd}\n")
c.run(cmd, pty=True) # run the docker run
@task(aliases=("ds", "dash", "dsh"))
def docker_shell(c, image_name=None):
"""
Run docker within an interactive shell
https://stackoverflow.com/a/44769468
"""
ENV_KEY = "TASKS_DOCKER_IMAGE_NAME"
if image_name is not None:
print("hey run this to make life easier...")
print(f"export {ENV_KEY}={image_name}")
else:
try:
image_name = os.environ[ENV_KEY]
except Exception as e:
image_name = input("docker image_name? ")
print("hey run this to make life easier...")
print(f"export {ENV_KEY}={image_name}")
# --rm will make sure to remove the container on exit of shell
# otherwise docker containers will eat up your storage space
cmd = f"docker run -it --rm {image_name} sh"
print(f"$ {cmd}\n")
c.run(cmd, pty=True) # run the docker interactive shell