Skip to content

Commit

Permalink
Do not sort/mix the order of role dependencies.
Browse files Browse the repository at this point in the history
It is expected that the order of role dependencies has at least some
meaning so that the sorting applied by the author of the role is more
important than sorting by Ansigenome or as it was done before this
commit by applying a kind of random order (dict data structure sorting)
to it.

Also use a more Ansible`ish` Jinja2 by using common filters from Jinja2
`map` and a custom one from Ansible `unique`.
  • Loading branch information
ypid committed Nov 10, 2015
1 parent 7e3eda4 commit 7aab032
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions ansigenome/data/README.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ ansible-galaxy install {{ role.galaxy_name }}
{% block dependencies %}
### Role dependencies

{% for dependency in dependencies | unique_dict("role") %}
- `{{ dependency.role | trim }}`
{% for role_dependency_name in dependencies | map(attribute='role') | unique %}
- `{{ role_dependency_name | trim }}`
{% endfor %}
{% endblock %}
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions ansigenome/data/README.rst.j2
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ This role requires at least Ansible{% if galaxy_info.min_ansible_version is defi
Role dependencies
~~~~~~~~~~~~~~~~~

{% for dependency in dependencies | unique_dict("role") %}
- ``{{ dependency.role | trim }}``
{% for role_dependency_name in dependencies | map(attribute='role') | unique %}
- ``{{ role_dependency_name | trim }}``
{% endfor %}
{% endblock %}
{% endif %}
Expand Down
22 changes: 21 additions & 1 deletion ansigenome/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

import codecs
import errno
import json
Expand All @@ -6,6 +8,7 @@
import subprocess
import urllib2
import yaml
# import collections

import sys
reload(sys)
Expand Down Expand Up @@ -127,12 +130,29 @@ def template(path, extend_path, out):
# Use the subclassed relative environment class
env = RelEnvironment(trim_blocks=True)

# Add a unique dict filter, by key
# Add a unique dict filter, by key.
# Try to use normal filters as known from Jinja and Ansible instead of
# this custom one. This filter is included in Ansigenome for backwards
# compatibility reasons.
def unique_dict(items, key):
return {v[key]: v for v in items}.values()

env.filters["unique_dict"] = unique_dict

def unique(a):

# Don’t use that in Ansigenome as it resorts the role dependencies.
# if isinstance(a,collections.Hashable):
# c = set(a)

c = []
for x in a:
if x not in c:
c.append(x)
return c

env.filters["unique"] = unique

# create a dictionary of templates
templates = dict(
(name, codecs.open(name, "rb", 'UTF-8').read())
Expand Down

0 comments on commit 7aab032

Please sign in to comment.