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

import_subcommand.py: move functions from common.py

These functions are only used in this file
This commit is contained in:
linsui 2024-09-12 21:47:33 +08:00
parent 5da4e670dd
commit faac9b38c8
4 changed files with 88 additions and 76 deletions

View File

@ -101,9 +101,6 @@ VALID_APPLICATION_ID_REGEX = re.compile(r'''(?:^[a-z_]+(?:\d*[a-zA-Z_]*)*)(?:\.[
re.IGNORECASE)
ANDROID_PLUGIN_REGEX = re.compile(r'''\s*(:?apply plugin:|id)\(?\s*['"](android|com\.android\.application)['"]\s*\)?''')
SETTINGS_GRADLE_REGEX = re.compile(r'settings\.gradle(?:\.kts)?')
GRADLE_SUBPROJECT_REGEX = re.compile(r'''['"]:?([^'"]+)['"]''')
MAX_VERSION_CODE = 0x7fffffff # Java's Integer.MAX_VALUE (2147483647)
XMLNS_ANDROID = '{http://schemas.android.com/apk/res/android}'
@ -2120,37 +2117,6 @@ def is_strict_application_id(name):
and '.' in name
def get_all_gradle_and_manifests(build_dir):
paths = []
# TODO: Python3.6: Accepts a path-like object.
for root, dirs, files in os.walk(str(build_dir)):
for f in sorted(files):
if f == 'AndroidManifest.xml' \
or f.endswith('.gradle') or f.endswith('.gradle.kts'):
full = Path(root) / f
paths.append(full)
return paths
def get_gradle_subdir(build_dir, paths):
"""Get the subdir where the gradle build is based."""
first_gradle_dir = None
for path in paths:
if not first_gradle_dir:
first_gradle_dir = path.parent.relative_to(build_dir)
if path.exists() and SETTINGS_GRADLE_REGEX.match(str(path.name)):
for m in GRADLE_SUBPROJECT_REGEX.finditer(path.read_text(encoding='utf-8')):
for f in (path.parent / m.group(1)).glob('build.gradle*'):
with f.open(encoding='utf-8') as fp:
for line in fp.readlines():
if ANDROID_PLUGIN_REGEX.match(line):
return f.parent.relative_to(build_dir)
if first_gradle_dir and first_gradle_dir != Path('.'):
return first_gradle_dir
return
def parse_srclib_spec(spec):
if type(spec) != str:

View File

@ -43,6 +43,46 @@ from .exception import FDroidException
config = None
SETTINGS_GRADLE_REGEX = re.compile(r'settings\.gradle(?:\.kts)?')
GRADLE_SUBPROJECT_REGEX = re.compile(r'''['"]:?([^'"]+)['"]''')
APPLICATION_ID_REGEX = re.compile(r'''\s*applicationId\s=?\s?['"].*['"]''')
def get_all_gradle_and_manifests(build_dir):
paths = []
# TODO: Python3.6: Accepts a path-like object.
for root, dirs, files in os.walk(str(build_dir)):
for f in sorted(files):
if (
f == 'AndroidManifest.xml'
or f.endswith('.gradle')
or f.endswith('.gradle.kts')
):
full = Path(root) / f
paths.append(full)
return paths
def get_gradle_subdir(build_dir, paths):
"""Get the subdir where the gradle build is based."""
first_gradle_dir = None
for path in paths:
if not first_gradle_dir:
first_gradle_dir = path.parent.relative_to(build_dir)
if path.exists() and SETTINGS_GRADLE_REGEX.match(path.name):
for m in GRADLE_SUBPROJECT_REGEX.finditer(path.read_text(encoding='utf-8')):
for f in (path.parent / m.group(1)).glob('build.gradle*'):
with f.open(encoding='utf-8') as fp:
for line in fp.readlines():
if common.ANDROID_PLUGIN_REGEX.match(
line
) or APPLICATION_ID_REGEX.match(line):
return f.parent.relative_to(build_dir)
if first_gradle_dir and first_gradle_dir != Path('.'):
return first_gradle_dir
return
def handle_retree_error_on_windows(function, path, excinfo):
"""Python can't remove a readonly file on Windows so chmod first."""
@ -323,8 +363,8 @@ def main():
build.commit = common.get_head_commit_id(git_repo)
# Extract some information...
paths = common.get_all_gradle_and_manifests(tmp_importer_dir)
subdir = common.get_gradle_subdir(tmp_importer_dir, paths)
paths = get_all_gradle_and_manifests(tmp_importer_dir)
gradle_subdir = get_gradle_subdir(tmp_importer_dir, paths)
if paths:
versionName, versionCode, appid = common.parse_androidmanifests(paths, app)
if not appid:

View File

@ -1522,45 +1522,6 @@ class CommonTest(unittest.TestCase):
self.assertEqual(('2021-06-30', 34, 'de.varengold.activeTAN'),
fdroidserver.common.parse_androidmanifests(paths, app))
def test_get_all_gradle_and_manifests(self):
"""Test whether the function works with relative and absolute paths"""
a = fdroidserver.common.get_all_gradle_and_manifests(Path('source-files/cn.wildfirechat.chat'))
paths = [
'avenginekit/build.gradle',
'build.gradle',
'chat/build.gradle',
'client/build.gradle',
'client/src/main/AndroidManifest.xml',
'emojilibrary/build.gradle',
'gradle/build_libraries.gradle',
'imagepicker/build.gradle',
'mars-core-release/build.gradle',
'push/build.gradle',
'settings.gradle',
]
paths = [Path('source-files/cn.wildfirechat.chat') / path for path in paths]
self.assertEqual(sorted(paths), sorted(a))
abspath = Path(self.basedir) / 'source-files/realm'
p = fdroidserver.common.get_all_gradle_and_manifests(abspath)
self.assertEqual(1, len(p))
self.assertTrue(p[0].is_relative_to(abspath))
def test_get_gradle_subdir(self):
subdirs = {
'cn.wildfirechat.chat': 'chat',
'com.anpmech.launcher': 'app',
'org.tasks': 'app',
'ut.ewh.audiometrytest': 'app',
'org.noise_planet.noisecapture': 'app',
}
for k, v in subdirs.items():
build_dir = Path('source-files') / k
paths = fdroidserver.common.get_all_gradle_and_manifests(build_dir)
logging.info(paths)
subdir = fdroidserver.common.get_gradle_subdir(build_dir, paths)
self.assertEqual(v, str(subdir))
def test_parse_srclib_spec_good(self):
self.assertEqual(fdroidserver.common.parse_srclib_spec('osmand-external-skia@android/oreo'),
('osmand-external-skia', 'android/oreo', None, None))

View File

@ -42,6 +42,49 @@ class ImportTest(unittest.TestCase):
os.chdir(self.basedir)
self._td.cleanup()
def test_get_all_gradle_and_manifests(self):
"""Test whether the function works with relative and absolute paths"""
a = fdroidserver.import_subcommand.get_all_gradle_and_manifests(
Path('source-files/cn.wildfirechat.chat')
)
paths = [
'avenginekit/build.gradle',
'build.gradle',
'chat/build.gradle',
'client/build.gradle',
'client/src/main/AndroidManifest.xml',
'emojilibrary/build.gradle',
'gradle/build_libraries.gradle',
'imagepicker/build.gradle',
'mars-core-release/build.gradle',
'push/build.gradle',
'settings.gradle',
]
paths = [Path('source-files/cn.wildfirechat.chat') / path for path in paths]
self.assertEqual(sorted(paths), sorted(a))
abspath = Path(self.basedir) / 'source-files/realm'
p = fdroidserver.import_subcommand.get_all_gradle_and_manifests(abspath)
self.assertEqual(1, len(p))
self.assertTrue(p[0].is_relative_to(abspath))
def test_get_gradle_subdir(self):
subdirs = {
'cn.wildfirechat.chat': 'chat',
'com.anpmech.launcher': 'app',
'org.tasks': 'app',
'ut.ewh.audiometrytest': 'app',
'org.noise_planet.noisecapture': 'app',
}
for k, v in subdirs.items():
build_dir = Path('source-files') / k
paths = fdroidserver.import_subcommand.get_all_gradle_and_manifests(
build_dir
)
logging.info(paths)
subdir = fdroidserver.import_subcommand.get_gradle_subdir(build_dir, paths)
self.assertEqual(v, str(subdir))
def test_import_gitlab(self):
with tempfile.TemporaryDirectory() as testdir, TmpCwd(testdir):
# FDroidPopen needs some config to work
@ -107,7 +150,9 @@ class ImportTest(unittest.TestCase):
self.assertEqual(url, app.Repo)
self.assertEqual(url, app.SourceCode)
logging.info(build_dir)
paths = fdroidserver.common.get_all_gradle_and_manifests(build_dir)
paths = fdroidserver.import_subcommand.get_all_gradle_and_manifests(
build_dir
)
self.assertNotEqual(paths, [])
(
versionName,