diff options
author | Scott Moser <smoser@ubuntu.com> | 2012-01-12 17:03:26 +0100 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2012-01-12 17:03:26 +0100 |
commit | 1d00c0936bfc63117493d89268da8c81611b3c40 (patch) | |
tree | 23077c9f5d348a1b29408bba04cdac049a6ec76b | |
parent | 982856531a3498d784e3977f307a9cbf2a673977 (diff) | |
parent | 7c38d4b469d3863443ec3322f0def25672c545db (diff) | |
download | vyos-cloud-init-1d00c0936bfc63117493d89268da8c81611b3c40.tar.gz vyos-cloud-init-1d00c0936bfc63117493d89268da8c81611b3c40.zip |
fix pylint warnings (LP: #914739) [Juerg Haefliger]
LP: #914739
44 files changed, 249 insertions, 257 deletions
@@ -18,6 +18,7 @@ - part-handlers now get base64 decoded content rather than 2xbase64 encoded in the payload parameter. (LP: #874342) - add test case framework [Mike Milner] (LP: #890851) + - fix pylint warnings [Juerg Haefliger] (LP: #914739) 0.6.2: - fix bug where update was not done unless update was explicitly set. It would not be run if 'upgrade' or packages were set to be installed diff --git a/cloud-init-cfg.py b/cloud-init-cfg.py index 2930cd88..de64ef9c 100755 --- a/cloud-init-cfg.py +++ b/cloud-init-cfg.py @@ -23,7 +23,6 @@ import cloudinit.util as util import cloudinit.CloudConfig as CC import logging import os -import traceback def Usage(out = sys.stdout): out.write("Usage: %s name\n" % sys.argv[0]) diff --git a/cloud-init-query.py b/cloud-init-query.py index 3e8c24ab..cfe9b429 100755 --- a/cloud-init-query.py +++ b/cloud-init-query.py @@ -20,9 +20,6 @@ import sys import cloudinit import cloudinit.CloudConfig -import logging -import os -import traceback def Usage(out = sys.stdout): out.write("Usage: %s name\n" % sys.argv[0]) @@ -34,7 +31,6 @@ def main(): sys.exit(1) cc = cloudinit.CloudConfig.CloudConfig(cloudinit.cloud_config) - cloud_config = cc.cfg data = { 'user_data' : cc.cloud.get_userdata(), 'user_data_raw' : cc.cloud.get_userdata_raw(), diff --git a/cloud-init.py b/cloud-init.py index bc673265..8279a0b0 100755 --- a/cloud-init.py +++ b/cloud-init.py @@ -56,12 +56,12 @@ def main(): now = time.strftime("%a, %d %b %Y %H:%M:%S %z",time.gmtime()) try: - uptimef=open("/proc/uptime") - uptime=uptimef.read().split(" ")[0] - uptimef.close() + uptimef=open("/proc/uptime") + uptime=uptimef.read().split(" ")[0] + uptimef.close() except IOError as e: - warn("unable to open /proc/uptime\n") - uptime = "na" + warn("unable to open /proc/uptime\n") + uptime = "na" try: cfg = cloudinit.get_base_cfg(cfg_path) diff --git a/cloudinit/CloudConfig/__init__.py b/cloudinit/CloudConfig/__init__.py index 82f422fc..f5c4143c 100644 --- a/cloudinit/CloudConfig/__init__.py +++ b/cloudinit/CloudConfig/__init__.py @@ -33,7 +33,7 @@ class CloudConfig(): cfgfile = None cfg = None - def __init__(self,cfgfile, cloud=None, ds_deps=[]): + def __init__(self,cfgfile, cloud=None, ds_deps=None): if cloud == None: self.cloud = cloudinit.CloudInit(ds_deps) self.cloud.get_data_source() @@ -233,7 +233,9 @@ def run_per_instance(name, func, args, clear_on_fail=False): raise # apt_get top level command (install, update...), and args to pass it -def apt_get(tlc,args=[]): +def apt_get(tlc,args=None): + if args is None: + args = [] e=os.environ.copy() e['DEBIAN_FRONTEND']='noninteractive' cmd=[ 'apt-get', diff --git a/cloudinit/CloudConfig/cc_apt_update_upgrade.py b/cloudinit/CloudConfig/cc_apt_update_upgrade.py index 0677ceb5..0cbe02d4 100644 --- a/cloudinit/CloudConfig/cc_apt_update_upgrade.py +++ b/cloudinit/CloudConfig/cc_apt_update_upgrade.py @@ -22,7 +22,7 @@ import os import glob import cloudinit.CloudConfig as cc -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): update = util.get_cfg_option_bool(cfg, 'apt_update', False) upgrade = util.get_cfg_option_bool(cfg, 'apt_upgrade', False) @@ -102,14 +102,14 @@ def handle(name,cfg,cloud,log,args): return(True) def mirror2lists_fileprefix(mirror): - file=mirror + string=mirror # take of http:// or ftp:// - if file.endswith("/"): file=file[0:-1] - pos=file.find("://") + if string.endswith("/"): string=string[0:-1] + pos=string.find("://") if pos >= 0: - file=file[pos+3:] - file=file.replace("/","_") - return file + string=string[pos+3:] + string=string.replace("/","_") + return string def rename_apt_lists(omirror,new_mirror,lists_d="/var/lib/apt/lists"): @@ -117,11 +117,11 @@ def rename_apt_lists(omirror,new_mirror,lists_d="/var/lib/apt/lists"): nprefix="%s/%s" % (lists_d,mirror2lists_fileprefix(new_mirror)) if(oprefix==nprefix): return olen=len(oprefix) - for file in glob.glob("%s_*" % oprefix): - os.rename(file,"%s%s" % (nprefix, file[olen:])) + for filename in glob.glob("%s_*" % oprefix): + os.rename(filename,"%s%s" % (nprefix, filename[olen:])) def get_release(): - stdout, stderr = subprocess.Popen(['lsb_release', '-cs'], stdout=subprocess.PIPE).communicate() + stdout, _stderr = subprocess.Popen(['lsb_release', '-cs'], stdout=subprocess.PIPE).communicate() return(stdout.strip()) def generate_sources_list(codename, mirror): @@ -131,7 +131,9 @@ def generate_sources_list(codename, mirror): # srclist is a list of dictionaries, # each entry must have: 'source' # may have: key, ( keyid and keyserver) -def add_sources(srclist, searchList={ }): +def add_sources(srclist, searchList=None): + if searchList is None: + searchList = {} elst = [] for ent in srclist: @@ -202,7 +204,7 @@ def find_apt_mirror(cloud, cfg): if not mirror and cloud: # if we have a fqdn, then search its domain portion first - ( hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud) + ( _hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud) mydom = ".".join(fqdn.split(".")[1:]) if mydom: doms.append(".%s" % mydom) diff --git a/cloudinit/CloudConfig/cc_bootcmd.py b/cloudinit/CloudConfig/cc_bootcmd.py index 5a9e4356..fc925447 100644 --- a/cloudinit/CloudConfig/cc_bootcmd.py +++ b/cloudinit/CloudConfig/cc_bootcmd.py @@ -21,7 +21,7 @@ import tempfile from cloudinit.CloudConfig import per_always frequency = per_always -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): if not cfg.has_key("bootcmd"): return diff --git a/cloudinit/CloudConfig/cc_byobu.py b/cloudinit/CloudConfig/cc_byobu.py index 406a1f67..dd510dda 100644 --- a/cloudinit/CloudConfig/cc_byobu.py +++ b/cloudinit/CloudConfig/cc_byobu.py @@ -19,7 +19,7 @@ import cloudinit.util as util import subprocess import traceback -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,args): if len(args) != 0: value = args[0] else: diff --git a/cloudinit/CloudConfig/cc_chef.py b/cloudinit/CloudConfig/cc_chef.py index 14960a3c..977fe80f 100644 --- a/cloudinit/CloudConfig/cc_chef.py +++ b/cloudinit/CloudConfig/cc_chef.py @@ -14,19 +14,16 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import os -import pwd -import socket import subprocess import json -import StringIO -import ConfigParser import cloudinit.CloudConfig as cc import cloudinit.util as util ruby_version_default = "1.8" -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): # If there isn't a chef key in the configuration don't do anything if not cfg.has_key('chef'): return chef_cfg = cfg['chef'] @@ -44,7 +41,6 @@ def handle(name,cfg,cloud,log,args): with open('/etc/chef/validation.pem', 'w') as validation_key_fh: validation_key_fh.write(validation_key) - validation_name = chef_cfg.get('validation_name','chef-validator') # create the chef config from template util.render_to_file('chef_client.rb', '/etc/chef/client.rb', {'server_url': chef_cfg['server_url'], @@ -90,9 +86,9 @@ def get_ruby_packages(version): def install_chef_from_gems(ruby_version, chef_version = None): cc.install_packages(get_ruby_packages(ruby_version)) if not os.path.exists('/usr/bin/gem'): - os.symlink('/usr/bin/gem%s' % ruby_version, '/usr/bin/gem') + os.symlink('/usr/bin/gem%s' % ruby_version, '/usr/bin/gem') if not os.path.exists('/usr/bin/ruby'): - os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby') + os.symlink('/usr/bin/ruby%s' % ruby_version, '/usr/bin/ruby') if chef_version: subprocess.check_call(['/usr/bin/gem','install','chef', '-v %s' % chef_version, '--no-ri', diff --git a/cloudinit/CloudConfig/cc_disable_ec2_metadata.py b/cloudinit/CloudConfig/cc_disable_ec2_metadata.py index 5cd1d1c1..f06d4dfc 100644 --- a/cloudinit/CloudConfig/cc_disable_ec2_metadata.py +++ b/cloudinit/CloudConfig/cc_disable_ec2_metadata.py @@ -21,7 +21,7 @@ from cloudinit.CloudConfig import per_always frequency = per_always -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,_log,_args): if util.get_cfg_option_bool(cfg, "disable_ec2_metadata", False): fwall="route add -host 169.254.169.254 reject" subprocess.call(fwall.split(' ')) diff --git a/cloudinit/CloudConfig/cc_final_message.py b/cloudinit/CloudConfig/cc_final_message.py index fbdb5c94..c8631d01 100644 --- a/cloudinit/CloudConfig/cc_final_message.py +++ b/cloudinit/CloudConfig/cc_final_message.py @@ -24,7 +24,7 @@ frequency = per_always final_message = "cloud-init boot finished at $TIMESTAMP. Up $UPTIME seconds" -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,args): if len(args) != 0: msg_in = args[0] else: diff --git a/cloudinit/CloudConfig/cc_foo.py b/cloudinit/CloudConfig/cc_foo.py index aa95578f..48d20e5b 100644 --- a/cloudinit/CloudConfig/cc_foo.py +++ b/cloudinit/CloudConfig/cc_foo.py @@ -15,10 +15,12 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import cloudinit -import cloudinit.util as util + +#import cloudinit +#import cloudinit.util as util from cloudinit.CloudConfig import per_instance frequency = per_instance -def handle(name,cfg,cloud,log,args): - print "hi" + +def handle(_name,_cfg,_cloud,_log,_args): + print "hi" diff --git a/cloudinit/CloudConfig/cc_grub_dpkg.py b/cloudinit/CloudConfig/cc_grub_dpkg.py index b26e90e8..97d79bdb 100644 --- a/cloudinit/CloudConfig/cc_grub_dpkg.py +++ b/cloudinit/CloudConfig/cc_grub_dpkg.py @@ -15,12 +15,12 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit.util as util -import subprocess import traceback import os -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,_args): idevs=None idevs_empty=None diff --git a/cloudinit/CloudConfig/cc_keys_to_console.py b/cloudinit/CloudConfig/cc_keys_to_console.py index 79dd2ea9..08e8f085 100644 --- a/cloudinit/CloudConfig/cc_keys_to_console.py +++ b/cloudinit/CloudConfig/cc_keys_to_console.py @@ -21,7 +21,7 @@ import subprocess frequency = per_instance -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,_args): cmd = [ '/usr/lib/cloud-init/write-ssh-key-fingerprints' ] fp_blacklist = util.get_cfg_option_list_or_str(cfg, "ssh_fp_console_blacklist", []) key_blacklist = util.get_cfg_option_list_or_str(cfg, "ssh_key_console_blacklist", ["ssh-dss"]) diff --git a/cloudinit/CloudConfig/cc_landscape.py b/cloudinit/CloudConfig/cc_landscape.py index e3e4f7ba..22a90665 100644 --- a/cloudinit/CloudConfig/cc_landscape.py +++ b/cloudinit/CloudConfig/cc_landscape.py @@ -32,7 +32,7 @@ lsc_builtincfg = { } } -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,_args): """ Basically turn a top level 'landscape' entry with a 'client' dict and render it to ConfigObj format under '[client]' section in diff --git a/cloudinit/CloudConfig/cc_locale.py b/cloudinit/CloudConfig/cc_locale.py index 8542f641..8e91d3bf 100644 --- a/cloudinit/CloudConfig/cc_locale.py +++ b/cloudinit/CloudConfig/cc_locale.py @@ -28,7 +28,7 @@ def apply_locale(locale, cfgfile): util.render_to_file('default-locale', cfgfile, { 'locale' : locale }) -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,args): if len(args) != 0: locale = args[0] else: diff --git a/cloudinit/CloudConfig/cc_mcollective.py b/cloudinit/CloudConfig/cc_mcollective.py index c7912aa4..38fe4a3c 100644 --- a/cloudinit/CloudConfig/cc_mcollective.py +++ b/cloudinit/CloudConfig/cc_mcollective.py @@ -16,11 +16,9 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import os -import pwd -import socket import subprocess -import fileinput import StringIO import ConfigParser import cloudinit.CloudConfig as cc @@ -40,7 +38,7 @@ class FakeSecHead(object): finally: self.sechead = None else: return self.fp.readline() -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,_log,_args): # If there isn't a mcollective key in the configuration don't do anything if not cfg.has_key('mcollective'): return mcollective_cfg = cfg['mcollective'] diff --git a/cloudinit/CloudConfig/cc_mounts.py b/cloudinit/CloudConfig/cc_mounts.py index db382f04..a3036d5a 100644 --- a/cloudinit/CloudConfig/cc_mounts.py +++ b/cloudinit/CloudConfig/cc_mounts.py @@ -31,7 +31,7 @@ def is_mdname(name): return True return False -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): # fs_spec, fs_file, fs_vfstype, fs_mntops, fs-freq, fs_passno defvals = [ None, None, "auto", "defaults,nobootwait", "0", "2" ] defvals = cfg.get("mount_default_fields", defvals) @@ -116,7 +116,7 @@ def handle(name,cfg,cloud,log,args): # now, each entry in the cfgmnt list has all fstab values # if the second field is None (not the string, the value) we skip it - actlist = filter(lambda x: x[1] is not None, cfgmnt) + actlist = [x for x in cfgmnt if x[1] is not None] if len(actlist) == 0: return diff --git a/cloudinit/CloudConfig/cc_phone_home.py b/cloudinit/CloudConfig/cc_phone_home.py index f291e1d4..7897d31b 100644 --- a/cloudinit/CloudConfig/cc_phone_home.py +++ b/cloudinit/CloudConfig/cc_phone_home.py @@ -31,7 +31,7 @@ post_list_all = [ 'pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', 'instance_id', # url: http://my.foo.bar/$INSTANCE_ID/ # post: [ pub_key_dsa, pub_key_rsa, pub_key_ecdsa, instance_id # -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,args): if len(args) != 0: ph_cfg = util.readconf(args[0]) else: @@ -68,7 +68,6 @@ def handle(name,cfg,cloud,log,args): try: fp = open(path, "rb") all_keys[n] = fp.read() - all_keys[n] fp.close() except: log.warn("%s: failed to open in phone_home" % path) diff --git a/cloudinit/CloudConfig/cc_puppet.py b/cloudinit/CloudConfig/cc_puppet.py index c635d1f5..3748559a 100644 --- a/cloudinit/CloudConfig/cc_puppet.py +++ b/cloudinit/CloudConfig/cc_puppet.py @@ -25,7 +25,7 @@ import ConfigParser import cloudinit.CloudConfig as cc import cloudinit.util as util -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): # If there isn't a puppet key in the configuration don't do anything if not cfg.has_key('puppet'): return puppet_cfg = cfg['puppet'] diff --git a/cloudinit/CloudConfig/cc_resizefs.py b/cloudinit/CloudConfig/cc_resizefs.py index 6615636f..adec70be 100644 --- a/cloudinit/CloudConfig/cc_resizefs.py +++ b/cloudinit/CloudConfig/cc_resizefs.py @@ -15,9 +15,9 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit.util as util import subprocess -import traceback import os import stat import tempfile @@ -25,7 +25,7 @@ from cloudinit.CloudConfig import per_always frequency = per_always -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,args): if len(args) != 0: resize_root = False if str(args[0]).lower() in [ 'true', '1', 'on', 'yes']: @@ -41,9 +41,9 @@ def handle(name,cfg,cloud,log,args): os.close(fd) try: - st_dev=os.stat("/").st_dev - dev=os.makedev(os.major(st_dev),os.minor(st_dev)) - os.mknod(devpth, 0400 | stat.S_IFBLK, dev) + st_dev=os.stat("/").st_dev + dev=os.makedev(os.major(st_dev),os.minor(st_dev)) + os.mknod(devpth, 0400 | stat.S_IFBLK, dev) except: if util.islxc(): log.debug("inside lxc, ignoring mknod failure in resizefs") @@ -53,7 +53,7 @@ def handle(name,cfg,cloud,log,args): cmd = [ 'blkid', '-c', '/dev/null', '-sTYPE', '-ovalue', devpth ] try: - (fstype,err) = util.subp(cmd) + (fstype,_err) = util.subp(cmd) except subprocess.CalledProcessError as e: log.warn("Failed to get filesystem type of maj=%s, min=%s via: %s" % (os.major(st_dev), os.minor(st_dev), cmd)) @@ -74,7 +74,7 @@ def handle(name,cfg,cloud,log,args): return try: - (out,err) = util.subp(resize_cmd) + util.subp(resize_cmd) except subprocess.CalledProcessError as e: log.warn("Failed to resize filesystem (%s)" % resize_cmd) log.warn("output=%s\nerror=%s\n", e.output[0], e.output[1]) diff --git a/cloudinit/CloudConfig/cc_rightscale_userdata.py b/cloudinit/CloudConfig/cc_rightscale_userdata.py index 4875acbe..2b43023c 100644 --- a/cloudinit/CloudConfig/cc_rightscale_userdata.py +++ b/cloudinit/CloudConfig/cc_rightscale_userdata.py @@ -32,8 +32,9 @@ ## Therefore, this must run before that. ## ## + import cloudinit.util as util -from cloudinit.CloudConfig import per_once, per_always, per_instance +from cloudinit.CloudConfig import per_instance from cloudinit import get_ipath_cur from urlparse import parse_qs @@ -41,7 +42,7 @@ frequency = per_instance my_name = "cc_rightscale_userdata" my_hookname = 'CLOUD_INIT_REMOTE_HOOK' -def handle(name,cfg,cloud,log,args): +def handle(_name,_cfg,cloud,log,_args): try: ud = cloud.get_userdata_raw() except: @@ -49,7 +50,7 @@ def handle(name,cfg,cloud,log,args): return try: - mdict = parse_qs(cloud.get_userdata_raw()) + mdict = parse_qs(ud) if not my_hookname in mdict: return except: log.warn("failed to urlparse.parse_qa(userdata_raw())") @@ -57,7 +58,6 @@ def handle(name,cfg,cloud,log,args): scripts_d = get_ipath_cur('scripts') i = 0 - errors = [ ] first_e = None for url in mdict[my_hookname]: fname = "%s/rightscale-%02i" % (scripts_d,i) diff --git a/cloudinit/CloudConfig/cc_rsyslog.py b/cloudinit/CloudConfig/cc_rsyslog.py index a8ee8476..ab85a6d8 100644 --- a/cloudinit/CloudConfig/cc_rsyslog.py +++ b/cloudinit/CloudConfig/cc_rsyslog.py @@ -15,16 +15,16 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit import logging import cloudinit.util as util -import subprocess import traceback DEF_FILENAME = "20-cloud-config.conf" DEF_DIR = "/etc/rsyslog.d" -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,_args): # rsyslog: # - "*.* @@192.158.1.1" # - content: "*.* @@192.0.2.1:10514" @@ -38,8 +38,6 @@ def handle(name,cfg,cloud,log,args): def_dir = cfg.get('rsyslog_dir', DEF_DIR) def_fname = cfg.get('rsyslog_filename', DEF_FILENAME) - entries = cfg['rsyslog'] - files = [ ] elst = [ ] for ent in cfg['rsyslog']: @@ -80,7 +78,7 @@ def handle(name,cfg,cloud,log,args): # it will also return failure on the attempt, so 'restarted' # won't get set log.debug("restarting rsyslog") - p = util.subp(['service', 'rsyslog', 'restart']) + util.subp(['service', 'rsyslog', 'restart']) restarted = True except Exception as e: diff --git a/cloudinit/CloudConfig/cc_runcmd.py b/cloudinit/CloudConfig/cc_runcmd.py index f030980a..d255223b 100644 --- a/cloudinit/CloudConfig/cc_runcmd.py +++ b/cloudinit/CloudConfig/cc_runcmd.py @@ -15,10 +15,10 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import cloudinit + import cloudinit.util as util -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): if not cfg.has_key("runcmd"): return outfile="%s/runcmd" % cloud.get_ipath('scripts') diff --git a/cloudinit/CloudConfig/cc_scripts_per_boot.py b/cloudinit/CloudConfig/cc_scripts_per_boot.py index 4e407fb7..fb643c6d 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_boot.py +++ b/cloudinit/CloudConfig/cc_scripts_per_boot.py @@ -15,14 +15,15 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit.util as util -from cloudinit.CloudConfig import per_once, per_always, per_instance -from cloudinit import get_cpath, get_ipath_cur +from cloudinit.CloudConfig import per_always +from cloudinit import get_cpath frequency = per_always runparts_path = "%s/%s" % (get_cpath(), "scripts/per-boot") -def handle(name,cfg,cloud,log,args): +def handle(_name,_cfg,_cloud,log,_args): try: util.runparts(runparts_path) except: diff --git a/cloudinit/CloudConfig/cc_scripts_per_instance.py b/cloudinit/CloudConfig/cc_scripts_per_instance.py index 22b41185..b0f0601a 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_instance.py +++ b/cloudinit/CloudConfig/cc_scripts_per_instance.py @@ -15,14 +15,15 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit.util as util -from cloudinit.CloudConfig import per_once, per_always, per_instance -from cloudinit import get_cpath, get_ipath_cur +from cloudinit.CloudConfig import per_instance +from cloudinit import get_cpath frequency = per_instance runparts_path = "%s/%s" % (get_cpath(), "scripts/per-instance") -def handle(name,cfg,cloud,log,args): +def handle(_name,_cfg,_cloud,log,_args): try: util.runparts(runparts_path) except: diff --git a/cloudinit/CloudConfig/cc_scripts_per_once.py b/cloudinit/CloudConfig/cc_scripts_per_once.py index 9d752325..2ab81840 100644 --- a/cloudinit/CloudConfig/cc_scripts_per_once.py +++ b/cloudinit/CloudConfig/cc_scripts_per_once.py @@ -15,14 +15,15 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit.util as util -from cloudinit.CloudConfig import per_once, per_always, per_instance -from cloudinit import get_cpath, get_ipath_cur +from cloudinit.CloudConfig import per_once +from cloudinit import get_cpath frequency = per_once runparts_path = "%s/%s" % (get_cpath(), "scripts/per-once") -def handle(name,cfg,cloud,log,args): +def handle(_name,_cfg,_cloud,log,_args): try: util.runparts(runparts_path) except: diff --git a/cloudinit/CloudConfig/cc_scripts_user.py b/cloudinit/CloudConfig/cc_scripts_user.py index bafecd23..9c7f2322 100644 --- a/cloudinit/CloudConfig/cc_scripts_user.py +++ b/cloudinit/CloudConfig/cc_scripts_user.py @@ -15,14 +15,15 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit.util as util -from cloudinit.CloudConfig import per_once, per_always, per_instance -from cloudinit import get_cpath, get_ipath_cur +from cloudinit.CloudConfig import per_instance +from cloudinit import get_ipath_cur frequency = per_instance runparts_path = "%s/%s" % (get_ipath_cur(), "scripts") -def handle(name,cfg,cloud,log,args): +def handle(_name,_cfg,_cloud,log,_args): try: util.runparts(runparts_path) except: diff --git a/cloudinit/CloudConfig/cc_set_hostname.py b/cloudinit/CloudConfig/cc_set_hostname.py index bc190049..4f19b0c8 100644 --- a/cloudinit/CloudConfig/cc_set_hostname.py +++ b/cloudinit/CloudConfig/cc_set_hostname.py @@ -15,18 +15,18 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit.util as util -import subprocess -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): if util.get_cfg_option_bool(cfg,"preserve_hostname",False): log.debug("preserve_hostname is set. not setting hostname") return(True) - ( hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud) + ( hostname, _fqdn ) = util.get_hostname_fqdn(cfg, cloud) try: set_hostname(hostname, log) - except Exception as e: + except Exception: util.logexc(log) log.warn("failed to set hostname to %s\n", hostname) diff --git a/cloudinit/CloudConfig/cc_set_passwords.py b/cloudinit/CloudConfig/cc_set_passwords.py index edfbaeeb..07e3ca1b 100644 --- a/cloudinit/CloudConfig/cc_set_passwords.py +++ b/cloudinit/CloudConfig/cc_set_passwords.py @@ -15,13 +15,13 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + import cloudinit.util as util -import subprocess import sys import random import string -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,args): if len(args) != 0: # if run from command line, and give args, wipe the chpasswd['list'] password = args[0] @@ -115,7 +115,7 @@ def handle(name,cfg,cloud,log,args): return def rand_str(strlen=32, select_from=string.letters+string.digits): - return("".join([random.choice(select_from) for x in range(0, strlen)])) + return("".join([random.choice(select_from) for _x in range(0, strlen)])) def rand_user_password(pwlen=9): selfrom=(string.letters.translate(None,'loLOI') + diff --git a/cloudinit/CloudConfig/cc_ssh.py b/cloudinit/CloudConfig/cc_ssh.py index 9f5dc567..0aad2187 100644 --- a/cloudinit/CloudConfig/cc_ssh.py +++ b/cloudinit/CloudConfig/cc_ssh.py @@ -26,7 +26,7 @@ DISABLE_ROOT_OPTS="no-port-forwarding,no-agent-forwarding,no-X11-forwarding,comm global_log = None -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): global global_log global_log = log diff --git a/cloudinit/CloudConfig/cc_ssh_import_id.py b/cloudinit/CloudConfig/cc_ssh_import_id.py index bf1314be..7e7a54a1 100644 --- a/cloudinit/CloudConfig/cc_ssh_import_id.py +++ b/cloudinit/CloudConfig/cc_ssh_import_id.py @@ -19,7 +19,7 @@ import cloudinit.util as util import subprocess import traceback -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,args): if len(args) != 0: user = args[0] ids = [ ] diff --git a/cloudinit/CloudConfig/cc_timezone.py b/cloudinit/CloudConfig/cc_timezone.py index e8a453f9..26b2796d 100644 --- a/cloudinit/CloudConfig/cc_timezone.py +++ b/cloudinit/CloudConfig/cc_timezone.py @@ -15,16 +15,16 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. + from cloudinit.CloudConfig import per_instance from cloudinit import util -import subprocess import os.path import shutil frequency = per_instance tz_base = "/usr/share/zoneinfo" -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,_cloud,log,args): if len(args) != 0: timezone = args[0] else: diff --git a/cloudinit/CloudConfig/cc_update_etc_hosts.py b/cloudinit/CloudConfig/cc_update_etc_hosts.py index 1dc9c1e9..1c245e67 100644 --- a/cloudinit/CloudConfig/cc_update_etc_hosts.py +++ b/cloudinit/CloudConfig/cc_update_etc_hosts.py @@ -21,7 +21,7 @@ import StringIO frequency = per_always -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): ( hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud) manage_hosts = util.get_cfg_option_bool(cfg,"manage_etc_hosts", False) @@ -34,7 +34,7 @@ def handle(name,cfg,cloud,log,args): util.render_to_file('hosts', '/etc/hosts', \ { 'hostname' : hostname, 'fqdn' : fqdn }) - except Exception as e: + except Exception: log.warn("failed to update /etc/hosts") raise elif manage_hosts == "localhost": @@ -48,37 +48,37 @@ def handle(name,cfg,cloud,log,args): log.debug("not managing /etc/hosts") -def update_etc_hosts(hostname, fqdn, log): - with open('/etc/hosts', 'r') as etchosts: - header = "# Added by cloud-init\n" - hosts_line = "127.0.1.1\t%s %s\n" % (fqdn, hostname) - need_write = False - need_change = True - new_etchosts = StringIO.StringIO() - for line in etchosts: - split_line = [s.strip() for s in line.split()] - if len(split_line) < 2: +def update_etc_hosts(hostname, fqdn, _log): + with open('/etc/hosts', 'r') as etchosts: + header = "# Added by cloud-init\n" + hosts_line = "127.0.1.1\t%s %s\n" % (fqdn, hostname) + need_write = False + need_change = True + new_etchosts = StringIO.StringIO() + for line in etchosts: + split_line = [s.strip() for s in line.split()] + if len(split_line) < 2: new_etchosts.write(line) continue - if line == header: + if line == header: continue - ip, hosts = split_line[0], split_line[1:] - if ip == "127.0.1.1": - if sorted([hostname, fqdn]) == sorted(hosts): - need_change = False - if need_change == True: - line = "%s%s" % (header, hosts_line) - need_change = False - need_write = True - new_etchosts.write(line) - etchosts.close() - if need_change == True: - new_etchosts.write("%s%s" % (header, hosts_line)) - need_write = True - if need_write == True: - new_etcfile = open ('/etc/hosts','wb') - new_etcfile.write(new_etchosts.getvalue()) - new_etcfile.close() - new_etchosts.close() - return + ip, hosts = split_line[0], split_line[1:] + if ip == "127.0.1.1": + if sorted([hostname, fqdn]) == sorted(hosts): + need_change = False + if need_change == True: + line = "%s%s" % (header, hosts_line) + need_change = False + need_write = True + new_etchosts.write(line) + etchosts.close() + if need_change == True: + new_etchosts.write("%s%s" % (header, hosts_line)) + need_write = True + if need_write == True: + new_etcfile = open ('/etc/hosts','wb') + new_etcfile.write(new_etchosts.getvalue()) + new_etcfile.close() + new_etchosts.close() + return diff --git a/cloudinit/CloudConfig/cc_update_hostname.py b/cloudinit/CloudConfig/cc_update_hostname.py index 3f55c73b..4bc1cb2b 100644 --- a/cloudinit/CloudConfig/cc_update_hostname.py +++ b/cloudinit/CloudConfig/cc_update_hostname.py @@ -22,16 +22,16 @@ from cloudinit.CloudConfig import per_always frequency = per_always -def handle(name,cfg,cloud,log,args): +def handle(_name,cfg,cloud,log,_args): if util.get_cfg_option_bool(cfg,"preserve_hostname",False): log.debug("preserve_hostname is set. not updating hostname") return - ( hostname, fqdn ) = util.get_hostname_fqdn(cfg, cloud) + ( hostname, _fqdn ) = util.get_hostname_fqdn(cfg, cloud) try: prev ="%s/%s" % (cloud.get_cpath('data'),"previous-hostname") update_hostname(hostname, prev, log) - except Exception as e: + except Exception: log.warn("failed to set hostname\n") raise diff --git a/cloudinit/DataSource.py b/cloudinit/DataSource.py index 06061a2e..ac79f757 100644 --- a/cloudinit/DataSource.py +++ b/cloudinit/DataSource.py @@ -69,7 +69,7 @@ class DataSource: if isinstance(self.metadata['public-keys'], str): return([self.metadata['public-keys'],]) - for keyname, klist in self.metadata['public-keys'].items(): + for _keyname, klist in self.metadata['public-keys'].items(): # lp:506332 uec metadata service responds with # data that makes boto populate a string for 'klist' rather # than a list. @@ -82,7 +82,7 @@ class DataSource: return(keys) - def device_name_to_device(self, name): + def device_name_to_device(self, _name): # translate a 'name' to a device # the primary function at this point is on ec2 # to consult metadata service, that has @@ -157,7 +157,9 @@ class DataSource: # ie, pkglist=[ "foo", "" ] # will first try to load foo.DataSource<item> # then DataSource<item> -def list_sources(cfg_list, depends, pkglist=[]): +def list_sources(cfg_list, depends, pkglist=None): + if pkglist is None: + pkglist = [] retlist = [] for ds_coll in cfg_list: for pkg in pkglist: @@ -197,8 +199,8 @@ def is_ipv4(instr): return False try: - r = filter(lambda x: int(x) < 256 and x > 0, toks) + toks = [x for x in toks if (int(x) < 256 and int(x) > 0)] except: return False - return True + return (len(toks) == 4) diff --git a/cloudinit/DataSourceEc2.py b/cloudinit/DataSourceEc2.py index aee10ffa..9191e647 100644 --- a/cloudinit/DataSourceEc2.py +++ b/cloudinit/DataSourceEc2.py @@ -18,16 +18,13 @@ import DataSource -from cloudinit import seeddir, log +from cloudinit import seeddir, log # pylint: disable=W0611 import cloudinit.util as util import socket import urllib2 import time -import sys import boto.utils as boto_utils import os.path -import errno -import urlparse class DataSourceEc2(DataSource.DataSource): api_ver = '2009-04-04' @@ -92,7 +89,7 @@ class DataSourceEc2(DataSource.DataSource): max_wait = 120 try: max_wait = int(mcfg.get("max_wait",max_wait)) - except Exception as e: + except Exception: util.logexc(log) log.warn("Failed to get max wait. using %s" % max_wait) @@ -102,7 +99,7 @@ class DataSourceEc2(DataSource.DataSource): timeout = 50 try: timeout = int(mcfg.get("timeout",timeout)) - except Exception as e: + except Exception: util.logexc(log) log.warn("Failed to get timeout, using %s" % timeout) @@ -229,7 +226,6 @@ def wait_for_metadata_service(urls, max_wait=None, timeout=None, status_cb=None) starttime = time.time() sleeptime = 1 - timeout_orig = timeout if status_cb == None: def status_cb(msg): return diff --git a/cloudinit/DataSourceNoCloud.py b/cloudinit/DataSourceNoCloud.py index 956b1a5e..2f4bd604 100644 --- a/cloudinit/DataSourceNoCloud.py +++ b/cloudinit/DataSourceNoCloud.py @@ -18,12 +18,8 @@ import DataSource -from cloudinit import seeddir, log +from cloudinit import seeddir, log # pylint: disable=W0611 import cloudinit.util as util -import sys -import os.path -import os -import errno class DataSourceNoCloud(DataSource.DataSource): metadata = None @@ -94,7 +90,7 @@ class DataSourceNoCloud(DataSource.DataSource): md = util.mergedict(md,defaults) self.seed = ",".join(found) - self.metadata = md; + self.metadata = md self.userdata_raw = ud return True diff --git a/cloudinit/DataSourceOVF.py b/cloudinit/DataSourceOVF.py index a9089d70..1c510688 100644 --- a/cloudinit/DataSourceOVF.py +++ b/cloudinit/DataSourceOVF.py @@ -23,9 +23,7 @@ import cloudinit.util as util import sys import os.path import os -import errno from xml.dom import minidom -from xml.dom import Node import base64 import re import tempfile @@ -66,13 +64,13 @@ class DataSourceOVF(DataSource.DataSource): np = { 'iso' : transport_iso9660, 'vmware-guestd' : transport_vmware_guestd, } for name, transfunc in np.iteritems(): - (contents, dev, fname) = transfunc() + (contents, _dev, _fname) = transfunc() if contents: break if contents: (md, ud, cfg) = read_ovf_environment(contents) self.environment = contents - found.append(name) + found.append(name) # pylint: disable=W0631 # There was no OVF transports found if len(found) == 0: @@ -99,7 +97,7 @@ class DataSourceOVF(DataSource.DataSource): md = util.mergedict(md,defaults) self.seed = ",".join(found) - self.metadata = md; + self.metadata = md self.userdata_raw = ud self.cfg = cfg return True @@ -173,7 +171,7 @@ def transport_iso9660(require_iso=False): mounted = { } for mpline in mounts: - (dev,mp,fstype,opts,freq,passno) = mpline.split() + (dev,mp,fstype,_opts,_freq,_passno) = mpline.split() mounted[dev]=(dev,fstype,mp,False) mp = mp.replace("\\040"," ") if fstype != "iso9660" and require_iso: continue diff --git a/cloudinit/SshUtil.py b/cloudinit/SshUtil.py index fdd3bb27..125ca618 100644 --- a/cloudinit/SshUtil.py +++ b/cloudinit/SshUtil.py @@ -46,7 +46,7 @@ class AuthKeyEntry(): elif curc == '"': quoted = not quoted i = i + 1 - except IndexError as e: + except IndexError: self.is_comment = True return() @@ -54,7 +54,7 @@ class AuthKeyEntry(): self.options = ent[0:i] (self.keytype, self.base64, self.comment) = \ ent[i+1:].split(None,3) - except ValueError as e: + except ValueError: # we did not understand this line self.is_comment = True @@ -84,7 +84,7 @@ def update_authorized_keys(fname, keys): fp = open(fname, "r") lines = fp.readlines() # lines have carriage return fp.close() - except IOError as e: + except IOError: lines = [ ] ka_stats = { } # keys_added status @@ -133,7 +133,7 @@ def setup_user_keys(keys, user, key_prefix, log=None): akeys = akeys.replace("%h", pwent.pw_dir) akeys = akeys.replace("%u", user) authorized_keys = akeys - except Exception as e: + except Exception: authorized_keys = '%s/.ssh/authorized_keys' % pwent.pw_dir if log: util.logexc(log) diff --git a/cloudinit/UserDataHandler.py b/cloudinit/UserDataHandler.py index 541ee87a..14aea58b 100644 --- a/cloudinit/UserDataHandler.py +++ b/cloudinit/UserDataHandler.py @@ -20,12 +20,10 @@ import email from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase -from email import encoders import yaml import cloudinit import cloudinit.util as util import hashlib -import os import urllib starts_with_mappings={ @@ -39,15 +37,15 @@ starts_with_mappings={ '#cloud-config-archive' : 'text/cloud-config-archive', } -# if 'str' is compressed return decompressed otherwise return it -def decomp_str(str): +# if 'string' is compressed return decompressed otherwise return it +def decomp_str(string): import StringIO import gzip try: - uncomp = gzip.GzipFile(None,"rb",1,StringIO.StringIO(str)).read() + uncomp = gzip.GzipFile(None,"rb",1,StringIO.StringIO(string)).read() return(uncomp) except: - return(str) + return(string) def do_include(content, appendmsg): import os @@ -79,7 +77,7 @@ def do_include(content, appendmsg): content = urllib.urlopen(line).read() if includeonce: util.write_file(includeonce_filename, content, mode=0600) - except Exception as e: + except Exception: raise process_includes(message_from_string(decomp_str(content)), appendmsg) @@ -190,9 +188,10 @@ def process_includes(msg, appendmsg=None): _attach_part(appendmsg, part) -def message_from_string(data, headers={}): +def message_from_string(data, headers=None): + if headers is None: + headers = {} if "mime-version:" in data[0:4096].lower(): - was_mime = True msg = email.message_from_string(data) for (key,val) in headers.items(): if key in msg: @@ -200,7 +199,6 @@ def message_from_string(data, headers={}): else: msg[key] = val else: - was_mime = False mtype = headers.get("Content-Type","text/plain") maintype, subtype = mtype.split("/", 1) msg = MIMEBase(maintype, subtype, *headers) diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py index d01d443d..d74a0f1f 100644 --- a/cloudinit/__init__.py +++ b/cloudinit/__init__.py @@ -115,6 +115,8 @@ class CloudInit: old_conffile = '/etc/ec2-init/ec2-config.cfg' ds_deps = [ DataSource.DEP_FILESYSTEM, DataSource.DEP_NETWORK ] datasource = None + cloud_config_str = '' + datasource_name = '' builtin_handlers = [ ] @@ -137,7 +139,7 @@ class CloudInit: try: conf = util.get_base_cfg(self.sysconfig,cfg_builtin, parsed_cfgs) - except Exception as e: + except Exception: conf = get_builtin_cfg() # support reading the old ConfigObj format file and merging @@ -176,7 +178,7 @@ class CloudInit: try: f=open(cache, "wb") - data = cPickle.dump(self.datasource,f) + cPickle.dump(self.datasource,f) f.close() os.chmod(cache,0400) except: @@ -191,7 +193,8 @@ class CloudInit: cfglist=self.cfg['datasource_list'] dslist = list_sources(cfglist, self.ds_deps) - dsnames = map(lambda f: f.__name__, dslist) + dsnames = [f.__name__ for f in dslist] + log.debug("searching for data source in %s" % dsnames) for cls in dslist: ds = cls.__name__ @@ -205,7 +208,6 @@ class CloudInit: except Exception as e: log.warn("get_data of %s raised %s" % (ds,e)) util.logexc(log) - pass msg = "Did not find data source. searched classes: %s" % dsnames log.debug(msg) raise DataSourceNotFoundException(msg) @@ -298,7 +300,9 @@ class CloudInit: # if that does not exist, then call 'func' with given 'args' # if 'clear_on_fail' is True and func throws an exception # then remove the lock (so it would run again) - def sem_and_run(self,semname,freq,func,args=[],clear_on_fail=False): + def sem_and_run(self,semname,freq,func,args=None,clear_on_fail=False): + if args is None: + args = [] if self.sem_has_run(semname,freq): log.debug("%s already ran %s", semname, freq) return False @@ -360,12 +364,12 @@ class CloudInit: # give callbacks opportunity to finalize called = [ ] - for (mtype, mod) in part_handlers.iteritems(): + for (_mtype, mod) in part_handlers.iteritems(): if mod in called: continue handler_call_end(mod, data, frequency) - def handle_user_script(self,data,ctype,filename,payload, frequency): + def handle_user_script(self,_data,ctype,filename,payload, _frequency): if ctype == "__end__": return if ctype == "__begin__": # maybe delete existing things here @@ -376,7 +380,7 @@ class CloudInit: util.write_file("%s/%s" % (scriptsdir,filename), util.dos2unix(payload), 0700) - def handle_upstart_job(self,data,ctype,filename,payload, frequency): + def handle_upstart_job(self,_data,ctype,filename,payload, frequency): # upstart jobs are only written on the first boot if frequency != per_instance: return @@ -388,7 +392,7 @@ class CloudInit: util.write_file("%s/%s" % ("/etc/init",filename), util.dos2unix(payload), 0644) - def handle_cloud_config(self,data,ctype,filename,payload, frequency): + def handle_cloud_config(self,_data,ctype,filename,payload, _frequency): if ctype == "__begin__": self.cloud_config_str="" return @@ -408,7 +412,7 @@ class CloudInit: self.cloud_config_str+="\n#%s\n%s" % (filename,payload) - def handle_cloud_boothook(self,data,ctype,filename,payload, frequency): + def handle_cloud_boothook(self,_data,ctype,filename,payload, _frequency): if ctype == "__end__": return if ctype == "__begin__": return @@ -425,7 +429,7 @@ class CloudInit: try: env=os.environ.copy() env['INSTANCE_ID']= self.datasource.get_instance_id() - ret = subprocess.check_call([filepath], env=env) + subprocess.check_call([filepath], env=env) except subprocess.CalledProcessError as e: log.error("boothooks script %s returned %i" % (filepath,e.returncode)) @@ -538,7 +542,7 @@ def handler_handle_part(mod, data, ctype, filename, payload, frequency): else: mod.handle_part(data, ctype, filename, payload, frequency) -def partwalker_handle_handler(pdata, ctype, filename, payload): +def partwalker_handle_handler(pdata, _ctype, _filename, payload): curcount = pdata['handlercount'] modname = 'part-handler-%03d' % curcount diff --git a/cloudinit/netinfo.py b/cloudinit/netinfo.py index 3ba922e8..9ca72b77 100644 --- a/cloudinit/netinfo.py +++ b/cloudinit/netinfo.py @@ -24,7 +24,7 @@ def netdev_info(): if toks[i] == "hwaddr": try: devs[curdev]["hwaddr"] = toks[i+1] - except IndexError as e: + except IndexError: pass for field in ("addr", "bcast", "mask"): target = "%s%s" % (field, fieldpost) @@ -33,7 +33,7 @@ def netdev_info(): if toks[i] == "%s:" % field: try: devs[curdev][target] = toks[i+1] - except IndexError as e: + except IndexError: pass elif toks[i].startswith("%s:" % field): devs[curdev][target] = toks[i][len(field)+1:] @@ -61,23 +61,23 @@ def debug_info(pre="ci-info: "): lines = [ ] try: netdev = netdev_info() - except Exception as e: + except Exception: lines.append("netdev_info failed!") netdev = {} for (dev, d) in netdev.iteritems(): lines.append("%s%-6s: %i %-15s %-15s %s" % (pre, dev, d["up"], d["addr"],d["mask"], d["hwaddr"])) try: - routes = route_info() - except Exception as e: - lines.append("route_info failed") - routes = [] + routes = route_info() + except Exception: + lines.append("route_info failed") + routes = [] n = 0 for r in routes: - lines.append("%sroute-%d: %-15s %-15s %-15s %-6s %s" % - (pre, n, r[0], r[1], r[2], r[7], r[3])) - n = n+1 + lines.append("%sroute-%d: %-15s %-15s %-15s %-6s %s" % + (pre, n, r[0], r[1], r[2], r[7], r[3])) + n = n+1 return('\n'.join(lines)) if __name__ == '__main__': - print debug_info() + print debug_info() diff --git a/cloudinit/util.py b/cloudinit/util.py index de95ec79..5bf8e8b2 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -39,10 +39,10 @@ except ImportError: def read_conf(fname): try: - stream = open(fname,"r") - conf = yaml.load(stream) - stream.close() - return conf + stream = open(fname,"r") + conf = yaml.load(stream) + stream.close() + return conf except IOError as e: if e.errno == errno.ENOENT: return { } @@ -111,19 +111,19 @@ def mergedict(src,cand): src[k] = mergedict(src[k],v) return src -def write_file(file,content,mode=0644,omode="wb"): - try: - os.makedirs(os.path.dirname(file)) - except OSError as e: - if e.errno != errno.EEXIST: - raise e +def write_file(filename,content,mode=0644,omode="wb"): + try: + os.makedirs(os.path.dirname(filename)) + except OSError as e: + if e.errno != errno.EEXIST: + raise e - f=open(file,omode) - if mode != None: - os.chmod(file,mode) - f.write(content) - f.close() - restorecon_if_possible(file) + f=open(filename,omode) + if mode != None: + os.chmod(filename,mode) + f.write(content) + f.close() + restorecon_if_possible(filename) def restorecon_if_possible(path, recursive=False): if HAVE_LIBSELINUX and selinux.is_selinux_enabled(): @@ -131,20 +131,20 @@ def restorecon_if_possible(path, recursive=False): # get keyid from keyserver def getkeybyid(keyid,keyserver): - shcmd=""" - k=${1} ks=${2}; - exec 2>/dev/null - [ -n "$k" ] || exit 1; - armour=$(gpg --list-keys --armour "${k}") - if [ -z "${armour}" ]; then - gpg --keyserver ${ks} --recv $k >/dev/null && - armour=$(gpg --export --armour "${k}") && - gpg --batch --yes --delete-keys "${k}" - fi - [ -n "${armour}" ] && echo "${armour}" - """ - args=['sh', '-c', shcmd, "export-gpg-keyid", keyid, keyserver] - return(subp(args)[0]) + shcmd=""" + k=${1} ks=${2}; + exec 2>/dev/null + [ -n "$k" ] || exit 1; + armour=$(gpg --list-keys --armour "${k}") + if [ -z "${armour}" ]; then + gpg --keyserver ${ks} --recv $k >/dev/null && + armour=$(gpg --export --armour "${k}") && + gpg --batch --yes --delete-keys "${k}" + fi + [ -n "${armour}" ] && echo "${armour}" + """ + args=['sh', '-c', shcmd, "export-gpg-keyid", keyid, keyserver] + return(subp(args)[0]) def runparts(dirp, skip_no_exist=True): if skip_no_exist and not os.path.isdir(dirp): return @@ -159,10 +159,10 @@ def runparts(dirp, skip_no_exist=True): raise subprocess.CalledProcessError(sp.returncode,cmd) return -def subp(args, input=None): +def subp(args, input_=None): sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) - out,err = sp.communicate(input) + out,err = sp.communicate(input_) if sp.returncode is not 0: raise subprocess.CalledProcessError(sp.returncode,args, (out,err)) return(out,err) @@ -238,7 +238,9 @@ def logexc(log,lvl=logging.DEBUG): class RecursiveInclude(Exception): pass -def read_file_with_includes(fname, rel = ".", stack=[], patt = None): +def read_file_with_includes(fname, rel = ".", stack=None, patt = None): + if stack is None: + stack = [] if not fname.startswith("/"): fname = os.sep.join((rel, fname)) @@ -264,7 +266,6 @@ def read_file_with_includes(fname, rel = ".", stack=[], patt = None): stack.append(fname) cur = 0 - clen = len(contents) while True: match = patt.search(contents[cur:]) if not match: break @@ -282,7 +283,7 @@ def read_file_with_includes(fname, rel = ".", stack=[], patt = None): else: raise contents = contents[0:loc] + inc_contents + contents[endl+1:] - cur = loc + len(inc_contents) + cur = loc + len(inc_contents) stack.pop() return(contents) @@ -291,10 +292,10 @@ def read_conf_d(confd): confs = sorted(os.listdir(confd),reverse=True) # remove anything not ending in '.cfg' - confs = filter(lambda f: f.endswith(".cfg"), confs) + confs = [f for f in confs if f.endswith(".cfg")] # remove anything not a file - confs = filter(lambda f: os.path.isfile("%s/%s" % (confd,f)),confs) + confs = [f for f in confs if os.path.isfile("%s/%s" % (confd,f))] cfg = { } for conf in confs: @@ -377,17 +378,17 @@ def ensure_dirs(dirlist, mode=0755): os.chmod(d, mode) def chownbyname(fname,user=None,group=None): - uid = -1 - gid = -1 - if user == None and group == None: return - if user: - import pwd - uid = pwd.getpwnam(user).pw_uid - if group: - import grp - gid = grp.getgrnam(group).gr_gid - - os.chown(fname,uid,gid) + uid = -1 + gid = -1 + if user == None and group == None: return + if user: + import pwd + uid = pwd.getpwnam(user).pw_uid + if group: + import grp + gid = grp.getgrnam(group).gr_gid + + os.chown(fname,uid,gid) def readurl(url, data=None, timeout=None): openargs = { } @@ -422,11 +423,11 @@ def shellify(cmdlist): content="%s%s\n" % ( content, str(args) ) return content -def dos2unix(input): +def dos2unix(string): # find first end of line - pos = input.find('\n') - if pos <= 0 or input[pos-1] != '\r': return(input) - return(input.replace('\r\n','\n')) + pos = string.find('\n') + if pos <= 0 or string[pos-1] != '\r': return(string) + return(string.replace('\r\n','\n')) def islxc(): # is this host running lxc? @@ -443,7 +444,7 @@ def islxc(): # we're inside a container. otherwise, no sp = subprocess.Popen(['lxc-is-container'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - out,err = sp.communicate(None) + sp.communicate(None) return(sp.returncode == 0) except OSError as e: if e.errno != errno.ENOENT: @@ -507,7 +508,7 @@ def is_resolvable(name): try: socket.getaddrinfo(name, None) return True - except socket.gaierror as e: + except socket.gaierror: return False def is_resolvable_url(url): @@ -520,20 +521,20 @@ def search_for_mirror(candidates): try: if is_resolvable_url(cand): return cand - except Exception as e: + except Exception: raise return None def close_stdin(): - """ - reopen stdin as /dev/null so even subprocesses or other os level things get - /dev/null as input. - - if _CLOUD_INIT_SAVE_STDIN is set in environment to a non empty or '0' value - then input will not be closed (only useful potentially for debugging). - """ - if os.environ.get("_CLOUD_INIT_SAVE_STDIN") in ("", "0", False): - return - with open(os.devnull) as fp: - os.dup2(fp.fileno(), sys.stdin.fileno()) + """ + reopen stdin as /dev/null so even subprocesses or other os level things get + /dev/null as input. + + if _CLOUD_INIT_SAVE_STDIN is set in environment to a non empty or '0' value + then input will not be closed (only useful potentially for debugging). + """ + if os.environ.get("_CLOUD_INIT_SAVE_STDIN") in ("", "0", False): + return + with open(os.devnull) as fp: + os.dup2(fp.fileno(), sys.stdin.fileno()) |