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

file handling cleanup

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