Massive pylint + pep8 fixups!

This commit is contained in:
Joshua Harlow 2012-06-20 17:13:55 -07:00
parent 8ed740ce0f
commit ea70a98476
38 changed files with 159 additions and 161 deletions

View File

@ -75,6 +75,7 @@ def welcome(action):
sys.stderr.flush() sys.stderr.flush()
LOG.info(welcome_msg) LOG.info(welcome_msg)
def extract_fns(args): def extract_fns(args):
# Files are already opened so lets just pass that along # Files are already opened so lets just pass that along
# since it would of broke if it couldn't have # since it would of broke if it couldn't have
@ -394,10 +395,10 @@ def main():
' pass to this module')) ' pass to this module'))
parser_single.set_defaults(action=('single', main_single)) parser_single.set_defaults(action=('single', main_single))
args = parser.parse_args() args = parser.parse_args()
# Setup basic logging to start (until reinitialized) # Setup basic logging to start (until reinitialized)
# iff in debug mode...
if args.debug: if args.debug:
logging.setupBasicLogging() logging.setupBasicLogging()
@ -407,4 +408,3 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())

View File

@ -38,6 +38,7 @@ LOG = logging.getLogger(__name__)
# as providing a backwards compatible object that can be maintained # as providing a backwards compatible object that can be maintained
# while the stages/other objects can be worked on independently... # while the stages/other objects can be worked on independently...
class Cloud(object): class Cloud(object):
def __init__(self, datasource, paths, cfg, distro, runners): def __init__(self, datasource, paths, cfg, distro, runners):
self.datasource = datasource self.datasource = datasource

View File

@ -24,7 +24,7 @@ import os
from cloudinit import templater from cloudinit import templater
from cloudinit import util from cloudinit import util
ruby_version_default = "1.8" RUBY_VERSION_DEFAULT = "1.8"
def handle(name, cfg, cloud, log, _args): def handle(name, cfg, cloud, log, _args):
@ -92,7 +92,7 @@ def handle(name, cfg, cloud, log, _args):
# this will install and run the chef-client from gems # this will install and run the chef-client from gems
chef_version = util.get_cfg_option_str(chef_cfg, 'version', None) chef_version = util.get_cfg_option_str(chef_cfg, 'version', None)
ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version', ruby_version = util.get_cfg_option_str(chef_cfg, 'ruby_version',
ruby_version_default) RUBY_VERSION_DEFAULT)
install_chef_from_gems(cloud.distro, ruby_version, chef_version) install_chef_from_gems(cloud.distro, ruby_version, chef_version)
# and finally, run chef-client # and finally, run chef-client
log.debug('Running chef-client') log.debug('Running chef-client')

View File

@ -24,13 +24,13 @@ from cloudinit.settings import PER_ALWAYS
frequency = PER_ALWAYS frequency = PER_ALWAYS
reject_cmd = ['route', 'add', '-host', '169.254.169.254', 'reject'] REJECT_CMD = ['route', 'add', '-host', '169.254.169.254', 'reject']
def handle(name, cfg, _cloud, log, _args): def handle(name, cfg, _cloud, log, _args):
disabled = util.get_cfg_option_bool(cfg, "disable_ec2_metadata", False) disabled = util.get_cfg_option_bool(cfg, "disable_ec2_metadata", False)
if disabled: if disabled:
util.subp(reject_cmd) util.subp(REJECT_CMD)
else: else:
log.debug(("Skipping transform named %s," log.debug(("Skipping transform named %s,"
" disabling the ec2 route not enabled"), name) " disabling the ec2 route not enabled"), name)

View File

@ -28,7 +28,7 @@ from cloudinit.settings import PER_ALWAYS
frequency = PER_ALWAYS frequency = PER_ALWAYS
final_message_def = ("Cloud-init v. {{version}} finished at {{timestamp}}." FINAL_MESSAGE_DEF = ("Cloud-init v. {{version}} finished at {{timestamp}}."
" Up {{uptime}} seconds.") " Up {{uptime}} seconds.")
@ -46,7 +46,7 @@ def handle(_name, cfg, cloud, log, args):
msg_in = util.load_file(template_fn) msg_in = util.load_file(template_fn)
if not msg_in: if not msg_in:
msg_in = final_message_def msg_in = FINAL_MESSAGE_DEF
uptime = util.uptime() uptime = util.uptime()
ts = util.time_rfc2822() ts = util.time_rfc2822()

View File

