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
28 import common, metadata
30 # Get the repo type and address from the given web page. The page is scanned
31 # in a rather naive manner for 'git clone xxxx', 'hg clone xxxx', etc, and
32 # when one of these is found it's assumed that's the information we want.
33 # Returns repotype, address, or None, reason
34 def getrepofrompage(url):
36 req = urllib.urlopen(url)
37 if req.getcode() != 200:
38 return (None, 'Unable to get ' + url + ' - return code ' + str(req.getcode()))
41 # Works for Google Code and BitBucket...
42 index = page.find('hg clone')
45 repo = page[index + 9:]
46 index = repo.find('<')
48 return (None, "Error while getting repo address")
50 repo = repo.split('"')[0]
51 return (repotype, repo)
53 # Works for Google Code and BitBucket...
54 index=page.find('git clone')
57 repo = page[index + 10:]
58 index = repo.find('<')
60 return (None, "Error while getting repo address")
62 repo = repo.split('"')[0]
63 return (repotype, repo)
66 index=page.find('svn checkout')
69 repo = page[index + 13:]
70 prefix = '<strong><em>http</em></strong>'
71 if not repo.startswith(prefix):
72 return (None, "Unexpected checkout instructions format")
73 repo = 'http' + repo[len(prefix):]
74 index = repo.find('<')
76 return (None, "Error while getting repo address - no end tag? '" + repo + "'")
79 index = repo.find(' ')
81 return (None, "Error while getting repo address - no space? '" + repo + "'")
83 repo = repo.split('"')[0]
84 return (repotype, repo)
86 return (None, "No information found." + page)
93 global config, options
95 # Parse command line...
96 parser = OptionParser()
97 parser.add_option("-v", "--verbose", action="store_true", default=False,
98 help="Spew out even more information than normal")
99 parser.add_option("-q", "--quiet", action="store_true", default=False,
100 help="Restrict output to warnings and errors")
101 parser.add_option("-u", "--url", default=None,
102 help="Project URL to import from.")
103 parser.add_option("-s", "--subdir", default=None,
104 help="Path to main android project subdirectory, if not in root.")
105 parser.add_option("-r", "--repo", default=None,
106 help="Allows a different repo to be specified for a multi-repo google code project")
107 parser.add_option("--rev", default=None,
108 help="Allows a different revision (or git branch) to be specified for the initial import")
109 (options, args) = parser.parse_args()
111 config = common.read_config(options)
114 logging.info("Specify project url.")
119 if not os.path.isdir(tmp_dir):
120 logging.info("Creating temporary directory")
124 apps = metadata.read_metadata()
126 # Figure out what kind of project it is...
130 website = url #by default, we might override it
131 if url.startswith('git://'):
137 elif url.startswith('https://github.com'):
138 if url.endswith('/'):
140 if url.endswith('.git'):
141 logging.info("A github URL should point to the project, not the git repo")
143 projecttype = 'github'
147 issuetracker = url + '/issues'
148 elif url.startswith('https://gitorious.org/'):
149 projecttype = 'gitorious'
150 repo = 'https://git.gitorious.org/' + url[22:] + '.git'
153 elif url.startswith('https://bitbucket.org/'):
154 if url.endswith('/'):
156 projecttype = 'bitbucket'
157 sourcecode = url + '/src'
158 issuetracker = url + '/issues'
159 # Figure out the repo type and adddress...
160 repotype, repo = getrepofrompage(sourcecode)
162 logging.info("Unable to determine vcs type. " + repo)
164 elif url.startswith('http://code.google.com/p/'):
165 if not url.endswith('/'):
167 projecttype = 'googlecode'
168 sourcecode = url + 'source/checkout'
170 sourcecode += "?repo=" + options.repo
171 issuetracker = url + 'issues/list'
173 # Figure out the repo type and adddress...
174 repotype, repo = getrepofrompage(sourcecode)
176 logging.info("Unable to determine vcs type. " + repo)
179 # Figure out the license...
180 req = urllib.urlopen(url)
181 if req.getcode() != 200:
182 logging.info('Unable to find project page at ' + sourcecode + ' - return code ' + str(req.getcode()))
185 index = page.find('Code license')
187 logging.info("Couldn't find license data")
190 lprefix = 'rel="nofollow">'
191 index = ltext.find(lprefix)
193 logging.info("Couldn't find license text")
195 ltext = ltext[index + len(lprefix):]
196 index = ltext.find('<')
198 logging.info("License text not formatted as expected")
200 ltext = ltext[:index]
201 if ltext == 'GNU GPL v3':
203 elif ltext == 'GNU GPL v2':
205 elif ltext == 'Apache License 2.0':
207 elif ltext == 'MIT License':
209 elif ltext == 'GNU Lesser GPL':
211 elif ltext == 'Mozilla Public License 1.1':
213 elif ltext == 'New BSD License':
216 logging.info("License " + ltext + " is not recognised")
220 logging.info("Unable to determine the project type.")
221 logging.info("The URL you supplied was not in one of the supported formats. Please consult")
222 logging.info("the manual for a list of supported formats, and supply one of those.")
225 # Get a copy of the source so we can extract some info...
226 logging.info('Getting source from ' + repotype + ' repo at ' + repo)
227 src_dir = os.path.join(tmp_dir, 'importer')
228 if os.path.exists(src_dir):
229 shutil.rmtree(src_dir)
230 vcs = common.getvcs(repotype, repo, src_dir)
231 vcs.gotorevision(options.rev)
233 root_dir = os.path.join(src_dir, options.subdir)
237 # Extract some information...
238 paths = common.manifest_paths(root_dir, None)
241 version, vercode, package = common.parse_androidmanifests(paths)
243 logging.info("Couldn't find package ID")
246 logging.info("WARNING: Couldn't find latest version name")
248 logging.info("WARNING: Couldn't find latest version code")
250 spec = os.path.join(root_dir, 'buildozer.spec')
251 if os.path.exists(spec):
252 defaults = {'orientation': 'landscape', 'icon': '',
253 'permissions': '', 'android.api': "18"}
254 bconfig = ConfigParser(defaults, allow_no_value=True)
256 package = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
257 version = bconfig.get('app', 'version')
260 logging.info("No android or kivy project could be found. Specify --subdir?")
263 # Make sure it's actually new...
265 if app['id'] == package:
266 logging.info("Package " + package + " already exists")
269 # Construct the metadata...
270 app = metadata.parse_metadata(None)
272 app['Web Site'] = website
273 app['Source Code'] = sourcecode
275 app['Issue Tracker'] = issuetracker
277 app['License'] = license
278 app['Repo Type'] = repotype
280 app['Update Check Mode'] = "Tags"
282 # Create a build line...
284 build['version'] = version if version else '?'
285 build['vercode'] = vercode if vercode else '?'
286 build['commit'] = '?'
287 build['disable'] = 'Generated by import.py - check/set version fields and commit id'
289 build['subdir'] = options.subdir
290 if os.path.exists(os.path.join(root_dir, 'jni')):
291 build['buildjni'] = ['yes']
292 app['builds'].append(build)
294 # Keep the repo directory to save bandwidth...
295 if not os.path.exists('build'):
297 shutil.move(src_dir, os.path.join('build', package))
298 with open('build/.fdroidvcs-' + package, 'w') as f:
299 f.write(repotype + ' ' + repo)
301 metafile = os.path.join('metadata', package + '.txt')
302 metadata.write_metadata(metafile, app)
303 logging.info("Wrote " + metafile)
306 if __name__ == "__main__":