2 # -*- coding: utf-8 -*-
4 # import.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013-2014 Daniel Martà <mvdan@mvdan.cc>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from optparse import OptionParser
26 from ConfigParser import ConfigParser
32 # Get the repo type and address from the given web page. The page is scanned
33 # in a rather naive manner for 'git clone xxxx', 'hg clone xxxx', etc, and
34 # when one of these is found it's assumed that's the information we want.
35 # Returns repotype, address, or None, reason
36 def getrepofrompage(url):
38 req = urllib.urlopen(url)
39 if req.getcode() != 200:
40 return (None, 'Unable to get ' + url + ' - return code ' + str(req.getcode()))
43 # Works for Google Code and BitBucket...
44 index = page.find('hg clone')
47 repo = page[index + 9:]
48 index = repo.find('<')
50 return (None, "Error while getting repo address")
52 repo = repo.split('"')[0]
53 return (repotype, repo)
55 # Works for Google Code and BitBucket...
56 index = page.find('git clone')
59 repo = page[index + 10:]
60 index = repo.find('<')
62 return (None, "Error while getting repo address")
64 repo = repo.split('"')[0]
65 return (repotype, repo)
68 index = page.find('svn checkout')
71 repo = page[index + 13:]
72 prefix = '<strong><em>http</em></strong>'
73 if not repo.startswith(prefix):
74 return (None, "Unexpected checkout instructions format")
75 repo = 'http' + repo[len(prefix):]
76 index = repo.find('<')
78 return (None, "Error while getting repo address - no end tag? '" + repo + "'")
81 index = repo.find(' ')
83 return (None, "Error while getting repo address - no space? '" + repo + "'")
85 repo = repo.split('"')[0]
86 return (repotype, repo)
88 return (None, "No information found." + page)
96 global config, options
98 # Parse command line...
99 parser = OptionParser()
100 parser.add_option("-v", "--verbose", action="store_true", default=False,
101 help="Spew out even more information than normal")
102 parser.add_option("-q", "--quiet", action="store_true", default=False,
103 help="Restrict output to warnings and errors")
104 parser.add_option("-u", "--url", default=None,
105 help="Project URL to import from.")
106 parser.add_option("-s", "--subdir", default=None,
107 help="Path to main android project subdirectory, if not in root.")
108 parser.add_option("-r", "--repo", default=None,
109 help="Allows a different repo to be specified for a multi-repo google code project")
110 parser.add_option("--rev", default=None,
111 help="Allows a different revision (or git branch) to be specified for the initial import")
112 (options, args) = parser.parse_args()
114 config = common.read_config(options)
117 logging.info("Specify project url.")
122 if not os.path.isdir(tmp_dir):
123 logging.info("Creating temporary directory")
127 apps = metadata.read_metadata()
129 # Figure out what kind of project it is...
133 website = url #by default, we might override it
134 if url.startswith('git://'):
140 elif url.startswith('https://github.com'):
141 if url.endswith('/'):
143 if url.endswith('.git'):
144 logging.info("A github URL should point to the project, not the git repo")
146 projecttype = 'github'
150 issuetracker = url + '/issues'
151 elif url.startswith('https://gitorious.org/'):
152 projecttype = 'gitorious'
153 repo = 'https://git.gitorious.org/' + url[22:] + '.git'
156 elif url.startswith('https://bitbucket.org/'):
157 if url.endswith('/'):
159 projecttype = 'bitbucket'
160 sourcecode = url + '/src'
161 issuetracker = url + '/issues'
162 # Figure out the repo type and adddress...
163 repotype, repo = getrepofrompage(sourcecode)
165 logging.info("Unable to determine vcs type. " + repo)
167 elif url.startswith('http://code.google.com/p/'):
168 if not url.endswith('/'):
170 projecttype = 'googlecode'
171 sourcecode = url + 'source/checkout'
173 sourcecode += "?repo=" + options.repo
174 issuetracker = url + 'issues/list'
176 # Figure out the repo type and adddress...
177 repotype, repo = getrepofrompage(sourcecode)
179 logging.info("Unable to determine vcs type. " + repo)
182 # Figure out the license...
183 req = urllib.urlopen(url)
184 if req.getcode() != 200:
185 logging.info('Unable to find project page at ' + sourcecode + ' - return code ' + str(req.getcode()))
188 index = page.find('Code license')
190 logging.info("Couldn't find license data")
193 lprefix = 'rel="nofollow">'
194 index = ltext.find(lprefix)
196 logging.info("Couldn't find license text")
198 ltext = ltext[index + len(lprefix):]
199 index = ltext.find('<')
201 logging.info("License text not formatted as expected")
203 ltext = ltext[:index]
204 if ltext == 'GNU GPL v3':
206 elif ltext == 'GNU GPL v2':
208 elif ltext == 'Apache License 2.0':
210 elif ltext == 'MIT License':
212 elif ltext == 'GNU Lesser GPL':
214 elif ltext == 'Mozilla Public License 1.1':
216 elif ltext == 'New BSD License':
219 logging.info("License " + ltext + " is not recognised")
223 logging.info("Unable to determine the project type.")
224 logging.info("The URL you supplied was not in one of the supported formats. Please consult")
225 logging.info("the manual for a list of supported formats, and supply one of those.")
228 # Get a copy of the source so we can extract some info...
229 logging.info('Getting source from ' + repotype + ' repo at ' + repo)
230 src_dir = os.path.join(tmp_dir, 'importer')
231 if os.path.exists(src_dir):
232 shutil.rmtree(src_dir)
233 vcs = common.getvcs(repotype, repo, src_dir)
234 vcs.gotorevision(options.rev)
236 root_dir = os.path.join(src_dir, options.subdir)
240 # Extract some information...
241 paths = common.manifest_paths(root_dir, None)
244 version, vercode, package = common.parse_androidmanifests(paths)
246 logging.info("Couldn't find package ID")
249 logging.warn("Couldn't find latest version name")
251 logging.warn("Couldn't find latest version code")
253 spec = os.path.join(root_dir, 'buildozer.spec')
254 if os.path.exists(spec):
255 defaults = {'orientation': 'landscape', 'icon': '',
256 'permissions': '', 'android.api': "18"}
257 bconfig = ConfigParser(defaults, allow_no_value=True)
259 package = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
260 version = bconfig.get('app', 'version')
263 logging.info("No android or kivy project could be found. Specify --subdir?")
266 # Make sure it's actually new...
268 if app['id'] == package:
269 logging.info("Package " + package + " already exists")
272 # Construct the metadata...
273 app = metadata.parse_metadata(None)
275 app['Web Site'] = website
276 app['Source Code'] = sourcecode
278 app['Issue Tracker'] = issuetracker
280 app['License'] = license
281 app['Repo Type'] = repotype
283 app['Update Check Mode'] = "Tags"
285 # Create a build line...
287 build['version'] = version if version else '?'
288 build['vercode'] = vercode if vercode else '?'
289 build['commit'] = '?'
290 build['disable'] = 'Generated by import.py - check/set version fields and commit id'
292 build['subdir'] = options.subdir
293 if os.path.exists(os.path.join(root_dir, 'jni')):
294 build['buildjni'] = ['yes']
295 app['builds'].append(build)
297 # Keep the repo directory to save bandwidth...
298 if not os.path.exists('build'):
300 shutil.move(src_dir, os.path.join('build', package))
301 with open('build/.fdroidvcs-' + package, 'w') as f:
302 f.write(repotype + ' ' + repo)
304 metafile = os.path.join('metadata', package + '.txt')
305 metadata.write_metadata(metafile, app)
306 logging.info("Wrote " + metafile)
309 if __name__ == "__main__":