Commit 225e6d79 authored by Marco Mariani's avatar Marco Mariani

file handling cleanup

parent beb698a5
......@@ -582,9 +582,8 @@ class Slapgrid(object):
except NotFoundError:
pass
software.install()
file_descriptor = open(completed_tag, 'w')
file_descriptor.write(time.asctime())
file_descriptor.close()
with open(completed_tag, 'w') as fout:
fout.write(time.asctime())
elif state == 'destroyed':
if os.path.exists(software_path):
logger.info('Destroying %r...' % software_release_uri)
......@@ -665,7 +664,6 @@ class Slapgrid(object):
promise = os.path.basename(command[0])
self.logger.info("Checking promise %r.", promise)
process_handler = subprocess.Popen(command,
preexec_fn=lambda: dropPrivileges(uid, gid),
cwd=cwd,
......@@ -1169,9 +1167,7 @@ class Slapgrid(object):
file_path = os.path.join(dir_reports, filename)
if os.path.exists(file_path):
usage_file = open(file_path, 'r')
usage = usage_file.read()
usage_file.close()
usage = open(file_path, 'r').read()
#We check the validity of xml content of each reports
if not self.validateXML(usage, partition_consumption_model):
......
......@@ -111,9 +111,7 @@ class SlapPopen(subprocess.Popen):
if line == '' and self.poll() != None:
break
output_lines.append(line)
if line[-1:] == '\n':
line = line[:-1]
logger.info(line)
logger.info(line.rstrip('\n'))
self.output = ''.join(output_lines)
......@@ -168,11 +166,9 @@ def setFinished(pid_file):
def write_pid(pid_file):
logger = logging.getLogger('Slapgrid')
pid = os.getpid()
try:
f = open(pid_file, 'w')
f.write('%s' % pid)
f.close()
with open(pid_file, 'w') as fout:
fout.write('%s' % os.getpid())
except (IOError, OSError):
logger.critical('slapgrid could not write pidfile %s' % pid_file)
raise
......@@ -302,9 +298,7 @@ def launchBuildout(path, buildout_binary,
uid = stat_info.st_uid
gid = stat_info.st_gid
# Extract python binary to prevent shebang size limit
file = open(buildout_binary, 'r')
line = file.readline()
file.close()
line = open(buildout_binary, 'r').readline()
invocation_list = []
if line.startswith('#!'):
line = line[2:]
......@@ -341,11 +335,9 @@ def updateFile(file_path, content, mode=0o600):
if not (os.path.isfile(file_path)) or \
not(hashlib.md5(open(file_path).read()).digest() ==\
hashlib.md5(content).digest()):
with open(file_path, 'w') as fout:
fout.write(content)
altered = True
file_file = open(file_path, 'w')
file_file.write(content)
file_file.flush()
file_file.close()
os.chmod(file_path, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
if stat.S_IMODE(os.stat(file_path).st_mode) != mode:
os.chmod(file_path, mode)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment