Skip to content

Commit

Permalink
Add check to prevent single locales from being pushed
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLorenzo committed Nov 7, 2016
1 parent c70dacd commit a9eee6d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
41 changes: 41 additions & 0 deletions mozapkpublisher/apk.py
@@ -0,0 +1,41 @@
import logging
import re

from io import BytesIO
from zipfile import ZipFile

logger = logging.getLogger(__name__)

_LOCALE_LINE_PATTERN = re.compile(r'^locale \S+ (\S+) .+')
_OMNI_JA_LOCATION = 'assets/omni.ja'
_CHROME_MANIFEST_LOCATION = 'chrome/chrome.manifest'


def check_if_apk_is_multilocale(apk_path):
with ZipFile(apk_path) as apk_zip:
omni_ja_data = BytesIO(apk_zip.read(_OMNI_JA_LOCATION))
with ZipFile(omni_ja_data) as omni_ja:
with omni_ja.open(_CHROME_MANIFEST_LOCATION) as manifest:
manifest_raw_lines = manifest.readlines()

unique_locales = _get_unique_locales(manifest_raw_lines)
number_of_unique_locales = len(unique_locales)
logger.debug('"{}" contains {} locales: '.format(apk_path, number_of_unique_locales, unique_locales))

if number_of_unique_locales == 0:
raise Exception('No locale detected in {}:{}:{}'.format(
apk_path, _OMNI_JA_LOCATION, _CHROME_MANIFEST_LOCATION
))
elif number_of_unique_locales == 1:
raise Exception('Not a multilocale APK. "{}" contains only: {}'.format(apk_path, unique_locales))


def _get_unique_locales(manifest_raw_lines):
manifest_lines = [line.decode('utf-8') for line in manifest_raw_lines]

locales = [
_LOCALE_LINE_PATTERN.match(line).group(1) for line in manifest_lines
if _LOCALE_LINE_PATTERN.match(line) is not None
]

return list(set(locales))
3 changes: 3 additions & 0 deletions mozapkpublisher/push_apk.py
Expand Up @@ -7,6 +7,7 @@
from oauth2client import client

from mozapkpublisher import googleplay
from mozapkpublisher.apk import check_if_apk_is_multilocale
from mozapkpublisher.base import Base
from mozapkpublisher.exceptions import WrongArgumentGiven
from mozapkpublisher.storel10n import StoreL10n
Expand Down Expand Up @@ -60,6 +61,8 @@ def upload_apks(self, service, apk_files):
service -- The session to Google play
apk_files -- The files
"""
[check_if_apk_is_multilocale(apk_file.name) for apk_file in apk_files]

edit_request = service.edits().insert(body={},
packageName=self.config.package_name)
package_code = googleplay.PACKAGE_NAME_VALUES[self.config.package_name]
Expand Down

0 comments on commit a9eee6d

Please sign in to comment.