Skip to content

Instantly share code, notes, and snippets.

@william-andre
Last active November 23, 2022 14:59
Show Gist options
  • Save william-andre/01a5ff34f9bfc1f407eb162ed8b53a5f to your computer and use it in GitHub Desktop.
Save william-andre/01a5ff34f9bfc1f407eb162ed8b53a5f to your computer and use it in GitHub Desktop.
Update the .pot files automatically when runbot detects outdated files.
#!/usr/bin/env python3
"""Update the .pot files automatically when runbot detects outdated files.
positional arguments:
url Link to the runbot instance
optional arguments:
-h, --help show this help message and exit
-m MODULES, --modules MODULES
-u USERNAME, --username USERNAME
-p PASSWORD, --password PASSWORD
--archive ARCHIVE
--odoo_root ODOO_ROOT
"""
import argparse
import base64
import tarfile
import tempfile
import sys
import xmlrpc.client
try:
import git
except ImportError:
print("Install GitPython to manage your history.")
git = None
# Parsing
parser = argparse.ArgumentParser(
prog='updatepot'
)
parser.add_argument('url', help="Link to the runbot instance")
parser.add_argument('-m', '--modules', default=False)
parser.add_argument('-u', '--username', default="admin")
parser.add_argument('-p', '--password', default="admin")
parser.add_argument('-d', '--database', default=False)
parser.add_argument('--archive', default="/tmp/odoo-translation.tgz")
parser.add_argument('--odoo-root', default=".")
args = parser.parse_args()
if git is not None:
repo = git.Repo(f"{args.odoo_root}/odoo")
if repo.is_dirty():
print("Repo is not clean")
sys.exit(-1)
# Generate translations
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(args.url))
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(args.url))
db = args.database or args.url.split('//')[1].split('.')[0]
uid = common.authenticate(db, args.username, args.password, {})
export_id = models.execute_kw(
db, uid, args.password, 'base.language.export', 'create', [{
'lang': '__new__',
'format': 'tgz',
'modules': args.modules,
}],
)
models.execute_kw(
db, uid, args.password, 'base.language.export', 'act_getfile', [export_id]
)
data = models.execute_kw(
db, uid, args.password, 'base.language.export', 'read', [export_id], {
'fields': ['data']
}
)[0]['data']
# Extract translation in repo
with tempfile.NamedTemporaryFile(mode='w+b') as tmp_tgz:
tmp_tgz.write(base64.b64decode(data))
with tarfile.open(tmp_tgz.name) as archive:
archive.extractall(
path=f'{args.odoo_root}/odoo/addons',
members=[
info for info in archive.getmembers()
if not info.name.startswith('base/')
and not info.name.startswith('test_')
],
)
archive.extractall(
path=f'{args.odoo_root}/odoo/odoo/addons',
members=[
info for info in archive.getmembers()
if info.name.startswith('base/')
],
)
# Manage the diff
if git is not None:
repo.git.add('*')
repo.git.commit("-m", "[I18N] update translations")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment