neutralize.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. import logging
  3. import optparse
  4. import sys
  5. from pathlib import Path
  6. import odoo
  7. from . import Command
  8. _logger = logging.getLogger(__name__)
  9. class Neutralize(Command):
  10. """Neutralize a production database for testing: no emails sent, etc."""
  11. def run(self, args):
  12. parser = odoo.tools.config.parser
  13. parser.prog = f'{Path(sys.argv[0]).name} {self.name}'
  14. group = optparse.OptionGroup(parser, "Neutralize", "Neutralize the database specified by the `-d` argument.")
  15. group.add_option("--stdout", action="store_true", dest="to_stdout",
  16. help="Output the neutralization SQL instead of applying it")
  17. parser.add_option_group(group)
  18. opt = odoo.tools.config.parse_config(args)
  19. dbname = odoo.tools.config['db_name']
  20. if not dbname:
  21. _logger.error('Neutralize command needs a database name. Use "-d" argument')
  22. sys.exit(1)
  23. if not opt.to_stdout:
  24. _logger.info("Starting %s database neutralization", dbname)
  25. try:
  26. with odoo.sql_db.db_connect(dbname).cursor() as cursor:
  27. if opt.to_stdout:
  28. installed_modules = odoo.modules.neutralize.get_installed_modules(cursor)
  29. queries = odoo.modules.neutralize.get_neutralization_queries(installed_modules)
  30. # pylint: disable=bad-builtin
  31. print('BEGIN;')
  32. for query in queries:
  33. # pylint: disable=bad-builtin
  34. print(query.rstrip(";") + ";")
  35. # pylint: disable=bad-builtin
  36. print("COMMIT;")
  37. else:
  38. odoo.modules.neutralize.neutralize_database(cursor)
  39. except Exception:
  40. _logger.critical("An error occurred during the neutralization. THE DATABASE IS NOT NEUTRALIZED!")
  41. sys.exit(1)