Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check to prevent single locales from being pushed
- Loading branch information
1 parent
c70dacd
commit a9eee6d
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters