2016-01-04 16:33:20 +01:00
|
|
|
#!/usr/bin/env python3
|
2012-02-05 11:02:01 +00:00
|
|
|
#
|
|
|
|
# import.py - part of the FDroid server tools
|
2013-10-31 15:37:39 +00:00
|
|
|
# Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
|
2014-01-28 14:07:19 +01:00
|
|
|
# Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
|
2012-02-05 11:02:01 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import shutil
|
2012-02-09 09:06:51 +00:00
|
|
|
import urllib
|
2015-09-04 11:37:05 +02:00
|
|
|
from argparse import ArgumentParser
|
2013-11-24 22:11:29 +00:00
|
|
|
from ConfigParser import ConfigParser
|
2014-01-27 17:04:22 +01:00
|
|
|
import logging
|
2014-05-01 22:36:12 -04:00
|
|
|
import common
|
|
|
|
import metadata
|
2012-08-20 11:34:32 +01:00
|
|
|
|
2014-05-01 23:39:33 -04:00
|
|
|
|
2012-08-20 11:34:32 +01:00
|
|
|
# Get the repo type and address from the given web page. The page is scanned
|
|
|
|
# in a rather naive manner for 'git clone xxxx', 'hg clone xxxx', etc, and
|
|
|
|
# when one of these is found it's assumed that's the information we want.
|
|
|
|
# Returns repotype, address, or None, reason
|
|
|
|
def getrepofrompage(url):
|
|
|
|
|
|
|
|
req = urllib.urlopen(url)
|
|
|
|
if req.getcode() != 200:
|
2013-10-31 15:47:24 +00:00
|
|
|
return (None, 'Unable to get ' + url + ' - return code ' + str(req.getcode()))
|
2012-08-20 11:34:32 +01:00
|
|
|
page = req.read()
|
|
|
|
|
2015-08-18 16:26:12 -07:00
|
|
|
# Works for BitBucket
|
2012-08-20 11:34:32 +01:00
|
|
|
index = page.find('hg clone')
|
|
|
|
if index != -1:
|
|
|
|
repotype = 'hg'
|
|
|
|
repo = page[index + 9:]
|
|
|
|
index = repo.find('<')
|
|
|
|
if index == -1:
|
|
|
|
return (None, "Error while getting repo address")
|
|
|
|
repo = repo[:index]
|
2013-09-16 14:25:39 +02:00
|
|
|
repo = repo.split('"')[0]
|
2012-08-20 11:34:32 +01:00
|
|
|
return (repotype, repo)
|
|
|
|
|
2015-08-18 16:26:12 -07:00
|
|
|
# Works for BitBucket
|
2014-05-01 22:30:44 -04:00
|
|
|
index = page.find('git clone')
|
2012-08-20 11:34:32 +01:00
|
|
|
if index != -1:
|
|
|
|
repotype = 'git'
|
|
|
|
repo = page[index + 10:]
|
|
|
|
index = repo.find('<')
|
|
|
|
if index == -1:
|
|
|
|
return (None, "Error while getting repo address")
|
|
|
|
repo = repo[:index]
|
2013-09-16 14:25:39 +02:00
|
|
|
repo = repo.split('"')[0]
|
2012-08-20 11:34:32 +01:00
|
|
|
return (repotype, repo)
|
|
|
|
|
|
|
|
return (None, "No information found." + page)
|
|
|
|
|
2013-11-01 12:10:57 +01:00
|
|
|
config = None
|
|
|
|
options = None
|
2012-08-20 11:34:32 +01:00
|
|
|
|
2014-05-01 23:39:33 -04:00
|
|
|
|
2015-08-05 20:42:58 +02:00
|
|
|
def get_metadata_from_url(app, url):
|
2012-02-26 14:18:58 +00:00
|
|
|
|
|
|
|
tmp_dir = 'tmp'
|
|
|
|
if not os.path.isdir(tmp_dir):
|
2014-01-27 17:04:22 +01:00
|
|
|
logging.info("Creating temporary directory")
|
2012-02-26 14:18:58 +00:00
|
|
|
os.makedirs(tmp_dir)
|
|
|
|
|
|
|
|
# Figure out what kind of project it is...
|
|
|
|
projecttype = None
|
2015-11-28 13:09:47 +01:00
|
|
|
app.WebSite = url # by default, we might override it
|
2012-04-03 21:13:51 +01:00
|
|
|
if url.startswith('git://'):
|
|
|
|
projecttype = 'git'
|
|
|
|
repo = url
|
|
|
|
repotype = 'git'
|
2015-11-28 13:09:47 +01:00
|
|
|
app.SourceCode = ""
|
|
|
|
app.WebSite = ""
|
2013-08-08 16:47:26 +02:00
|
|
|
elif url.startswith('https://github.com'):
|
2012-02-26 14:18:58 +00:00
|
|
|
projecttype = 'github'
|
2014-06-08 21:36:22 +02:00
|
|
|
repo = url
|
2014-06-01 16:37:33 +02:00
|
|
|
repotype = 'git'
|
2015-11-28 13:09:47 +01:00
|
|
|
app.SourceCode = url
|
|
|
|
app.IssueTracker = url + '/issues'
|
|
|
|
app.WebSite = ""
|
2014-06-01 16:37:33 +02:00
|
|
|
elif url.startswith('https://gitlab.com/'):
|
|
|
|
projecttype = 'gitlab'
|
2015-08-05 21:54:25 +02:00
|
|
|
# git can be fussy with gitlab URLs unless they end in .git
|
|
|
|
if url.endswith('.git'):
|
2016-03-06 11:14:14 +01:00
|
|
|
url = url[:-4]
|
|
|
|
repo = url + '.git'
|
2012-02-26 14:18:58 +00:00
|
|
|
repotype = 'git'
|
2016-03-06 11:14:14 +01:00
|
|
|
app.WebSite = url
|
2015-11-28 13:09:47 +01:00
|
|
|
app.SourceCode = url + '/tree/HEAD'
|
|
|
|
app.IssueTracker = url + '/issues'
|
2012-03-06 19:43:29 +00:00
|
|
|
elif url.startswith('https://bitbucket.org/'):
|
|
|
|
if url.endswith('/'):
|
|
|
|
url = url[:-1]
|
|
|
|
projecttype = 'bitbucket'
|
2015-11-28 13:09:47 +01:00
|
|
|
app.SourceCode = url + '/src'
|
|
|
|
app.IssueTracker = url + '/issues'
|
2012-08-20 11:34:32 +01:00
|
|
|
# Figure out the repo type and adddress...
|
2015-11-28 13:09:47 +01:00
|
|
|
repotype, repo = getrepofrompage(app.SourceCode)
|
2012-08-20 11:34:32 +01:00
|
|
|
if not repotype:
|
2014-07-01 20:32:49 +02:00
|
|
|
logging.error("Unable to determine vcs type. " + repo)
|
2012-08-20 11:34:32 +01:00
|
|
|
sys.exit(1)
|
2012-02-26 14:18:58 +00:00
|
|
|
if not projecttype:
|
2014-07-01 20:32:49 +02:00
|
|
|
logging.error("Unable to determine the project type.")
|
|
|
|
logging.error("The URL you supplied was not in one of the supported formats. Please consult")
|
|
|
|
logging.error("the manual for a list of supported formats, and supply one of those.")
|
2012-02-14 20:37:55 +00:00
|
|
|
sys.exit(1)
|
2012-02-26 14:18:58 +00:00
|
|
|
|
2015-01-31 12:07:33 +00:00
|
|
|
# Ensure we have a sensible-looking repo address at this point. If not, we
|
|
|
|
# might have got a page format we weren't expecting. (Note that we
|
|
|
|
# specifically don't want git@...)
|
|
|
|
if ((repotype != 'bzr' and (not repo.startswith('http://') and
|
|
|
|
not repo.startswith('https://') and
|
|
|
|
not repo.startswith('git://'))) or
|
|
|
|
' ' in repo):
|
|
|
|
logging.error("Repo address '{0}' does not seem to be valid".format(repo))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-26 14:18:58 +00:00
|
|
|
# Get a copy of the source so we can extract some info...
|
2014-01-27 17:04:22 +01:00
|
|
|
logging.info('Getting source from ' + repotype + ' repo at ' + repo)
|
2015-10-25 11:41:46 +01:00
|
|
|
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)
|
2013-10-30 08:05:02 +00:00
|
|
|
vcs.gotorevision(options.rev)
|
2015-10-25 11:41:46 +01:00
|
|
|
root_dir = get_subdir(build_dir)
|
2012-02-26 14:18:58 +00:00
|
|
|
|
2015-11-28 13:09:47 +01:00
|
|
|
app.RepoType = repotype
|
|
|
|
app.Repo = repo
|
2015-08-05 20:42:58 +02:00
|
|
|
|
2015-10-25 11:41:46 +01:00
|
|
|
return root_dir, build_dir
|
2015-08-05 20:42:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
config = None
|
|
|
|
options = None
|
|
|
|
|
|
|
|
|
2015-10-25 11:41:46 +01:00
|
|
|
def get_subdir(build_dir):
|
2015-09-14 17:40:44 -07:00
|
|
|
if options.subdir:
|
2015-10-25 11:41:46 +01:00
|
|
|
return os.path.join(build_dir, options.subdir)
|
2015-09-14 17:40:44 -07:00
|
|
|
|
2015-10-25 11:41:46 +01:00
|
|
|
return build_dir
|
2015-09-14 17:40:44 -07:00
|
|
|
|
|
|
|
|
2015-08-05 20:42:58 +02:00
|
|
|
def main():
|
|
|
|
|
|
|
|
global config, options
|
|
|
|
|
|
|
|
# Parse command line...
|
|
|
|
parser = ArgumentParser()
|
2015-09-11 23:42:50 -07:00
|
|
|
common.setup_global_opts(parser)
|
2015-08-05 20:42:58 +02:00
|
|
|
parser.add_argument("-u", "--url", default=None,
|
|
|
|
help="Project URL to import from.")
|
|
|
|
parser.add_argument("-s", "--subdir", default=None,
|
|
|
|
help="Path to main android project subdirectory, if not in root.")
|
|
|
|
parser.add_argument("--rev", default=None,
|
|
|
|
help="Allows a different revision (or git branch) to be specified for the initial import")
|
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
config = common.read_config(options)
|
|
|
|
|
|
|
|
apps = metadata.read_metadata()
|
2015-12-07 20:31:23 +01:00
|
|
|
app = metadata.App()
|
2015-11-28 13:09:47 +01:00
|
|
|
app.UpdateCheckMode = "Tags"
|
2015-08-05 20:42:58 +02:00
|
|
|
|
2015-09-14 17:40:44 -07:00
|
|
|
root_dir = None
|
2015-10-25 11:41:46 +01:00
|
|
|
build_dir = None
|
2015-09-14 17:40:44 -07:00
|
|
|
|
2015-09-15 14:06:31 -07:00
|
|
|
if options.url:
|
2015-10-25 11:41:46 +01:00
|
|
|
root_dir, build_dir = get_metadata_from_url(app, options.url)
|
2015-09-15 14:06:31 -07:00
|
|
|
elif os.path.isdir('.git'):
|
2015-08-05 20:42:58 +02:00
|
|
|
if options.url:
|
2015-11-28 13:09:47 +01:00
|
|
|
app.WebSite = options.url
|
2015-09-14 17:40:44 -07:00
|
|
|
root_dir = get_subdir(os.getcwd())
|
2015-08-05 20:42:58 +02:00
|
|
|
else:
|
|
|
|
logging.error("Specify project url.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-26 14:18:58 +00:00
|
|
|
# Extract some information...
|
2014-09-20 22:35:58 +02:00
|
|
|
paths = common.manifest_paths(root_dir, [])
|
2013-11-24 22:11:29 +00:00
|
|
|
if paths:
|
2013-11-02 22:52:52 +01:00
|
|
|
|
2015-10-30 19:03:53 +01:00
|
|
|
version, vercode, package = common.parse_androidmanifests(paths, app)
|
2013-11-24 22:11:29 +00:00
|
|
|
if not package:
|
2014-07-01 20:32:49 +02:00
|
|
|
logging.error("Couldn't find package ID")
|
2013-11-24 22:11:29 +00:00
|
|
|
sys.exit(1)
|
|
|
|
if not version:
|
2014-03-18 08:22:36 +01:00
|
|
|
logging.warn("Couldn't find latest version name")
|
2013-11-24 22:11:29 +00:00
|
|
|
if not vercode:
|
2014-03-18 08:22:36 +01:00
|
|
|
logging.warn("Couldn't find latest version code")
|
2013-11-24 22:11:29 +00:00
|
|
|
else:
|
|
|
|
spec = os.path.join(root_dir, 'buildozer.spec')
|
|
|
|
if os.path.exists(spec):
|
2013-12-30 17:04:16 +01:00
|
|
|
defaults = {'orientation': 'landscape', 'icon': '',
|
2014-05-06 13:50:52 -04:00
|
|
|
'permissions': '', 'android.api': "18"}
|
2013-11-24 22:11:29 +00:00
|
|
|
bconfig = ConfigParser(defaults, allow_no_value=True)
|
|
|
|
bconfig.read(spec)
|
|
|
|
package = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
|
|
|
|
version = bconfig.get('app', 'version')
|
|
|
|
vercode = None
|
|
|
|
else:
|
2014-07-01 20:32:49 +02:00
|
|
|
logging.error("No android or kivy project could be found. Specify --subdir?")
|
2013-11-24 22:11:29 +00:00
|
|
|
sys.exit(1)
|
2012-02-05 11:02:01 +00:00
|
|
|
|
2012-02-26 14:18:58 +00:00
|
|
|
# Make sure it's actually new...
|
2014-08-16 12:46:02 +02:00
|
|
|
if package in apps:
|
|
|
|
logging.error("Package " + package + " already exists")
|
|
|
|
sys.exit(1)
|
2012-02-26 14:18:58 +00:00
|
|
|
|
|
|
|
# Create a build line...
|
2015-11-28 17:55:27 +01:00
|
|
|
build = metadata.Build()
|
|
|
|
build.version = version or '?'
|
|
|
|
build.vercode = vercode or '?'
|
|
|
|
build.commit = '?'
|
|
|
|
build.disable = 'Generated by import.py - check/set version fields and commit id'
|
2012-02-26 14:18:58 +00:00
|
|
|
if options.subdir:
|
2015-11-28 17:55:27 +01:00
|
|
|
build.subdir = options.subdir
|
2012-02-26 14:18:58 +00:00
|
|
|
if os.path.exists(os.path.join(root_dir, 'jni')):
|
2015-11-28 17:55:27 +01:00
|
|
|
build.buildjni = ['yes']
|
2014-06-15 12:30:03 +02:00
|
|
|
|
2015-11-28 13:09:47 +01:00
|
|
|
app.builds.append(build)
|
2012-02-26 14:18:58 +00:00
|
|
|
|
2012-08-10 11:53:56 +01:00
|
|
|
# Keep the repo directory to save bandwidth...
|
|
|
|
if not os.path.exists('build'):
|
|
|
|
os.mkdir('build')
|
2015-10-25 11:41:46 +01:00
|
|
|
if build_dir is not None:
|
|
|
|
shutil.move(build_dir, os.path.join('build', package))
|
2013-06-24 10:40:27 +01:00
|
|
|
with open('build/.fdroidvcs-' + package, 'w') as f:
|
2015-11-28 13:09:47 +01:00
|
|
|
f.write(app.RepoType + ' ' + app.Repo)
|
2012-08-10 11:53:56 +01:00
|
|
|
|
2015-08-05 11:37:30 +02:00
|
|
|
metadatapath = os.path.join('metadata', package + '.txt')
|
2015-09-24 22:27:38 -07:00
|
|
|
with open(metadatapath, 'w') as f:
|
2015-10-04 18:01:23 +02:00
|
|
|
metadata.write_metadata('txt', f, app)
|
2015-08-05 11:37:30 +02:00
|
|
|
logging.info("Wrote " + metadatapath)
|
2012-02-26 14:18:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|