@ -26,13 +26,13 @@ from cloudinit import util
frequency = PER_INSTANCE frequency = PER_INSTANCE
# This is a tool that cloud init provides # This is a tool that cloud init provides
helper_tool = '/usr/lib/cloud-init/write-ssh-key-fingerprints' HELPER_TOOL = '/usr/lib/cloud-init/write-ssh-key-fingerprints'
def handle(name, cfg, cloud, log, _args): def handle(name, cfg, cloud, log, _args):
if not os.path.exists(helper_tool): if not os.path.exists(HELPER_TOOL):
log.warn(("Unable to activate transform %s," log.warn(("Unable to activate transform %s,"
" helper tool not found at %s"), name, helper_tool) " helper tool not found at %s"), name, HELPER_TOOL)
return return
fp_blacklist = util.get_cfg_option_list(cfg, fp_blacklist = util.get_cfg_option_list(cfg,
@ -42,7 +42,7 @@ def handle(name, cfg, cloud, log, _args):
["ssh-dss"]) ["ssh-dss"])
try: try:
cmd = [helper_tool] cmd = [HELPER_TOOL]
cmd.append(','.join(fp_blacklist)) cmd.append(','.join(fp_blacklist))
cmd.append(','.join(key_blacklist)) cmd.append(','.join(key_blacklist))
(stdout, _stderr) = util.subp(cmd) (stdout, _stderr) = util.subp(cmd)

View File

@ -24,10 +24,10 @@ import re
from cloudinit import util from cloudinit import util
# shortname matches 'sda', 'sda1', 'xvda', 'hda', 'sdb', xvdb, vda, vdd1 # Shortname matches 'sda', 'sda1', 'xvda', 'hda', 'sdb', xvdb, vda, vdd1
shortname_filter = r"^[x]{0,1}[shv]d[a-z][0-9]*$" SHORTNAME_FILTER = r"^[x]{0,1}[shv]d[a-z][0-9]*$"
shortname = re.compile(shortname_filter) SHORTNAME = re.compile(SHORTNAME_FILTER)
ws = re.compile("[%s]+" % (whitespace)) WS = re.compile("[%s]+" % (whitespace))
def is_mdname(name): def is_mdname(name):
@ -55,7 +55,6 @@ def handle(_name, cfg, cloud, log, _args):
if "mounts" in cfg: if "mounts" in cfg:
cfgmnt = cfg["mounts"] cfgmnt = cfg["mounts"]
for i in range(len(cfgmnt)): for i in range(len(cfgmnt)):
# skip something that wasn't a list # skip something that wasn't a list
if not isinstance(cfgmnt[i], list): if not isinstance(cfgmnt[i], list):
@ -85,7 +84,7 @@ def handle(_name, cfg, cloud, log, _args):
cfgmnt[i][0] = renamed cfgmnt[i][0] = renamed
log.debug("Mapped metadata name %s to %s", startname, renamed) log.debug("Mapped metadata name %s to %s", startname, renamed)
else: else:
if shortname.match(startname): if SHORTNAME.match(startname):
renamed = "/dev/%s" % startname renamed = "/dev/%s" % startname
log.debug("Mapped shortname name %s to %s", startname, renamed) log.debug("Mapped shortname name %s to %s", startname, renamed)
cfgmnt[i][0] = renamed cfgmnt[i][0] = renamed
@ -171,7 +170,7 @@ def handle(_name, cfg, cloud, log, _args):
fstab = util.load_file(cloud.paths.join(True, "/etc/fstab")) fstab = util.load_file(cloud.paths.join(True, "/etc/fstab"))
for line in fstab.splitlines(): for line in fstab.splitlines():
try: try:
toks = ws.split(line) toks = WS.split(line)
if toks[3].find(comment) != -1: if toks[3].find(comment) != -1:
continue continue
except: except:

View File

@ -26,8 +26,13 @@ from cloudinit.settings import PER_INSTANCE
frequency = PER_INSTANCE frequency = PER_INSTANCE
post_list_all = ['pub_key_dsa', 'pub_key_rsa', 'pub_key_ecdsa', POST_LIST_ALL = [
'instance_id', 'hostname'] 'pub_key_dsa',
'pub_key_rsa',
'pub_key_ecdsa',
'instance_id',
'hostname'
]
# phone_home: # phone_home:
@ -63,7 +68,7 @@ def handle(name, cfg, cloud, log, args):
" is not an integer, using %s instead"), tries) " is not an integer, using %s instead"), tries)
if post_list == "all": if post_list == "all":
post_list = post_list_all post_list = POST_LIST_ALL
all_keys = {} all_keys = {}
all_keys['instance_id'] = cloud.get_instance_id() all_keys['instance_id'] = cloud.get_instance_id()

