From c0747b7baa7e25ed8508b84d1eb388d36c25ad5b Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Tue, 30 Oct 2012 20:42:07 -0400 Subject: [PATCH] pep8 fixes --- lib/ansible/inventory/__init__.py | 2 +- lib/ansible/playbook/play.py | 2 +- lib/ansible/playbook/task.py | 50 +++++++++++++++---------------- lib/ansible/runner/__init__.py | 2 +- lib/ansible/utils.py | 3 ++ library/cron | 2 +- library/fireball | 18 +++++------ library/lineinfile | 2 ++ library/mysql_user | 5 ++-- library/postgresql_db | 2 +- library/postgresql_user | 2 +- library/setup | 26 ++++++++-------- 12 files changed, 60 insertions(+), 56 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index df09d47ff9..e7788a3712 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -300,7 +300,7 @@ class Inventory(object): groups = [ g.name for g in host.get_groups() if g.name != 'all' ] results['group_names'] = sorted(groups) vars.update(results) - else: + else: vars.update(host.get_variables()) return vars diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index bb1e5382ef..c12a3077c3 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -123,7 +123,7 @@ class Play(object): include_file = utils.template(self.basedir, tokens[0], mv) data = utils.parse_yaml_from_file(utils.path_dwim(self.basedir, include_file)) for y in data: - results.append(Task(self,y,module_vars=mv.copy())) + results.append(Task(self,y,module_vars=mv.copy())) elif type(x) == dict: task_vars = self.vars.copy() results.append(Task(self,x,module_vars=task_vars)) diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index 81db27733d..a9ceebce08 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -99,10 +99,10 @@ class Task(object): # delegate_to can use variables if not (self.delegate_to is None): - self.delegate_to = utils.template(None, self.delegate_to, self.module_vars) - # delegate_to: localhost should use local transport - if self.delegate_to in ['127.0.0.1', 'localhost']: - self.transport = 'local' + self.delegate_to = utils.template(None, self.delegate_to, self.module_vars) + # delegate_to: localhost should use local transport + if self.delegate_to in ['127.0.0.1', 'localhost']: + self.transport = 'local' # notified by is used by Playbook code to flag which hosts # need to run a notifier @@ -195,37 +195,37 @@ class Task(object): # when: str $x != $y if type(expression) not in [ str, unicode ]: - raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression) + raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression) tokens = expression.split() if len(tokens) < 2: - raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression) + raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression) # when_set / when_unset if tokens[0] in [ 'set', 'unset' ]: - if len(tokens) != 2: - raise errors.AnsibleError("usage: when: <$variableName>") - return "is_%s('%s')" % (tokens[0], tokens[1]) + if len(tokens) != 2: + raise errors.AnsibleError("usage: when: <$variableName>") + return "is_%s('%s')" % (tokens[0], tokens[1]) # when_integer / when_float / when_string elif tokens[0] in [ 'integer', 'float', 'string' ]: - cast = None - if tokens[0] == 'integer': - cast = 'int' - elif tokens[0] == 'string': - cast = 'str' - elif tokens[0] == 'float': - cast = 'float' - tcopy = tokens[1:] - for (i,t) in enumerate(tokens[1:]): - if t.find("$") != -1: - # final variable substitution will happen in Runner code - tcopy[i] = "%s('%s')" % (cast, t) - else: - tcopy[i] = t - return " ".join(tcopy) + cast = None + if tokens[0] == 'integer': + cast = 'int' + elif tokens[0] == 'string': + cast = 'str' + elif tokens[0] == 'float': + cast = 'float' + tcopy = tokens[1:] + for (i,t) in enumerate(tokens[1:]): + if t.find("$") != -1: + # final variable substitution will happen in Runner code + tcopy[i] = "%s('%s')" % (cast, t) + else: + tcopy[i] = t + return " ".join(tcopy) else: - raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression) + raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 510a4489ca..6d4feaf7fd 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -369,7 +369,7 @@ class Runner(object): conn = None actual_host = inject.get('ansible_ssh_host', host) actual_port = port - if self.transport in ['paramiko', 'ssh' ]: + if self.transport in [ 'paramiko', 'ssh' ]: actual_port = inject.get('ansible_ssh_port', port) # the delegated host may have different SSH port configured, etc diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 30f995ee1c..aeeadd72cc 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -142,10 +142,13 @@ def is_failed(result): return ((result.get('rc', 0) != 0) or (result.get('failed', False) in [ True, 'True', 'true'])) def check_conditional(conditional): + def is_set(var): return not var.startswith("$") + def is_unset(var): return var.startswith("$") + return eval(conditional.replace("\n", "\\n")) def is_executable(path): diff --git a/library/cron b/library/cron index 01b2d96761..c04cd27337 100644 --- a/library/cron +++ b/library/cron @@ -154,7 +154,7 @@ def add_job(name,job,tmpfile): def update_job(name,job,tmpfile): return _update_job(name,job,tmpfile,do_add_job) -def do_add_job (lines, comment, job): +def do_add_job(lines, comment, job): lines.append(comment) lines.append(job) diff --git a/library/fireball b/library/fireball index 0976ae2338..ecc1df4bbe 100755 --- a/library/fireball +++ b/library/fireball @@ -77,7 +77,7 @@ syslog.openlog('ansible-%s' % os.path.basename(__file__)) PIDFILE = os.path.expanduser("~/.fireball.pid") def log(msg): - syslog.syslog(syslog.LOG_NOTICE, msg) + syslog.syslog(syslog.LOG_NOTICE, msg) if os.path.exists(PIDFILE): try: @@ -92,17 +92,17 @@ if os.path.exists(PIDFILE): HAS_ZMQ = False try: - import zmq - HAS_ZMQ = True + import zmq + HAS_ZMQ = True except ImportError: - pass + pass HAS_KEYCZAR = False try: - from keyczar.keys import AesKey - HAS_KEYCZAR = True + from keyczar.keys import AesKey + HAS_KEYCZAR = True except ImportError: - pass + pass # NOTE: this shares a fair amount of code in common with async_wrapper, if async_wrapper were a new module we could move # this into utils.module_common and probably should anyway @@ -163,9 +163,9 @@ def command(data): def fetch(data): if 'data' not in data: - return dict(failed=True, msg='internal error: data is required') + return dict(failed=True, msg='internal error: data is required') if 'in_path' not in data: - return dict(failed=True, msg='internal error: out_path is required') + return dict(failed=True, msg='internal error: out_path is required') fh = open(data['in_path']) data = fh.read() diff --git a/library/lineinfile b/library/lineinfile index 053fd1726f..060256174b 100755 --- a/library/lineinfile +++ b/library/lineinfile @@ -157,12 +157,14 @@ def absent(module, dest, regexp, backup): f.close() cre = re.compile(regexp) found = [] + def matcher(line): if cre.search(line): found.append(line) return False else: return True + lines = filter(matcher, lines) changed = len(found) > 0 if changed: diff --git a/library/mysql_user b/library/mysql_user index 9b6e7946b5..e30fde8ee6 100755 --- a/library/mysql_user +++ b/library/mysql_user @@ -176,7 +176,7 @@ def privileges_get(cursor, user,host): privileges = res.group(1).split(", ") privileges = ['ALL' if x=='ALL PRIVILEGES' else x for x in privileges] if "WITH GRANT OPTION" in res.group(4): - privileges.append('GRANT') + privileges.append('GRANT') db = res.group(2).replace('`', '') output[db] = privileges return output @@ -213,8 +213,7 @@ def privileges_grant(cursor, user,host,db_table,priv): priv_string = ",".join(filter(lambda x: x != 'GRANT', priv)) query = "GRANT %s ON %s TO '%s'@'%s'" % (priv_string,db_table,user,host) if 'GRANT' in priv: - query = query + " WITH GRANT OPTION" - + query = query + " WITH GRANT OPTION" cursor.execute(query) def load_mycnf(): diff --git a/library/postgresql_db b/library/postgresql_db index 98e52f62cd..1759f5ee2b 100755 --- a/library/postgresql_db +++ b/library/postgresql_db @@ -149,7 +149,7 @@ def main(): # To use defaults values, keyword arguments must be absent, so # check which values are empty and don't include in the **kw # dictionary - params_map = { + params_map = { "login_host":"host", "login_user":"user", "login_password":"password", diff --git a/library/postgresql_user b/library/postgresql_user index 61fc1a2a53..b38949c43c 100755 --- a/library/postgresql_user +++ b/library/postgresql_user @@ -379,7 +379,7 @@ def main(): # To use defaults values, keyword arguments must be absent, so # check which values are empty and don't include in the **kw # dictionary - params_map = { + params_map = { "login_host":"host", "login_user":"user", "login_password":"password", diff --git a/library/setup b/library/setup index 4a2eac7874..0c4ffae769 100755 --- a/library/setup +++ b/library/setup @@ -853,19 +853,19 @@ def run_setup(module): # templating w/o making a nicer key for it (TODO) if os.path.exists("/usr/bin/ohai"): - cmd = subprocess.Popen("/usr/bin/ohai", shell=True, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - out, err = cmd.communicate() - ohai = True - try: - ohai_ds = json.loads(out) - except: - ohai = False - if ohai: - for (k,v) in ohai_ds.items(): - if type(v) == str or type(v) == unicode: - k2 = "ohai_%s" % k.replace('-', '_') - setup_options[k2] = v + cmd = subprocess.Popen("/usr/bin/ohai", shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = cmd.communicate() + ohai = True + try: + ohai_ds = json.loads(out) + except: + ohai = False + if ohai: + for (k,v) in ohai_ds.items(): + if type(v) == str or type(v) == unicode: + k2 = "ohai_%s" % k.replace('-', '_') + setup_options[k2] = v setup_result = {} setup_result['ansible_facts'] = setup_options