-
Notifications
You must be signed in to change notification settings - Fork 14
/
models.py
310 lines (244 loc) · 9.53 KB
/
models.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
# Standard library
import json
import os
import shutil
import subprocess
import sys
import time
from glob import glob
# Packages
from dotenv import load_dotenv
from termcolor import cprint
# Local
from canonicalwebteam.dotrun.file_helpers import file_md5
class State:
def __init__(self, filepath):
"""
Manage a JSON object in a file,
as you would a dictionary
"""
self.filepath = filepath
def __getitem__(self, key):
if not os.path.isfile(self.filepath):
return None
with open(self.filepath) as state_file:
return json.load(state_file).get(key)
def __setitem__(self, key, value):
if os.path.isfile(self.filepath):
with open(self.filepath) as state_file:
state = json.load(state_file)
else:
state = {}
state[key] = value
with open(self.filepath, "w") as state_file:
return json.dump(state, state_file)
class Logger:
def note(self, note):
cprint(f"- {note}", "magenta")
def error(self, error):
cprint(f"\n= {error} =\n", "red")
def step(self, title, aside=None):
if aside:
cprint(f"\n[ {title} ]", "cyan", end="")
cprint(f" ( {aside} )\n", "magenta", attrs=["dark"])
else:
cprint(f"\n[ {title} ]\n", "cyan")
class Project:
"""
A class for performing operations on a project directory
"""
def __init__(self, path, env_extra):
self.path = path
self.log = Logger()
self.statefile_path = f"{self.path}/.dotrun.json"
self.state = State(self.statefile_path)
self.env_extra = {}
self.pyenv_dir = ".venv"
self.pyenv_path = f"{self.path}/{self.pyenv_dir}"
# Check all env values are string format
for key, value in env_extra.items():
self.env_extra[key] = str(value)
def has_script(self, script_name):
"""
Check if the project's package.json contains the named script
"""
with open(f"{self.path}/package.json") as json_file:
return script_name in json.load(json_file).get("scripts", {})
def install(self, force=False):
"""
Install dependencies from requirements.txt and package.json,
if there have been any changes detected
"""
self._install_yarn_dependencies(force=force)
self._install_python_dependencies(force=force)
def clean(self):
"""
Clean all dotrun data from project
"""
self.yarn_run("clean", exit_on_error=False)
if os.path.isfile(self.statefile_path):
self.log.step(f"Removing `.dotrun.json` state file")
os.remove(self.statefile_path)
if os.path.isdir("node_modules"):
self.log.step(f"Removing `node_modules`")
shutil.rmtree("node_modules")
if os.path.isdir(self.pyenv_path):
self.log.step(f"Removing `.venv` python environment")
shutil.rmtree(self.pyenv_path)
def yarn_run(self, script_name, arguments=[], exit_on_error=True):
"""
Run `yarn run {script_name}`, but check if the script exists first
"""
error = ""
if not os.path.isfile(os.path.join(f"{self.path}/package.json")):
error = f"package.json not found in {self.path}"
else:
with open(f"{self.path}/package.json") as json_file:
if script_name not in json.load(json_file).get("scripts", {}):
error = f"'{script_name}' script not found in package.json"
if error:
if exit_on_error:
self.log.error(error)
sys.exit(1)
else:
self.log.note(error)
return False
self.exec(
["yarn", "run", script_name] + arguments,
exit_on_error=exit_on_error,
)
def exec(self, commands, exit_on_error=True):
"""
Run commands in the environment
"""
result = None
load_dotenv(dotenv_path=f"{self.path}/.env")
load_dotenv(dotenv_path=f"{self.path}/.env.local")
env = os.environ
env.update(self.env_extra)
# This is kinda nuts, but adding this line to `.yarnrc` before
# we run yarn is actually the only way
# to avoid an error about access to /home/{user}/.npmrc
snap_home = os.environ.get("SNAP_USER_DATA")
if snap_home:
with open(f"{snap_home}/.yarnrc", "w") as yarnconfig:
yarnconfig.write("--no-default-rc true")
if os.path.isfile(f"{self.pyenv_path}/bin/python3"):
env["VIRTUAL_ENV"] = self.pyenv_path
env["PATH"] = self.pyenv_path + "/bin:" + env["PATH"]
env.pop("PYTHONHOME", None)
self.log.step(
f"$ {' '.join(commands)}",
aside=f"virtualenv `{self.pyenv_dir}`",
)
else:
self.log.step(f"$ {' '.join(commands)}")
try:
result = subprocess.check_call(commands, env=env, cwd=self.path)
except KeyboardInterrupt:
self.log.step(f"`{' '.join(commands)}` cancelled - exiting")
time.sleep(1)
except subprocess.CalledProcessError as call_error:
self.log.error(f"`{' '.join(commands)}` errored")
if exit_on_error:
sys.exit(call_error.returncode)
print("")
return result
# Node dependencies
def _get_yarn_state(self):
"""
Save package.json, lockfile and installed dependencies state
"""
# Get installed package versions
with open(
os.path.join(self.path, "package.json"), "rb"
) as package_json:
package_settings = json.load(package_json)
dependencies = package_settings.get("dependencies", {})
dependencies.update(package_settings.get("devDependencies", {}))
package_jsons = glob(f"{self.path}/node_modules/*/package.json")
packages = {}
# Read package.json dependencies
for package_json in package_jsons:
with open(package_json, "r") as package_contents:
package = json.load(package_contents)
packages[package["name"]] = package["version"]
lock_hash = None
if os.path.isfile(os.path.join(self.path, "yarn.lock")):
lock_hash = file_md5(os.path.join(self.path, "yarn.lock"))
return {
"dependencies": dependencies,
"installed_packages": packages,
"lockfile_hash": lock_hash,
}
def _install_yarn_dependencies(self, force=False):
"""
Install yarn dependencies if anything has changed
"""
if not os.path.isfile(os.path.join(f"{self.path}/package.json")):
self.log.error(f"package.json not found in {self.path}")
sys.exit(1)
changes = False
if not force:
current_state = self._get_yarn_state()
previous_state = self.state["yarn"]
changes = current_state != previous_state
if changes or force:
if changes:
self.log.note("Yarn dependencies have changed, reinstalling")
if force:
self.log.note("Installing yarn dependencies (forced)")
self.exec(["yarn", "install"])
self.state["yarn"] = self._get_yarn_state()
else:
self.log.note("Yarn dependencies up to date")
# Python dependencies
def _get_python_state(self):
"""
Save requirements.txt and installed dependencies state
"""
package_dirs = glob(
f"{self.pyenv_path}/lib/python*/site-packages/*.*-info"
)
packages = [os.path.basename(path) for path in package_dirs]
with open(f"{self.path}/requirements.txt", "r") as requirements_file:
requirements = requirements_file.read()
return {"requirements": requirements, "installed_packages": packages}
def _install_python_dependencies(self, force=False):
"""
Install python dependencies if anything has changed
"""
if not os.path.isfile(os.path.join(self.path, "requirements.txt")):
self.log.note(f"No requirements.txt found")
return False
changes = False
if not os.path.isdir(self.pyenv_path):
self.log.note(f"Creating python environment: {self.pyenv_dir}")
python_path = shutil.which("python3")
if "SNAP_REVISION" in os.environ:
python_path = python_path.replace(
"/snap/dotrun/" + os.environ["SNAP_REVISION"],
"/snap/dotrun/current",
)
self.exec(
[
"virtualenv",
"--always-copy",
"--python",
python_path,
self.pyenv_path,
]
)
if not force:
current_state = self._get_python_state()
previous_state = self.state["python"]
changes = current_state != previous_state
if changes or force:
if changes:
self.log.note("Python dependenices have changed, reinstalling")
if force:
self.log.note("Installing python dependencies (forced)")
self.exec(["pip3", "install", "--requirement", "requirements.txt"])
self.state["python"] = self._get_python_state()
else:
self.log.note("Python dependencies up to date")