1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-10-03 17:50:11 +02:00

handle file type detection using Pythonic methods

This ditches the custom common.get_extension() for straight core Python
methods.  This should make the code closer to Python conventions.  For
example, pathlib also includes the "." in the extension it returns.
This commit is contained in:
Hans-Christoph Steiner 2020-08-24 16:52:16 +02:00
parent da31120b5a
commit 0b92e60266
4 changed files with 25 additions and 31 deletions

View File

@ -520,15 +520,15 @@ def check_for_unsupported_metadata_files(basedir=""):
if not os.path.exists(f + '.yml'): if not os.path.exists(f + '.yml'):
print(_('"%s/" has no matching metadata file!') % f) print(_('"%s/" has no matching metadata file!') % f)
return_value = True return_value = True
elif os.path.splitext(f)[1][1:] == "yml": elif f.endswith('.yml'):
packageName = os.path.splitext(os.path.basename(f))[0] packageName = os.path.splitext(os.path.basename(f))[0]
if not common.is_valid_package_name(packageName): if not common.is_valid_package_name(packageName):
print('"' + packageName + '" is an invalid package name!\n' print('"' + packageName + '" is an invalid package name!\n'
+ 'https://developer.android.com/studio/build/application-id') + 'https://developer.android.com/studio/build/application-id')
return_value = True return_value = True
else: else:
print('"' + f.replace(basedir, '') print(_('"{path}" is not a supported file format (use: metadata/*.yml)')
+ '" is not a supported file format (use: .yml)') .format(path=f.replace(basedir, '')))
return_value = True return_value = True
return return_value return return_value

View File

@ -927,8 +927,6 @@ def _decode_bool(s):
def parse_metadata(metadatapath, check_vcs=False, refresh=True): def parse_metadata(metadatapath, check_vcs=False, refresh=True):
'''parse metadata file, optionally checking the git repo for metadata first''' '''parse metadata file, optionally checking the git repo for metadata first'''
_ignored, ext = fdroidserver.common.get_extension(metadatapath)
app = App() app = App()
app.metadatapath = metadatapath app.metadatapath = metadatapath
name, _ignored = fdroidserver.common.get_extension(os.path.basename(metadatapath)) name, _ignored = fdroidserver.common.get_extension(os.path.basename(metadatapath))
@ -937,11 +935,11 @@ def parse_metadata(metadatapath, check_vcs=False, refresh=True):
else: else:
app.id = name app.id = name
if ext == 'yml': if metadatapath.endswith('.yml'):
with open(metadatapath, 'r') as mf: with open(metadatapath, 'r') as mf:
parse_yaml_metadata(mf, app) parse_yaml_metadata(mf, app)
else: else:
warn_or_exception(_('Unknown metadata format: {path} (use: .yml)') warn_or_exception(_('Unknown metadata format: {path} (use: *.yml)')
.format(path=metadatapath)) .format(path=metadatapath))
if check_vcs and app.Repo: if check_vcs and app.Repo:
@ -1167,14 +1165,12 @@ build_cont = re.compile(r'^[ \t]')
def write_metadata(metadatapath, app): def write_metadata(metadatapath, app):
_ignored, ext = fdroidserver.common.get_extension(metadatapath) if metadatapath.endswith('.yml'):
if ext == 'yml':
if importlib.util.find_spec('ruamel.yaml'): if importlib.util.find_spec('ruamel.yaml'):
with open(metadatapath, 'w') as mf: with open(metadatapath, 'w') as mf:
return write_yaml(mf, app) return write_yaml(mf, app)
else: else:
raise FDroidException('ruamel.yaml not installed, can not write metadata.') raise FDroidException(_('ruamel.yaml not installed, can not write metadata.'))
warn_or_exception(_('Unknown metadata format: %s') % metadatapath) warn_or_exception(_('Unknown metadata format: %s') % metadatapath)

View File

@ -38,8 +38,7 @@ def proper_format(app):
# read in metadata.py # read in metadata.py
with open(app.metadatapath, 'r') as f: with open(app.metadatapath, 'r') as f:
cur_content = f.read() cur_content = f.read()
_ignored, extension = common.get_extension(app.metadatapath) if app.metadatapath.endswith('.yml'):
if extension == 'yml':
metadata.write_yaml(s, app) metadata.write_yaml(s, app)
content = s.getvalue() content = s.getvalue()
s.close() s.close()
@ -68,12 +67,11 @@ def main():
for appid, app in apps.items(): for appid, app in apps.items():
path = app.metadatapath path = app.metadatapath
base, ext = common.get_extension(path) if path.endswith('.yml'):
if ext != "yml":
logging.info(_("Ignoring {ext} file at '{path}'").format(ext=ext, path=path))
continue
else:
logging.info(_("Rewriting '{appid}'").format(appid=appid)) logging.info(_("Rewriting '{appid}'").format(appid=appid))
else:
logging.warning(_('Cannot rewrite "{path}"').format(path=path))
continue
if options.list: if options.list:
if not proper_format(app): if not proper_format(app):

