|
12 | 12 | import tempfile |
13 | 13 | import time |
14 | 14 | from datetime import datetime |
| 15 | +from glob import iglob |
15 | 16 | from itertools import chain |
16 | 17 | from logging import getLogger |
17 | 18 | from pathlib import Path |
@@ -629,6 +630,102 @@ def install( |
629 | 630 | ) |
630 | 631 |
|
631 | 632 |
|
| 633 | +@task( |
| 634 | + help={ |
| 635 | + "module": "Specific Odoo module to update.", |
| 636 | + "all": "Update all modules. Takes a lot of time. [default: False]", |
| 637 | + "repo": "Update all modules from a specific repository.", |
| 638 | + "msgmerge": "Merge .pot changes into all .po files. [default: True]", |
| 639 | + "fuzzy_matching": "Use fuzzy matching when merging. [default: False]", |
| 640 | + "purge_old_translations": "Remove lines with old translations. [default: True]", |
| 641 | + "remove_dates": "Remove dates from .po files. [default: True]", |
| 642 | + } |
| 643 | +) |
| 644 | +def updatepot( |
| 645 | + c, |
| 646 | + module=None, |
| 647 | + _all=False, |
| 648 | + repo=None, |
| 649 | + msgmerge=True, |
| 650 | + fuzzy_matching=False, |
| 651 | + purge_old_translations=True, |
| 652 | + remove_dates=True, |
| 653 | +): |
| 654 | + """Updates POT of a given module""" |
| 655 | + if not module and not _all and not repo: |
| 656 | + cur_module = _get_cwd_addon(Path.cwd()) |
| 657 | + if not cur_module: |
| 658 | + raise exceptions.ParseError( |
| 659 | + msg="Odoo addon to update translation not found " |
| 660 | + "You must provide at least one of: -m {module}, " |
| 661 | + "be in the subdirectory of a module, --all or -r {repo} " |
| 662 | + "See --help for details." |
| 663 | + ) |
| 664 | + module = cur_module |
| 665 | + |
| 666 | + cmd = ( |
| 667 | + DOCKER_COMPOSE_CMD |
| 668 | + + f" run --rm -v {PROJECT_ROOT}/odoo/custom:/tmp/odoo/custom:rw,z " |
| 669 | + f"-v {PROJECT_ROOT}/odoo/auto:/tmp/odoo/auto:rw,z odoo " |
| 670 | + "click-odoo-makepot --addons-dir " |
| 671 | + f"{'/tmp/odoo/auto/addons' if not repo else '/tmp/odoo/custom/src/' + repo}/" |
| 672 | + ) |
| 673 | + |
| 674 | + cmd += " --msgmerge" if msgmerge else " --no-msgmerge" |
| 675 | + cmd += " --no-fuzzy-matching" if not fuzzy_matching else " --fuzzy-matching" |
| 676 | + cmd += ( |
| 677 | + " --purge-old-translations" |
| 678 | + if purge_old_translations |
| 679 | + else " --no-purge-old-translations" |
| 680 | + ) |
| 681 | + if not _all and not repo: |
| 682 | + cmd += f" -m {module}" |
| 683 | + |
| 684 | + with c.cd(str(PROJECT_ROOT)): |
| 685 | + c.run(DOCKER_COMPOSE_CMD + " stop odoo") |
| 686 | + c.run( |
| 687 | + cmd, |
| 688 | + env=UID_ENV, |
| 689 | + pty=True, |
| 690 | + ) |
| 691 | + glob = ( |
| 692 | + f"{PROJECT_ROOT}/odoo/custom/src/{'*' if not repo else repo}" |
| 693 | + f"/{'*' if _all or repo else module}/i18n/" |
| 694 | + ) |
| 695 | + new_files = iglob(f"{glob}/*.po*") |
| 696 | + for new_file in new_files: |
| 697 | + file_name = os.path.basename(new_file) |
| 698 | + if file_name.endswith("~"): |
| 699 | + os.remove(new_file) |
| 700 | + continue |
| 701 | + with open(new_file) as fd: |
| 702 | + content = fd.read() |
| 703 | + new_lines = [] |
| 704 | + for line in content.splitlines(): |
| 705 | + if remove_dates and ( |
| 706 | + line.startswith('"POT-Creation-Date') |
| 707 | + or line.startswith('"PO-Revision-Date') |
| 708 | + ): |
| 709 | + continue |
| 710 | + new_lines.append(line) |
| 711 | + content = "\n".join(new_lines) |
| 712 | + with open(new_file, "w") as fd: |
| 713 | + fd.write(content.strip() + "\n") |
| 714 | + _logger.info(".po[t] files updated") |
| 715 | + precommit_cmd = f"pre-commit run --files {' '.join(iglob(f'{glob}/*.po*'))}" |
| 716 | + |
| 717 | + if not repo and module: |
| 718 | + for folder in iglob(f"{PROJECT_ROOT}/odoo/custom/src/*/*"): |
| 719 | + if os.path.isdir(folder) and os.path.basename(folder) == module: |
| 720 | + repo = os.path.basename(os.path.dirname(folder)) |
| 721 | + break |
| 722 | + precommit_folder = ( |
| 723 | + str(PROJECT_ROOT) + f"/odoo/custom/src/{repo}" if repo != "private" else "" |
| 724 | + ) |
| 725 | + with c.cd(str(precommit_folder)): |
| 726 | + c.run(precommit_cmd) |
| 727 | + |
| 728 | + |
632 | 729 | @task( |
633 | 730 | help={ |
634 | 731 | "modules": "Comma-separated list of modules to uninstall.", |
|
0 commit comments