Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Move impl of mazer version from actions->mazer_version
Browse files Browse the repository at this point in the history
Move the impl details of collecting the mazer version info
for `mazer version` to ansible_galaxy.mazer_version so that
other modules can use it (namely, ansible_galaxy.cli.galaxy
for logging info at cli startup)

Fixes ansible#267
  • Loading branch information
alikins committed May 30, 2019
1 parent 4dd2fce commit b3daad3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 78 deletions.
38 changes: 2 additions & 36 deletions ansible_galaxy/actions/version.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
import logging
import os
import sys

from ansible_galaxy.utils.text import to_text
from ansible_galaxy import mazer_version

log = logging.getLogger(__name__)

VERSION_FIELDS = ('name', 'version', 'config_file', 'uname', 'executable_location', 'python_version', 'python_executable')


def version_data(config_file_path, cli_version, argv):
data = {}

data['name'] = 'mazer'
data['version'] = cli_version
data['executable_location'] = argv[0]
data['uname'] = u', '.join(os.uname())

sys_ver = u"%s" % ''.join(sys.version.splitlines())

data['python_version'] = sys_ver
data['python_executable'] = sys.executable

if config_file_path:
data['config_file'] = to_text(config_file_path)
else:
data['config_file'] = u'No config file found; using defaults'

return data


def version_repr(version_data):
lines = []

for field in VERSION_FIELDS:
lines.append(u'%s = %s' % (field, version_data.get(field, '')))

buf = u'\n'.join(lines)
return buf


def version(config_file_path, cli_version, display_callback=None):
version_buf = version_repr(version_data(config_file_path, cli_version, sys.argv))
version_buf = mazer_version.version_repr(mazer_version.version_data(config_file_path, cli_version, sys.argv))
display_callback(version_buf)
return 0
40 changes: 40 additions & 0 deletions ansible_galaxy/mazer_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import logging
import os
import sys

from ansible_galaxy.utils.text import to_text

log = logging.getLogger(__name__)

VERSION_FIELDS = ('name', 'version', 'config_file', 'uname', 'executable_location', 'python_version', 'python_executable')


def version_data(config_file_path, cli_version, argv):
data = {}

data['name'] = 'mazer'
data['version'] = cli_version
data['executable_location'] = argv[0]
data['uname'] = u', '.join(os.uname())

sys_ver = u"%s" % ''.join(sys.version.splitlines())

data['python_version'] = sys_ver
data['python_executable'] = sys.executable

if config_file_path:
data['config_file'] = to_text(config_file_path)
else:
data['config_file'] = u'No config file found; using defaults'

return data


def version_repr(version_data):
lines = []

for field in VERSION_FIELDS:
lines.append(u'%s = %s' % (field, version_data.get(field, '')))

buf = u'\n'.join(lines)
return buf
42 changes: 0 additions & 42 deletions tests/ansible_galaxy/actions/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,6 @@
log = logging.getLogger(__name__)


def test_version_data():
config_path = '/dev/null/some/faux/mazer.yml'
cli_version = '1.2.3'
args = ['/bin/mazer', 'version']

res = version.version_data(config_file_path=config_path,
cli_version=cli_version,
argv=args)

log.debug('res: %s', res)

assert isinstance(res, dict)

for field in version.VERSION_FIELDS:
assert field in res

assert res['config_file'] == config_path
assert res['version'] == cli_version
assert res['executable_location'] == args[0]


def test_version():
config_path = '/dev/null/some/faux/mazer.yml'
cli_version = '1.2.3'
Expand All @@ -47,24 +26,3 @@ def callback(*args):

lines = display_items[0].splitlines()
assert 'mazer' in lines[0]


def test_version_repr():
config_path = '/dev/null/some/faux/mazer.yml'
cli_version = '1.2.3'
args = ['/bin/mazer', 'version']

data = version.version_data(config_file_path=config_path,
cli_version=cli_version,
argv=args)

res = version.version_repr(data)
assert 'mazer.yml' in res


def test_version_repr_empty_data():
data = {}
res = version.version_repr(data)

assert 'name =' in res
assert 'version =' in res
48 changes: 48 additions & 0 deletions tests/ansible_galaxy/test_mazer_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import logging

from ansible_galaxy import mazer_version

log = logging.getLogger(__name__)


def test_version_data():
config_path = '/dev/null/some/faux/mazer.yml'
cli_version = '1.2.3'
args = ['/bin/mazer', 'version']

res = mazer_version.version_data(config_file_path=config_path,
cli_version=cli_version,
argv=args)

log.debug('res: %s', res)

assert isinstance(res, dict)

for field in mazer_version.VERSION_FIELDS:
assert field in res

assert res['config_file'] == config_path
assert res['version'] == cli_version
assert res['executable_location'] == args[0]


def test_version_repr():
config_path = '/dev/null/some/faux/mazer.yml'
cli_version = '1.2.3'
args = ['/bin/mazer', 'version']

data = mazer_version.version_data(config_file_path=config_path,
cli_version=cli_version,
argv=args)

res = mazer_version.version_repr(data)
assert 'mazer.yml' in res


def test_version_repr_empty_data():
data = {}
res = mazer_version.version_repr(data)

assert 'name =' in res
assert 'version =' in res

0 comments on commit b3daad3

Please sign in to comment.