View File

@ -265,33 +265,33 @@ def scan_source(build_dir, build=metadata.Build()):
continue continue
path_in_build_dir = os.path.relpath(filepath, build_dir) path_in_build_dir = os.path.relpath(filepath, build_dir)
_ignored, ext = common.get_extension(path_in_build_dir) extension = os.path.splitext(path_in_build_dir)[1]
if curfile in ('gradle-wrapper.jar', 'gradlew', 'gradlew.bat'): if curfile in ('gradle-wrapper.jar', 'gradlew', 'gradlew.bat'):
removeproblem(curfile, path_in_build_dir, filepath) removeproblem(curfile, path_in_build_dir, filepath)
elif ext == 'apk': elif extension == '.apk':
removeproblem(_('Android APK file'), path_in_build_dir, filepath) removeproblem(_('Android APK file'), path_in_build_dir, filepath)
elif ext == 'a': elif extension == '.a':
count += handleproblem(_('static library'), path_in_build_dir, filepath) count += handleproblem(_('static library'), path_in_build_dir, filepath)
elif ext == 'aar': elif extension == '.aar':
count += handleproblem(_('Android AAR library'), path_in_build_dir, filepath) count += handleproblem(_('Android AAR library'), path_in_build_dir, filepath)
elif ext == 'class': elif extension == '.class':
count += handleproblem(_('Java compiled class'), path_in_build_dir, filepath) count += handleproblem(_('Java compiled class'), path_in_build_dir, filepath)
elif ext == 'dex': elif extension == '.dex':
count += handleproblem(_('Android DEX code'), path_in_build_dir, filepath) count += handleproblem(_('Android DEX code'), path_in_build_dir, filepath)
elif ext == 'gz': elif extension == '.gz':
count += handleproblem(_('gzip file archive'), path_in_build_dir, filepath) count += handleproblem(_('gzip file archive'), path_in_build_dir, filepath)
elif ext == 'so': elif extension == '.so':
count += handleproblem(_('shared library'), path_in_build_dir, filepath) count += handleproblem(_('shared library'), path_in_build_dir, filepath)
elif ext == 'zip': elif extension == '.zip':
count += handleproblem(_('ZIP file archive'), path_in_build_dir, filepath) count += handleproblem(_('ZIP file archive'), path_in_build_dir, filepath)
elif ext == 'jar': elif extension == '.jar':
for name in suspects_found(curfile): for name in suspects_found(curfile):
count += handleproblem('usual suspect \'%s\'' % name, path_in_build_dir, filepath) count += handleproblem('usual suspect \'%s\'' % name, path_in_build_dir, filepath)
count += handleproblem(_('Java JAR file'), path_in_build_dir, filepath) count += handleproblem(_('Java JAR file'), path_in_build_dir, filepath)
elif ext == 'java': elif extension == '.java':
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
continue continue
with open(filepath, 'r', errors='replace') as f: with open(filepath, 'r', errors='replace') as f:
@ -300,7 +300,7 @@ def scan_source(build_dir, build=metadata.Build()):
count += handleproblem('DexClassLoader', path_in_build_dir, filepath) count += handleproblem('DexClassLoader', path_in_build_dir, filepath)
break break
elif ext == 'gradle': elif extension == '.gradle':
if not os.path.isfile(filepath): if not os.path.isfile(filepath):
continue continue
with open(filepath, 'r', errors='replace') as f: with open(filepath, 'r', errors='replace') as f:
@ -316,7 +316,7 @@ def scan_source(build_dir, build=metadata.Build()):
if not any(r.match(url) for r in allowed_repos): if not any(r.match(url) for r in allowed_repos):
count += handleproblem('unknown maven repo \'%s\'' % url, path_in_build_dir, filepath) count += handleproblem('unknown maven repo \'%s\'' % url, path_in_build_dir, filepath)
elif ext in ['', 'bin', 'out', 'exe']: elif extension in ['', '.bin', '.out', '.exe']:
if is_binary(filepath): if is_binary(filepath):
count += handleproblem('binary', path_in_build_dir, filepath) count += handleproblem('binary', path_in_build_dir, filepath)