chiark / gitweb /
Open metadata files in only one place
[fdroidserver.git] / fdroidserver / import.py
index e055dd70dba0debc9d843fbce405fd04f3ba0d45..e1d69c3c3eb91f63bcbf65469775d74ff8404735 100644 (file)
@@ -22,7 +22,7 @@ import sys
 import os
 import shutil
 import urllib
-from optparse import OptionParser
+from argparse import ArgumentParser
 from ConfigParser import ConfigParser
 import logging
 import common
@@ -40,7 +40,7 @@ def getrepofrompage(url):
         return (None, 'Unable to get ' + url + ' - return code ' + str(req.getcode()))
     page = req.read()
 
-    # Works for Google Code and BitBucket...
+    # Works for BitBucket
     index = page.find('hg clone')
     if index != -1:
         repotype = 'hg'
@@ -52,7 +52,7 @@ def getrepofrompage(url):
         repo = repo.split('"')[0]
         return (repotype, repo)
 
-    # Works for Google Code and BitBucket...
+    # Works for BitBucket
     index = page.find('git clone')
     if index != -1:
         repotype = 'git'
@@ -64,188 +64,139 @@ def getrepofrompage(url):
         repo = repo.split('"')[0]
         return (repotype, repo)
 
-    # Google Code only...
-    index = page.find('svn checkout')
-    if index != -1:
-        repotype = 'git-svn'
-        repo = page[index + 13:]
-        prefix = '<strong><em>http</em></strong>'
-        if not repo.startswith(prefix):
-            return (None, "Unexpected checkout instructions format")
-        repo = 'http' + repo[len(prefix):]
-        index = repo.find('<')
-        if index == -1:
-            return (None, "Error while getting repo address - no end tag? '" + repo + "'")
-            sys.exit(1)
-        repo = repo[:index]
-        index = repo.find(' ')
-        if index == -1:
-            return (None, "Error while getting repo address - no space? '" + repo + "'")
-        repo = repo[:index]
-        repo = repo.split('"')[0]
-        return (repotype, repo)
-
     return (None, "No information found." + page)
 
 config = None
 options = None
 
 
-def main():
-
-    global config, options
-
-    # Parse command line...
-    parser = OptionParser()
-    parser.add_option("-v", "--verbose", action="store_true", default=False,
-                      help="Spew out even more information than normal")
-    parser.add_option("-q", "--quiet", action="store_true", default=False,
-                      help="Restrict output to warnings and errors")
-    parser.add_option("-u", "--url", default=None,
-                      help="Project URL to import from.")
-    parser.add_option("-s", "--subdir", default=None,
-                      help="Path to main android project subdirectory, if not in root.")
-    parser.add_option("-r", "--repo", default=None,
-                      help="Allows a different repo to be specified for a multi-repo google code project")
-    parser.add_option("--rev", default=None,
-                      help="Allows a different revision (or git branch) to be specified for the initial import")
-    (options, args) = parser.parse_args()
-
-    config = common.read_config(options)
-
-    if not options.url:
-        logging.info("Specify project url.")
-        sys.exit(1)
-    url = options.url
+def get_metadata_from_url(app, url):
 
     tmp_dir = 'tmp'
     if not os.path.isdir(tmp_dir):
         logging.info("Creating temporary directory")
         os.makedirs(tmp_dir)
 
-    # Get all apps...
-    apps = metadata.read_metadata()
-
     # Figure out what kind of project it is...
     projecttype = None
-    issuetracker = None
-    license = None
-    website = url  # by default, we might override it
+    app.WebSite = url  # by default, we might override it
     if url.startswith('git://'):
         projecttype = 'git'
         repo = url
         repotype = 'git'
-        sourcecode = ""
-        website = ""
+        app.SourceCode = ""
+        app.WebSite = ""
     elif url.startswith('https://github.com'):
         projecttype = 'github'
         repo = url
         repotype = 'git'
