import: make it work most of the time with git repos

This includes real tests too.
This commit is contained in:
Hans-Christoph Steiner 2020-01-31 23:49:50 +01:00
parent e049a120f8
commit bfe587979d
28 changed files with 1184 additions and 42 deletions

View file

@ -18,12 +18,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import binascii
import glob
import os
import re
import shutil
import urllib.request
from argparse import ArgumentParser
from configparser import ConfigParser
import logging
from . import _
@ -31,8 +31,9 @@ from . import common
from . import metadata
from .exception import FDroidException
SETTINGS_GRADLE = re.compile(r'''include\s+['"]:([^'"]*)['"]''')
SETTINGS_GRADLE = re.compile(r'settings\.gradle(?:\.kts)?')
GRADLE_SUBPROJECT = re.compile(r'''['"]:([^'"]+)['"]''')
ANDROID_PLUGIN = re.compile(r'''\s*(:?apply plugin:|id)\(?\s*['"](android|com\.android\.application)['"]\s*\)?''')
# Get the repo type and address from the given web page. The page is scanned
@ -168,32 +169,47 @@ def get_metadata_from_url(app, url):
shutil.rmtree(build_dir)
vcs = common.getvcs(repotype, repo, build_dir)
vcs.gotorevision(options.rev)
root_dir = get_subdir(build_dir)
app.RepoType = repotype
app.Repo = repo
return root_dir, build_dir
config = None
options = None
def get_subdir(build_dir):
if options.subdir:
return os.path.join(build_dir, options.subdir)
settings_gradle = os.path.join(build_dir, 'settings.gradle')
if os.path.exists(settings_gradle):
with open(settings_gradle) as fp:
m = SETTINGS_GRADLE.search(fp.read())
if m:
return os.path.join(build_dir, m.group(1))
return build_dir
def get_all_gradle_and_manifests(build_dir):
paths = []
for root, dirs, files in os.walk(build_dir):
for f in sorted(files):
if f == 'AndroidManifest.xml' \
or f.endswith('.gradle') or f.endswith('.gradle.kts'):
full = os.path.join(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 = os.path.relpath(os.path.dirname(path), build_dir)
if os.path.exists(path) and SETTINGS_GRADLE.match(os.path.basename(path)):
with open(path) as fp:
for m in GRADLE_SUBPROJECT.finditer(fp.read()):
for f in glob.glob(os.path.join(os.path.dirname(path), m.group(1), 'build.gradle*')):
with open(f) as fp:
while True:
line = fp.readline()
if not line:
break
if ANDROID_PLUGIN.match(line):
return os.path.relpath(os.path.dirname(f), build_dir)
if first_gradle_dir and first_gradle_dir != '.':
return first_gradle_dir
return ''
def main():
global config, options
@ -221,7 +237,6 @@ def main():
app = metadata.App()
app.UpdateCheckMode = "Tags"
root_dir = None
build_dir = None
local_metadata_files = common.get_local_metadata_files()
@ -233,12 +248,11 @@ def main():
app.AutoName = os.path.basename(os.getcwd())
app.RepoType = 'git'
root_dir = get_subdir(os.getcwd())
if os.path.exists('build.gradle'):
build.gradle = ['yes']
import git
repo = git.repo.Repo(root_dir) # git repo
repo = git.repo.Repo(os.getcwd()) # git repo
for remote in git.Remote.iter_items(repo):
if remote.name == 'origin':
url = repo.remotes.origin.url
@ -250,7 +264,7 @@ def main():
build.commit = binascii.hexlify(bytearray(repo.head.commit.binsha))
write_local_file = True
elif options.url:
root_dir, build_dir = get_metadata_from_url(app, options.url)
build_dir = get_metadata_from_url(app, options.url)
build.commit = '?'
build.disable = 'Generated by import.py - check/set version fields and commit id'
write_local_file = False
@ -258,9 +272,9 @@ def main():
raise FDroidException("Specify project url.")
# Extract some information...
paths = common.manifest_paths(root_dir, [])
paths = get_all_gradle_and_manifests(build_dir)
subdir = get_gradle_subdir(build_dir, paths)
if paths:
versionName, versionCode, package = common.parse_androidmanifests(paths, app)
if not package:
raise FDroidException(_("Couldn't find package ID"))
@ -269,17 +283,7 @@ def main():
if not versionCode:
logging.warn(_("Couldn't find latest version code"))
else:
spec = os.path.join(root_dir, 'buildozer.spec')
if os.path.exists(spec):
defaults = {'orientation': 'landscape', 'icon': '',
'permissions': '', 'android.api': "18"}
bconfig = ConfigParser(defaults, allow_no_value=True)
bconfig.read(spec)
package = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
versionName = bconfig.get('app', 'version')
versionCode = None
else:
raise FDroidException(_("No android or kivy project could be found. Specify --subdir?"))
raise FDroidException(_("No gradle project could be found. Specify --subdir?"))
# Make sure it's actually new...
if package in apps:
@ -290,13 +294,16 @@ def main():
build.versionCode = versionCode or '0' # TODO heinous but this is still a str
if options.subdir:
build.subdir = options.subdir
elif subdir:
build.subdir = subdir
if options.license:
app.License = options.license
if options.categories:
app.Categories = options.categories.split(',')
if os.path.exists(os.path.join(root_dir, 'jni')):
if os.path.exists(os.path.join(subdir, 'jni')):
build.buildjni = ['yes']
if os.path.exists(os.path.join(root_dir, 'build.gradle')):
if os.path.exists(os.path.join(subdir, 'build.gradle')):
build.gradle = ['yes']
metadata.post_metadata_parse(app)