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