summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2012-01-12 17:03:26 +0100
committerScott Moser <smoser@ubuntu.com>2012-01-12 17:03:26 +0100
commit1d00c0936bfc63117493d89268da8c81611b3c40 (patch)
tree23077c9f5d348a1b29408bba04cdac049a6ec76b /cloudinit/util.py
parent982856531a3498d784e3977f307a9cbf2a673977 (diff)
parent7c38d4b469d3863443ec3322f0def25672c545db (diff)
downloadvyos-cloud-init-1d00c0936bfc63117493d89268da8c81611b3c40.tar.gz
vyos-cloud-init-1d00c0936bfc63117493d89268da8c81611b3c40.zip
fix pylint warnings (LP: #914739) [Juerg Haefliger]
LP: #914739
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py133
1 files changed, 67 insertions, 66 deletions
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())