standardize on path.endswith() for file extension checks

Its already widely used, this just removes the exceptions.  It is also more
Pythonic
This commit is contained in:
Hans-Christoph Steiner 2020-02-14 08:48:30 +01:00
parent 202602937d
commit fafaa8f81f
No known key found for this signature in database
GPG key ID: 3E177817BA1B9BFA

View file

@ -587,17 +587,13 @@ def read_app_args(appid_versionCode_pairs, allapps, allow_vercodes=False):
def get_extension(filename): def get_extension(filename):
"""get name and extension of filename, with extension always lower case"""
base, ext = os.path.splitext(filename) base, ext = os.path.splitext(filename)
if not ext: if not ext:
return base, '' return base, ''
return base, ext.lower()[1:] return base, ext.lower()[1:]
def has_extension(filename, ext):
_ignored, f_ext = get_extension(filename)
return ext == f_ext
publish_name_regex = re.compile(r"^(.+)_([0-9]+)\.(apk|zip)$") publish_name_regex = re.compile(r"^(.+)_([0-9]+)\.(apk|zip)$")
@ -1316,7 +1312,7 @@ def manifest_paths(app_dir, flavours):
def fetch_real_name(app_dir, flavours): def fetch_real_name(app_dir, flavours):
'''Retrieve the package name. Returns the name, or None if not found.''' '''Retrieve the package name. Returns the name, or None if not found.'''
for path in manifest_paths(app_dir, flavours): for path in manifest_paths(app_dir, flavours):
if not has_extension(path, 'xml') or not os.path.isfile(path): if not path.endswith('.xml') or not os.path.isfile(path):
continue continue
logging.debug("fetch_real_name: Checking manifest at " + path) logging.debug("fetch_real_name: Checking manifest at " + path)
xml = parse_xml(path) xml = parse_xml(path)
@ -1808,11 +1804,11 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
for path in manifest_paths(root_dir, flavours): for path in manifest_paths(root_dir, flavours):
if not os.path.isfile(path): if not os.path.isfile(path):
continue continue
if has_extension(path, 'xml'): if path.endswith('.xml'):
regsub_file(r'android:versionName="[^"]*"', regsub_file(r'android:versionName="[^"]*"',
r'android:versionName="%s"' % build.versionName, r'android:versionName="%s"' % build.versionName,
path) path)
elif has_extension(path, 'gradle'): elif path.endswith('.gradle'):
regsub_file(r"""(\s*)versionName[\s'"=]+.*""", regsub_file(r"""(\s*)versionName[\s'"=]+.*""",
r"""\1versionName '%s'""" % build.versionName, r"""\1versionName '%s'""" % build.versionName,
path) path)
@ -1822,11 +1818,11 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
for path in manifest_paths(root_dir, flavours): for path in manifest_paths(root_dir, flavours):
if not os.path.isfile(path): if not os.path.isfile(path):
continue continue
if has_extension(path, 'xml'): if path.endswith('.xml'):
regsub_file(r'android:versionCode="[^"]*"', regsub_file(r'android:versionCode="[^"]*"',
r'android:versionCode="%s"' % build.versionCode, r'android:versionCode="%s"' % build.versionCode,
path) path)
elif has_extension(path, 'gradle'): elif path.endswith('.gradle'):
regsub_file(r'versionCode[ =]+[0-9]+', regsub_file(r'versionCode[ =]+[0-9]+',
r'versionCode %s' % build.versionCode, r'versionCode %s' % build.versionCode,
path) path)