Fix prfromtransifex.py script.

As expected the script still had a lot of bugs. This fixes
various syntax and logical flaws.
This commit is contained in:
Stefan Hacker 2014-08-10 23:46:53 +02:00
parent c658a41775
commit b31a39a3e6
2 changed files with 54 additions and 43 deletions

View file

@ -3,6 +3,8 @@
user = MumbleTransifexBot user = MumbleTransifexBot
; Github password of bot ; Github password of bot
password = password =
; Email used for commits
email = mumbletransifexbot@mumble.info
[transifex] [transifex]
; Modes: default fetches all string, reviewed only reviewed ones ; Modes: default fetches all string, reviewed only reviewed ones
@ -10,7 +12,6 @@ mode = default
; Minimum number of percent needed for a translation to be included ; Minimum number of percent needed for a translation to be included
minpercent = 0 minpercent = 0
[workingrepo] [workingrepo]
owner = MumbleTransifexBot owner = MumbleTransifexBot
repo = mumble repo = mumble
@ -44,7 +45,7 @@ commit = Transifex translation update
Minimum percent translated: %(minpercent)s Minimum percent translated: %(minpercent)s
Matched %(langcount)d languages Matched %(langcount)d languages
[misc] [misc]
; File to store language bookkeeping data in ; File to store language bookkeeping data in
; Will be re-written and commited to the repository ; Will be re-written and commited to the repository
; if changes occur. ; if changes occur.

34
prfromtransifex.py Normal file → Executable file
View file

@ -39,13 +39,14 @@ import ConfigParser
from argparse import ArgumentParser from argparse import ArgumentParser
from logging import basicConfig, getLogger, DEBUG, INFO, WARNING, ERROR, debug, error, info, warning, exception from logging import basicConfig, getLogger, DEBUG, INFO, WARNING, ERROR, debug, error, info, warning, exception
from github import Github from github import Github
from plumbum.cmd import git, tx, cd from plumbum.cmd import git, tx
from plumbum import ProcessExecutionError from plumbum import ProcessExecutionError, local
import sys import sys
import os import os
import re
def getExistingPullRequest(g, user, repo): def getExistingPullRequest(g, user, repo):
s = g.search("type:pr is:open repo:%(repo)s author:%(user)s" % {'user': user, s = g.search_issues("type:pr is:open repo:%(repo)s author:%(user)s" % {'user': user,
'repo': repo}) 'repo': repo})
pullrequests = s.get_page(0) pullrequests = s.get_page(0)
total = len(pullrequests) total = len(pullrequests)
@ -95,6 +96,7 @@ if __name__ == "__main__":
user = cfg.get('github', 'user') user = cfg.get('github', 'user')
password = cfg.get('github', 'password') password = cfg.get('github', 'password')
email = cfg.get('github', 'email')
mode = cfg.get('transifex', 'mode') mode = cfg.get('transifex', 'mode')
minpercent = cfg.get('transifex', 'minpercent') minpercent = cfg.get('transifex', 'minpercent')
@ -118,17 +120,24 @@ if __name__ == "__main__":
translationstemplate = cfg.get('misc', 'template') translationstemplate = cfg.get('misc', 'template')
additionaltsfiles = cfg.get('misc', 'additionaltsfiles') additionaltsfiles = cfg.get('misc', 'additionaltsfiles')
if args.setup: if args.setup or not os.path.exists(wr_path):
info("Setting up git repo") info("Setting up git repo")
debug(git["clone", wr_url, wr_path]()) debug(git["clone", wr_url, wr_path]())
with local.cwd(wr_path):
debug(git["config", "user.name", user]())
debug(git["config", "user.email", email]())
debug(git["remote", "add", "target", tr_url]()) debug(git["remote", "add", "target", tr_url]())
info("Done") info("Done")
if args.setup:
sys.exit(0) sys.exit(0)
info("Checking for pending PR") info("Checking for pending PR")
g = Github(user, password) g = Github(user, password)
pr = getExistingPullRequest(user = user, pr = getExistingPullRequest(g,
repo = tr_repo) user = user,
repo = tr_owner + "/" + tr_repo)
if pr: if pr:
info("Already have pending PR %d", pr.number) info("Already have pending PR %d", pr.number)
# As long as we have a pending PR we want to make sure we # As long as we have a pending PR we want to make sure we
@ -145,22 +154,23 @@ if __name__ == "__main__":
remote = "target" remote = "target"
branch = tr_branch branch = tr_branch
with local.cwd(wr_path):
info("Updating remote '%s'", remote) info("Updating remote '%s'", remote)
debug(git["fetch", remote]())) debug(git["fetch", remote]())
info("Resetting to branch '%s'", branch info("Resetting to branch '%s'", branch)
debug(git["reset", remote + "/" + branch, "--hard"]()) debug(git["reset", remote + "/" + branch, "--hard"]())
info("Cleaning repository") info("Cleaning repository")
debug(git["clean", "-f", "-x", "-d"]()) debug(git["clean", "-f", "-x", "-d"]())
info("Pulling translations") info("Pulling translations")
txout = tx["pull", "-f", "--mode=" + mode, "--minimum-perc" + minpercent]() txout = tx["pull", "-f", "-a", "--mode=" + mode, "--minimum-perc=" + minpercent]()
debug(txout) debug(txout)
tfilepath = os.path.join(wr_path, translationsfile) tfilepath = os.path.join(wr_path, translationsfile)
info("Updating translations listing file '%s'", tfilepath) info("Updating translations listing file '%s'", tfilepath)
paths, files = zip(*re.findall(r"^\s->\s[\w_]+:\s([\w/\_]+/([\w_]+\.ts))$", t, flags=re.MULTILINE)) paths, files = zip(*re.findall(r"^\s->\s[\w_]+:\s([\w/\_]+/([\w_]+\.ts))$", txout, flags=re.MULTILINE))
debug(git["add"](*paths)) debug(git["add"](*paths))
translations = additionaltsfiles + " ".join(files)" translations = additionaltsfiles + " " + (" ".join(sorted(files)))
with open(tfilepath, "w") as f: with open(tfilepath, "w") as f:
f.write(translationstemplate % {'files': translations}) f.write(translationstemplate % {'files': translations})
@ -168,7 +178,7 @@ if __name__ == "__main__":
debug(git["add"](tfilepath)) debug(git["add"](tfilepath))
debug("Checking for modifications") debug("Checking for modifications")
changed, changedfiles, _ = git["diff", "--cached", "--name-only", "--exit-code"].run() changed, changedfiles, _ = git["diff", "--cached", "--name-only", "--exit-code"].run(retcode=(0,1))
if not changed: if not changed:
info("No changes to translations, done") info("No changes to translations, done")
sys.exit(0) sys.exit(0)