Commit 107adf3e authored by Sandro Knauß's avatar Sandro Knauß
Browse files

[weblate] save-suggestions: Add commandline arguments

* Set the repo to work with as argument
* add verbose flag, so we see logging in the console
* rename subprocessOutputToLogger->log_subprocess_output
parent eba0a7f6
#!/usr/bin/env python3 #!/usr/bin/env python3
# This script generates a temporary copy of all translations including suggestions. # This script generates a temporary copy of all translations including suggestions.
# TMPDIR - the directory where the temporary copy is located. # We have created a local git clone before running this script.
# We have created TMPDIR manually, by git cloning locally. # It takes the default branch to work on.
# #
# Usage: # Usage:
# #
# /var/lib/weblate/scripts/save-suggestions.py repopath # /var/lib/weblate/scripts/save-suggestions.py repopath
import argparse
import logging import logging
import logging.config import logging.config
import os import os
import pathlib
import subprocess import subprocess
import sys import sys
...@@ -23,7 +25,6 @@ application = get_wsgi_application() ...@@ -23,7 +25,6 @@ application = get_wsgi_application()
from weblate.trans import models from weblate.trans import models
TMPDIR = "/var/lib/weblate/repositories/vcs/staging/"
logging.config.fileConfig('/var/lib/weblate/config/saveSuggestions.conf') logging.config.fileConfig('/var/lib/weblate/config/saveSuggestions.conf')
...@@ -32,7 +33,8 @@ logger = logging.getLogger('saveSuggestions') ...@@ -32,7 +33,8 @@ logger = logging.getLogger('saveSuggestions')
logger.info("Start a new run.") logger.info("Start a new run.")
def subprocessOutputToLogger(cmd, **kwargs): def log_subprocess_output(cmd, **kwargs):
"""Add subprocess output to logger."""
logger.info("Running command '%s':", " ".join(cmd)) logger.info("Running command '%s':", " ".join(cmd))
popen = subprocess.Popen(cmd, universal_newlines=True, popen = subprocess.Popen(cmd, universal_newlines=True,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
...@@ -62,11 +64,30 @@ def update_unit(translation, unit, target): ...@@ -62,11 +64,30 @@ def update_unit(translation, unit, target):
def commandline(): def commandline():
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--verbose",
action='store_true',
help="verbose logging.")
parser.add_argument(
"repopath",
type=pathlib.Path,
help="path to the repository.")
args = parser.parse_args()
prog = parser.prog
if args.verbose:
for handler in logging.getLogger().handlers:
if isinstance(handler, logging.StreamHandler):
handler.level = logging.DEBUG
repopath = args.repopath
try: try:
subprocessOutputToLogger(["git", "clean", "-fd"], cwd=TMPDIR) log_subprocess_output(["git", "clean", "-fd"], cwd=str(repopath))
subprocessOutputToLogger(["git", "fetch"], cwd=TMPDIR) log_subprocess_output(["git", "fetch"], cwd=str(repopath))
subprocessOutputToLogger(["git", "reset", 'FETCH_HEAD', "--hard"], log_subprocess_output(["git", "reset", 'FETCH_HEAD', "--hard"],
cwd=TMPDIR) cwd=str(repopath))
except: except:
logger.exception("Got an exception") logger.exception("Got an exception")
raise raise
...@@ -95,13 +116,12 @@ def commandline(): ...@@ -95,13 +116,12 @@ def commandline():
# save with suggestions # save with suggestions
if changed: if changed:
with open(TMPDIR+"/"+translation.filename, "wb") as f: with (repopath/translation.filename).open("wb") as f:
translation.store.store.serialize(f) translation.store.store.serialize(f)
except: except:
logger.exception("Got an exception for %s(%i)", subproject.name, i) logger.exception("Got an exception for %s(%i)", subproject.name, i)
raise raise
logger.info("Successfully updated %s.", repopath)
logger.info("Successfully updated %s.", TMPDIR)
if __name__ == "__main__": if __name__ == "__main__":
commandline() commandline()
...@@ -7,13 +7,14 @@ set -e ...@@ -7,13 +7,14 @@ set -e
set -u set -u
STAGING_WEB_DIR=/var/www/staging STAGING_WEB_DIR=/var/www/staging
STAGING_REPO_DIR=/var/lib/weblate/repositories/vcs/staging
SANITY_CHECK_LOGFILE_BASENAME=last-sanity-errors.txt SANITY_CHECK_LOGFILE_BASENAME=last-sanity-errors.txt
SANITY_CHECK_LOGFILE="${STAGING_WEB_DIR}/${SANITY_CHECK_LOGFILE_BASENAME}" SANITY_CHECK_LOGFILE="${STAGING_WEB_DIR}/${SANITY_CHECK_LOGFILE_BASENAME}"
IKIWIKI_LOGFILE=/var/log/weblate/update-staging-website_ikiwiki.log IKIWIKI_LOGFILE=/var/log/weblate/update-staging-website_ikiwiki.log
/var/lib/weblate/scripts/save-suggestions.py /var/lib/weblate/scripts/save-suggestions.py "$STAGING_REPO_DIR"
cd /var/lib/weblate/repositories/vcs/staging cd "STAGING_REPO_DIR"
# Note: cron ignores the exit code so on errors, we print a message to # Note: cron ignores the exit code so on errors, we print a message to
# STDERR, which cron will then send over email # STDERR, which cron will then send over email
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment