Skip to content

Commit

Permalink
use command callability from ubiquerg; generalize identical logic/flo…
Browse files Browse the repository at this point in the history
…w across data structures
  • Loading branch information
vreuter committed Jul 1, 2019
1 parent 4d2bd20 commit da647d7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 54 deletions.
6 changes: 5 additions & 1 deletion docs/changelog.md
@@ -1,8 +1,12 @@
# Changelog

## [0.23.0] - Unreleased
### Changed
- Remove `is_command_callable` from `utils` module; instead, refer to [`ubiquerg`](https://pypi.org/project/ubiquerg/).

## [0.22.2] - 2019-06-20
### Changed
- Remove `ngstk` requirement
- Remove `ngstk` requirement.

## [0.22.1] - 2019-06-19
### Changed
Expand Down
2 changes: 1 addition & 1 deletion peppy/_version.py
@@ -1 +1 @@
__version__ = "0.22.2"
__version__ = "0.23.0-dev"
64 changes: 13 additions & 51 deletions peppy/utils.py
@@ -1,23 +1,23 @@
""" Helpers without an obvious logical home. """

from collections import Counter, defaultdict, Iterable, Sized
from collections import Counter, defaultdict, Iterable, Mapping, Sized
import contextlib
import logging
import os
import random
import string
import yaml
from .const import SAMPLE_INDEPENDENT_PROJECT_SECTIONS
from ubiquerg import is_collection_like
from ubiquerg import is_collection_like, is_command_callable


_LOGGER = logging.getLogger(__name__)


__all__ = [
"CommandChecker", "add_project_sample_constants", "count_repeats",
"get_logger", "fetch_samples", "grab_project_data",
"has_null_value", "is_command_callable", "type_check_strict"
"get_logger", "fetch_samples", "grab_project_data", "has_null_value",
"type_check_strict"
]


Expand Down Expand Up @@ -450,38 +450,20 @@ def __init__(self, path_conf_file,
s, self.path)
continue
# Test each of the section's commands.
try:
# Is section's data a mapping?
commands_iter = section_data.items()
self._logger.debug("Processing section '%s' data "
"as mapping", s)
for name, command in commands_iter:
failed = self._store_status(section=s, command=command,
name=name)
self._logger.debug("Command '%s': %s", command,
"FAILURE" if failed else "SUCCESS")
except AttributeError:
self._logger.debug("Processing section '%s' data as list", s)
commands_iter = conf_data[s]
for cmd_item in commands_iter:
# Item is K-V pair?
try:
name, command = cmd_item
except ValueError:
# Treat item as command itself.
name, command = "", cmd_item
success = self._store_status(section=s, command=command,
name=name)
self._logger.debug("Command '%s': %s", command,
"SUCCESS" if success else "FAILURE")

def _store_status(self, section, command, name):
cmds = list(section_data.values()) if isinstance(section_data, Mapping) \
else [c[1] if isinstance(c, (tuple, list)) else c for c in section_data]
for command in cmds:
succ = self._store_status(section=s, command=command)
self._logger.debug("Command '%s': %s", command,
"SUCCESS" if succ else "FAILURE")

def _store_status(self, section, command):
"""
Based on new command execution attempt, update instance's
data structures with information about the success/fail status.
Return the result of the execution test.
"""
succeeded = is_command_callable(command, name)
succeeded = is_command_callable(command)
# Store status regardless of its value in the instance's largest DS.
self.section_to_status_by_command[section][command] = succeeded
if not succeeded:
Expand All @@ -505,23 +487,3 @@ def failed(self):
if not self.section_to_status_by_command:
raise ValueError("No commands validated")
return 0 == len(self.failures)


def is_command_callable(command, name=""):
"""
Check if command can be called.
:param str command: actual command to call
:param str name: nickname/alias by which to reference the command, optional
:return bool: whether given command's call succeeded
"""

# Use `command` to see if command is callable, store exit code
code = os.system(
"command -v {0} >/dev/null 2>&1 || {{ exit 1; }}".format(command))

if code != 0:
alias_value = " ('{}') ".format(name) if name else " "
_LOGGER.debug("Command '{0}' is not callable: {1}".
format(alias_value, command))
return not bool(code)
2 changes: 1 addition & 1 deletion requirements/requirements-all.txt
Expand Up @@ -3,4 +3,4 @@ pandas>=0.20.2
pyyaml>=5
divvy>=0.3.2
logmuse>=0.2
ubiquerg>=0.4.3
ubiquerg>=0.4.5

0 comments on commit da647d7

Please sign in to comment.