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

Commit

Permalink
Drop option to produce rankfiles in yaml (there is more to come).
Browse files Browse the repository at this point in the history
  • Loading branch information
spookey committed Dec 8, 2015
1 parent 60bc625 commit 2b91152
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
37 changes: 17 additions & 20 deletions ffflash/inc/rankfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,56 @@
def _rankfile_load(ff):
'''
Load either existing ``rankfile`` from disk, or create empty stub
if one does not exist yet. Path and extension (*json* or *yaml*)
if one does not exist yet. Path and extension (*json*)
get validated.
:param ff: running :class:`ffflash.main.FFFlash` instance
:return: Tuple of either (``False``, ``None``, ``None``) on error or:
:return: Tuple of either (``False``, ``None``) on error or:
* validated path to the ``rankfile``
* ``rankfile`` content
* ``True`` if ``rankfile`` is a *yaml* file, ``False`` if it's *json*
'''
if not ff.access_for('rankfile'):
return (False, None, None)
return (False, None)
ff.log('handling rankfile {}'.format(ff.args.rankfile))

rankfile = check_file_location(ff.args.rankfile, must_exist=False)
if not rankfile:
return ff.log(
'wrong path for rankfile {}'.format(ff.args.rankfile),
level=False
), None, None
), None

_, ext = path.splitext(rankfile)
if not ext or ext.lower() not in ['.yaml', '.json']:
if not ext or ext.lower() not in ['.json']:
return ff.log(
'rankfile {} {} is neither json nor yaml'.format(rankfile, ext),
'rankfile {} {} is no json'.format(rankfile, ext),
level=False
), None, None
), None

as_yaml = True if ext == '.yaml' else False
ranks = load_file(rankfile, fallback={
'updated_at': 'never', 'nodes': []
}, as_yaml=as_yaml)
}, as_yaml=False)

if not ranks or not isinstance(ranks, dict):
return ff.log(
'could not load rankfile {}'.format(rankfile),
level=False
), None, None
), None

if not all([(a in ranks) for a in ['nodes', 'updated_at']]):
return ff.log(
'this is no rankfile {}'.format(rankfile),
level=False
), None, None
), None

lranks = len(ranks.get('nodes', 0))
ff.log((
'creating new rankfile {}'.format(rankfile)
if lranks == 0 else
'loaded {} nodes'.format(lranks)
))
return rankfile, ranks, as_yaml
return rankfile, ranks


def _rankfile_score(ff, ranks, nodelist):
Expand Down Expand Up @@ -111,29 +109,28 @@ def _rankfile_score(ff, ranks, nodelist):
return ranks


def _rankfile_dump(ff, rankfile, ranks, as_yaml):
def _rankfile_dump(ff, rankfile, ranks):
'''
Store ranks in ``rankfile``. Also sets a timestamp and writes the
release string into the output.
:param ff: running :class:`ffflash.main.FFFlash` instance
:param rankfile: validated path to the ``rankfile``
:param ranks: content to store
:param as_yaml: dump as *yaml* instead of *json*
:return: ``True`` on success, or ``False`` on error
'''
if not ff.access_for('rankfile'):
return False
if not all([
rankfile, ranks, isinstance(ranks, dict), all([
(a in ranks) for a in ['nodes', 'updated_at'] if ranks
]), (as_yaml is not None)
])
]):
return ff.log('wrong input data passed', level=False)

ranks['updated_at'] = get_iso_timestamp()
ranks['version'] = info.release
dump_file(rankfile, ranks, as_yaml=as_yaml)
dump_file(rankfile, ranks, as_yaml=False)

ff.log('successfully stored rankfile {}'.format(rankfile))
return True
Expand All @@ -152,12 +149,12 @@ def handle_rankfile(ff, nodelist):
if not nodelist or not isinstance(nodelist, dict):
return False

rankfile, ranks, as_yaml = _rankfile_load(ff)
if not all([rankfile, ranks, (as_yaml is not None)]):
rankfile, ranks = _rankfile_load(ff)
if not all([rankfile, ranks]):
return False

ranks = _rankfile_score(ff, ranks, nodelist)
if not ranks:
return False

return _rankfile_dump(ff, rankfile, ranks, as_yaml)
return _rankfile_dump(ff, rankfile, ranks)
22 changes: 11 additions & 11 deletions tests/inc/rankfile/test_rankfile_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,43 @@
def test_rankfile_dump_no_access(tmpdir, fffake):
ff = fffake(tmpdir.join('api_file.json'), dry=True)

assert _rankfile_dump(ff, None, {}, {}) is False
assert _rankfile_dump(ff, None, {}) is False

assert tmpdir.remove() is None


def test_rankfile_dump_wrong_input(tmpdir, fffake):
apifile = tmpdir.join('api_file.json')
apifile.write_text(dumps({'a': 'b'}), 'utf-8')
rf = tmpdir.join('rankfile.yaml')
rf = tmpdir.join('rankfile.json')

