Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

python: Convert subprocess.Popen.communicate to run or check_output.

Anders Kaseorg75c59a820dd7
diff --git a/puppet/zulip/files/postgresql/pg_backup_and_purge b/puppet/zulip/files/postgresql/pg_backup_and_purge
index 314135f1811..ad4f66686fa 100755
--- a/puppet/zulip/files/postgresql/pg_backup_and_purge
+++ b/puppet/zulip/files/postgresql/pg_backup_and_purge
@@ -20,14 +20,7 @@ def run(args: List[str], dry_run: bool = False) -> str:
print("Would have run: " + " ".join(map(shlex.quote, args)))
return ""
- p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- stdout, stderr = p.communicate()
- if p.returncode:
- logger.error("Could not invoke %s\nstdout: %r\nstderror: %r",
- args[0], stdout, stderr)
- sys.exit(1)
- return stdout.decode()
+ return subprocess.check_output(args, stdin=subprocess.DEVNULL, universal_newlines=True)
recovery_val = run(['psql', '-v', 'ON_ERROR_STOP=1', '-t', '-c', 'SELECT pg_is_in_recovery()']).strip()
# Assertion to check that we're extracting the value correctly.
diff --git a/tools/lib/pretty_print.py b/tools/lib/pretty_print.py
index c23c1cacfe7..006b2ac675e 100644
--- a/tools/lib/pretty_print.py
+++ b/tools/lib/pretty_print.py
@@ -201,12 +201,7 @@ def validate_indent_html(fn: str, fix: bool) -> int:
return 0
print('Invalid Indentation detected in file: '
f'{fn}\nDiff for the file against expected indented file:', flush=True)
- with subprocess.Popen(
- ['diff', fn, '-'],
- stdin=subprocess.PIPE,
- stderr=subprocess.STDOUT,
- universal_newlines=True) as p:
- p.communicate(phtml)
+ subprocess.run(['diff', fn, '-'], input=phtml, universal_newlines=True)
print()
print("This problem can be fixed with the `--fix` option.")
return 0
diff --git a/zerver/lib/test_fixtures.py b/zerver/lib/test_fixtures.py
index 923c390a4f1..e92d78f52af 100644
--- a/zerver/lib/test_fixtures.py
+++ b/zerver/lib/test_fixtures.py
@@ -360,12 +360,9 @@ def destroy_leaked_test_databases(expiry_time: int = 60 * 60) -> int:
return 0
commands = "\n".join(f"DROP DATABASE IF EXISTS {db};" for db in databases_to_drop)
- p = subprocess.Popen(["psql", "-q", "-v", "ON_ERROR_STOP=1", "-h", "localhost",
- "postgres", "zulip_test"],
- stdin=subprocess.PIPE)
- p.communicate(input=commands.encode())
- if p.returncode != 0:
- raise RuntimeError("Error cleaning up test databases!")
+ subprocess.run(["psql", "-q", "-v", "ON_ERROR_STOP=1", "-h", "localhost",
+ "postgres", "zulip_test"],
+ input=commands, check=True, universal_newlines=True)
return len(databases_to_drop)
def remove_test_run_directories(expiry_time: int = 60 * 60) -> int:
diff --git a/zerver/lib/tex.py b/zerver/lib/tex.py
index a53f2d4d160..a12ef032e7c 100644
--- a/zerver/lib/tex.py
+++ b/zerver/lib/tex.py
@@ -33,14 +33,11 @@ def render_tex(tex: str, is_inline: bool=True) -> Optional[str]:
command = ['node', katex_path]
if not is_inline:
command.extend(['--display-mode'])
- katex = subprocess.Popen(command,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- stdout = katex.communicate(input=tex.encode())[0]
- if katex.returncode == 0:
+ try:
+ stdout = subprocess.check_output(
+ command, input=tex, stderr=subprocess.DEVNULL, universal_newlines=True
+ )
# stdout contains a newline at the end
- assert stdout is not None
- return stdout.decode('utf-8').strip()
- else:
+ return stdout.strip()
+ except subprocess.CalledProcessError:
return None