Skip to content

Commit

Permalink
Set maximum McCabe complexity for project to 10 and refactor violations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Sep 28, 2013
1 parent a30f79a commit 79beff4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ install:
- pip install drmaa
- sudo adduser --quiet --disabled-password --gecos TEST u1 ## Create user for run-as-user test.
# - sudo apt-get install condor
script: . local_env.sh; pyflakes lwr test && flake8 lwr test && nosetests
script: . local_env.sh; pyflakes lwr test && flake8 --max-complexity 10 lwr test && nosetests
after_success:
- coveralls

Expand Down
74 changes: 46 additions & 28 deletions lwr/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,38 +94,56 @@ def __add_args(self, args, func_args, arg_values):
if func_arg not in args and func_arg in arg_values:
args[func_arg] = arg_values[func_arg]

def __handle_access(self, req, environ, start_response):
access_response = None
if hasattr(self, '_check_access'):
access_response = self._check_access(req, environ, start_response)
return access_response

def __build_args(self, func, args, req, environ):
args = build_func_args(func, args, req.GET, self._app_args(args, req))
func_args = inspect.getargspec(func).args

for func_arg in func_args:
if func_arg == "ip":
args["ip"] = self.__get_client_address(environ)

if 'body' in func_args:
args['body'] = req.body_file

return args

def __execute_request(self, func, args, req, environ):
args = self.__build_args(func, args, req, environ)
try:
result = func(**args)
except exc.HTTPException as e:
result = e
return result

def __build_response(self, result):
if self.response_type == 'file':
resp = Response()
path = result
if exists(path):
resp.app_iter = FileIterator(path)
else:
raise exc.HTTPNotFound("No file found with path %s." % path)
else:
resp = Response(body=self.body(result))
return resp

def __call__(self, func):
def controller_replacement(environ, start_response, **args):
req = Request(environ)

if hasattr(self, '_check_access'):
access_response = self._check_access(req, environ, start_response)
if access_response:
return access_response

args = build_func_args(func, args, req.GET, self._app_args(args, req))
func_args = inspect.getargspec(func).args

for func_arg in func_args:
if func_arg == "ip":
args["ip"] = self.__get_client_address(environ)

if 'body' in func_args:
args['body'] = req.body_file

try:
result = func(**args)
except exc.HTTPException as e:
result = e
if self.response_type == 'file':
resp = Response()
path = result
if exists(path):
resp.app_iter = FileIterator(path)
else:
raise exc.HTTPNotFound("No file found with path %s." % path)
else:
resp = Response(body=self.body(result))
access_response = self.__handle_access(req, environ, start_response)
if access_response:
return access_response

result = self.__execute_request(func, args, req, environ)
resp = self.__build_response(result)

return resp(environ, start_response)
controller_replacement.func = func
controller_replacement.response_type = self.response_type
Expand Down
14 changes: 7 additions & 7 deletions lwr/lwr_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,21 @@ def fetch_output(self, path, working_directory, action='transfer'):
"""
name = os.path.basename(path)
output_type = self._get_output_type(name)

if output_type == "none":
# Just make sure the file was created.
if not os.path.exists(path):
raise OutputNotFoundException(path)
return

if output_type == "direct":
output_path = path
elif output_type == "task":
output_path = os.path.join(working_directory, name)
elif output_type == "none":
if action == "transfer":
raise OutputNotFoundException(path)
else:
raise Exception("Unknown output_type returned from LWR server %s" % output_type)
if action == 'transfer':
self.__raw_download_output(name, self.job_id, output_type, output_path)
elif output_type == 'none':
# Just make sure the file was created.
if not os.path.exists(path):
raise OutputNotFoundException(path)
elif action == 'copy':
lwr_path = self._output_path(name, self.job_id, output_type)['path']
self._copy(lwr_path, output_path)
Expand Down
44 changes: 26 additions & 18 deletions lwr/managers/util/kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,35 @@ def _psutil_kill_pid(pid):


def _stock_kill_pid(pid):
def __check_pid():
is_windows = system() == 'Windows'

if is_windows:
__kill_windows(pid)
else:
__kill_posix(pid)


def __kill_windows(pid):
try:
Popen("taskkill /F /T /PID %i" % pid, shell=True)
except Exception:
pass


def __kill_posix(pid):
def __check_pid(pid):
try:
os.kill(pid, 0)
return True
except OSError:
return False

is_windows = system() == 'Windows'

if is_windows:
try:
Popen("taskkill /F /T /PID %i" % pid, shell=True)
except Exception:
pass
else:
if __check_pid():
for sig in [15, 9]:
try:
os.killpg(pid, sig)
except OSError:
return
sleep(1)
if not __check_pid():
return
if __check_pid():
for sig in [15, 9]:
try:
os.killpg(pid, sig)
except OSError:
return
sleep(1)
if not __check_pid():
return
15 changes: 11 additions & 4 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,17 @@ def outputs_directory(self, job_id):

@contextmanager
def test_job_directory():
temp_directory = mkdtemp()
job_directory = JobDirectory(temp_directory, '1')
yield job_directory
rmtree(temp_directory)
with temp_directory() as directory:
yield JobDirectory(directory, '1')


@contextmanager
def temp_directory():
directory = mkdtemp()
try:
yield directory
finally:
rmtree(directory)


@contextmanager
Expand Down

0 comments on commit 79beff4

Please sign in to comment.