forked from dagster-io/dagster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_library.py
64 lines (48 loc) · 1.94 KB
/
create_library.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
#!/usr/bin/env python3
"""
To use, run
python bin/create_library.py --name library_name
"""
from __future__ import absolute_import
import os
import shutil
import click
from git_tag import get_most_recent_git_tag
def copy_directory(src, dest):
try:
shutil.copytree(src, dest, ignore=shutil.ignore_patterns('.DS_Store'))
# Directories are the same
except shutil.Error as e:
print('Directory not copied. Error: %s' % e)
# Any error saying that the directory doesn't exist
except OSError as e:
print('Directory not copied. Error: %s' % e)
@click.command()
@click.option('--name', prompt='Name of library', help='Name of library')
def main(name):
template_library_path = os.path.abspath('bin/assets/dagster-library-tmpl')
new_template_library_path = os.path.abspath(
'python_modules/libraries/dagster-{name}'.format(name=name)
)
if os.path.exists(new_template_library_path):
raise click.UsageError('Library with name {name} already exists'.format(name=name))
copy_directory(template_library_path, new_template_library_path)
version = get_most_recent_git_tag()
for dname, _, files in os.walk(new_template_library_path):
for fname in files:
fpath = os.path.join(dname, fname)
with open(fpath) as f:
s = f.read()
s = s.replace('{{LIBRARY_NAME}}', name)
s = s.replace('{{VERSION}}', version)
with open(fpath, 'w') as f:
f.write(s)
new_fname = fname.replace('.tmpl', '')
new_fpath = os.path.join(dname, new_fname)
shutil.move(fpath, new_fpath)
print('Created {path}'.format(path=new_fpath))
new_dname = dname.replace('library-tmpl', name)
shutil.move(dname, new_dname)
print('Library created at {path}'.format(path=new_template_library_path))
if __name__ == "__main__":
main() # pylint: disable=no-value-for-parameter