bb.debug(2, "Executing shell function %s" % func)
try:
- bb.process.run(cmd, shell=False, stdin=NULL, log=logfile)
+ with open(os.devnull, 'r+') as stdin:
+ bb.process.run(cmd, shell=False, stdin=stdin, log=logfile)
except bb.process.CmdError:
logfn = d.getVar('BB_LOGFILE', True)
raise FuncFailed(func, logfn)
# Document the order of the tasks...
logorder = os.path.join(tempdir, 'log.task_order')
try:
- logorderfile = file(logorder, 'a')
+ with open(logorder, 'a') as logorderfile:
+ logorderfile.write('{0} ({1}): {2}\n'.format(task, os.getpid(), logbase))
except OSError:
logger.exception("Opening log file '%s'", logorder)
pass
- logorderfile.write('{0} ({1}): {2}\n'.format(task, os.getpid(), logbase))
- logorderfile.close()
# Setup the courtesy link to the logfn
loglink = os.path.join(tempdir, 'log.{0}'.format(task))
self.triggered = True
# Handle logfiles
- si = file('/dev/null', 'r')
+ si = open('/dev/null', 'r')
try:
bb.utils.mkdirhier(os.path.dirname(logfn))
- logfile = file(logfn, 'w')
+ logfile = open(logfn, 'w')
except OSError:
logger.exception("Opening log file '%s'", logfn)
pass
# change on broken NFS filesystems
if stamp:
bb.utils.remove(stamp)
- f = open(stamp, "w")
- f.close()
+ open(stamp, "w").close()
# If we're in task context, write out a signature file for each task
# as it completes
logger.debug(1, "Using cache in '%s'", self.cachefile)
try:
- p = pickle.Unpickler(file(self.cachefile, "rb"))
- data, version = p.load()
+ with open(self.cachefile, "rb") as f:
+ p = pickle.Unpickler(f)
+ data, version = p.load()
except:
return
i = i + 1
continue
- p = pickle.Pickler(file(self.cachefile + "-" + str(i), "wb"), -1)
- p.dump([self.cachedata_extras, self.__class__.CACHE_VERSION])
+ with open(self.cachefile + "-" + str(i), "wb") as f:
+ p = pickle.Pickler(f, -1)
+ p.dump([self.cachedata_extras, self.__class__.CACHE_VERSION])
bb.utils.unlockfile(lf)
bb.utils.unlockfile(glf)
glf = bb.utils.lockfile(self.cachefile + ".lock")
try:
- p = pickle.Unpickler(file(self.cachefile, "rb"))
- data, version = p.load()
+ with open(self.cachefile, "rb") as f:
+ p = pickle.Unpickler(f)
+ data, version = p.load()
except (IOError, EOFError):
data, version = None, None
for f in [y for y in os.listdir(os.path.dirname(self.cachefile)) if y.startswith(os.path.basename(self.cachefile) + '-')]:
f = os.path.join(os.path.dirname(self.cachefile), f)
try:
- p = pickle.Unpickler(file(f, "rb"))
- extradata, version = p.load()
+ with open(f, "rb") as fd:
+ p = pickle.Unpickler(fd)
+ extradata, version = p.load()
except (IOError, EOFError):
extradata, version = self.create_cachedata(), None
self.compress_keys(data)
- p = pickle.Pickler(file(self.cachefile, "wb"), -1)
- p.dump([data, self.__class__.CACHE_VERSION])
+ with open(self.cachefile, "wb") as f:
+ p = pickle.Pickler(f, -1)
+ p.dump([data, self.__class__.CACHE_VERSION])
bb.utils.unlockfile(glf)
if not s: break
s = s.rstrip()
feeder(lineno, s, filename, base_name, statements)
+ file.close()
if __inpython__:
# add a blank line to close out any python definition
feeder(IN_PYTHON_EOF, "", filename, base_name, statements)
if oldfile:
data.setVar('FILE', oldfile)
+ f.close()
+
for f in confFilters:
f(fn, data)
log.write(data)
finally:
log.flush()
+ if pipe.stdout is not None:
+ pipe.stdout.close()
+ if pipe.stderr is not None:
+ pipe.stderr.close()
return ''.join(outdata), ''.join(errdata)
def run(cmd, input=None, log=None, **options):
return lf
lf.close()
except Exception:
+ try:
+ lf.close()
+ except Exception:
+ pass
pass
if not retry:
return None
import md5
m = md5.new()
- for line in open(filename):
- m.update(line)
+ with open(filename, "rb") as f:
+ for line in f:
+ m.update(line)
return m.hexdigest()
def sha256_file(filename):
return None
s = hashlib.sha256()
- for line in open(filename):
- s.update(line)
+ with open(filename, "rb") as f:
+ for line in f:
+ s.update(line)
return s.hexdigest()
def preserved_envvars_exported():