Skip to content

Commit

Permalink
Add option to run continuously
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
jacebrowning committed Jul 3, 2015
1 parent af413ab commit 762d736
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
15 changes: 8 additions & 7 deletions CHANGES.md
@@ -1,13 +1,14 @@
Changelog
=========
# Version History

0.1.2 (2015/05/17)
------------------
## 0.2 (dev)

- Upgrade to YORM v0.4.
- Added '--daemon' option to run continuously.

0.1.1 (2015/03/19)
------------------
## 0.1.2 (2015/05/17)

- Upgraded to YORM v0.4.

## 0.1.1 (2015/03/19)

- Initial release.

2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -174,7 +174,7 @@ pep257: depends-ci

.PHONY: pylint
pylint: depends-ci
$(PYLINT) $(PACKAGE) --rcfile=.pylintrc
$(PYLINT) $(PACKAGE) --rcfile=.pylintrc --disable=R0913

.PHONY: fix
fix: depends-dev
Expand Down
2 changes: 1 addition & 1 deletion mine/__init__.py
Expand Up @@ -3,7 +3,7 @@
import sys

__project__ = 'mine'
__version__ = '0.1.2'
__version__ = '0.2dev1'

CLI = 'mine'
VERSION = __project__ + '-' + __version__
Expand Down
29 changes: 22 additions & 7 deletions mine/cli.py
Expand Up @@ -3,6 +3,7 @@
"""Command-line interface."""

import sys
import time
import argparse

from . import CLI, VERSION, DESCRIPTION
Expand Down Expand Up @@ -34,6 +35,8 @@ def main(args=None):
# Build main parser
parser = argparse.ArgumentParser(prog=CLI, description=DESCRIPTION,
**shared)
parser.add_argument('-d', '--daemon', metavar='DELAY', nargs='?', const=60,
type=int, help="run continuously with delay [seconds]")
parser.add_argument('-f', '--file', help="custom settings file path")
subs = parser.add_subparsers(help="", dest='command', metavar="<command>")

Expand All @@ -45,15 +48,15 @@ def main(args=None):
help="computer to queue for launch (default: current)")

# Build clean parser
info = "clean up configuration and remove conflicted files"
info = "display and delete conflicted files"
sub = subs.add_parser('clean', description=info.capitalize() + '.',
help=info, **shared)
sub.add_argument('-f', '--force', action='store_true',
help="actually delete the conflicted files")

# Parse arguments
args = parser.parse_args(args=args)
kwargs = {'switch': None}
kwargs = {'delay': args.daemon}
if args.command == 'switch':
kwargs['switch'] = args.name if args.name else True
elif args.command == 'clean':
Expand Down Expand Up @@ -81,19 +84,27 @@ def main(args=None):
sys.exit(1)


def run(path=None, cleanup=True, delete=False, force=False, switch=None):
def run(path=None, cleanup=True, delay=None,
switch=None,
delete=False, force=False):
"""Run the program.
:param path: custom settings file path
:param cleanup: remove unused items from the config
:param delay: number of seconds to delay before repeating
:param switch: computer name to queue for launch
:param delete: attempt to delete conflicted files
:param force: actually delete conflicted files
"""
manager = get_manager()
root = services.find_root()
path = path or services.find_config_path(root=root)

data = Data()
yorm.sync(data, path, auto=False)
yorm.sync(data, path)

config = data.config
status = data.status
Expand All @@ -114,10 +125,14 @@ def run(path=None, cleanup=True, delete=False, force=False, switch=None):
if switch:
queue(config, status, switch)

launch(config, status, computer, manager)
update(config, status, computer, manager)
while True:
launch(config, status, computer, manager)
update(config, status, computer, manager)

yorm.update_file(data)
if delay is None:
break
log.info("delaying for %s seconds...", delay)
time.sleep(delay)

return True

Expand Down
33 changes: 30 additions & 3 deletions mine/test/test_cli.py
@@ -1,5 +1,5 @@
"""Unit tests for the 'cli' module."""
# pylint: disable=R0201
# pylint: disable=R,C

import os
import pytest
Expand Down Expand Up @@ -54,6 +54,16 @@ def test_path(self, tmpdir):

assert os.path.isfile(path)

@patch('mine.cli.run')
def test_daemon(self, mock_run):
cli.main(['--daemon'])
mock_run.assert_called_once_with(path=None, delay=60)

@patch('mine.cli.run')
def test_daemon_with_specific_delay(self, mock_run):
cli.main(['--daemon', '30'])
mock_run.assert_called_once_with(path=None, delay=30)


class TestSwitch:

Expand All @@ -63,13 +73,30 @@ class TestSwitch:
def test_switch(self, mock_run):
"""Verify a the current computer can be queued."""
cli.main(['switch'])
mock_run.assert_called_once_with(path=None, switch=True)
mock_run.assert_called_once_with(path=None, delay=None,
switch=True)

@patch('mine.cli.run')
def test_switch_specific(self, mock_run):
"""Verify a specific computer can be queued."""
cli.main(['switch', 'foobar'])
mock_run.assert_called_once_with(path=None, switch='foobar')
mock_run.assert_called_once_with(path=None, delay=None,
switch='foobar')


class TestClean:

@patch('mine.cli.run')
def test_clean(self, mock_run):
cli.main(['clean'])
mock_run.assert_called_once_with(path=None, delay=None,
delete=True, force=False)

@patch('mine.cli.run')
def test_clean_with_force(self, mock_run):
cli.main(['clean', '--force'])
mock_run.assert_called_once_with(path=None, delay=None,
delete=True, force=True)


def _mock_run(*args, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions scent.py
@@ -1,3 +1,5 @@
# pylint: disable=W0613,R,C

import os
import time
import subprocess
Expand Down

0 comments on commit 762d736

Please sign in to comment.