Skip to content
Permalink
Browse files

Merge pull request #1262 from bhipple/feature/context-manager

Use context manager for file opens
  • Loading branch information
grahamc committed Mar 23, 2020
2 parents b30372e + 28fcfe8 commit b8f019abe2e2d0e3e0d6232f84f45039beaf49b5
Showing with 8 additions and 12 deletions.
  1. +2 −3 nixops/known_hosts.py
  2. +6 −9 nixops/util.py
@@ -23,9 +23,8 @@ def _rewrite(ip_address: str, add_ip: bool, public_host_key: str) -> None:
fcntl.flock(
lockfile, fcntl.LOCK_EX
) # unlock is implicit at the end of the with
f = open(path, "r")
contents = f.read()
f.close()
with open(path, "r") as f:
contents = f.read()

def rewrite(lst: str) -> Optional[str]:
if " " not in lst:
@@ -329,12 +329,10 @@ def create_key_pair(
)
if res != 0:
raise Exception("unable to generate an SSH key")
f = open(key_dir + "/key")
private = f.read()
f.close()
f = open(key_dir + "/key.pub")
public = f.read().rstrip()
f.close()
with open(key_dir + "/key") as f:
private = f.read()
with open(key_dir + "/key.pub") as f:
public = f.read().rstrip()
shutil.rmtree(key_dir)
return (private, public)

@@ -430,9 +428,8 @@ def enum(**enums):


def write_file(path: str, contents: str) -> None:
f = open(path, "w")
f.write(contents)
f.close()
with open(path, "w") as f:
f.write(contents)


def xml_expr_to_python(node):

0 comments on commit b8f019a

Please sign in to comment.
You can’t perform that action at this time.