Tweaking role path searching in v2 to be a bit more like v1

This commit is contained in:
James Cammarata 2015-01-29 10:55:00 -06:00
parent e4a7b973fd
commit 171a67cfef
3 changed files with 22 additions and 15 deletions

View file

@ -26,11 +26,11 @@ from yaml import load, YAMLError
from ansible.errors import AnsibleParserError from ansible.errors import AnsibleParserError
from ansible.errors.yaml_strings import YAML_SYNTAX_ERROR from ansible.errors.yaml_strings import YAML_SYNTAX_ERROR
from ansible.parsing.vault import VaultLib from ansible.parsing.vault import VaultLib
from ansible.parsing.splitter import unquote from ansible.parsing.splitter import unquote
from ansible.parsing.yaml.loader import AnsibleLoader from ansible.parsing.yaml.loader import AnsibleLoader
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
from ansible.utils.path import unfrackpath
class DataLoader(): class DataLoader():
@ -179,8 +179,7 @@ class DataLoader():
basedir = os.path.dirname(role_path) basedir = os.path.dirname(role_path)
if os.path.islink(basedir): if os.path.islink(basedir):
# FIXME: implement unfrackpath basedir = unfrackpath(basedir)
#basedir = unfrackpath(basedir)
template2 = os.path.join(basedir, dirname, source) template2 = os.path.join(basedir, dirname, source)
else: else:
template2 = os.path.join(basedir, '..', dirname, source) template2 = os.path.join(basedir, '..', dirname, source)

View file

@ -30,6 +30,7 @@ from ansible.playbook.attribute import Attribute, FieldAttribute
from ansible.playbook.base import Base from ansible.playbook.base import Base
from ansible.playbook.conditional import Conditional from ansible.playbook.conditional import Conditional
from ansible.playbook.taggable import Taggable from ansible.playbook.taggable import Taggable
from ansible.utils.path import unfrackpath
__all__ = ['RoleDefinition'] __all__ = ['RoleDefinition']
@ -113,30 +114,28 @@ class RoleDefinition(Base, Conditional, Taggable):
append it to the default role path append it to the default role path
''' '''
# FIXME: this should use unfrackpath once the utils code has been sorted out role_path = unfrackpath(role_name)
role_path = os.path.normpath(role_name)
if self._loader.path_exists(role_path): if self._loader.path_exists(role_path):
role_name = os.path.basename(role_name) role_name = os.path.basename(role_name)
return (role_name, role_path) return (role_name, role_path)
else: else:
# we always start the search for roles in the base directory of the playbook # we always start the search for roles in the base directory of the playbook
role_search_paths = [os.path.join(self._loader.get_basedir(), 'roles'), './roles'] role_search_paths = [os.path.join(self._loader.get_basedir(), 'roles'), './roles', './']
# also search in the configured roles path # also search in the configured roles path
configured_paths = C.DEFAULT_ROLES_PATH if C.DEFAULT_ROLES_PATH:
if ':' in configured_paths: configured_paths = C.DEFAULT_ROLES_PATH.split(os.pathsep)
configured_paths = configured_paths.split(':')
role_search_paths.extend(configured_paths) role_search_paths.extend(configured_paths)
else:
role_search_paths.append(configured_paths)
print("role search paths are: %s" % role_search_paths) # finally, append the roles basedir, if it was set, so we can
# search relative to that directory for dependent roles
if self._role_basedir: if self._role_basedir:
role_search_paths = [self._role_basedir] + role_search_paths role_search_paths.append(self._role_basedir)
# now iterate through the possible paths and return the first one we find
for path in role_search_paths: for path in role_search_paths:
role_path = os.path.join(path, role_name) role_path = unfrackpath(os.path.join(path, role_name))
if self._loader.path_exists(role_path): if self._loader.path_exists(role_path):
return (role_name, role_path) return (role_name, role_path)

View file

@ -18,9 +18,18 @@
import os import os
import stat import stat
__all__ = ['is_executable'] __all__ = ['is_executable', 'unfrackpath']
def is_executable(path): def is_executable(path):
'''is the given path executable?''' '''is the given path executable?'''
return (stat.S_IXUSR & os.stat(path)[stat.ST_MODE] or stat.S_IXGRP & os.stat(path)[stat.ST_MODE] or stat.S_IXOTH & os.stat(path)[stat.ST_MODE]) return (stat.S_IXUSR & os.stat(path)[stat.ST_MODE] or stat.S_IXGRP & os.stat(path)[stat.ST_MODE] or stat.S_IXOTH & os.stat(path)[stat.ST_MODE])
def unfrackpath(path):
'''
returns a path that is free of symlinks, environment
variables, relative path traversals and symbols (~)
example:
'$HOME/../../var/mail' becomes '/var/spool/mail'
'''
return os.path.normpath(os.path.realpath(os.path.expandvars(os.path.expanduser(path))))