ff = fffake(
apifile, nodelist=tmpdir.join('nodelist.json'),
rankfile=tmpdir.join('rankfile.yaml'), dry=True
rankfile=tmpdir.join('rankfile.json'), dry=True
)

assert _rankfile_dump(ff, None, None, None) is False
assert _rankfile_dump(ff, 'test', {}, None) is False
assert _rankfile_dump(ff, str(rf), {}, True) is False
assert _rankfile_dump(ff, str(rf), {'nodes': []}, True) is False
assert _rankfile_dump(ff, str(rf), {'updated_at': 'now'}, True) is False
assert _rankfile_dump(ff, None, None) is False
assert _rankfile_dump(ff, 'test', {}) is False
assert _rankfile_dump(ff, str(rf), {}) is False
assert _rankfile_dump(ff, str(rf), {'nodes': []}) is False
assert _rankfile_dump(ff, str(rf), {'updated_at': 'now'}) is False

assert tmpdir.remove() is None


def test_rankfile_dump_data(tmpdir, fffake):
apifile = tmpdir.join('api_file.json')
apifile.write_text(dumps({'a': 'b'}), 'utf-8')
rf = tmpdir.join('rankfile.yaml')
rf = tmpdir.join('rankfile.json')
rk = {'updated_at': 'never', 'nodes': [{'a': 'b'}, {'c': 'd'}]}

assert tmpdir.listdir() == [apifile]
ff = fffake(
apifile, nodelist=tmpdir.join('nodelist.json'),
rankfile=tmpdir.join('rankfile.json'), dry=True
rankfile=rf, dry=True
)

assert _rankfile_dump(ff, str(rf), rk, False) is True
assert _rankfile_dump(ff, str(rf), rk) is True

assert tmpdir.listdir() == [apifile, rf]
r = loads(rf.read_text('utf-8'))
Expand Down
17 changes: 8 additions & 9 deletions tests/inc/rankfile/test_rankfile_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def test_rankfile_load_no_access(tmpdir, fffake):
ff = fffake(tmpdir.join('api_file.json'), dry=True)

assert _rankfile_load(ff) == (False, None, None)
assert _rankfile_load(ff) == (False, None)

assert tmpdir.remove() is None

Expand All @@ -25,7 +25,7 @@ def test_rankfile_load_wrong_location(tmpdir, fffake):
assert ff.access_for('nodelist') is True
assert ff.access_for('rankfile') is True

assert _rankfile_load(ff) == (False, None, None)
assert _rankfile_load(ff) == (False, None)

assert tmpdir.remove() is None

Expand All @@ -42,11 +42,10 @@ def test_rankfile_load_wrong_extension(tmpdir, fffake, capsys):
assert ff.access_for('nodelist') is True
assert ff.access_for('rankfile') is True

assert _rankfile_load(ff) == (False, None, None)
assert _rankfile_load(ff) == (False, None)
out, err = capsys.readouterr()
assert 'ERROR' in out
assert 'json' in out
assert 'yaml' in out
assert err == ''

assert tmpdir.remove() is None
Expand All @@ -55,15 +54,15 @@ def test_rankfile_load_wrong_extension(tmpdir, fffake, capsys):
def test_rankfile_load_non_existing_file(tmpdir, fffake):
apifile = tmpdir.join('api_file.json')
apifile.write_text(dumps({'a': 'b'}), 'utf-8')
rf = tmpdir.join('rankfile.yaml')
rf = tmpdir.join('rankfile.json')

ff = fffake(
apifile, nodelist=tmpdir.join('nodelist.json'),
rankfile=rf, dry=True
)

assert _rankfile_load(ff) == (
str(rf), {'updated_at': 'never', 'nodes': []}, True
str(rf), {'updated_at': 'never', 'nodes': []}
)

assert tmpdir.remove() is None
Expand All @@ -80,7 +79,7 @@ def test_rankfile_load_existing_file_with_errors(tmpdir, fffake, capsys):
apifile, nodelist=tmpdir.join('nodelist.json'),
rankfile=rf, dry=True
)
assert _rankfile_load(ff) == (False, None, None)
assert _rankfile_load(ff) == (False, None)
out, err = capsys.readouterr()
assert 'ERROR' in out
assert 'could' in out
Expand All @@ -94,7 +93,7 @@ def test_rankfile_load_existing_file_with_errors(tmpdir, fffake, capsys):
apifile, nodelist=tmpdir.join('nodelist.json'),
rankfile=rf, dry=True
)
assert _rankfile_load(ff) == (False, None, None)
assert _rankfile_load(ff) == (False, None)
out, err = capsys.readouterr()
assert 'ERROR' in out
assert 'is' in out
Expand All @@ -115,6 +114,6 @@ def test_rankfile_load_existing_file(tmpdir, fffake):
apifile, nodelist=tmpdir.join('nodelist.json'),
rankfile=rf, dry=True
)
assert _rankfile_load(ff) == (str(rf), rankfile, False)
assert _rankfile_load(ff) == (str(rf), rankfile)

assert tmpdir.remove() is None

0 comments on commit 2b91152

Please sign in to comment.