Added and using new get_bool_option and get_int_option functions

This commit is contained in:
Joshua Harlow
2012-08-23 20:36:52 -07:00
parent 7c6e5dc742
commit 4bb0d61121
12 changed files with 102 additions and 158 deletions

View File

@@ -40,11 +40,9 @@ SHELL_QUOTE_REPLACERS = {
"$": '\$',
'`': '\`',
}
SHELL_WRAPPER = "\"%s\""
ROOT_PATH = os.sep
#root context guard
class Rooted(object):
def __init__(self, run_as_root):
self.root_mode = run_as_root
@@ -66,8 +64,9 @@ def is_dry_run():
dry_v = env.get_key('ANVIL_DRYRUN')
if not dry_v:
return False
dry_v = str(dry_v).lower().strip()
if dry_v in ['0', 'false', 'no', 'off']:
# TODO(harlowja) these checks are duplicated in utils, rework that...
dry_v = str(dry_v).lower().strip()
if dry_v in ['0', 'false', 'off', 'no', 'f', '', 'none']:
return False
return True
@@ -100,15 +99,6 @@ def execute(*cmd, **kwargs):
if shell:
execute_cmd = str_cmd.strip()
if not shell:
LOG.debug('Running cmd: %r' % (execute_cmd))
else:
LOG.debug('Running shell cmd: %r' % (execute_cmd))
if process_input is not None:
LOG.debug('With stdin: %s' % (process_input))
if cwd:
LOG.debug("In working directory: %r" % (cwd))
stdin_fh = subprocess.PIPE
stdout_fh = subprocess.PIPE
stderr_fh = subprocess.PIPE
@@ -124,6 +114,15 @@ def execute(*cmd, **kwargs):
if 'stderr_fh' in kwargs.keys():
stderr_fh = kwargs.get('stderr_fh')
if not shell:
LOG.debug('Running cmd: %r' % (execute_cmd))
else:
LOG.debug('Running shell cmd: %r' % (execute_cmd))
if process_input is not None:
LOG.debug('With stdin: %s' % (process_input))
if cwd:
LOG.debug("In working directory: %r" % (cwd))
process_env = None
if env_overrides and len(env_overrides):
process_env = env.get()
@@ -156,15 +155,9 @@ def execute(*cmd, **kwargs):
result = ('', '')
else:
try:
obj = subprocess.Popen(execute_cmd,
stdin=stdin_fh,
stdout=stdout_fh,
stderr=stderr_fh,
close_fds=close_file_descriptors,
cwd=cwd,
shell=shell,
preexec_fn=demoter,
env=process_env)
obj = subprocess.Popen(execute_cmd, stdin=stdin_fh, stdout=stdout_fh, stderr=stderr_fh,
close_fds=close_file_descriptors, cwd=cwd, shell=shell,
preexec_fn=demoter, env=process_env)
if process_input is not None:
result = obj.communicate(str(process_input))
else:
@@ -172,8 +165,7 @@ def execute(*cmd, **kwargs):
except OSError as e:
raise excp.ProcessExecutionError(description="%s: [%s, %s]" % (e, e.errno, e.strerror),
cmd=str_cmd)
if (stdin_fh != subprocess.PIPE
and obj.stdin and close_stdin):
if (stdin_fh != subprocess.PIPE and obj.stdin and close_stdin):
obj.stdin.close()
rc = obj.returncode
@@ -237,7 +229,7 @@ def pipe_in_out(in_fh, out_fh, chunk_size=1024, chunk_cb=None):
def shellquote(text):
# TODO since there doesn't seem to be a standard lib that actually works use this way...
# TODO(harlowja) find a better way - since there doesn't seem to be a standard lib that actually works
do_adjust = False
for srch in SHELL_QUOTE_REPLACERS.keys():
if text.find(srch) != -1:
@@ -250,7 +242,7 @@ def shellquote(text):
text.startswith((" ", "\t")) or \
text.endswith((" ", "\t")) or \
text.find("'") != -1:
text = SHELL_WRAPPER % (text)
text = "\"%s\"" % (text)
return text
@@ -625,38 +617,6 @@ def getgroupname():
return grp.getgrgid(gid).gr_name
def create_loopback_file(fname, size, bsize=1024, fs_type='ext3', run_as_root=False):
dd_cmd = ['dd', 'if=/dev/zero', 'of=%s' % fname, 'bs=%d' % bsize,
'count=0', 'seek=%d' % size]
mkfs_cmd = ['mkfs.%s' % fs_type, '-f', '-i', 'size=%d' % bsize, fname]
# Make sure folder exists
files = mkdirslist(dirname(fname))
# Create file
touch_file(fname)
# Fill with zeroes
execute(*dd_cmd, run_as_root=run_as_root)
# Create fs on the file
execute(*mkfs_cmd, run_as_root=run_as_root)
return files
def mount_loopback_file(fname, device_name, fs_type='ext3'):
mount_cmd = ['mount', '-t', fs_type, '-o',
'loop,noatime,nodiratime,nobarrier,logbufs=8', fname,
device_name]
files = mkdirslist(device_name)
execute(*mount_cmd, run_as_root=True)
return files
def umount(dev_name, ignore_errors=True):
try:
execute('umount', dev_name, run_as_root=True)
@@ -757,6 +717,8 @@ def getegid():
def sleep(winks):
if winks <= 0:
return
if is_dry_run():
LOG.debug("Not really sleeping for: %s seconds" % (winks))
else: