mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 19:31:26 -07:00
Misc code cleanup, mostly whitespace preferences, removing unused imports, plus a few fixes here and there.
This commit is contained in:
parent
4b73931351
commit
1754de3335
15 changed files with 117 additions and 131 deletions
|
@ -15,8 +15,6 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
###############################################################
|
||||
|
||||
import sys
|
||||
import os
|
||||
import shlex
|
||||
|
@ -45,15 +43,18 @@ except ImportError:
|
|||
|
||||
def err(msg):
|
||||
''' print an error message to stderr '''
|
||||
|
||||
print >> sys.stderr, msg
|
||||
|
||||
def exit(msg, rc=1):
|
||||
''' quit with an error to stdout and a failure code '''
|
||||
|
||||
err(msg)
|
||||
sys.exit(rc)
|
||||
|
||||
def jsonify(result, format=False):
|
||||
''' format JSON output (uncompressed or uncompressed) '''
|
||||
|
||||
result2 = result.copy()
|
||||
if format:
|
||||
return json.dumps(result2, sort_keys=True, indent=4)
|
||||
|
@ -62,6 +63,7 @@ def jsonify(result, format=False):
|
|||
|
||||
def write_tree_file(tree, hostname, buf):
|
||||
''' write something into treedir/hostname '''
|
||||
|
||||
# TODO: might be nice to append playbook runs per host in a similar way
|
||||
# in which case, we'd want append mode.
|
||||
path = os.path.join(tree, hostname)
|
||||
|
@ -71,10 +73,12 @@ def write_tree_file(tree, hostname, buf):
|
|||
|
||||
def is_failed(result):
|
||||
''' is a given JSON result a failed result? '''
|
||||
|
||||
return ((result.get('rc', 0) != 0) or (result.get('failed', False) in [ True, 'True', 'true']))
|
||||
|
||||
def prepare_writeable_dir(tree):
|
||||
''' make sure a directory exists and is writeable '''
|
||||
|
||||
if tree != '/':
|
||||
tree = os.path.realpath(os.path.expanduser(tree))
|
||||
if not os.path.exists(tree):
|
||||
|
@ -87,6 +91,7 @@ def prepare_writeable_dir(tree):
|
|||
|
||||
def path_dwim(basedir, given):
|
||||
''' make relative paths work like folks expect '''
|
||||
|
||||
if given.startswith("/"):
|
||||
return given
|
||||
elif given.startswith("~/"):
|
||||
|
@ -96,10 +101,12 @@ def path_dwim(basedir, given):
|
|||
|
||||
def json_loads(data):
|
||||
''' parse a JSON string and return a data structure '''
|
||||
|
||||
return json.loads(data)
|
||||
|
||||
def parse_json(data):
|
||||
''' this version for module return data only '''
|
||||
|
||||
try:
|
||||
return json.loads(data)
|
||||
except:
|
||||
|
@ -132,6 +139,7 @@ _LISTRE = re.compile(r"(\w+)\[(\d+)\]")
|
|||
|
||||
def _varLookup(name, vars):
|
||||
''' find the contents of a possibly complex variable in vars. '''
|
||||
|
||||
path = name.split('.')
|
||||
space = vars
|
||||
for part in path:
|
||||
|
@ -153,22 +161,17 @@ _KEYCRE = re.compile(r"\$(?P<complex>\{){0,1}((?(complex)[\w\.\[\]]+|\w+))(?(com
|
|||
|
||||
def varLookup(varname, vars):
|
||||
''' helper function used by varReplace '''
|
||||
|
||||
m = _KEYCRE.search(varname)
|
||||
if not m:
|
||||
return None
|
||||
return _varLookup(m.group(2), vars)
|
||||
|
||||
def varReplace(raw, vars):
|
||||
'''Perform variable replacement of $vars
|
||||
|
||||
@param raw: String to perform substitution on.
|
||||
@param vars: Dictionary of variables to replace. Key is variable name
|
||||
(without $ prefix). Value is replacement string.
|
||||
@return: Input raw string with substituted values.
|
||||
'''
|
||||
''' Perform variable replacement of $variables in string raw using vars dictionary '''
|
||||
# this code originally from yum
|
||||
|
||||
done = [] # Completed chunks to return
|
||||
done = [] # Completed chunks to return
|
||||
|
||||
while raw:
|
||||
m = _KEYCRE.search(raw)
|
||||
|
@ -191,13 +194,14 @@ def varReplace(raw, vars):
|
|||
|
||||
def _template(text, vars, setup_cache=None):
|
||||
''' run a text buffer through the templating engine '''
|
||||
|
||||
vars = vars.copy()
|
||||
vars['hostvars'] = setup_cache
|
||||
return varReplace(unicode(text), vars)
|
||||
|
||||
def template(text, vars, setup_cache=None):
|
||||
''' run a text buffer through the templating engine
|
||||
until it no longer changes '''
|
||||
''' run a text buffer through the templating engine until it no longer changes '''
|
||||
|
||||
prev_text = ''
|
||||
depth = 0
|
||||
while prev_text != text:
|
||||
|
@ -210,6 +214,7 @@ def template(text, vars, setup_cache=None):
|
|||
|
||||
def template_from_file(basedir, path, vars, setup_cache):
|
||||
''' run a file through the templating engine '''
|
||||
|
||||
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(basedir), trim_blocks=False)
|
||||
data = codecs.open(path_dwim(basedir, path), encoding="utf8").read()
|
||||
t = environment.from_string(data)
|
||||
|
@ -222,10 +227,12 @@ def template_from_file(basedir, path, vars, setup_cache):
|
|||
|
||||
def parse_yaml(data):
|
||||
''' convert a yaml string to a data structure '''
|
||||
|
||||
return yaml.load(data)
|
||||
|
||||
def parse_yaml_from_file(path):
|
||||
''' convert a yaml file to a data structure '''
|
||||
|
||||
try:
|
||||
data = file(path).read()
|
||||
except IOError:
|
||||
|
@ -234,6 +241,7 @@ def parse_yaml_from_file(path):
|
|||
|
||||
def parse_kv(args):
|
||||
''' convert a string of key/value items to a dict '''
|
||||
|
||||
options = {}
|
||||
if args is not None:
|
||||
vargs = shlex.split(args, posix=True)
|
||||
|
@ -245,6 +253,7 @@ def parse_kv(args):
|
|||
|
||||
def md5(filename):
|
||||
''' Return MD5 hex digest of local file, or None if file is not present. '''
|
||||
|
||||
if not os.path.exists(filename):
|
||||
return None
|
||||
digest = _md5()
|
||||
|
@ -263,6 +272,7 @@ def md5(filename):
|
|||
|
||||
class SortedOptParser(optparse.OptionParser):
|
||||
'''Optparser which sorts the options by opt before outputting --help'''
|
||||
|
||||
def format_help(self, formatter=None):
|
||||
self.option_list.sort(key=operator.methodcaller('get_opt_string'))
|
||||
return optparse.OptionParser.format_help(self, formatter=None)
|
||||
|
@ -321,4 +331,3 @@ def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, asyn
|
|||
|
||||
return parser
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue