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

Added prefix option to allow prefixed command. #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 16 additions & 12 deletions tdaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
import subprocess
import datetime
import re
import subprocess

SPECIAL_CHARS_REGEX_PATTERN = r'[#&;`|*?~<>^()\[\]{}$\\]+'
SPECIAL_CHARS_REGEX_PATTERN = r'[#&;`|*?~<>^()\[\]{}$\\]+'
IGNORE_EXTENSIONS = ('pyc', 'pyo')
IGNORE_DIRS = ('.bzr', '.git', '.hg', '.darcs', '.svn', '.tox')
IMPLEMENTED_TEST_PROGRAMS = ('nose', 'nosetests', 'django', 'py', 'symfony',
Expand Down Expand Up @@ -53,7 +52,8 @@ def ask(message='Are you sure? [y/N]'):
def escapearg(args):
"""Escapes characters you don't want in arguments (preventing shell
injection)"""
return re.sub(SPECIAL_CHARS_REGEX_PATTERN, '', args)
return re.sub(SPECIAL_CHARS_REGEX_PATTERN, '', args)


class Watcher(object):
"""
Expand All @@ -63,8 +63,8 @@ class Watcher(object):
file_list = {}
debug = False

def __init__(self, file_path, test_program, debug=False, custom_args='',
ignore_dirs=None, quiet=False):
def __init__(self, file_path, test_program, debug=False, custom_args='',
ignore_dirs=None, quiet=False, prefix=''):
# Safe filter
custom_args = escapearg(custom_args)

Expand All @@ -76,6 +76,7 @@ def __init__(self, file_path, test_program, debug=False, custom_args='',
self.test_program = test_program
self.custom_args = custom_args
self.quiet = quiet
self.prefix = prefix

# check configuration
self.check_configuration(file_path, test_program, custom_args)
Expand All @@ -84,7 +85,6 @@ def __init__(self, file_path, test_program, debug=False, custom_args='',
self.debug = debug
self.cmd = self.get_cmd()


def check_configuration(self, file_path, test_program, custom_args):
"""Checks if configuration is ok."""
# checking filepath
Expand Down Expand Up @@ -119,7 +119,7 @@ def check_dependencies(self):
sys.exit('django is not available on your system. Please install it and try to run it again')
if self.test_program == 'phpunit':
try:
process = subprocess.check_call(['phpunit','--version'])
process = subprocess.check_call(['phpunit','--version'])
except:
sys.exit('phpunit is not available on your system. Please install it and try to run it again')
if self.test_program == 'tox':
Expand All @@ -128,7 +128,6 @@ def check_dependencies(self):
except ImportError:
sys.exit('tox is not available on your system. Please install it and try to run it again')


def get_cmd(self):
"""Returns the full command to be executed at runtime"""

Expand All @@ -154,6 +153,9 @@ def get_cmd(self):
cmd = 'make html'
elif self.test_program == 'tox':
cmd = 'tox'
# Add custom prefix to the start of the command.
if cmd and self.prefix:
cmd = ' '.join([self.prefix, cmd])

if not cmd:
raise InvalidTestProgram("The test program %s is unknown. Valid options are: `nose`, `django` and `py`" % self.test_program)
Expand Down Expand Up @@ -201,7 +203,6 @@ def file_sizes(self):
size = sum(map(os.path.getsize, self.file_list))
return size / 1024 / 1024


def diff_list(self, list1, list2):
"""Extracts differences between lists. For debug purposes"""
for key in list1:
Expand Down Expand Up @@ -232,6 +233,7 @@ def loop(self):
self.run_tests()
self.file_list = new_file_list


def main(prog_args=None):
"""
What do you expect?
Expand All @@ -258,6 +260,9 @@ def main(prog_args=None):
parser.add_option('-y', '--quiet', dest='quiet', action="store_true",
default=False,
help="Don't ask for any input.")
parser.add_option(
'-p', '--prefix', dest='prefix', default='', type='str',
help='Define a prefix argument to append before the test program command.')

opt, args = parser.parse_args(prog_args)

Expand All @@ -266,10 +271,9 @@ def main(prog_args=None):
else:
path = '.'


try:
watcher = Watcher(path, opt.test_program, opt.debug, opt.custom_args,
opt.ignore_dirs, opt.quiet)
watcher = Watcher(path, opt.test_program, opt.debug, opt.custom_args,
opt.ignore_dirs, opt.quiet, opt.prefix)
watcher_file_size = watcher.file_sizes()
if watcher_file_size > opt.size_max:
message = "It looks like the total file size (%dMb) is larger than the `max size` option (%dMb).\nThis may slow down the file comparison process, and thus the daemon performances.\nDo you wish to continue? [y/N] " % (watcher_file_size, opt.size_max)
Expand Down