Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for running commands after a sync has happened #33

Merged
merged 1 commit into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ Overlay a directory from a remote repository into the destination provided.

$ gilt overlay

Overlay files and a directory and run post-overlay commands.

.. code-block:: yaml
:caption: gilt.yml

- git: https://github.com/example/subproject.git
version: master
files:
- src: subtool/test
dst: ext/subtool.test/
post_commands:
- make

- git: https://github.com/example/subtool2.git
version: master
dst: ext/subtool2/
post_commands:
- make

.. code-block:: bash

$ gilt overlay

Display the git commands being executed.

.. code-block:: bash
Expand Down
19 changes: 15 additions & 4 deletions gilt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ def config(filename):
:return: list
"""
Config = collections.namedtuple('Config', [
'git', 'lock_file', 'version', 'name', 'src', 'dst', 'files'
'git',
'lock_file',
'version',
'name',
'src',
'dst',
'files',
'post_commands',
])

return [Config(**d) for d in _get_config_generator(filename)]
Expand All @@ -59,7 +66,8 @@ def _get_files_config(src_dir, files_list):
to overlay.
:return: list
"""
FilesConfig = collections.namedtuple('FilesConfig', ['src', 'dst'])
FilesConfig = collections.namedtuple('FilesConfig',
['src', 'dst', 'post_commands'])

return [
FilesConfig(**d) for d in _get_files_generator(src_dir, files_list)
Expand All @@ -79,6 +87,7 @@ def _get_config_generator(filename):
name = '{}.{}'.format(parsedrepo.owner, parsedrepo.repo)
src_dir = os.path.join(_get_clone_dir(), name)
files = d.get('files')
post_commands = d.get('post_commands', [])
dst_dir = None
if not files:
dst_dir = _get_dst_dir(d['dst'])
Expand All @@ -89,7 +98,8 @@ def _get_config_generator(filename):
'name': name,
'src': src_dir,
'dst': dst_dir,
'files': _get_files_config(src_dir, files)
'files': _get_files_config(src_dir, files),
'post_commands': post_commands,
}


Expand All @@ -106,7 +116,8 @@ def _get_files_generator(src_dir, files_list):
for d in files_list:
yield {
'src': os.path.join(src_dir, d['src']),
'dst': _get_dst_dir(d['dst'])
'dst': _get_dst_dir(d['dst']),
'post_commands': d.get('post_commands', []),
}


Expand Down
12 changes: 12 additions & 0 deletions gilt/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,20 @@ def overlay(ctx): # pragma: no cover
git.clone(c.name, c.git, c.src, debug=debug)
if c.dst:
git.extract(c.src, c.dst, c.version, debug=debug)
post_commands = {c.dst: c.post_commands}
else:
git.overlay(c.src, c.files, c.version, debug=debug)
post_commands = {
conf.dst: conf.post_commands
for conf in c.files
}
# Run post commands if any.
for dst, commands in post_commands.items():
for command in commands:
msg = ' - running `{}` in {}'.format(command, dst)
util.print_info(msg)
cmd = util.build_sh_cmd(command, cwd=dst)
util.run_command(cmd, debug=debug)


def _setup(filename):
Expand Down
12 changes: 12 additions & 0 deletions gilt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import contextlib
import errno
import os
import sh
import shutil

import colorama
Expand Down Expand Up @@ -58,6 +59,17 @@ def run_command(cmd, debug=False):
cmd()


def build_sh_cmd(cmd, cwd=None):
"""Build a `sh.Command` from a string.

:param cmd: String with the command to convert.
:param cwd: Optional path to use as working directory.
:return: `sh.Command`
"""
args = cmd.split()
return getattr(sh, args[0]).bake(_cwd=cwd, *args[1:])


@contextlib.contextmanager
def saved_cwd():
""" Context manager to restore previous working directory. """
Expand Down
17 changes: 17 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,20 @@ def test_copy_dir(temp_dir):
def test_copy_raises(temp_dir):
with pytest.raises(OSError):
util.copy('invalid-src', 'invalid-dst')


def test_build_sh_cmd_simple_command():
cmd = util.build_sh_cmd('ls')
assert '/bin/ls' == cmd._path


def test_build_sh_cmd_command_with_args():
cmd = util.build_sh_cmd('ls /tmp')
assert '/bin/ls' == cmd._path
assert ['/tmp'] == cmd._partial_baked_args


def test_build_sh_cmd_command_with_cwd(temp_dir):
cmd = util.build_sh_cmd('ls', cwd=temp_dir)
assert '/bin/ls' == cmd._path
assert temp_dir == cmd._partial_call_args['cwd']