chiark / gitweb /
import: split out URL handling into its own function
authorHans-Christoph Steiner <hans@eds.org>
Wed, 5 Aug 2015 18:42:58 +0000 (20:42 +0200)
committerHans-Christoph Steiner <hans@eds.org>
Thu, 10 Sep 2015 09:08:39 +0000 (11:08 +0200)
This is preparation to add other import methods, like checking if the
command was run in a currently checked out git repo.

fdroidserver/import.py
fdroidserver/metadata.py

index 9323f9bdfb93589a953f0b1c8087b266e5db9619..5c6fbe485c5850078823fbbc444888d6e9dd5aeb 100644 (file)
@@ -70,71 +70,43 @@ config = None
 options = None
 
 
-def main():
-
-    global config, options
-
-    # Parse command line...
-    parser = ArgumentParser()
-    parser.add_argument("-v", "--verbose", action="store_true", default=False,
-                        help="Spew out even more information than normal")
-    parser.add_argument("-q", "--quiet", action="store_true", default=False,
-                        help="Restrict output to warnings and errors")
-    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)
-
-    if not options.url:
-        logging.error("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['Web Site'] = url  # by default, we might override it
     if url.startswith('git://'):
         projecttype = 'git'
         repo = url
         repotype = 'git'
-        sourcecode = ""
-        website = ""
+        app['Source Code'] = ""
+        app['Web Site'] = ""
     elif url.startswith('https://github.com'):
         projecttype = 'github'
         repo = url
         repotype = 'git'
-        sourcecode = url
-        issuetracker = url + '/issues'
-        website = ""
+        app['Source Code'] = url
+        app['issuetracker'] = url + '/issues'
+        app['Web Site'] = ""
     elif url.startswith('https://gitlab.com/'):
         projecttype = 'gitlab'
         repo = url
         repotype = 'git'
-        sourcecode = url + '/tree/HEAD'
-        issuetracker = url + '/issues'
+        app['Source Code'] = 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'
+        app['Source Code'] = url + '/src'
+        app['issuetracker'] = url + '/issues'
         # Figure out the repo type and adddress...
-        repotype, repo = getrepofrompage(sourcecode)
+        repotype, repo = getrepofrompage(app['Source Code'])
         if not repotype:
             logging.error("Unable to determine vcs type. " + repo)
             sys.exit(1)
@@ -166,6 +138,49 @@ def main():
     else:
         root_dir = src_dir
 
+    app['Repo Type'] = repotype
+    app['Repo'] = repo
+
+    return root_dir, src_dir
+
+
+config = None
+options = None
+
+
+def main():
+
+    global config, options
+
+    # Parse command line...
+    parser = ArgumentParser()
+    parser.add_argument("-v", "--verbose", action="store_true", default=False,
+                        help="Spew out even more information than normal")
+    parser.add_argument("-q", "--quiet", action="store_true", default=False,
+                        help="Restrict output to warnings and errors")
+    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()
+    package, app = metadata.get_default_app_info_list(apps)
+    app['Update Check Mode'] = "Tags"
+
+    if os.path.isdir('.git'):
+        if options.url:
+            app['Web Site'] = options.url
+    elif options.url:
+        root_dir, src_dir = get_metadata_from_url(app, options.url)
+    else:
+        logging.error("Specify project url.")
+        sys.exit(1)
+
     # Extract some information...
     paths = common.manifest_paths(root_dir, [])
     if paths:
@@ -197,18 +212,6 @@ def main():
         logging.error("Package " + package + " already exists")
         sys.exit(1)
 
-    # Construct the metadata...
-    app = metadata.parse_txt_metadata(None)[1]
-    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"
-
     # Create a build line...
     build = {}
     build['version'] = version or '?'
@@ -230,9 +233,10 @@ def main():
     # 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 src_dir is not None:
+        shutil.move(src_dir, os.path.join('build', package))
     with open('build/.fdroidvcs-' + package, 'w') as f:
-        f.write(repotype + ' ' + repo)
+        f.write(app['Repo Type'] + ' ' + app['Repo'])
 
     metadatapath = os.path.join('metadata', package + '.txt')
     metadata.write_metadata(metadatapath, app)
index 303aab6c706a75b6c07fe26e5e027c8bce376124..2afb590b9cb588494ebefe9d6a740fa6b52be8af 100644 (file)
@@ -579,8 +579,11 @@ def split_list_values(s):
     return [v for v in l if v]
 
 
-def get_default_app_info_list(apps, metadatapath):
-    appid = os.path.splitext(os.path.basename(metadatapath))[0]
+def get_default_app_info_list(apps, metadatapath=None):
+    if metadatapath is None:
+        appid = None
+    else:
+        appid = os.path.splitext(os.path.basename(metadatapath))[0]
     if appid in apps:
         logging.critical("'%s' is a duplicate! '%s' is already provided by '%s'"
                          % (metadatapath, appid, apps[appid]['metadatapath']))