From 4598cb24f4a8ccca751898c1a7cdc3e462265bb0 Mon Sep 17 00:00:00 2001 From: Revant Nandgaonkar Date: Fri, 10 Jul 2020 16:18:25 +0530 Subject: [PATCH] fix: file input with subprocess --- build/common/commands/restore_backup.py | 8 ++++---- build/common/commands/utils.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build/common/commands/restore_backup.py b/build/common/commands/restore_backup.py index d2e4f0ee..2ae6ca78 100644 --- a/build/common/commands/restore_backup.py +++ b/build/common/commands/restore_backup.py @@ -209,7 +209,8 @@ def restore_postgres(config, site_config, database_file): run_command(psql_command + [psql_uri, "-c", f"CREATE DATABASE \"{db_name}\""]) run_command(psql_command + [psql_uri, "-c", f"CREATE user {db_name} password '{db_password}'"]) run_command(psql_command + [psql_uri, "-c", f"GRANT ALL PRIVILEGES ON DATABASE \"{db_name}\" TO {db_name}"]) - run_command(psql_command + [f"{psql_uri}/{db_name}", "<", database_file.replace('.gz', '')]) + with open(database_file.replace('.gz', ''), 'r') as db_file: + run_command(psql_command + [f"{psql_uri}/{db_name}", "<"], stdin=db_file) def restore_mariadb(config, site_config, database_file): @@ -244,10 +245,9 @@ def restore_mariadb(config, site_config, database_file): grant_privileges = mysql_command + ["-e", f"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%' IDENTIFIED BY '{db_password}'; FLUSH PRIVILEGES;"] run_command(grant_privileges) - command = mysql_command + [f"{db_name}", "<", database_file.replace(".gz", "")] - print('Restoring MariaDB') - run_command(command) + with open(database_file.replace(".gz", ""), "r") as db_file: + run_command(mysql_command + [f"{db_name}", "<"], stdin=db_file) def main(): diff --git a/build/common/commands/utils.py b/build/common/commands/utils.py index d962fbd5..8ebffb63 100644 --- a/build/common/commands/utils.py +++ b/build/common/commands/utils.py @@ -1,10 +1,10 @@ import subprocess -def run_command(command, stdout=None, stderr=None): +def run_command(command, stdout=None, stdin=None, stderr=None): stdout = stdout or subprocess.PIPE stderr = stderr or subprocess.PIPE - process = subprocess.Popen(command, stdout=stdout, stderr=stderr) + process = subprocess.Popen(command, stdout=stdout, stdin=stdin, stderr=stderr) out, error = process.communicate() if process.returncode: print("Something went wrong:")