diff options
author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2013-05-09 21:06:45 +0000 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2013-05-09 22:28:04 +0100 |
commit | 4a081b5a52e3d27da8d4b062f3fda292e8d8fb0a (patch) | |
tree | a555b39b41e4ec36c212481fcd2887cde2ee30dd /bitbake/lib/bb | |
parent | 7f2bf08280f11daa002f4a9e870c2b77711cbf90 (diff) | |
download | poky-4a081b5a52e3d27da8d4b062f3fda292e8d8fb0a.tar.gz |
bitbake: lib: Clean up various file access syntax
Python 3 is stricter about how files are accessed. Specficially:
* Use open(), not file()
* Use binary mode for binary files (when checksumming)
* Use with statements to ensure files get closed
* Add missing file close statements
(Bitbake rev: 9f08b901375ba640f47596f1bcf43f98a931550f)
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb')
-rw-r--r-- | bitbake/lib/bb/build.py | 15 | ||||
-rw-r--r-- | bitbake/lib/bb/cache.py | 25 | ||||
-rw-r--r-- | bitbake/lib/bb/parse/parse_py/BBHandler.py | 1 | ||||
-rw-r--r-- | bitbake/lib/bb/parse/parse_py/ConfHandler.py | 2 | ||||
-rw-r--r-- | bitbake/lib/bb/process.py | 4 | ||||
-rw-r--r-- | bitbake/lib/bb/utils.py | 14 |
6 files changed, 39 insertions, 22 deletions
diff --git a/bitbake/lib/bb/build.py b/bitbake/lib/bb/build.py index d91ff53fcf..b5681b13e3 100644 --- a/bitbake/lib/bb/build.py +++ b/bitbake/lib/bb/build.py | |||
@@ -275,7 +275,8 @@ def exec_func_shell(func, d, runfile, cwd=None): | |||
275 | bb.debug(2, "Executing shell function %s" % func) | 275 | bb.debug(2, "Executing shell function %s" % func) |
276 | 276 | ||
277 | try: | 277 | try: |
278 | bb.process.run(cmd, shell=False, stdin=NULL, log=logfile) | 278 | with open(os.devnull, 'r+') as stdin: |
279 | bb.process.run(cmd, shell=False, stdin=stdin, log=logfile) | ||
279 | except bb.process.CmdError: | 280 | except bb.process.CmdError: |
280 | logfn = d.getVar('BB_LOGFILE', True) | 281 | logfn = d.getVar('BB_LOGFILE', True) |
281 | raise FuncFailed(func, logfn) | 282 | raise FuncFailed(func, logfn) |
@@ -319,12 +320,11 @@ def _exec_task(fn, task, d, quieterr): | |||
319 | # Document the order of the tasks... | 320 | # Document the order of the tasks... |
320 | logorder = os.path.join(tempdir, 'log.task_order') | 321 | logorder = os.path.join(tempdir, 'log.task_order') |
321 | try: | 322 | try: |
322 | logorderfile = file(logorder, 'a') | 323 | with open(logorder, 'a') as logorderfile: |
324 | logorderfile.write('{0} ({1}): {2}\n'.format(task, os.getpid(), logbase)) | ||
323 | except OSError: | 325 | except OSError: |
324 | logger.exception("Opening log file '%s'", logorder) | 326 | logger.exception("Opening log file '%s'", logorder) |
325 | pass | 327 | pass |
326 | logorderfile.write('{0} ({1}): {2}\n'.format(task, os.getpid(), logbase)) | ||
327 | logorderfile.close() | ||
328 | 328 | ||
329 | # Setup the courtesy link to the logfn | 329 | # Setup the courtesy link to the logfn |
330 | loglink = os.path.join(tempdir, 'log.{0}'.format(task)) | 330 | loglink = os.path.join(tempdir, 'log.{0}'.format(task)) |
@@ -348,10 +348,10 @@ def _exec_task(fn, task, d, quieterr): | |||
348 | self.triggered = True | 348 | self.triggered = True |
349 | 349 | ||
350 | # Handle logfiles | 350 | # Handle logfiles |
351 | si = file('/dev/null', 'r') | 351 | si = open('/dev/null', 'r') |
352 | try: | 352 | try: |
353 | bb.utils.mkdirhier(os.path.dirname(logfn)) | 353 | bb.utils.mkdirhier(os.path.dirname(logfn)) |
354 | logfile = file(logfn, 'w') | 354 | logfile = open(logfn, 'w') |
355 | except OSError: | 355 | except OSError: |
356 | logger.exception("Opening log file '%s'", logfn) | 356 | logger.exception("Opening log file '%s'", logfn) |
357 | pass | 357 | pass |
@@ -533,8 +533,7 @@ def make_stamp(task, d, file_name = None): | |||
533 | # change on broken NFS filesystems | 533 | # change on broken NFS filesystems |
534 | if stamp: | 534 | if stamp: |
535 | bb.utils.remove(stamp) | 535 | bb.utils.remove(stamp) |
536 | f = open(stamp, "w") | 536 | open(stamp, "w").close() |
537 | f.close() | ||
538 | 537 | ||
539 | # If we're in task context, write out a signature file for each task | 538 | # If we're in task context, write out a signature file for each task |
540 | # as it completes | 539 | # as it completes |
diff --git a/bitbake/lib/bb/cache.py b/bitbake/lib/bb/cache.py index 1c975b62e1..c92ba35641 100644 --- a/bitbake/lib/bb/cache.py +++ b/bitbake/lib/bb/cache.py | |||
@@ -738,8 +738,9 @@ class MultiProcessCache(object): | |||
738 | logger.debug(1, "Using cache in '%s'", self.cachefile) | 738 | logger.debug(1, "Using cache in '%s'", self.cachefile) |
739 | 739 | ||
740 | try: | 740 | try: |
741 | p = pickle.Unpickler(file(self.cachefile, "rb")) | 741 | with open(self.cachefile, "rb") as f: |
742 | data, version = p.load() | 742 | p = pickle.Unpickler(f) |
743 | data, version = p.load() | ||
743 | except: | 744 | except: |
744 | return | 745 | return |
745 | 746 | ||
@@ -779,8 +780,9 @@ class MultiProcessCache(object): | |||
779 | i = i + 1 | 780 | i = i + 1 |
780 | continue | 781 | continue |
781 | 782 | ||
782 | p = pickle.Pickler(file(self.cachefile + "-" + str(i), "wb"), -1) | 783 | with open(self.cachefile + "-" + str(i), "wb") as f: |
783 | p.dump([self.cachedata_extras, self.__class__.CACHE_VERSION]) | 784 | p = pickle.Pickler(f, -1) |
785 | p.dump([self.cachedata_extras, self.__class__.CACHE_VERSION]) | ||
784 | 786 | ||
785 | bb.utils.unlockfile(lf) | 787 | bb.utils.unlockfile(lf) |
786 | bb.utils.unlockfile(glf) | 788 | bb.utils.unlockfile(glf) |
@@ -798,8 +800,9 @@ class MultiProcessCache(object): | |||
798 | glf = bb.utils.lockfile(self.cachefile + ".lock") | 800 | glf = bb.utils.lockfile(self.cachefile + ".lock") |
799 | 801 | ||
800 | try: | 802 | try: |
801 | p = pickle.Unpickler(file(self.cachefile, "rb")) | 803 | with open(self.cachefile, "rb") as f: |
802 | data, version = p.load() | 804 | p = pickle.Unpickler(f) |
805 | data, version = p.load() | ||
803 | except (IOError, EOFError): | 806 | except (IOError, EOFError): |
804 | data, version = None, None | 807 | data, version = None, None |
805 | 808 | ||
@@ -809,8 +812,9 @@ class MultiProcessCache(object): | |||
809 | for f in [y for y in os.listdir(os.path.dirname(self.cachefile)) if y.startswith(os.path.basename(self.cachefile) + '-')]: | 812 | for f in [y for y in os.listdir(os.path.dirname(self.cachefile)) if y.startswith(os.path.basename(self.cachefile) + '-')]: |
810 | f = os.path.join(os.path.dirname(self.cachefile), f) | 813 | f = os.path.join(os.path.dirname(self.cachefile), f) |
811 | try: | 814 | try: |
812 | p = pickle.Unpickler(file(f, "rb")) | 815 | with open(f, "rb") as fd: |
813 | extradata, version = p.load() | 816 | p = pickle.Unpickler(fd) |
817 | extradata, version = p.load() | ||
814 | except (IOError, EOFError): | 818 | except (IOError, EOFError): |
815 | extradata, version = self.create_cachedata(), None | 819 | extradata, version = self.create_cachedata(), None |
816 | 820 | ||
@@ -822,8 +826,9 @@ class MultiProcessCache(object): | |||
822 | 826 | ||
823 | self.compress_keys(data) | 827 | self.compress_keys(data) |
824 | 828 | ||
825 | p = pickle.Pickler(file(self.cachefile, "wb"), -1) | 829 | with open(self.cachefile, "wb") as f: |
826 | p.dump([data, self.__class__.CACHE_VERSION]) | 830 | p = pickle.Pickler(f, -1) |
831 | p.dump([data, self.__class__.CACHE_VERSION]) | ||
827 | 832 | ||
828 | bb.utils.unlockfile(glf) | 833 | bb.utils.unlockfile(glf) |
829 | 834 | ||
diff --git a/bitbake/lib/bb/parse/parse_py/BBHandler.py b/bitbake/lib/bb/parse/parse_py/BBHandler.py index 81fb8d3adf..87a1530cb5 100644 --- a/bitbake/lib/bb/parse/parse_py/BBHandler.py +++ b/bitbake/lib/bb/parse/parse_py/BBHandler.py | |||
@@ -104,6 +104,7 @@ def get_statements(filename, absolute_filename, base_name): | |||
104 | if not s: break | 104 | if not s: break |
105 | s = s.rstrip() | 105 | s = s.rstrip() |
106 | feeder(lineno, s, filename, base_name, statements) | 106 | feeder(lineno, s, filename, base_name, statements) |
107 | file.close() | ||
107 | if __inpython__: | 108 | if __inpython__: |
108 | # add a blank line to close out any python definition | 109 | # add a blank line to close out any python definition |
109 | feeder(IN_PYTHON_EOF, "", filename, base_name, statements) | 110 | feeder(IN_PYTHON_EOF, "", filename, base_name, statements) |
diff --git a/bitbake/lib/bb/parse/parse_py/ConfHandler.py b/bitbake/lib/bb/parse/parse_py/ConfHandler.py index 3255c8beb8..7d4a5b14a7 100644 --- a/bitbake/lib/bb/parse/parse_py/ConfHandler.py +++ b/bitbake/lib/bb/parse/parse_py/ConfHandler.py | |||
@@ -145,6 +145,8 @@ def handle(fn, data, include): | |||
145 | if oldfile: | 145 | if oldfile: |
146 | data.setVar('FILE', oldfile) | 146 | data.setVar('FILE', oldfile) |
147 | 147 | ||
148 | f.close() | ||
149 | |||
148 | for f in confFilters: | 150 | for f in confFilters: |
149 | f(fn, data) | 151 | f(fn, data) |
150 | 152 | ||
diff --git a/bitbake/lib/bb/process.py b/bitbake/lib/bb/process.py index 05b51725f1..1aeec788c1 100644 --- a/bitbake/lib/bb/process.py +++ b/bitbake/lib/bb/process.py | |||
@@ -102,6 +102,10 @@ def _logged_communicate(pipe, log, input): | |||
102 | log.write(data) | 102 | log.write(data) |
103 | finally: | 103 | finally: |
104 | log.flush() | 104 | log.flush() |
105 | if pipe.stdout is not None: | ||
106 | pipe.stdout.close() | ||
107 | if pipe.stderr is not None: | ||
108 | pipe.stderr.close() | ||
105 | return ''.join(outdata), ''.join(errdata) | 109 | return ''.join(outdata), ''.join(errdata) |
106 | 110 | ||
107 | def run(cmd, input=None, log=None, **options): | 111 | def run(cmd, input=None, log=None, **options): |
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 462eb689b9..2e10fc24dd 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py | |||
@@ -414,6 +414,10 @@ def lockfile(name, shared=False, retry=True): | |||
414 | return lf | 414 | return lf |
415 | lf.close() | 415 | lf.close() |
416 | except Exception: | 416 | except Exception: |
417 | try: | ||
418 | lf.close() | ||
419 | except Exception: | ||
420 | pass | ||
417 | pass | 421 | pass |
418 | if not retry: | 422 | if not retry: |
419 | return None | 423 | return None |
@@ -443,8 +447,9 @@ def md5_file(filename): | |||
443 | import md5 | 447 | import md5 |
444 | m = md5.new() | 448 | m = md5.new() |
445 | 449 | ||
446 | for line in open(filename): | 450 | with open(filename, "rb") as f: |
447 | m.update(line) | 451 | for line in f: |
452 | m.update(line) | ||
448 | return m.hexdigest() | 453 | return m.hexdigest() |
449 | 454 | ||
450 | def sha256_file(filename): | 455 | def sha256_file(filename): |
@@ -460,8 +465,9 @@ def sha256_file(filename): | |||
460 | return None | 465 | return None |
461 | 466 | ||
462 | s = hashlib.sha256() | 467 | s = hashlib.sha256() |
463 | for line in open(filename): | 468 | with open(filename, "rb") as f: |
464 | s.update(line) | 469 | for line in f: |
470 | s.update(line) | ||
465 | return s.hexdigest() | 471 | return s.hexdigest() |
466 | 472 | ||
467 | def preserved_envvars_exported(): | 473 | def preserved_envvars_exported(): |