View File

@ -27,7 +27,7 @@ from cloudinit.settings import PER_ALWAYS
frequency = PER_ALWAYS frequency = PER_ALWAYS
resize_fs_prefixes_cmds = [ RESIZE_FS_PREFIXES_CMDS = [
('ext', 'resize2fs'), ('ext', 'resize2fs'),
('xfs', 'xfs_growfs'), ('xfs', 'xfs_growfs'),
] ]
@ -98,7 +98,7 @@ def handle(name, cfg, cloud, log, args):
resizer = None resizer = None
fstype_lc = fs_type.lower() fstype_lc = fs_type.lower()
for (pfix, root_cmd) in resize_fs_prefixes_cmds: for (pfix, root_cmd) in RESIZE_FS_PREFIXES_CMDS:
if fstype_lc.startswith(pfix): if fstype_lc.startswith(pfix):
resizer = root_cmd resizer = root_cmd
break break

View File

@ -26,16 +26,16 @@ from cloudinit.settings import PER_ALWAYS
frequency = PER_ALWAYS frequency = PER_ALWAYS
script_subdir = 'per-boot' SCRIPT_SUBDIR = 'per-boot'
def handle(name, _cfg, cloud, log, _args): def handle(name, _cfg, cloud, log, _args):
# Comes from the following: # Comes from the following:
# https://forums.aws.amazon.com/thread.jspa?threadID=96918 # https://forums.aws.amazon.com/thread.jspa?threadID=96918
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', script_subdir) runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
try: try:
util.runparts(runparts_path) util.runparts(runparts_path)
except: except:
log.warn("Failed to run transform %s (%s in %s)", log.warn("Failed to run transform %s (%s in %s)",
name, script_subdir, runparts_path) name, SCRIPT_SUBDIR, runparts_path)
raise raise

View File

@ -26,16 +26,16 @@ from cloudinit.settings import PER_INSTANCE
frequency = PER_INSTANCE frequency = PER_INSTANCE
script_subdir = 'per-instance' SCRIPT_SUBDIR = 'per-instance'
def handle(name, _cfg, cloud, log, _args): def handle(name, _cfg, cloud, log, _args):
# Comes from the following: # Comes from the following:
# https://forums.aws.amazon.com/thread.jspa?threadID=96918 # https://forums.aws.amazon.com/thread.jspa?threadID=96918
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', script_subdir) runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
try: try:
util.runparts(runparts_path) util.runparts(runparts_path)
except: except:
log.warn("Failed to run transform %s (%s in %s)", log.warn("Failed to run transform %s (%s in %s)",
name, script_subdir, runparts_path) name, SCRIPT_SUBDIR, runparts_path)
raise raise

View File

@ -26,16 +26,16 @@ from cloudinit.settings import PER_ONCE
frequency = PER_ONCE frequency = PER_ONCE
script_subdir = 'per-once' SCRIPT_SUBDIR = 'per-once'
def handle(name, _cfg, cloud, log, _args): def handle(name, _cfg, cloud, log, _args):
# Comes from the following: # Comes from the following:
# https://forums.aws.amazon.com/thread.jspa?threadID=96918 # https://forums.aws.amazon.com/thread.jspa?threadID=96918
runparts_path = os.path.join(cloud.get_cpath(), 'scripts', script_subdir) runparts_path = os.path.join(cloud.get_cpath(), 'scripts', SCRIPT_SUBDIR)
try: try:
util.runparts(runparts_path) util.runparts(runparts_path)
except: except:
log.warn("Failed to run transform %s (%s in %s)", log.warn("Failed to run transform %s (%s in %s)",
name, script_subdir, runparts_path) name, SCRIPT_SUBDIR, runparts_path)
raise raise

View File

@ -26,17 +26,17 @@ from cloudinit.settings import PER_INSTANCE
frequency = PER_INSTANCE frequency = PER_INSTANCE
script_subdir = 'scripts' SCRIPT_SUBDIR = 'scripts'
def handle(name, _cfg, cloud, log, _args): def handle(name, _cfg, cloud, log, _args):
# This is written to by the user data handlers # This is written to by the user data handlers
# Ie, any custom shell scripts that come down # Ie, any custom shell scripts that come down
# go here... # go here...
runparts_path = os.path.join(cloud.get_ipath_cur(), script_subdir) runparts_path = os.path.join(cloud.get_ipath_cur(), SCRIPT_SUBDIR)
try: try:
util.runparts(runparts_path) util.runparts(runparts_path)
except: except:
log.warn("Failed to run transform %s (%s in %s)", log.warn("Failed to run transform %s (%s in %s)",
name, script_subdir, runparts_path) name, SCRIPT_SUBDIR, runparts_path)
raise raise