-        sourcecode = url
-        issuetracker = url + '/issues'
+        app.SourceCode = url
+        app.IssueTracker = url + '/issues'
+        app.WebSite = ""
     elif url.startswith('https://gitlab.com/'):
         projecttype = 'gitlab'
-        repo = url
-        repotype = 'git'
-        sourcecode = url
-        issuetracker = url + '/issues'
-    elif url.startswith('https://gitorious.org/'):
-        projecttype = 'gitorious'
-        repo = 'https://git.gitorious.org/' + url[22:] + '.git'
+        # git can be fussy with gitlab URLs unless they end in .git
+        if url.endswith('.git'):
+            repo = url
+        else:
+            repo = url + '.git'
         repotype = 'git'
-        sourcecode = url
+        app.SourceCode = url + '/tree/HEAD'
+        app.IssueTracker = url + '/issues'
     elif url.startswith('https://bitbucket.org/'):
         if url.endswith('/'):
             url = url[:-1]
         projecttype = 'bitbucket'
-        sourcecode = url + '/src'
-        issuetracker = url + '/issues'
-        # Figure out the repo type and adddress...
-        repotype, repo = getrepofrompage(sourcecode)
-        if not repotype:
-            logging.info("Unable to determine vcs type. " + repo)
-            sys.exit(1)
-    elif (url.startswith('http://code.google.com/p/') or
-            url.startswith('https://code.google.com/p/')):
-        if not url.endswith('/'):
-            url += '/'
-        projecttype = 'googlecode'
-        sourcecode = url + 'source/checkout'
-        if options.repo:
-            sourcecode += "?repo=" + options.repo
-        issuetracker = url + 'issues/list'
-
+        app.SourceCode = url + '/src'
+        app.IssueTracker = url + '/issues'
         # Figure out the repo type and adddress...
-        repotype, repo = getrepofrompage(sourcecode)
+        repotype, repo = getrepofrompage(app.SourceCode)
         if not repotype:
-            logging.info("Unable to determine vcs type. " + repo)
-            sys.exit(1)
-
-        # Figure out the license...
-        req = urllib.urlopen(url)
-        if req.getcode() != 200:
-            logging.info('Unable to find project page at ' + sourcecode + ' - return code ' + str(req.getcode()))
-            sys.exit(1)
-        page = req.read()
-        index = page.find('Code license')
-        if index == -1:
-            logging.info("Couldn't find license data")
-            sys.exit(1)
-        ltext = page[index:]
-        lprefix = 'rel="nofollow">'
-        index = ltext.find(lprefix)
-        if index == -1:
-            logging.info("Couldn't find license text")
-            sys.exit(1)
-        ltext = ltext[index + len(lprefix):]
-        index = ltext.find('<')
-        if index == -1:
-            logging.info("License text not formatted as expected")
-            sys.exit(1)
-        ltext = ltext[:index]
-        if ltext == 'GNU GPL v3':
-            license = 'GPLv3'
-        elif ltext == 'GNU GPL v2':
-            license = 'GPLv2'
-        elif ltext == 'Apache License 2.0':
-            license = 'Apache2'
-        elif ltext == 'MIT License':
-            license = 'MIT'
-        elif ltext == 'GNU Lesser GPL':
-            license = 'LGPL'
-        elif ltext == 'Mozilla Public License 1.1':
-            license = 'MPL'
-        elif ltext == 'New BSD License':
-            license = 'NewBSD'
-        else:
-            logging.info("License " + ltext + " is not recognised")
+            logging.error("Unable to determine vcs type. " + repo)
             sys.exit(1)
-
     if not projecttype:
-        logging.info("Unable to determine the project type.")
-        logging.info("The URL you supplied was not in one of the supported formats. Please consult")
-        logging.info("the manual for a list of supported formats, and supply one of those.")
+        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.")
+        sys.exit(1)
+
+    # 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)
 
     # Get a copy of the source so we can extract some info...
     logging.info('Getting source from ' + repotype + ' repo at ' + repo)
