Skip to content

Commit

Permalink
commander updates
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed May 4, 2021
1 parent 626ce88 commit 2a54c13
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/ploomber/io/_commander.py
Expand Up @@ -96,29 +96,29 @@ def rm(self, *args):
for f in args:
_delete(f)

def copy_template(self, path, requires_manual_edit=False, **render_kwargs):
def copy_template(self, path, **render_kwargs):
path = Path(path)
dst = Path(self.workspace, path.name)

if path.exists():
# This message is no longer valid since this is only called
# when there is no env yet
if dst.exists():
self.success(f'Using existing {path!s}...')
else:
self.warn(f'Missing {path!s}, adding it...')
path.parent.mkdir(exist_ok=True, parents=True)
self.warn(f'Missing {dst!s}, adding it...')
dst.parent.mkdir(exist_ok=True, parents=True)
content = self._env.get_template(str(path)).render(**render_kwargs)
path.write_text(content)

if requires_manual_edit:
raise ValueError(f'Edit {path}')
dst.write_text(content)

def cd(self, dir_):
os.chdir(dir_)

def cp(self, src, dir_):
def cp(self, src):
"""
Copies a file from another folder, replacing it if necessary. Used
mainly for preparing Dockerfiles since they can only copy from the
current working directory. Files are deleted after exiting the
context manager
Copies a file from another folder to the workspace, replacing it if
necessary. Used mainly for preparing Dockerfiles since they can only
copy from the current working directory. Files are deleted after
exiting the context manager
"""
path = Path(src)

Expand All @@ -127,7 +127,7 @@ def cp(self, src, dir_):
f'Missing {src} file. Add it to your root folder.')

# convert to absolute to ensure we delete the right file on __exit__
dst = Path(dir_, path.name).resolve()
dst = Path(self.workspace, path.name).resolve()
self._to_delete.append(dst)

_delete(dst)
Expand All @@ -144,10 +144,11 @@ def create_if_not_exists(self, name):
Path(name).write_text(content)
self._names.append(name)

# ONLY used by lambda config
def append(self, name, dst):
# TODO: keep a copy of the original content to restore if needed
content = self._env.get_template(name).render()
def append(self, name, dst, **kwargs):
if not Path(dst).exists():
Path(dst).touch()

content = self._env.get_template(name).render(**kwargs)
original = Path(dst).read_text()
Path(dst).write_text(original + '\n' + content)

Expand Down

0 comments on commit 2a54c13

Please sign in to comment.