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

import: split URL parsing from code cloning

This makes things testable and easier to follow.
This commit is contained in:
Hans-Christoph Steiner 2020-02-03 12:39:41 +01:00
parent bfe587979d
commit e9a6c84efd
3 changed files with 29 additions and 24 deletions

View File

@ -89,12 +89,9 @@ config = None
options = None
def get_metadata_from_url(app, url):
def get_app_from_url(url):
tmp_dir = 'tmp'
if not os.path.isdir(tmp_dir):
logging.info(_("Creating temporary directory"))
os.makedirs(tmp_dir)
app = metadata.App()
# Figure out what kind of project it is...
projecttype = None
@ -162,18 +159,25 @@ def get_metadata_from_url(app, url):
or ' ' in repo):
raise FDroidException("Repo address '{0}' does not seem to be valid".format(repo))
# Get a copy of the source so we can extract some info...
logging.info('Getting source from ' + repotype + ' repo at ' + repo)
build_dir = os.path.join(tmp_dir, 'importer')
if os.path.exists(build_dir):
shutil.rmtree(build_dir)
vcs = common.getvcs(repotype, repo, build_dir)
vcs.gotorevision(options.rev)
app.RepoType = repotype
app.Repo = repo
return build_dir
return app
def clone_to_tmp_dir(app):
tmp_dir = 'tmp'
if not os.path.isdir(tmp_dir):
logging.info(_("Creating temporary directory"))
os.makedirs(tmp_dir)
tmp_dir = os.path.join(tmp_dir, 'importer')
if os.path.exists(tmp_dir):
shutil.rmtree(tmp_dir)
vcs = common.getvcs(app.RepoType, app.Repo, tmp_dir)
vcs.gotorevision(options.rev)
return tmp_dir
def get_all_gradle_and_manifests(build_dir):
@ -234,8 +238,7 @@ def main():
config = common.read_config(options)
apps = metadata.read_metadata()
app = metadata.App()
app.UpdateCheckMode = "Tags"
app = None
build_dir = None
@ -245,8 +248,10 @@ def main():
build = metadata.Build()
if options.url is None and os.path.isdir('.git'):
app = metadata.App()
app.AutoName = os.path.basename(os.getcwd())
app.RepoType = 'git'
app.UpdateCheckMode = "Tags"
if os.path.exists('build.gradle'):
build.gradle = ['yes']
@ -264,7 +269,8 @@ def main():
build.commit = binascii.hexlify(bytearray(repo.head.commit.binsha))
write_local_file = True
elif options.url:
build_dir = get_metadata_from_url(app, options.url)
app = get_app_from_url(options.url)
build_dir = clone_to_tmp_dir(app)
build.commit = '?'
build.disable = 'Generated by import.py - check/set version fields and commit id'
write_local_file = False

View File

@ -49,9 +49,8 @@ class ImportTest(unittest.TestCase):
print('Skipping ImportTest!')
return
app = fdroidserver.metadata.App()
app.UpdateCheckMode = "Tags"
build_dir = import_proxy.get_metadata_from_url(app, url)
app = import_proxy.get_app_from_url(url)
import_proxy.clone_to_tmp_dir(app)
self.assertEqual(app.RepoType, 'git')
self.assertEqual(app.WebSite, 'https://gitlab.com/fdroid/ci-test-app')
self.assertEqual(app.Repo, 'https://gitlab.com/fdroid/ci-test-app.git')
@ -87,7 +86,7 @@ class ImportTest(unittest.TestCase):
subdir = import_proxy.get_gradle_subdir(build_dir, paths)
self.assertEqual(subdirs[f], subdir)
def test_get_metadata_from_url(self):
def test_get_app_from_url(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
os.chdir(testdir)
os.mkdir(os.path.join(testdir, 'tmp'))
@ -102,14 +101,13 @@ class ImportTest(unittest.TestCase):
shutil.copytree(os.path.join(self.basedir, 'source-files', appid),
tmp_importer)
app = fdroidserver.metadata.App()
app.UpdateCheckMode = "Tags"
app = import_proxy.get_app_from_url(url)
with mock.patch('fdroidserver.common.getvcs',
lambda a, b, c: fdroidserver.common.vcs(url, testdir)):
with mock.patch('fdroidserver.common.vcs.gotorevision',
lambda s, rev: None):
with mock.patch('shutil.rmtree', lambda a: None):
build_dir = import_proxy.get_metadata_from_url(app, url)
build_dir = import_proxy.clone_to_tmp_dir(app)
self.assertEqual('git', app.RepoType)
self.assertEqual(url, app.Repo)
self.assertEqual(url, app.SourceCode)

View File

@ -18,8 +18,9 @@ class Options:
module = __import__('fdroidserver.import')
for name, obj in inspect.getmembers(module):
if name == 'import':
clone_to_tmp_dir = obj.clone_to_tmp_dir
get_all_gradle_and_manifests = obj.get_all_gradle_and_manifests
get_metadata_from_url = obj.get_metadata_from_url
get_app_from_url = obj.get_app_from_url
get_gradle_subdir = obj.get_gradle_subdir
obj.options = Options()
options = obj.options