Skip to content

Commit

Permalink
Merge pull request #102 from nrhodes91/update-command-output
Browse files Browse the repository at this point in the history
Review run_command function and clean up for PEP8
  • Loading branch information
rhodesn authored Jun 16, 2019
2 parents 9cd7aba + 3c785d5 commit 18f7a29
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions configsnap
Original file line number Diff line number Diff line change
Expand Up @@ -285,61 +285,67 @@ class dataGather:

def run_command(self, command, filename, fail_ok=False, sort=False, stdout=False, shell=False):
try:
command_proc = subprocess.Popen(
cmd_proc = subprocess.Popen(
command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=shell)

except OSError, e:
if not fail_ok:
report_error("Error running %s: %s" %
(command[0], e.strerror))
return None
else:
return None
return False

output = command_proc.stdout.readlines()
returncode = command_proc.wait()
# Can't change this to Popen.communicate() as it needs to be a list for the sort below
cmd_stdout = cmd_proc.stdout.readlines()

returncode = cmd_proc.wait()
if not fail_ok and returncode != 0:
report_error("%s failed\nPlease troubleshoot manually" %
(' '.join(command), ))
return None
return False

if len(output) == 0:
if len(cmd_stdout) == 0:
return True

if sort:
output.sort()
cmd_stdout.sort()

filename = os.path.join(self.workdir, "%s.%s" % (filename, self.phase))

if os.path.exists(filename):
report_error("%s already exists" % filename)

try:
output_fd = open(filename, 'w')
with open(filename, 'w') as output_fd:
output_fd.writelines(cmd_stdout)
report_verbose("Recording %s to %s" % (command, filename))

if stdout:
sys.stdout.writelines(cmd_stdout)

except IOError, e:
report_error("Unable to open %s: %s" % (filename, e.strerror))
return False

output_fd.writelines(output)
output_fd.close()
report_verbose("Recording %s to %s" % (command, filename))
if stdout:
sys.stdout.writelines(output)

def get_diff_files(self, filename):
global diffs_found_msg
filename = os.path.join(self.workdir, filename)
pre_filename = "%s.%s" % (filename, options.pre_suffix)
post_filename = "%s.%s" % (filename, self.phase)

try:
diff_proc = subprocess.Popen(['diff', '--unified=0', '--ignore-blank-lines', '--ignore-space-change', pre_filename, post_filename],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
diff_proc = subprocess.Popen(['diff', '--unified=0',
'--ignore-blank-lines',
'--ignore-space-change',
pre_filename,
post_filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False)
except OSError, e:
report_error("Error running diff: %s" %
(e.strerror))
return None
return False

diff_stdout, diff_stderr = diff_proc.communicate(input=None)

Expand Down

0 comments on commit 18f7a29

Please sign in to comment.