mirror of
https://github.com/EttusResearch/uhd.git
synced 2026-10-25 17:36:10 +00:00
uhd: Surround all open() calls with with-statement
In addition to commit ab8ec99733
this adds `with` statements to remaining files in the UHD code base.
This commit is contained in:
parent
16890674f2
commit
699cff5941
@ -29,10 +29,11 @@ ALL_MAP_FILES = """\
|
||||
def extract_map_from_file(path):
|
||||
output = ''
|
||||
found = False
|
||||
for line in open(path).readlines():
|
||||
if line.strip() == 'Mapping completed.': found = False
|
||||
if line.strip() == 'Logic Utilization:': found = True
|
||||
if found: output += line
|
||||
with open(path) as f:
|
||||
for line in f:
|
||||
if line.strip() == 'Mapping completed.': found = False
|
||||
if line.strip() == 'Logic Utilization:': found = True
|
||||
if found: output += line
|
||||
return output
|
||||
|
||||
def extract_maps():
|
||||
@ -54,5 +55,9 @@ def extract_maps():
|
||||
|
||||
if __name__ == '__main__':
|
||||
summary = extract_maps()
|
||||
if len(sys.argv) == 1: print(summary)
|
||||
else: open(sys.argv[1], 'w').write(summary)
|
||||
if len(sys.argv) == 1:
|
||||
print(summary)
|
||||
else:
|
||||
with open(sys.argv[1], 'w') as f:
|
||||
f.write(summary)
|
||||
|
||||
|
||||
@ -22,11 +22,12 @@ def print_timing_constraint_summary(twr_file):
|
||||
output = ""
|
||||
keep = False
|
||||
done = False
|
||||
for line in open(twr_file).readlines():
|
||||
if 'Derived Constraint Report' in line: keep = True
|
||||
if 'constraint' in line and 'met' in line: done = True
|
||||
if not keep and done: keep = True
|
||||
if keep: output += line
|
||||
with open(twr_file) as f:
|
||||
for line in f:
|
||||
if 'Derived Constraint Report' in line: keep = True
|
||||
if 'constraint' in line and 'met' in line: done = True
|
||||
if not keep and done: keep = True
|
||||
if keep: output += line
|
||||
if done: break
|
||||
print("\n\n"+output)
|
||||
|
||||
|
||||
@ -102,7 +102,8 @@ def main():
|
||||
print('Bitstream Size: ' + str(header['bitstream_len']))
|
||||
# Write a bin file
|
||||
if args.bin_out:
|
||||
open(args.bin_out, 'wb').write(flip32(data) if args.flip else data)
|
||||
with open(args.bin_out, 'wb') as f:
|
||||
f.write(flip32(data) if args.flip else data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@ -26,11 +26,12 @@ def print_timing_constraint_summary(twr_file):
|
||||
except IOError:
|
||||
print("cannot open or find %s; no timing summary to print!"%twr_file)
|
||||
exit(-1)
|
||||
for line in open(twr_file).readlines():
|
||||
if 'Derived Constraint Report' in line: keep = True
|
||||
if 'constraint' in line and 'met' in line: done = True
|
||||
if not keep and done: keep = True
|
||||
if keep: output += line
|
||||
with open(twr_file) as f:
|
||||
for line in f:
|
||||
if 'Derived Constraint Report' in line: keep = True
|
||||
if 'constraint' in line and 'met' in line: done = True
|
||||
if not keep and done: keep = True
|
||||
if keep: output += line
|
||||
if done: break
|
||||
print(("\n\n"+output))
|
||||
|
||||
|
||||
@ -312,4 +312,6 @@ if __name__ == '__main__':
|
||||
width=width, to_host='uhd::wtohx', to_wire='uhd::htowx',
|
||||
cpu_type=cpu_type, do_scale=do_scale
|
||||
)
|
||||
open(sys.argv[1], 'w').write(output)
|
||||
|
||||
with open(sys.argv[1], 'w') as f:
|
||||
f.write(output)
|
||||
|
||||
@ -44,10 +44,11 @@ def main():
|
||||
for fname in files:
|
||||
path = os.path.join(root, fname)
|
||||
print("Modifying {}...".format(path))
|
||||
new_file = comment_remover(open(os.path.join(root, fname)).read())
|
||||
with open(os.path.join(root, fname), 'r') as f:
|
||||
new_file = comment_remover(f.read())
|
||||
new_file = remove_trailing_whitespace(new_file)
|
||||
open(os.path.join(root, fname), 'w').write(
|
||||
DEFAULT_HEADER + new_file)
|
||||
with open(os.path.join(root, fname), 'w') as f:
|
||||
f.write(DEFAULT_HEADER + new_file)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@ -349,14 +349,16 @@ def parse_tmpl(_tmpl_text, **kwargs):
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
open(sys.argv[1], 'w').write(parse_tmpl(
|
||||
TMPL_TEXT,
|
||||
file=__file__,
|
||||
sid_p = 0b0000001,
|
||||
cid_p = 0b0000010,
|
||||
tsi_p = 0b0000100,
|
||||
tsf_p = 0b0001000,
|
||||
tlr_p = 0b0010000,
|
||||
sob_p = 0b0100000,
|
||||
eob_p = 0b1000000,
|
||||
))
|
||||
|
||||
with open(sys.argv[1], 'w') as f:
|
||||
f.write(parse_tmpl(
|
||||
TMPL_TEXT,
|
||||
file=__file__,
|
||||
sid_p = 0b0000001,
|
||||
cid_p = 0b0000010,
|
||||
tsi_p = 0b0000100,
|
||||
tsf_p = 0b0001000,
|
||||
tlr_p = 0b0010000,
|
||||
sob_p = 0b0100000,
|
||||
eob_p = 0b1000000,
|
||||
))
|
||||
|
||||
@ -51,8 +51,10 @@ def get_dd_path():
|
||||
dd_path = os.path.join(tempfile.gettempdir(), 'dd.exe')
|
||||
if not os.path.exists(dd_path):
|
||||
print('Downloading dd.exe to %s'%dd_path)
|
||||
dd_bin = urllib.request.urlopen('http://files.ettus.com/dd.exe').read()
|
||||
open(dd_path, 'wb').write(dd_bin)
|
||||
with urllib.request.urlopen('http://files.ettus.com/dd.exe') as response:
|
||||
dd_bin = response.read()
|
||||
with open(dd_path, 'wb') as f:
|
||||
f.write(dd_bin)
|
||||
return dd_path
|
||||
return 'dd'
|
||||
|
||||
@ -143,7 +145,8 @@ def verify_image(image_file, device_file, offset):
|
||||
tmp_file = get_tmp_file()
|
||||
|
||||
#read the image data
|
||||
img_data = open(image_file, 'rb').read()
|
||||
with open(image_file, 'rb') as f:
|
||||
img_data = f.read()
|
||||
count = int_ceil_div(len(img_data), SECTOR_SIZE)
|
||||
|
||||
#execute a dd subprocess
|
||||
@ -157,7 +160,8 @@ def verify_image(image_file, device_file, offset):
|
||||
)
|
||||
|
||||
#verfy the data
|
||||
tmp_data = open(tmp_file, 'rb').read(len(img_data))
|
||||
with open(tmp_file, 'rb') as f:
|
||||
tmp_data = f.read(len(img_data))
|
||||
if img_data != tmp_data: return 'Verification Failed:\n%s'%verbose
|
||||
return 'Verification Passed:\n%s'%verbose
|
||||
|
||||
@ -166,11 +170,13 @@ def write_image(image_file, device_file, offset):
|
||||
tmp_file = get_tmp_file()
|
||||
|
||||
#write the padded image data
|
||||
img_data = open(image_file, 'rb').read()
|
||||
with open(image_file, 'rb') as f:
|
||||
img_data = f.read()
|
||||
count = int_ceil_div(len(img_data), SECTOR_SIZE)
|
||||
pad_len = SECTOR_SIZE*count - len(img_data)
|
||||
padding = bytes(b'\x00')*pad_len #zero-padding
|
||||
open(tmp_file, 'wb').write(img_data + padding)
|
||||
with open(tmp_file, 'wb') as f:
|
||||
f.write(img_data + padding)
|
||||
|
||||
#execute a dd subprocess
|
||||
verbose = command(
|
||||
|
||||
@ -238,9 +238,8 @@ def get_tpm_caps_info():
|
||||
for tpm_device in os.listdir(base_path):
|
||||
if tpm_device.startswith("tpm"):
|
||||
for key in props_to_read:
|
||||
result["{}_{}".format(tpm_device, key)] = (
|
||||
open(os.path.join(base_path, tpm_device, key), "r").read().strip()
|
||||
)
|
||||
with open(os.path.join(base_path, tpm_device, key), "r") as f:
|
||||
result["{}_{}".format(tpm_device, key)] = f.read().strip()
|
||||
return result
|
||||
|
||||
|
||||
|
||||
@ -65,7 +65,8 @@ def get_gpio_map_info(gpio_dev):
|
||||
for info_file in os.listdir(map_info_path):
|
||||
if not os.path.isfile(os.path.join(map_info_path, info_file)):
|
||||
continue
|
||||
map_info_value = open(os.path.join(map_info_path, info_file), "r").read().strip()
|
||||
with open(os.path.join(map_info_path, info_file), "r") as f:
|
||||
map_info_value = f.read().strip()
|
||||
try:
|
||||
map_info[info_file] = int(map_info_value, 0)
|
||||
except ValueError:
|
||||
@ -92,7 +93,8 @@ def get_map_data(gpio_dev, path, logger=None):
|
||||
if logger:
|
||||
logger.trace("Couldn't find a device tree file to match: `{0}'".format(map_path))
|
||||
return None
|
||||
map_info_value = open(map_path, "r").read().strip().rstrip("\x00")
|
||||
with open(map_path, "r") as f:
|
||||
map_info_value = f.read().strip().rstrip("\x00")
|
||||
if logger:
|
||||
logger.trace("File at `{0}' has value `{1}'".format(map_path, map_info_value))
|
||||
try:
|
||||
@ -182,13 +184,15 @@ class SysFSGPIO(object):
|
||||
gpio_path = os.path.join(GPIO_SYSFS_BASE_DIR, "gpio{}".format(gpio_num))
|
||||
if not os.path.exists(gpio_path):
|
||||
self.log.trace("Creating GPIO path `{}'...".format(gpio_path))
|
||||
open(os.path.join(GPIO_SYSFS_BASE_DIR, "export"), "w").write("{}".format(gpio_num))
|
||||
with open(os.path.join(GPIO_SYSFS_BASE_DIR, "export"), "w") as f:
|
||||
f.write("{}".format(gpio_num))
|
||||
ddr_str = "out" if ddr_out else "in"
|
||||
ddr_str = "high" if ini_v else ddr_str
|
||||
if ini_v and ddr_out:
|
||||
self._out_value |= 1 << gpio_idx
|
||||
self.log.trace("On GPIO path `{}', setting DDR mode to {}.".format(gpio_path, ddr_str))
|
||||
open(os.path.join(GPIO_SYSFS_BASE_DIR, gpio_path, "direction"), "w").write(ddr_str)
|
||||
with open(os.path.join(GPIO_SYSFS_BASE_DIR, gpio_path, "direction"), "w") as f:
|
||||
f.write(ddr_str)
|
||||
|
||||
def set(self, gpio_idx, value=None):
|
||||
"""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user