aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/wrap/wrap.py
diff options
context:
space:
mode:
authorElliott Sales de Andrade <quantum.analyst@gmail.com>2016-08-24 19:29:11 -0400
committerElliott Sales de Andrade <quantum.analyst@gmail.com>2016-08-27 18:29:55 -0400
commit4c71695e41a50dda3199d26ed7aedbaaf3150768 (patch)
tree8bd499a2a113c3da5c1dee8ed29f4b3c69d27dd7 /mesonbuild/wrap/wrap.py
parent7830cb61c39fbaf57933ac403dcdf5007667d87d (diff)
downloadmeson-4c71695e41a50dda3199d26ed7aedbaaf3150768.zip
meson-4c71695e41a50dda3199d26ed7aedbaaf3150768.tar.gz
meson-4c71695e41a50dda3199d26ed7aedbaaf3150768.tar.bz2
Use context manager for file I/O.
There are a few cases where a context manager cannot be used, such as the logger.
Diffstat (limited to 'mesonbuild/wrap/wrap.py')
-rw-r--r--mesonbuild/wrap/wrap.py39
1 files changed, 21 insertions, 18 deletions
diff --git a/mesonbuild/wrap/wrap.py b/mesonbuild/wrap/wrap.py
index bfb7ed4..33f52f3 100644
--- a/mesonbuild/wrap/wrap.py
+++ b/mesonbuild/wrap/wrap.py
@@ -58,23 +58,23 @@ def open_wrapdburl(urlstring):
class PackageDefinition:
def __init__(self, fname):
self.values = {}
- ifile = open(fname)
- first = ifile.readline().strip()
+ with open(fname) as ifile:
+ first = ifile.readline().strip()
- if first == '[wrap-file]':
- self.type = 'file'
- elif first == '[wrap-git]':
- self.type = 'git'
- else:
- raise RuntimeError('Invalid format of package file')
- for line in ifile:
- line = line.strip()
- if line == '':
- continue
- (k, v) = line.split('=', 1)
- k = k.strip()
- v = v.strip()
- self.values[k] = v
+ if first == '[wrap-file]':
+ self.type = 'file'
+ elif first == '[wrap-git]':
+ self.type = 'git'
+ else:
+ raise RuntimeError('Invalid format of package file')
+ for line in ifile:
+ line = line.strip()
+ if line == '':
+ continue
+ (k, v) = line.split('=', 1)
+ k = k.strip()
+ v = v.strip()
+ self.values[k] = v
def get(self, key):
return self.values[key]
@@ -177,7 +177,8 @@ class Resolver:
expected = p.get('source_hash')
if dhash != expected:
raise RuntimeError('Incorrect hash for source %s:\n %s expected\n %s actual.' % (packagename, expected, dhash))
- open(ofname, 'wb').write(srcdata)
+ with open(ofname, 'wb') as f:
+ f.write(srcdata)
if p.has_patch():
purl = p.get('patch_url')
mlog.log('Downloading patch from', mlog.bold(purl))
@@ -186,7 +187,9 @@ class Resolver:
expected = p.get('patch_hash')
if phash != expected:
raise RuntimeError('Incorrect hash for patch %s:\n %s expected\n %s actual' % (packagename, expected, phash))
- open(os.path.join(self.cachedir, p.get('patch_filename')), 'wb').write(pdata)
+ filename = os.path.join(self.cachedir, p.get('patch_filename'))
+ with open(filename, 'wb') as f:
+ f.write(pdata)
else:
mlog.log('Package does not require patch.')