Skip to content

Commit

Permalink
changes according to review.\n- FILE instead ARCHIVE\n- Better --vers…
Browse files Browse the repository at this point in the history
…ion\n- CWD as anchor for extracted files
  • Loading branch information
dorpvom committed May 3, 2019
1 parent 12ed6cc commit 294fe68
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
47 changes: 37 additions & 10 deletions extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@
from pathlib import Path
from tempfile import TemporaryDirectory

__VERSION__ = '0.1'
VERSION = '0.1'
NAME = 'FACT_extractor interface'
DEFAULT_CONTAINER = 'fkiecad/fact_extractor'


def parse_arguments():
parser = argparse.ArgumentParser(description='FACT extractor CLI')
parser.add_argument('-v', '--version', action='version', version=__VERSION__)
parser.add_argument('-c', '--container', help='docker container', default='fkiecad/fact_extractor')
parser = argparse.ArgumentParser(
description='Command line interface for FACT_extractor.\nExtract arbitrary container or compression formats with one utility.'
)
parser.add_argument('-v', '--version', action='version', version=set_version())
parser.add_argument('-c', '--container', help='docker container', default=DEFAULT_CONTAINER)
parser.add_argument('-m', '--memory', help='memory limit for docker container (in MB)', default='512')
parser.add_argument('-o', '--output_directory', help='path to extracted files', default=None)
parser.add_argument('-r', '--report_file', help='write report to a file', default=None)
parser.add_argument('-V', '--verbose', action='store_true', default=False, help='increase verbosity')
parser.add_argument('ARCHIVE', type=str, nargs=1, help='Archive for extraction')
parser.add_argument('FILE', type=str, nargs=1, help='File for extraction')

return parser.parse_args()

Expand All @@ -39,7 +43,29 @@ def setup_logging(verbose):


def container_exists(container):
return subprocess.run('docker history {}'.format(container), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).returncode == 0
return subprocess.run('docker history {}'.format(container), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).returncode == 0


def default_container_status():
try:
process_result = subprocess.run(
'docker image ls {} --format "{{{{.Tag}}}},{{{{.CreatedAt}}}}"'.format(DEFAULT_CONTAINER), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
tag, creation_time = process_result.stdout.decode().strip().split(',')
except ValueError:
tag, creation_time = 'n/e', 'n/e'
return tag, creation_time


def set_version():
container_tag, container_creation = default_container_status()

return '{}\nProgramm version:\t0.1\nDefault container:\t{}\nContainer tag:\t\t{}\nContainer creation:\t{}'.format(
NAME,
DEFAULT_CONTAINER,
container_tag,
container_creation
)


def call_docker(input_file, container, target, report_file, memory_limit, tmpdir=None):
Expand Down Expand Up @@ -75,17 +101,18 @@ def main():
arguments = parse_arguments()
setup_logging(arguments.verbose)

output_directory = arguments.output_directory if arguments.output_directory else str(Path(__file__).parent / 'extracted_files')
output_directory = arguments.output_directory if arguments.output_directory else str(Path('.') / 'extracted_files')
if Path(output_directory).exists():
logging.error('Target directory exists ({}). Please choose a non-existing directory with -o option.'.format(output_directory))
return 1

if not container_exists(arguments.container):
logging.error('Container {} doesn\'t exist. Please specify an existing container with the -c option.'.format(arguments.container))
logging.info('You can download the default container with "docker pull {}"'.format(DEFAULT_CONTAINER))
return 1

if not Path(arguments.ARCHIVE[0]).is_file():
logging.error('Given input file {} doesn\'t exist. Please give an existing path.'.format(arguments.ARCHIVE[0]))
if not Path(arguments.FILE[0]).is_file():
logging.error('Given input file {} doesn\'t exist. Please give an existing path.'.format(arguments.FILE[0]))
return 1

if arguments.report_file and not Path(arguments.report_file).parent.is_dir():
Expand All @@ -96,7 +123,7 @@ def main():
logging.warning('Warning: Report file will be overwritten.')

call_docker(
input_file=arguments.ARCHIVE[0],
input_file=arguments.FILE[0],
container=arguments.container,
target=output_directory,
report_file=arguments.report_file,
Expand Down
8 changes: 6 additions & 2 deletions test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class ProcessResult:
def __init__(self, rc):
self.returncode = rc

@property
def stdout(self):
return b''

if command.endswith('fail'):
return ProcessResult(255)
return ProcessResult(0)
Expand All @@ -20,15 +24,15 @@ def __init__(self, rc):
def test_parse_arguments(monkeypatch):
monkeypatch.setattr('extract.sys.argv', ['extract.py', 'ANY'])
args = parse_arguments()
assert args.ARCHIVE[0] == 'ANY'
assert args.FILE[0] == 'ANY'


def test_parse_arguments_no_archive(monkeypatch, capsys):
monkeypatch.setattr('extract.sys.argv', ['extract.py'])

with pytest.raises(SystemExit) as sys_exit:
parse_arguments()
assert 'required: ARCHIVE' in capsys.readouterr().err
assert 'required: FILE' in capsys.readouterr().err
assert sys_exit.value.code == 2


Expand Down

0 comments on commit 294fe68

Please sign in to comment.