-    src_dir = os.path.join(tmp_dir, 'importer')
-    if os.path.exists(src_dir):
-        shutil.rmtree(src_dir)
-    vcs = common.getvcs(repotype, repo, src_dir)
+    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)
+    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:
-        root_dir = os.path.join(src_dir, options.subdir)
+        return os.path.join(build_dir, options.subdir)
+
+    return build_dir
+
+
+def main():
+
+    global config, options
+
+    # Parse command line...
+    parser = ArgumentParser()
+    common.setup_global_opts(parser)
+    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()
+    app = metadata.App()
+    app.UpdateCheckMode = "Tags"
+
+    root_dir = None
+    build_dir = None
+
+    if options.url:
+        root_dir, build_dir = get_metadata_from_url(app, options.url)
+    elif os.path.isdir('.git'):
+        if options.url:
+            app.WebSite = options.url
+        root_dir = get_subdir(os.getcwd())
     else:
-        root_dir = src_dir
+        logging.error("Specify project url.")
+        sys.exit(1)
 
     # Extract some information...
-    paths = common.manifest_paths(root_dir, None)
+    paths = common.manifest_paths(root_dir, [])
     if paths:
 
-        version, vercode, package = common.parse_androidmanifests(paths)
+        version, vercode, package = common.parse_androidmanifests(paths, app)
         if not package:
-            logging.info("Couldn't find package ID")
+            logging.error("Couldn't find package ID")
             sys.exit(1)
         if not version:
             logging.warn("Couldn't find latest version name")
@@ -262,56 +213,39 @@ def main():
             version = bconfig.get('app', 'version')
             vercode = None
         else:
-            logging.info("No android or kivy project could be found. Specify --subdir?")
+            logging.error("No android or kivy project could be found. Specify --subdir?")
             sys.exit(1)
 
     # Make sure it's actually new...
-    for app in apps:
-        if app['id'] == package:
-            logging.info("Package " + package + " already exists")
-            sys.exit(1)
-
-    # Construct the metadata...
-    app = metadata.parse_metadata(None)
-    app['id'] = package
-    app['Web Site'] = website
-    app['Source Code'] = sourcecode
-    if issuetracker:
-        app['Issue Tracker'] = issuetracker
-    if license:
-        app['License'] = license
-    app['Repo Type'] = repotype
-    app['Repo'] = repo
-    app['Update Check Mode'] = "Tags"
+    if package in apps:
+        logging.error("Package " + package + " already exists")
+        sys.exit(1)
 
     # Create a build line...
-    build = {}
-    build['version'] = version if version else '?'
-    build['vercode'] = vercode if vercode else '?'
-    build['commit'] = '?'
-    build['disable'] = 'Generated by import.py - check/set version fields and commit id'
+    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'
     if options.subdir:
-        build['subdir'] = options.subdir
+        build.subdir = options.subdir
     if os.path.exists(os.path.join(root_dir, 'jni')):
-        build['buildjni'] = ['yes']
-
-    for flag, value in metadata.flag_defaults.iteritems():
-        if flag in build:
-            continue
-        build[flag] = value
+        build.buildjni = ['yes']
 
-    app['builds'].append(build)
+    app.builds.append(build)
 
     # Keep the repo directory to save bandwidth...
     if not os.path.exists('build'):
         os.mkdir('build')
-    shutil.move(src_dir, os.path.join('build', package))
+    if build_dir is not None:
+        shutil.move(build_dir, os.path.join('build', package))
     with open('build/.fdroidvcs-' + package, 'w') as f:
-        f.write(repotype + ' ' + repo)
+        f.write(app.RepoType + ' ' + app.Repo)
 
-    metafile = os.path.join('metadata', package + '.txt')
-    metadata.write_metadata(metafile, app)
-    logging.info("Wrote " + metafile)
+    metadatapath = os.path.join('metadata', package + '.txt')
+    with open(metadatapath, 'w') as f:
+        metadata.write_metadata('txt', f, app)
+    logging.info("Wrote " + metadatapath)
 
 
 if __name__ == "__main__":