View File

@ -25,7 +25,7 @@ from cloudinit import util
from string import letters, digits # pylint: disable=W0402 from string import letters, digits # pylint: disable=W0402
# We are removing certain 'painful' letters/numbers # We are removing certain 'painful' letters/numbers
pw_set = (letters.translate(None, 'loLOI') + PW_SET = (letters.translate(None, 'loLOI') +
digits.translate(None, '01')) digits.translate(None, '01'))
@ -148,4 +148,4 @@ def handle(_name, cfg, cloud, log, args):
def rand_user_password(pwlen=9): def rand_user_password(pwlen=9):
return util.rand_str(pwlen, select_from=pw_set) return util.rand_str(pwlen, select_from=PW_SET)

View File

@ -28,7 +28,7 @@ DISABLE_ROOT_OPTS = ( "no-port-forwarding,no-agent-forwarding,"
"no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" " "no-X11-forwarding,command=\"echo \'Please login as the user \\\"$USER\\\" "
"rather than the user \\\"root\\\".\';echo;sleep 10\"") "rather than the user \\\"root\\\".\';echo;sleep 10\"")
key2file = { KEY_2_FILE = {
"rsa_private": ("/etc/ssh/ssh_host_rsa_key", 0600), "rsa_private": ("/etc/ssh/ssh_host_rsa_key", 0600),
"rsa_public": ("/etc/ssh/ssh_host_rsa_key.pub", 0644), "rsa_public": ("/etc/ssh/ssh_host_rsa_key.pub", 0644),
"dsa_private": ("/etc/ssh/ssh_host_dsa_key", 0600), "dsa_private": ("/etc/ssh/ssh_host_dsa_key", 0600),
@ -37,15 +37,17 @@ key2file = {
"ecdsa_public": ("/etc/ssh/ssh_host_ecdsa_key.pub", 0644), "ecdsa_public": ("/etc/ssh/ssh_host_ecdsa_key.pub", 0644),
} }
priv2pub = { PRIV_2_PUB = {
'rsa_private': 'rsa_public', 'rsa_private': 'rsa_public',
'dsa_private': 'dsa_public', 'dsa_private': 'dsa_public',
'ecdsa_private': 'ecdsa_public', 'ecdsa_private': 'ecdsa_public',
} }
key_gen_tpl = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"' KEY_GEN_TPL = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"'
generate_keys = ['rsa', 'dsa', 'ecdsa'] GENERATE_KEY_NAMES = ['rsa', 'dsa', 'ecdsa']
KEY_FILE_TPL = '/etc/ssh/ssh_host_%s_key'
def handle(_name, cfg, cloud, log, _args): def handle(_name, cfg, cloud, log, _args):
@ -62,17 +64,17 @@ def handle(_name, cfg, cloud, log, _args):
if "ssh_keys" in cfg: if "ssh_keys" in cfg:
# if there are keys in cloud-config, use them # if there are keys in cloud-config, use them
for (key, val) in cfg["ssh_keys"].iteritems(): for (key, val) in cfg["ssh_keys"].iteritems():
if key in key2file: if key in KEY_2_FILE:
tgt_fn = key2file[key][0] tgt_fn = KEY_2_FILE[key][0]
tgt_perms = key2file[key][1] tgt_perms = KEY_2_FILE[key][1]
util.write_file(cloud.paths.join(False, tgt_fn), util.write_file(cloud.paths.join(False, tgt_fn),
val, tgt_perms) val, tgt_perms)
for (priv, pub) in priv2pub.iteritems(): for (priv, pub) in PRIV_2_PUB.iteritems():
if pub in cfg['ssh_keys'] or not priv in cfg['ssh_keys']: if pub in cfg['ssh_keys'] or not priv in cfg['ssh_keys']:
continue continue
pair = (key2file[priv][0], key2file[pub][0]) pair = (KEY_2_FILE[priv][0], KEY_2_FILE[pub][0])
cmd = ['sh', '-xc', key_gen_tpl % pair] cmd = ['sh', '-xc', KEY_GEN_TPL % pair]
try: try:
# TODO: Is this guard needed? # TODO: Is this guard needed?
with util.SeLinuxGuard("/etc/ssh", recursive=True): with util.SeLinuxGuard("/etc/ssh", recursive=True):
@ -85,10 +87,9 @@ def handle(_name, cfg, cloud, log, _args):
# if not, generate them # if not, generate them
genkeys = util.get_cfg_option_list(cfg, genkeys = util.get_cfg_option_list(cfg,
'ssh_genkeytypes', 'ssh_genkeytypes',
generate_keys) GENERATE_KEY_NAMES)
for keytype in genkeys: for keytype in genkeys:
keyfile = '/etc/ssh/ssh_host_%s_key' % (keytype) keyfile = cloud.paths.join(False, KEY_FILE_TPL % (keytype))
keyfile = cloud.paths.join(False, keyfile)
util.ensure_dir(os.path.dirname(keyfile)) util.ensure_dir(os.path.dirname(keyfile))
if not os.path.exists(keyfile): if not os.path.exists(keyfile):
cmd = ['ssh-keygen', '-t', keytype, '-N', '', '-f', keyfile] cmd = ['ssh-keygen', '-t', keytype, '-N', '', '-f', keyfile]

View File

@ -157,4 +157,3 @@ def fetch(distro_name, mods=(__name__, )):
% (distro_name)) % (distro_name))
distro_cls = getattr(mod, 'Distro') distro_cls = getattr(mod, 'Distro')
return distro_cls return distro_cls

