/
examples.py
298 lines (231 loc) · 8.71 KB
/
examples.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
import platform
import stat
import os
import csv
import sys
from datetime import datetime
import json
import shutil
from pathlib import Path
import subprocess
from collections import defaultdict
import click
from ploomber.io.terminalwriter import TerminalWriter
from ploomber.table import Table
from ploomber.telemetry import telemetry
from pygments.formatters.terminal import TerminalFormatter
from pygments.lexers.markup import MarkdownLexer
from pygments import highlight
_URL = 'https://github.com/ploomber/projects'
_DEFAULT_BRANCH = 'master'
_home = Path('~', '.ploomber')
_lexer = MarkdownLexer()
_formatter = TerminalFormatter(bg="dark")
def _find_header(md):
"""Find header markers
"""
mark = '<!-- end header -->'
lines = md.splitlines()
for n, line in enumerate(lines):
if mark == line:
return n
return None
def _skip_header(md):
line = _find_header(md)
if line:
lines = md.splitlines()
return '\n'.join(lines[line + 1:])
else:
return md
def _delete_git_repo(path):
"""
If on windows, we need to change permissionsto delete the repo
"""
path_to_repo = Path(path, '.git')
if os.name == 'nt' and path_to_repo.exists():
for root, dirs, files in os.walk(path_to_repo):
for dir_ in dirs:
os.chmod(Path(root, dir_), stat.S_IRWXU)
for file_ in files:
os.chmod(Path(root, file_), stat.S_IRWXU)
def _delete(source, sub):
return source.replace(sub, '')
def _cleanup_markdown(source):
source = _delete(source, '<!-- start description -->\n')
source = _delete(source, '<!-- end description -->\n')
source = _skip_header(source)
return source
def _display_markdown(tw, path):
source = _cleanup_markdown(path.read_text())
lines = source.splitlines()
top_lines = '\n'.join(lines[:25])
tw.write(highlight(top_lines, _lexer, _formatter))
if len(lines) > 25:
tw.write(f'\n[...{str(path)} continues]\n', yellow=True)
class _ExamplesManager:
"""Class for managing examples data
"""
def __init__(self, home, branch=None):
self._home = Path(home).expanduser()
self._path_to_metadata = self._home / '.metadata'
self._examples = self._home / 'projects'
self._branch = branch or _DEFAULT_BRANCH
self._explicit_branch = branch is not None
@property
def home(self):
return self._home
@property
def examples(self):
return self._examples
@property
def path_to_metadata(self):
return self._path_to_metadata
@property
def branch(self):
return self._branch
def save_metadata(self, branch):
timestamp = datetime.now().timestamp()
metadata = json.dumps(dict(timestamp=timestamp, branch=branch))
self.path_to_metadata.write_text(metadata)
def load_metadata(self):
try:
return json.loads(self.path_to_metadata.read_text())
except Exception as e:
click.echo(f'Error loading metadata: {e}')
return None
def clone(self):
if not self.home.exists():
self.home.mkdir()
if self.examples.exists():
_delete_git_repo(self.examples)
shutil.rmtree(self.examples)
try:
subprocess.run([
'git',
'clone',
'--depth',
'1',
'--branch',
self.branch,
_URL,
str(self.examples),
],
check=True)
except Exception as e:
exception = e
else:
exception = None
if exception:
raise RuntimeError(
'An error occurred when downloading examples. '
'Verify git is installed and your internet '
f'connection. (Error message: {str(exception)!r})')
self.save_metadata(branch=self.branch)
def outdated(self):
metadata = self.load_metadata()
if metadata:
timestamp = metadata['timestamp']
then = datetime.fromtimestamp(timestamp)
now = datetime.now()
elapsed = (now - then).days
is_more_than_one_day_old = elapsed >= 1
is_different_branch = metadata.get('branch') != self.branch
if is_more_than_one_day_old:
click.echo('Examples copy is more than 1 day old...')
if is_different_branch and self._explicit_branch:
click.echo('Different branch requested...')
return is_more_than_one_day_old or (is_different_branch
and self._explicit_branch)
else:
click.echo('Cloning...')
return True
def path_to(self, name):
return self.examples / name
def path_to_readme(self):
return self.examples / 'README.md'
def list(self):
with open(self.examples / '_index.csv',
newline='',
encoding='utf-8-sig') as f:
rows = list(csv.DictReader(f))
categories = json.loads((self.examples / '_category.json').read_text())
by_category = defaultdict(lambda: [])
for row in rows:
category = row.pop('category')
del row['idx']
by_category[category].append(row)
tw = TerminalWriter()
click.echo(f'Branch: {self.branch}')
tw.sep('=', 'Ploomber examples', blue=True)
click.echo()
for category in sorted(by_category):
title = category.capitalize()
description = categories.get(category)
if description:
title = f'{title} ({description})'
tw.sep(' ', title, green=True)
click.echo()
click.echo(
Table.from_dicts(by_category[category]).to_format('simple'))
click.echo()
tw.sep('=', blue=True)
tw.write('\nTo run these examples in free, hosted '
f'environment, see instructions at: {_URL}')
tw.write('\nTo download: ploomber examples -n name -o path\n')
tw.write('Example: ploomber examples -n templates/ml-basic -o ml\n')
def main(name, force=False, branch=None, output=None):
"""
Entry point for examples
"""
manager = _ExamplesManager(home=_home, branch=branch)
tw = TerminalWriter()
if not manager.examples.exists() or manager.outdated() or force:
if not manager.examples.exists():
click.echo('Local copy does not exist...')
elif force:
click.echo('Forcing download...')
manager.clone()
if not name:
manager.list()
else:
selected = manager.path_to(name)
if not selected.exists():
telemetry.log_api("examples_error",
metadata={
'type': 'wrong_example_name',
'example_name': name
})
click.echo(f'There is no example named {name!r}.\n'
'To list examples: ploomber examples\n'
'To update local copy: ploomber examples -f')
else:
output = output or name
tw.sep('=', f'Copying example {name!r} to {output}/', green=True)
if Path(output).exists():
telemetry.log_api("examples_error",
metadata={
'type': 'duplicated_output',
'output_name': output
})
raise click.ClickException(
f"{output!r} already exists in the current working "
"directory, please rename it or move it "
"to another location and try again.")
shutil.copytree(selected, output)
path_to_readme = Path(output, 'README.md')
out_dir = output + ('\\'
if platform.system() == 'Windows' else '/')
tw.sep('=', str(path_to_readme), blue=True)
_display_markdown(tw, path_to_readme)
tw.sep('=', blue=True)
tw.sep('=', 'Installation', blue=True)
tw.write(f'Move to {out_dir} and run one of:'
f'\n* ploomber install'
f'\n* conda env create -f environment.yml'
f'\n* pip install -r requirements.txt\n')
tw.sep('=', blue=True)
telemetry.log_api("ploomber_examples",
metadata={
'argv': sys.argv,
'example_name': name
})