import_subcommand.py: misc fixes and updates

This commit is contained in:
linsui 2024-09-16 19:43:29 +08:00
parent faac9b38c8
commit 8648954f19

View file

@ -50,14 +50,9 @@ APPLICATION_ID_REGEX = re.compile(r'''\s*applicationId\s=?\s?['"].*['"]''')
def get_all_gradle_and_manifests(build_dir): def get_all_gradle_and_manifests(build_dir):
paths = [] paths = []
# TODO: Python3.6: Accepts a path-like object. for root, dirs, files in os.walk(build_dir):
for root, dirs, files in os.walk(str(build_dir)):
for f in sorted(files): for f in sorted(files):
if ( if f == 'AndroidManifest.xml' or f.endswith(('.gradle', '.gradle.kts')):
f == 'AndroidManifest.xml'
or f.endswith('.gradle')
or f.endswith('.gradle.kts')
):
full = Path(root) / f full = Path(root) / f
paths.append(full) paths.append(full)
return paths return paths
@ -73,7 +68,7 @@ def get_gradle_subdir(build_dir, paths):
for m in GRADLE_SUBPROJECT_REGEX.finditer(path.read_text(encoding='utf-8')): for m in GRADLE_SUBPROJECT_REGEX.finditer(path.read_text(encoding='utf-8')):
for f in (path.parent / m.group(1)).glob('build.gradle*'): for f in (path.parent / m.group(1)).glob('build.gradle*'):
with f.open(encoding='utf-8') as fp: with f.open(encoding='utf-8') as fp:
for line in fp.readlines(): for line in fp:
if common.ANDROID_PLUGIN_REGEX.match( if common.ANDROID_PLUGIN_REGEX.match(
line line
) or APPLICATION_ID_REGEX.match(line): ) or APPLICATION_ID_REGEX.match(line):
@ -81,8 +76,6 @@ def get_gradle_subdir(build_dir, paths):
if first_gradle_dir and first_gradle_dir != Path('.'): if first_gradle_dir and first_gradle_dir != Path('.'):
return first_gradle_dir return first_gradle_dir
return
def handle_retree_error_on_windows(function, path, excinfo): def handle_retree_error_on_windows(function, path, excinfo):
"""Python can't remove a readonly file on Windows so chmod first.""" """Python can't remove a readonly file on Windows so chmod first."""
@ -137,6 +130,7 @@ def getrepofrompage(url: str) -> tuple[Optional[str], str]:
The found repository type or None if an error occured. The found repository type or None if an error occured.
address_or_reason address_or_reason
The address to the found repository or the reason if an error occured. The address to the found repository or the reason if an error occured.
""" """
if not url.startswith('http'): if not url.startswith('http'):
return (None, _('{url} does not start with "http"!'.format(url=url))) return (None, _('{url} does not start with "http"!'.format(url=url)))
@ -205,6 +199,7 @@ def get_app_from_url(url: str) -> metadata.App:
If the VCS type could not be determined. If the VCS type could not be determined.
:exc:`ValueError` :exc:`ValueError`
If the URL is invalid. If the URL is invalid.
""" """
parsed = urllib.parse.urlparse(url) parsed = urllib.parse.urlparse(url)
invalid_url = False invalid_url = False
@ -280,33 +275,25 @@ def main():
# Parse command line... # Parse command line...
parser = ArgumentParser() parser = ArgumentParser()
common.setup_global_opts(parser) common.setup_global_opts(parser)
parser.add_argument( parser.add_argument("-u", "--url", help=_("Project URL to import from."))
"-u", "--url", default=None, help=_("Project URL to import from.")
)
parser.add_argument( parser.add_argument(
"-s", "-s",
"--subdir", "--subdir",
default=None,
help=_("Path to main Android project subdirectory, if not in root."), help=_("Path to main Android project subdirectory, if not in root."),
) )
parser.add_argument( parser.add_argument(
"-c", "-c",
"--categories", "--categories",
default=None,
help=_("Comma separated list of categories."), help=_("Comma separated list of categories."),
) )
parser.add_argument( parser.add_argument("-l", "--license", help=_("Overall license of the project."))
"-l", "--license", default=None, help=_("Overall license of the project.")
)
parser.add_argument( parser.add_argument(
"--omit-disable", "--omit-disable",
action="store_true", action="store_true",
default=False,
help=_("Do not add 'disable:' to the generated build entries"), help=_("Do not add 'disable:' to the generated build entries"),
) )
parser.add_argument( parser.add_argument(
"--rev", "--rev",
default=None,
help=_( help=_(
"Allows a different revision (or git branch) to be specified for the initial import" "Allows a different revision (or git branch) to be specified for the initial import"
), ),
@ -329,21 +316,15 @@ def main():
) )
build = metadata.Build() build = metadata.Build()
app = metadata.App()
if options.url is None and Path('.git').is_dir(): if options.url is None and Path('.git').is_dir():
app = metadata.App()
app.AutoName = Path.cwd().name
app.RepoType = 'git' app.RepoType = 'git'
tmp_importer_dir = Path.cwd()
if Path('build.gradle').exists() or Path('build.gradle.kts').exists(): git_repo = git.Repo(tmp_importer_dir)
build.gradle = ['yes']
git_repo = git.Repo(Path.cwd())
for remote in git.Remote.iter_items(git_repo): for remote in git.Remote.iter_items(git_repo):
if remote.name == 'origin': if remote.name == 'origin':
url = git_repo.remotes.origin.url url = git_repo.remotes.origin.url
if url.startswith('https://git'): # github, gitlab app = get_app_from_url(url)
app.SourceCode = url.rstrip('.git')
app.Repo = url
break break
write_local_file = True write_local_file = True
elif options.url: elif options.url:
@ -359,6 +340,7 @@ def main():
else: else:
raise FDroidException("Specify project url.") raise FDroidException("Specify project url.")
app.AutoUpdateMode = 'Version'
app.UpdateCheckMode = 'Tags' app.UpdateCheckMode = 'Tags'
build.commit = common.get_head_commit_id(git_repo) build.commit = common.get_head_commit_id(git_repo)
@ -382,16 +364,15 @@ def main():
# Create a build line... # Create a build line...
build.versionName = versionName or 'Unknown' build.versionName = versionName or 'Unknown'
app.CurrentVersion = build.versionName
build.versionCode = versionCode or 0 build.versionCode = versionCode or 0
app.CurrentVersionCode = build.versionCode
if options.subdir: if options.subdir:
build.subdir = options.subdir build.subdir = options.subdir
build.gradle = ['yes'] elif gradle_subdir:
elif subdir: build.subdir = gradle_subdir.as_posix()
build.subdir = subdir.as_posix() # subdir might be None
build.gradle = ['yes'] subdir = Path(tmp_importer_dir / build.subdir) if build.subdir else tmp_importer_dir
else:
# subdir might be None
subdir = Path()
if options.license: if options.license:
app.License = options.license app.License = options.license
@ -399,23 +380,23 @@ def main():
app.Categories = options.categories.split(',') app.Categories = options.categories.split(',')
if (subdir / 'jni').exists(): if (subdir / 'jni').exists():
build.buildjni = ['yes'] build.buildjni = ['yes']
if (subdir / 'build.gradle').exists() or (subdir / 'build.gradle').exists(): if (subdir / 'build.gradle').exists() or (subdir / 'build.gradle.kts').exists():
build.gradle = ['yes'] build.gradle = ['yes']
app.AutoName = common.fetch_real_name(subdir, build.gradle)
package_json = tmp_importer_dir / 'package.json' # react-native package_json = tmp_importer_dir / 'package.json' # react-native
pubspec_yaml = tmp_importer_dir / 'pubspec.yaml' # flutter pubspec_yaml = tmp_importer_dir / 'pubspec.yaml' # flutter
if package_json.exists(): if package_json.exists():
build.sudo = [ build.sudo = [
'sysctl fs.inotify.max_user_watches=524288 || true', 'sysctl fs.inotify.max_user_watches=524288 || true',
'curl -Lo node.tar.gz https://nodejs.org/download/release/v19.3.0/node-v19.3.0-linux-x64.tar.gz', 'apt-get update',
'echo "b525028ae5bb71b5b32cb7fce903ccce261dbfef4c7dd0f3e0ffc27cd6fc0b3f node.tar.gz" | sha256sum -c -', 'apt-get install -y npm',
'tar xzf node.tar.gz --strip-components=1 -C /usr/local/',
'npm -g install yarn',
] ]
build.init = ['npm install --build-from-source'] build.init = ['npm install --build-from-source']
with package_json.open() as fp: with package_json.open() as fp:
data = json.load(fp) data = json.load(fp)
app.AutoName = data.get('name', app.AutoName) app.AutoName = app.AutoName or data.get('name')
app.License = data.get('license', app.License) app.License = data.get('license', app.License)
app.Description = data.get('description', app.Description) app.Description = data.get('description', app.Description)
app.WebSite = data.get('homepage', app.WebSite) app.WebSite = data.get('homepage', app.WebSite)
@ -425,11 +406,11 @@ def main():
if app_json.exists(): if app_json.exists():
with app_json.open() as fp: with app_json.open() as fp:
data = json.load(fp) data = json.load(fp)
app.AutoName = data.get('name', app.AutoName) app.AutoName = app.AutoName or data.get('name')
if pubspec_yaml.exists(): if pubspec_yaml.exists():
with pubspec_yaml.open() as fp: with pubspec_yaml.open() as fp:
data = yaml.load(fp, Loader=SafeLoader) data = yaml.load(fp, Loader=SafeLoader)
app.AutoName = data.get('name', app.AutoName) app.AutoName = app.AutoName or data.get('name')
app.License = data.get('license', app.License) app.License = data.get('license', app.License)
app.Description = data.get('description', app.Description) app.Description = data.get('description', app.Description)
app.UpdateCheckData = 'pubspec.yaml|version:\\s.+\\+(\\d+)|.|version:\\s(.+)\\+' app.UpdateCheckData = 'pubspec.yaml|version:\\s.+\\+(\\d+)|.|version:\\s(.+)\\+'