View File

@ -226,5 +226,3 @@ def type_from_starts_with(payload, default=None):
if payload_lc.startswith(text): if payload_lc.startswith(text):
return INCLUSION_TYPES_MAP[text] return INCLUSION_TYPES_MAP[text]
return default return default

View File

@ -20,7 +20,6 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging import logging
import logging.handlers import logging.handlers
import logging.config import logging.config
@ -53,7 +52,6 @@ def setupBasicLogging():
root.setLevel(DEBUG) root.setLevel(DEBUG)
def setupLogging(cfg=None): def setupLogging(cfg=None):
# See if the config provides any logging conf... # See if the config provides any logging conf...
if not cfg: if not cfg:

View File

@ -251,6 +251,7 @@ datasources = [
(DataSourceMAAS, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)), (DataSourceMAAS, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
] ]
# Return a list of data sources that match this set of dependencies # Return a list of data sources that match this set of dependencies
def get_datasource_list(depends): def get_datasource_list(depends):
return sources.list_from_depends(depends, datasources) return sources.list_from_depends(depends, datasources)

View File

@ -211,7 +211,6 @@ def update_authorized_keys(fname, keys):
def setup_user_keys(keys, user, key_prefix, paths): def setup_user_keys(keys, user, key_prefix, paths):
# Make sure the users .ssh dir is setup accordingly # Make sure the users .ssh dir is setup accordingly
pwent = pwd.getpwnam(user) pwent = pwd.getpwnam(user)
ssh_dir = os.path.join(pwent.pw_dir, '.ssh') ssh_dir = os.path.join(pwent.pw_dir, '.ssh')

View File

@ -234,7 +234,3 @@ def convert_string(raw_data, headers=None):
msg = MIMEBase(maintype, subtype, *headers) msg = MIMEBase(maintype, subtype, *headers)
msg.set_payload(data) msg.set_payload(data)
return msg return msg

View File

@ -153,6 +153,7 @@ def SilentTemporaryFile(**kwargs):
# file to unlink has been unlinked elsewhere.. # file to unlink has been unlinked elsewhere..
LOG.debug("Created temporary file %s", fh.name) LOG.debug("Created temporary file %s", fh.name)
fh.unlink = del_file fh.unlink = del_file
# Add a new method that will unlink # Add a new method that will unlink
# right 'now' but still lets the exit # right 'now' but still lets the exit
# method attempt to remove it (which will # method attempt to remove it (which will
@ -160,6 +161,7 @@ def SilentTemporaryFile(**kwargs):
# about files that are not there) # about files that are not there)
def unlink_now(): def unlink_now():
fh.unlink(fh.name) fh.unlink(fh.name)
setattr(fh, 'unlink_now', unlink_now) setattr(fh, 'unlink_now', unlink_now)
return fh return fh
@ -214,7 +216,6 @@ def rand_str(strlen=32, select_from=None):
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 read_conf(fname): def read_conf(fname):
try: try:
return load_yaml(load_file(fname), default={}) return load_yaml(load_file(fname), default={})