[CHG] core: deprecate all exec_pg_* functions

They're not used much and they don't seem much of an advantage
compared to just calling `subprocess` with the discovery utility
functions.

While at it, fix `dump_db` to not unnecessarily have an open stdin to
`pg_dump`.

Part-of: odoo/odoo#98023
This commit is contained in:
Xavier Morel
2022-08-22 16:48:28 +02:00
parent 719e7d46e4
commit 7f14631fe8
2 changed files with 17 additions and 9 deletions
+13 -9
View File
@@ -4,6 +4,7 @@ import json
import logging
import os
import shutil
import subprocess
import tempfile
import threading
import traceback
@@ -26,6 +27,7 @@ import odoo.sql_db
import odoo.tools
from odoo.sql_db import db_connect
from odoo.release import version_info
from odoo.tools import find_pg_tool, exec_pg_environ
_logger = logging.getLogger(__name__)
@@ -229,8 +231,8 @@ def dump_db(db_name, stream, backup_format='zip'):
_logger.info('DUMP DB: %s format %s', db_name, backup_format)
cmd = ['pg_dump', '--no-owner']
cmd.append(db_name)
cmd = [find_pg_tool('pg_dump'), '--no-owner', db_name]
env = exec_pg_environ()
if backup_format == 'zip':
with tempfile.TemporaryDirectory() as dump_dir:
@@ -242,7 +244,7 @@ def dump_db(db_name, stream, backup_format='zip'):
with db.cursor() as cr:
json.dump(dump_db_manifest(cr), fh, indent=4)
cmd.insert(-1, '--file=' + os.path.join(dump_dir, 'dump.sql'))
odoo.tools.exec_pg_command(*cmd)
subprocess.run(cmd, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, check=True)
if stream:
odoo.tools.osutil.zip_dir(dump_dir, stream, include_dir=False, fnct_sort=lambda file_name: file_name != 'dump.sql')
else:
@@ -252,7 +254,7 @@ def dump_db(db_name, stream, backup_format='zip'):
return t
else:
cmd.insert(-1, '--format=c')
stdin, stdout = odoo.tools.exec_pg_command_pipe(*cmd)
stdout = subprocess.Popen(cmd, env=env, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE).stdout
if stream:
shutil.copyfileobj(stdout, stream)
else:
@@ -302,11 +304,13 @@ def restore_db(db, dump_file, copy=False):
pg_cmd = 'pg_restore'
pg_args = ['--no-owner', dump_file]
args = []
args.append('--dbname=' + db)
pg_args = args + pg_args
if odoo.tools.exec_pg_command(pg_cmd, *pg_args):
r = subprocess.run(
[find_pg_tool(pg_cmd), '--dbname=' + db, *pg_args],
env=exec_pg_environ(),
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
)
if r.returncode != 0:
raise Exception("Couldn't restore database")
registry = odoo.modules.registry.Registry.new(db)
+4
View File
@@ -74,6 +74,7 @@ def find_in_path(name):
return which(name, path=os.pathsep.join(path))
def _exec_pipe(prog, args, env=None):
warnings.warn("Since 16.0, just use `subprocess`.", DeprecationWarning, stacklevel=3)
cmd = (prog,) + args
# on win32, passing close_fds=True is not compatible
# with redirecting std[in/err/out]
@@ -82,6 +83,7 @@ def _exec_pipe(prog, args, env=None):
return pop.stdin, pop.stdout
def exec_command_pipe(name, *args):
warnings.warn("Since 16.0, use `subprocess` directly.", DeprecationWarning, stacklevel=2)
prog = find_in_path(name)
if not prog:
raise Exception('Command `%s` not found.' % name)
@@ -124,6 +126,7 @@ def exec_pg_environ():
return env
def exec_pg_command(name, *args):
warnings.warn("Since 16.0, use `subprocess` directly.", DeprecationWarning, stacklevel=2)
prog = find_pg_tool(name)
env = exec_pg_environ()
with open(os.devnull) as dn:
@@ -133,6 +136,7 @@ def exec_pg_command(name, *args):
raise Exception('Postgres subprocess %s error %s' % (args2, rc))
def exec_pg_command_pipe(name, *args):
warnings.warn("Since 16.0, use `subprocess` directly.", DeprecationWarning, stacklevel=2)
prog = find_pg_tool(name)
env = exec_pg_environ()
return _exec_pipe(prog, args, env)