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 python 3 compatibility #17

Open
wants to merge 1 commit 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
38 changes: 22 additions & 16 deletions tdaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
file for more details.
"""

from __future__ import print_function

import sys
import os
Expand All @@ -19,7 +20,8 @@
import subprocess
import datetime
import re
import subprocess

if sys.version[0]=="2": input=raw_input

SPECIAL_CHARS_REGEX_PATTERN = r'[#&;`|*?~<>^()\[\]{}$\\]+'
IGNORE_EXTENSIONS = ('pyc', 'pyo')
Expand All @@ -45,7 +47,7 @@ class CancelDueToUserRequest(Exception):
def ask(message='Are you sure? [y/N]'):
"""Asks the user his opinion."""
agree = False
answer = raw_input(message).lower()
answer = input(message).lower()
if answer.startswith('y'):
agree = True
return agree
Expand All @@ -64,15 +66,15 @@ class Watcher(object):
debug = False

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

self.file_path = file_path
self.ignore_dirs = list(IGNORE_DIRS)
if ignore_dirs:
self.ignore_dirs.extend([d for d in ignore_dirs.split(',')])
self.file_list = self.walk(file_path)
self.file_list = self.walk(file_path, encoding=encoding)
self.test_program = test_program
self.custom_args = custom_args
self.quiet = quiet
Expand Down Expand Up @@ -175,9 +177,10 @@ def include(self, path):
return False
return True

def walk(self, top, file_list={}):
def walk(self, top, file_list={}, encoding='utf-8'):
"""Walks the walk. nah, seriously: reads the file and stores a hashkey
corresponding to its content."""

for root, dirs, files in os.walk(top, topdown=False):
if os.path.basename(root) in self.ignore_dirs:
# Do not dig in ignored dirs
Expand All @@ -188,12 +191,12 @@ def walk(self, top, file_list={}):
if self.include(full_path):
if os.path.isfile(full_path):
# preventing fail if the file vanishes
content = open(full_path).read()
content = open(full_path).read().encode(encoding)
hashcode = hashlib.sha224(content).hexdigest()
file_list[full_path] = hashcode
for name in dirs:
if name not in self.ignore_dirs:
self.walk(os.path.join(root, name), file_list)
self.walk(os.path.join(root, name), file_list, encoding)
return file_list

def file_sizes(self):
Expand All @@ -206,16 +209,16 @@ def diff_list(self, list1, list2):
"""Extracts differences between lists. For debug purposes"""
for key in list1:
if key in list2 and list2[key] != list1[key]:
print key
print(key)
elif key not in list2:
print key
print(key)

def run(self, cmd):
"""Runs the appropriate command"""
print datetime.datetime.now()
print(datetime.datetime.now())
output = subprocess.Popen(cmd, shell=True)
output = output.communicate()[0]
print output
print(output)

def run_tests(self):
"""Execute tests"""
Expand Down Expand Up @@ -247,6 +250,9 @@ def main(prog_args=None):
'`symfony`, `jelix` `phpunit` and `tox`')
parser.add_option("-d", "--debug", dest="debug", action="store_true",
default=False)
parser.add_option('-e', '--file-encoding', dest='file_encoding', default='utf-8',
type="str",
help="Encoding of files")
parser.add_option('-s', '--size-max', dest='size_max', default=25,
type="int", help="Sets the maximum size (in MB) of files.")
parser.add_option('--custom-args', dest='custom_args', default='',
Expand All @@ -269,23 +275,23 @@ def main(prog_args=None):

try:
watcher = Watcher(path, opt.test_program, opt.debug, opt.custom_args,
opt.ignore_dirs, opt.quiet)
opt.ignore_dirs, opt.quiet, opt.file_encoding)
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)

if not opt.quiet and not ask(message):
raise CancelDueToUserRequest('Ok, thx, bye...')

print "Ready to watch file changes..."
print("Ready to watch file changes...")
watcher.loop()
except (KeyboardInterrupt, SystemExit):
# Ignore when you exit via Crtl-C
pass
except Exception, msg:
print msg
except Exception as msg:
print(msg)

print "Bye"
print("Bye")

if __name__ == '__main__':
main()