[Pkg-mozext-commits] [adblock-plus] 263/464: Added "build.py translate" command to automatically upload locale files to getlocalization.com

David Prévot taffit at moszumanska.debian.org
Tue Jul 22 20:44:23 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus.

commit 37b81fe6e0a5ee0c0718bce1bf598e5c98034182
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Wed Jul 4 17:30:16 2012 +0200

    Added "build.py translate" command to automatically upload locale files to getlocalization.com
---
 build.py       | 22 ++++++++++++++++++++++
 localeTools.py | 42 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/build.py b/build.py
index ed0e3a8..fd8e6c9 100644
--- a/build.py
+++ b/build.py
@@ -192,6 +192,22 @@ def runAutoInstall(baseDir, scriptName, opts, args, type):
   packager.autoInstall(baseDir, host, port, multicompartment=multicompartment)
 
 
+def updateTranslationMaster(baseDir, scriptName, opts, args, type):
+  if len(args) < 2:
+    print 'User name and password are required to update translation master files.'
+    usage(scriptName, type, 'translate')
+    return
+
+  user = args[0]
+  password = args[1]
+
+  import buildtools.packager as packager
+  defaultLocaleDir = os.path.join(packager.getLocalesDir(baseDir), packager.defaultLocale)
+  basename = packager.readMetadata(baseDir).get('general', 'baseName')
+
+  import buildtools.localeTools as localeTools
+  localeTools.updateTranslationMaster(defaultLocaleDir, packager.defaultLocale, basename, user, password)
+
 def showDescriptions(baseDir, scriptName, opts, args, type):
   locales = None
   for option, value in opts:
@@ -303,6 +319,12 @@ with addCommand(runAutoInstall, 'autoinstall') as command:
   command.addOption('Create a build for leak testing', short='m', long='multi-compartment')
   command.supportedTypes = ('gecko')
 
+with addCommand(updateTranslationMaster, 'translate') as command:
+  command.shortDescription = 'Updates translation master files'
+  command.description = 'Updates the translation master files on getlocalization.com.'
+  command.params = '[options] user password'
+  command.supportedTypes = ('gecko')
+
 with addCommand(showDescriptions, 'showdesc') as command:
   command.shortDescription = 'Print description strings for all locales'
   command.description = 'Display description strings for all locales as specified in the corresponding meta.properties files.'
diff --git a/localeTools.py b/localeTools.py
index d36adb4..c23b5b3 100644
--- a/localeTools.py
+++ b/localeTools.py
@@ -4,7 +4,7 @@
 # version 2.0 (the "License"). You can obtain a copy of the License at
 # http://mozilla.org/MPL/2.0/.
 
-import re, sys, codecs, json
+import re, os, sys, codecs, json, urllib2
 from StringIO import StringIO
 from ConfigParser import SafeConfigParser
 from xml.parsers.expat import ParserCreate, XML_PARAM_ENTITY_PARSING_ALWAYS
@@ -138,3 +138,43 @@ def toJSON(path):
       obj['description'] = comment
     result[name] = obj
   return json.dumps(result, indent=2)
+
+def updateTranslationMaster(dir, locale, projectName, user, password):
+  def encode_multipart_formdata(filename, data):
+    boundary = '----------ThIs_Is_tHe_bouNdaRY_$'
+    body =  '--%s\r\n' % boundary
+    body += 'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % ('file', filename)
+    body += 'Content-Type: application/octet-stream\r\n'
+    body += '\r\n' + data + '\r\n'
+    body += '--%s--\r\n' % boundary
+    content_type = 'multipart/form-data; boundary=%s' % boundary
+    return content_type, body
+
+  locale = re.sub(r'-.*', '', locale)
+  passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
+  passman.add_password(None, 'https://api.getlocalization.com/', user, password)
+  opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman))
+  result = json.load(opener.open('https://api.getlocalization.com/%s/api/list-master/json/' % projectName))
+  if not result.get('success', 0):
+    raise Exception('Server indicated the retrieving the list of masters failed')
+
+  existing = set(result['master_files'])
+  for file in os.listdir(dir):
+    path = os.path.join(dir, file)
+    if os.path.isfile(path):
+      data = toJSON(path)
+      if data:
+        if file in existing:
+          url = 'https://api.getlocalization.com/%s/api/update-master/' % projectName
+          existing.remove(file)
+        else:
+          url = 'https://api.getlocalization.com/%s/api/create-master/json/%s/' % (projectName, locale)
+
+        content_type, body = encode_multipart_formdata(file, data.encode('utf-8'))
+        request = urllib2.Request(url, body)
+        request.add_header('Content-Type', content_type)
+        request.add_header('Content-Length', len(body))
+        opener.open(request).read()
+
+  for file in existing:
+    print 'Warning: master file %s needs to be removed' % file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git



More information about the Pkg-mozext-commits mailing list