Compare commits

..

1 Commits

1 changed files with 61 additions and 52 deletions

View File

@ -23,37 +23,32 @@ from twisted.python import log
from dateutil.parser import parse as dateparse from dateutil.parser import parse as dateparse
HTTP_USER_AGENT = 'User-Agent' _HEADER_USER_AGENT = 'User-Agent'
_HEADER_SIGNATURE = 'X-TX-Signature' _HEADER_SIGNATURE = 'X-TX-Signature'
_EVENT_KEY = 'event' _EVENT_KEY = 'event'
branch = 'v0.6'
transifexSecret = "" transifexSecret = ""
transifexUsername = "" transifexUsername = ""
transifexPassword = "" transifexPassword = ""
transifex_webhook_url = 'https://buildbot.bitmessage.org/change_hook/transifex'
transifex_dict = {} transifex_dict = {}
secret = "" secret = ""
master = "" master = ""
from transifex.api import transifex_api gitHubToken = os.environ('gitHubToken')
transifex_api.setup(auth='my_token')
org = transifex_api.Organization.get(slug='my_org')
proj = org.fetch('projects').get(slug='my_project')
lang = transifex_api.Language.get(code='<lang>')
resource = proj.fetch('resources').get(slug='my_resource')
url = transifex_api.ResourceTranslationsAsyncDownload.download(
resource=resource, language=lang,content_encoding = 'text',
file_type = 'default', pseudo = False
)
class TransifexHandler(BaseHookHandler): class TransifexHandler(BaseHookHandler):
def __init__(self, transifex_dict, secret, master,): def __init__(self, master, secret, transifex_dict):
self.secret = secret self.secret = secret
self.master = master self.master = master
self.transifex_dict = transifex_dict self.transifex_dict = transifex_dict
def returnMessage(self, status = False, message = "Unimplemented"):
output = json.dumps({"status": "OK" if status else "FAIL", "message": message})
return [output, [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))
]]
def verifyTransifexSignature( def verifyTransifexSignature(
self, request, content, rendered_secret, signature, header_signature self, request, content, rendered_secret, signature, header_signature
): ):
@ -73,7 +68,7 @@ class TransifexHandler(BaseHookHandler):
) )
if tx_signature() != header_signature: if tx_signature() != header_signature:
raise ValueError('Invalid secret') raise ValueError('Invalid secret')
try: try:
if signature != os.environ.get('HTTP_X_TX_SIGNATURE'): if signature != os.environ.get('HTTP_X_TX_SIGNATURE'):
return False return False
@ -81,14 +76,7 @@ class TransifexHandler(BaseHookHandler):
except: except:
return False return False
def updateLocalTranslationDestination(self, ts, lang, branch): def downloadTranslatedLanguage(self, ts, lang):
call(["git", "pull", "--all", "-q"])
call(["git", "stash", "-q"])
call(["git", "checkout", "-q", branch])
call(["git", "checkout", "-q", "-b", "translate_" + lang + "_" + str(ts)])
call(["git", "branch", "-q", "--set-upstream-to=origin/v0.6"])
def downloadTranslatedLanguage(ts, lang):
headers = {"Authorization": "Basic " + b64encode(transifexUsername + ":" + transifexPassword)} headers = {"Authorization": "Basic " + b64encode(transifexUsername + ":" + transifexPassword)}
fname = "bitmessage_" + lang.lower() + ".po" fname = "bitmessage_" + lang.lower() + ".po"
with open("src/translations/" + fname, "wt") as handle: with open("src/translations/" + fname, "wt") as handle:
@ -99,32 +87,46 @@ class TransifexHandler(BaseHookHandler):
handle.write(content.encode("utf-8")) handle.write(content.encode("utf-8"))
return response return response
def process_translation_completed(self, transifex_dict, event_type, codebase): def commitTranslatedLanguage(self, ts, lang):
# refname = payload["ref"] call(["kivy", "src/translations/messages.pro"])
payload = transifex_dict call(["git", "add", "src/translations/bitmessage_" + lang + ".ts", "src/translations/bitmessage_" + lang + ".qm"])
call(["git", "commit", "-q", "-S", "-m", "Auto-updated language %s from transifex" % (lang)])
newbranch = "translate_" + lang + "_" + str(ts)
call(["git", "push", "-q", "translations", newbranch + ":" + newbranch])
branch = transifex_dict['branch']
request = {
"title": "Translation update " + lang,
"body": "Auto-updated from transifex",
"head": "PyBitmessageTranslations:" + newbranch,
"base": branch
}
headers = {"Authorization": "token " + gitHubToken}
response = requests.post("https://api.github.com/repos/Bitmessage/PyBitmessage/pulls",
headers=headers, data=json.dumps(request))
return response
def process_translation_completed(self, payload, transifex_dict, event_type, codebase):
changes = [] changes = []
# We only care about regular heads or tags transifex_response = self._transform_variables(payload, transifex_dict)
# match = re.match(r"^refs/(heads|tags)/(.+)$", refname) if 'pybitmessage-test' in transifex_response['project'] and 'messagespot' in transifex_response['resource']:
if "Transifex" in os.environ.get("HTTP_USER_AGENT"): if 'translation_completed' in transifex_response['event'] and 100 in transifex_response['translated']:
if not self.verifyTransifexSignature( ts = int(time.time())
request, content, rendered_secret, signature, header_signature): lang = transifex_response['language']
print('Invalid transifex signature!') branch = transifex_dict['branch']
self.updateLocalTranslationDestination(ts, lang.lower(), branch)
self.downloadTranslatedLanguage(ts, lang.lower())
response = self.commitTranslatedLanguage(ts, lang.lower())
if response.ok:
output, responseHeaders = self.returnMessage(True, "Processed.")
else:
output, responseHeaders = self.returnMessage(False, "Error: %i." % (response.status_code))
else: else:
payload = self.transifex_dict output, responseHeaders = self.returnMessage(False, "Nothing to do")
if 'pybitmessage-test' in payload['transifex_data']['project'] and 'messagespot' in payload['transifex_data']['translation_completed']['resource']: else:
if 'translation_completed' in payload['transifex_data'] and '100' in payload['transifex_data']['translated']: output, responseHeaders = self.returnMessage(False, "Nothing to do")
ts = int(time.time())
lang = payload['transifex_data']['translation_completed']['language']
branch = payload['transifex_data']['branch']
self.updateLocalTranslationDestination(ts, lang.lower(), branch)
self.downloadTranslatedLanguage(ts, lang.lower())
repository = payload['repository']
repo_url = repository['ssh_url']
project = repository['full_name']
commits = payload['commits']
# if isinstance(self.options, dict) and self.options.get('onlyIncludePushCommit', False): # if isinstance(self.options, dict) and self.options.get('onlyIncludePushCommit', False):
# commits = commits[:1] # commits = commits[:1]
@ -154,17 +156,19 @@ class TransifexHandler(BaseHookHandler):
# if codebase is not None: # if codebase is not None:
# change['codebase'] = codebase # change['codebase'] = codebase
# changes.insert(0, change) # changes.insert(0, change)
# return changes return changes
def process_review_completed(self, payload, transifex_data): def process_review_completed(self, payload, transifex_data):
pass pass
def _transform_variables(self, payload=transifex_dict): def _transform_variables(self, payload, transifex_dict):
retval = { retval = {
'project': payload[''].get('project'), 'project': payload['project'],
'repository': [payload.get('resource')], "translated": payload['translated'],
'branch': payload.get('language') "resource": payload['resource'],
"event": payload['event'],
"language": payload['language']
} }
return retval return retval
@ -190,6 +194,11 @@ class TransifexHandler(BaseHookHandler):
digestmod=hashlib.sha256) digestmod=hashlib.sha256)
header_signature = bytes2unicode( header_signature = bytes2unicode(
request.getHeader(_HEADER_SIGNATURE)) request.getHeader(_HEADER_SIGNATURE))
self.verifyTransifexSignature(
request, content, rendered_secret,
signature, header_signatur
)
event_type = bytes2unicode(payload.get(_EVENT_KEY), "None") event_type = bytes2unicode(payload.get(_EVENT_KEY), "None")
log.msg("Received event '{}' from transifex".format(event_type)) log.msg("Received event '{}' from transifex".format(event_type))