From 477ca2ed1a18b8673d3b1fd1141a5d592599516b Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 11 Aug 2012 12:35:58 -0400 Subject: [PATCH] Make pep8 tests run against the library directory as well, and associated tweaks (mostly to indentation) in the library directory. --- Makefile | 3 +- library/apt | 8 ++-- library/async_status | 2 +- library/command | 4 +- library/facter | 24 +++++----- library/file | 107 +++++++++++++++++++++---------------------- library/mount | 16 +++---- library/ohai | 4 +- library/service | 4 +- library/setup | 75 +++++++++++++++--------------- library/user | 2 +- library/virt | 2 +- library/yum | 10 ++-- 13 files changed, 128 insertions(+), 133 deletions(-) diff --git a/Makefile b/Makefile index ba0d94f0214..a428cdebce4 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,8 @@ pep8: @echo "#############################################" @echo "# Running PEP8 Compliance Tests" @echo "#############################################" - pep8 -r --ignore=E501,E221,W291,W391,E302,E251,E203,W293,E231,E303,E201,E225,E261 lib/ bin/ + -pep8 -r --ignore=E501,E221,W291,W391,E302,E251,E203,W293,E231,E303,E201,E225,E261 lib/ bin/ + -pep8 -r --ignore=E501,E221,W291,W391,E302,E251,E203,W293,E231,E303,E201,E225,E261 --filename "*" library/ pyflakes: pyflakes lib/ansible/*.py bin/* diff --git a/library/apt b/library/apt index ff7dd8a231a..b6b8954ee9b 100755 --- a/library/apt +++ b/library/apt @@ -21,7 +21,7 @@ import traceback # added to stave off future warnings about apt api -import warnings; +import warnings warnings.filterwarnings('ignore', "apt API not stable yet", FutureWarning) # APT related constants @@ -30,8 +30,7 @@ APT = "DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical %s" % APT_PATH def run_apt(command): try: - cmd = subprocess.Popen(command, shell=True, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + cmd = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = cmd.communicate() except (OSError, IOError), e: rc = 1 @@ -131,7 +130,8 @@ def main(): ) try: - import apt, apt_pkg + import apt + import apt_pkg except: module.fail_json("Could not import python modules: apt, apt_pkg. Please install python-apt package.") diff --git a/library/async_status b/library/async_status index 404cb177685..5eeb09c0b59 100755 --- a/library/async_status +++ b/library/async_status @@ -58,7 +58,7 @@ def main(): module_fail_json(ansible_job_id=jid, results_file=log_path, msg="Could not parse job output: %s" % data) - if not data.has_key("started"): + if not 'started' in data: data['finished'] = 1 data['ansible_job_id'] = jid module.exit_json(**data) diff --git a/library/command b/library/command index 933cfe19a9b..29661d26913 100755 --- a/library/command +++ b/library/command @@ -54,9 +54,9 @@ def main(): delta = endd - startd if out is None: - out = '' + out = '' if err is None: - err = '' + err = '' module.exit_json( cmd = args, diff --git a/library/facter b/library/facter index 373dbcab08f..ade4544bc0a 100755 --- a/library/facter +++ b/library/facter @@ -26,22 +26,22 @@ import subprocess def get_facter_data(): - p = subprocess.Popen(["/usr/bin/env", "facter", "--json"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - (out, err) = p.communicate() - rc = p.returncode - return rc, out, err + p = subprocess.Popen(["/usr/bin/env", "facter", "--json"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (out, err) = p.communicate() + rc = p.returncode + return rc, out, err def main(): - module = AnsibleModule( - argument_spec = dict() - ) + module = AnsibleModule( + argument_spec = dict() + ) - rc, out, err = get_facter_data() - if rc != 0: - module.fail_json(msg=err) - else: - module.exit_json(**json.loads(out)) + rc, out, err = get_facter_data() + if rc != 0: + module.fail_json(msg=err) + else: + module.exit_json(**json.loads(out)) # this is magic, see lib/ansible/module_common.py #<> diff --git a/library/file b/library/file index 425808714da..92c21ffdae1 100755 --- a/library/file +++ b/library/file @@ -112,13 +112,6 @@ def selinux_context(path): context = ret[1].split(':') return context -# =========================================== - - - - - - # =========================================== # support functions @@ -157,64 +150,64 @@ def set_context_if_different(path, context, changed): return changed def set_owner_if_different(path, owner, changed): - if owner is None: - return changed - user, group = user_and_group(path) - if owner != user: - try: - uid = pwd.getpwnam(owner).pw_uid - except KeyError: - module_fail_json(path=path, msg='chown failed: failed to look up user %s' % owner) - try: - os.chown(path, uid, -1) - except OSError: - module_fail_json(path=path, msg='chown failed') - return True + if owner is None: + return changed + user, group = user_and_group(path) + if owner != user: + try: + uid = pwd.getpwnam(owner).pw_uid + except KeyError: + module_fail_json(path=path, msg='chown failed: failed to look up user %s' % owner) + try: + os.chown(path, uid, -1) + except OSError: + module_fail_json(path=path, msg='chown failed') + return True - return changed + return changed def set_group_if_different(path, group, changed): - if group is None: - return changed - old_user, old_group = user_and_group(path) - if old_group != group: - try: - gid = grp.getgrnam(group).gr_gid - except KeyError: - module_fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group) - try: - os.chown(path, -1, gid) - except OSError: - module_fail_json(path=path, msg='chgrp failed') - return True - return changed + if group is None: + return changed + old_user, old_group = user_and_group(path) + if old_group != group: + try: + gid = grp.getgrnam(group).gr_gid + except KeyError: + module_fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group) + try: + os.chown(path, -1, gid) + except OSError: + module_fail_json(path=path, msg='chgrp failed') + return True + return changed def set_mode_if_different(path, mode, changed): - if mode is None: - return changed - try: - # FIXME: support English modes - mode = int(mode, 8) - except Exception, e: - module_fail_json(path=path, msg='mode needs to be something octalish', details=str(e)) + if mode is None: + return changed + try: + # FIXME: support English modes + mode = int(mode, 8) + except Exception, e: + module_fail_json(path=path, msg='mode needs to be something octalish', details=str(e)) - st = os.stat(path) - prev_mode = stat.S_IMODE(st[stat.ST_MODE]) + st = os.stat(path) + prev_mode = stat.S_IMODE(st[stat.ST_MODE]) - if prev_mode != mode: - # FIXME: comparison against string above will cause this to be executed - # every time - try: - os.chmod(path, mode) - except Exception, e: + if prev_mode != mode: + # FIXME: comparison against string above will cause this to be executed + # every time + try: + os.chmod(path, mode) + except Exception, e: module_fail_json(path=path, msg='chmod failed', details=str(e)) + + st = os.stat(path) + new_mode = stat.S_IMODE(st[stat.ST_MODE]) - st = os.stat(path) - new_mode = stat.S_IMODE(st[stat.ST_MODE]) - - if new_mode != prev_mode: - return True - return changed + if new_mode != prev_mode: + return True + return changed def rmtree_error(func, path, exc_info): @@ -222,7 +215,9 @@ def rmtree_error(func, path, exc_info): def main(): + # FIXME: pass this around, should not use global global module + module = AnsibleModule( check_invalid_arguments = False, argument_spec = dict( diff --git a/library/mount b/library/mount index 364f884a0c7..c45f4d272c7 100755 --- a/library/mount +++ b/library/mount @@ -59,8 +59,8 @@ def set_mount(**kwargs): changed = False for line in open(args['fstab'], 'r').readlines(): if not line.strip(): - to_write.append(line) - continue + to_write.append(line) + continue if line.strip().startswith('#'): to_write.append(line) continue @@ -85,9 +85,9 @@ def set_mount(**kwargs): ld[t] = args[t] if changed: - to_write.append(new_line % ld) + to_write.append(new_line % ld) else: - to_write.append(line) + to_write.append(line) if not exists: to_write.append(new_line % args) @@ -115,8 +115,8 @@ def unset_mount(**kwargs): changed = False for line in open(args['fstab'], 'r').readlines(): if not line.strip(): - to_write.append(line) - continue + to_write.append(line) + continue if line.strip().startswith('#'): to_write.append(line) continue @@ -147,9 +147,9 @@ def mount(**kwargs): name = kwargs['name'] if os.path.ismount(name): - cmd = ['/bin/mount', '-o', 'remount', name] + cmd = [ '/bin/mount', '-o', 'remount', name ] else: - cmd = ['/bin/mount', name ] + cmd = [ '/bin/mount', name ] call = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = call.communicate() diff --git a/library/ohai b/library/ohai index b752e6e9258..826ac50538a 100755 --- a/library/ohai +++ b/library/ohai @@ -31,9 +31,9 @@ def main(): ) rc, out, err = get_ohai_data() if rc != 0: - module.fail_json(msg=err) + module.fail_json(msg=err) else: - module.exit_json(**json.loads(out)) + module.exit_json(**json.loads(out)) # this is magic, see lib/ansible/module_common.py #<> diff --git a/library/service b/library/service index 166686720df..1f5d7476e0c 100755 --- a/library/service +++ b/library/service @@ -207,11 +207,9 @@ def main(): if state: result['state'] = state rc, stdout, stderr = _run("%s %s status" % (SERVICE, name)) - module.exit_json(**result); - + module.exit_json(**result) # this is magic, see lib/ansible/module_common.py #<> main() - diff --git a/library/setup b/library/setup index 630bc380799..b8ed8b4bec0 100755 --- a/library/setup +++ b/library/setup @@ -130,7 +130,7 @@ class Facts(object): self.facts['selinux']['policyvers'] = 'unknown' try: (rc, configmode) = selinux.selinux_getenforcemode() - if rc == 0 and Facts.SELINUX_MODE_DICT.has_key(configmode): + if rc == 0 and configmode in Facts.SELINUX_MODE_DICT: self.facts['selinux']['config_mode'] = Facts.SELINUX_MODE_DICT[configmode] else: self.facts['selinux']['config_mode'] = 'unknown' @@ -138,7 +138,7 @@ class Facts(object): self.facts['selinux']['config_mode'] = 'unknown' try: mode = selinux.security_getenforce() - if Facts.SELINUX_MODE_DICT.has_key(mode): + if mode in Facts.SELINUX_MODE_DICT: self.facts['selinux']['mode'] = Facts.SELINUX_MODE_DICT[mode] else: self.facts['selinux']['mode'] = 'unknown' @@ -196,19 +196,21 @@ class LinuxHardware(Hardware): In addition, it also defines number of DMI facts. """ + platform = 'Linux' MEMORY_FACTS = ['MemTotal', 'SwapTotal', 'MemFree', 'SwapFree'] # DMI bits - DMI_DICT = { 'form_factor': '/sys/devices/virtual/dmi/id/chassis_type', - 'product_name': '/sys/devices/virtual/dmi/id/product_name', - 'product_serial': '/sys/devices/virtual/dmi/id/product_serial', - 'product_uuid': '/sys/devices/virtual/dmi/id/product_uuid', - 'product_version': '/sys/devices/virtual/dmi/id/product_version', - 'system_vendor': '/sys/devices/virtual/dmi/id/sys_vendor', - 'bios_date': '/sys/devices/virtual/dmi/id/bios_date', - 'bios_version': '/sys/devices/virtual/dmi/id/bios_version' } - # From smolt and DMI spec - # See http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf + DMI_DICT = dict( + form_factor = '/sys/devices/virtual/dmi/id/chassis_type', + product_name = '/sys/devices/virtual/dmi/id/product_name', + product_serial = '/sys/devices/virtual/dmi/id/product_serial', + product_uuid = '/sys/devices/virtual/dmi/id/product_uuid', + product_version = '/sys/devices/virtual/dmi/id/product_version', + system_vendor = '/sys/devices/virtual/dmi/id/sys_vendor', + bios_date = '/sys/devices/virtual/dmi/id/bios_date', + bios_version = '/sys/devices/virtual/dmi/id/bios_version' + ) + # DMI SPEC -- http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.0.pdf FORM_FACTOR = [ "Unknown", "Other", "Unknown", "Desktop", "Low Profile Desktop", "Pizza Box", "Mini Tower", "Tower", "Portable", "Laptop", "Notebook", "Hand Held", "Docking Station", @@ -634,36 +636,36 @@ def run_setup(module): # ruby-json is ALSO installed, include facter data in the JSON if os.path.exists("/usr/bin/facter"): - cmd = subprocess.Popen("/usr/bin/facter --json", shell=True, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - out, err = cmd.communicate() - facter = True - try: - facter_ds = json.loads(out) - except: - facter = False - if facter: - for (k,v) in facter_ds.items(): - setup_options["facter_%s" % k] = v + cmd = subprocess.Popen("/usr/bin/facter --json", shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = cmd.communicate() + facter = True + try: + facter_ds = json.loads(out) + except: + facter = False + if facter: + for (k,v) in facter_ds.items(): + setup_options["facter_%s" % k] = v # ditto for ohai, but just top level string keys # because it contains a lot of nested stuff we can't use for # 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 - 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 + setup_options[k2] = v setup_result = {} setup_result['ansible_facts'] = setup_options @@ -683,4 +685,3 @@ def main(): # this is magic, see lib/ansible/module_common.py #<> main() - diff --git a/library/user b/library/user index 61d6ccfb623..c90037efe6b 100755 --- a/library/user +++ b/library/user @@ -134,7 +134,7 @@ def user_mod(module, user, **kwargs): cmd.append('-a') groups_need_mod = True else: - groups_need_mod = True + groups_need_mod = True if groups_need_mod: cmd.append('-G') diff --git a/library/virt b/library/virt index 3ed6dedda56..c1181d7a5d8 100755 --- a/library/virt +++ b/library/virt @@ -183,7 +183,7 @@ class Virt(object): "nrVirtCpu" : data[3], "cpuTime" : str(data[4]), } - info[vm]["autostart"] = self.conn.get_autostart(vm) + info[vm]["autostart"] = self.conn.get_autostart(vm) return info diff --git a/library/yum b/library/yum index 3c139c842fe..0cfb9d2a951 100755 --- a/library/yum +++ b/library/yum @@ -130,9 +130,9 @@ def run(command): out = '' if out is None: - out = '' + out = '' if err is None: - err = '' + err = '' else: rc = cmd.returncode @@ -144,7 +144,7 @@ def install_no_repoq(module, items, yum_basecmd, latest=False): to_install = [] if not latest: for item in items: - rc, out, err = run([rpmbin, "-q", "--whatprovides", item]) + rc, out, err = run([rpmbin, "-q", "--whatprovides", item]) if rc != 0: to_install.append(item) if len(to_install) > 0: @@ -163,7 +163,7 @@ def install_no_repoq(module, items, yum_basecmd, latest=False): if rc != 0: module.fail_json(msg=err) for item in to_install: - rc, out, err = run([rpmbin, "-q", "--whatprovides", item]) + rc, out, err = run([rpmbin, "-q", "--whatprovides", item]) if rc != 0: module.fail_json(msg="%s could not be installed" % item) @@ -174,7 +174,7 @@ def remove_no_repoq(module, items, yum_basecmd): to_remove = [] for item in items: - rc, out, err = run([rpmbin, "-q", "--whatprovides", "--qf", "%{NAME}\n", item]) + rc, out, err = run([rpmbin, "-q", "--whatprovides", "--qf", "%{NAME}\n", item]) if rc == 0: to_remove.append(out.strip()) if len(to_remove) > 0: