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("-u", "--url", default=None,
100 help="Project URL to import from.")
101 parser.add_option("-s", "--subdir", default=None,
102 help="Path to main android project subdirectory, if not in root.")
103 parser.add_option("-r", "--repo", default=None,
104 help="Allows a different repo to be specified for a multi-repo google code project")
105 parser.add_option("--rev", default=None,
106 help="Allows a different revision (or git branch) to be specified for the initial import")
107 (options, args) = parser.parse_args()
109 config = common.read_config(options)
112 logging.info("Specify project url.")
117 if not os.path.isdir(tmp_dir):
118 logging.info("Creating temporary directory")
122 apps = metadata.read_metadata()
124 # Figure out what kind of project it is...
128 website = url #by default, we might override it
129 if url.startswith('git://'):
135 elif url.startswith('https://github.com'):
136 if url.endswith('/'):
138 if url.endswith('.git'):
139 logging.info("A github URL should point to the project, not the git repo")
141 projecttype = 'github'
145 issuetracker = url + '/issues'
146 elif url.startswith('https://gitorious.org/'):
147 projecttype = 'gitorious'
148 repo = 'https://git.gitorious.org/' + url[22:] + '.git'
151 elif url.startswith('https://bitbucket.org/'):
152 if url.endswith('/'):
154 projecttype = 'bitbucket'
155 sourcecode = url + '/src'
156 issuetracker = url + '/issues'
157 # Figure out the repo type and adddress...
158 repotype, repo = getrepofrompage(sourcecode)
160 logging.info("Unable to determine vcs type. " + repo)
162 elif url.startswith('http://code.google.com/p/'):
163 if not url.endswith('/'):
165 projecttype = 'googlecode'
166 sourcecode = url + 'source/checkout'
168 sourcecode += "?repo=" + options.repo
169 issuetracker = url + 'issues/list'
171 # Figure out the repo type and adddress...
172 repotype, repo = getrepofrompage(sourcecode)
174 logging.info("Unable to determine vcs type. " + repo)
177 # Figure out the license...
178 req = urllib.urlopen(url)
179 if req.getcode() != 200:
180 logging.info('Unable to find project page at ' + sourcecode + ' - return code ' + str(req.getcode()))
183 index = page.find('Code license')
185 logging.info("Couldn't find license data")
188 lprefix = 'rel="nofollow">'
189 index = ltext.find(lprefix)
191 logging.info("Couldn't find license text")
193 ltext = ltext[index + len(lprefix):]
194 index = ltext.find('<')
196 logging.info("License text not formatted as expected")
198 ltext = ltext[:index]
199 if ltext == 'GNU GPL v3':
201 elif ltext == 'GNU GPL v2':
203 elif ltext == 'Apache License 2.0':
205 elif ltext == 'MIT License':
207 elif ltext == 'GNU Lesser GPL':
209 elif ltext == 'Mozilla Public License 1.1':
211 elif ltext == 'New BSD License':
214 logging.info("License " + ltext + " is not recognised")
218 logging.info("Unable to determine the project type.")
219 logging.info("The URL you supplied was not in one of the supported formats. Please consult")
220 logging.info("the manual for a list of supported formats, and supply one of those.")
223 # Get a copy of the source so we can extract some info...
224 logging.info('Getting source from ' + repotype + ' repo at ' + repo)
225 src_dir = os.path.join(tmp_dir, 'importer')
226 if os.path.exists(src_dir):
227 shutil.rmtree(src_dir)
228 vcs = common.getvcs(repotype, repo, src_dir)
229 vcs.gotorevision(options.rev)
231 root_dir = os.path.join(src_dir, options.subdir)
235 # Extract some information...
236 paths = common.manifest_paths(root_dir, None)
239 version, vercode, package = common.parse_androidmanifests(paths)
241 logging.info("Couldn't find package ID")
244 logging.info("WARNING: Couldn't find latest version name")
246 logging.info("WARNING: Couldn't find latest version code")
248 spec = os.path.join(root_dir, 'buildozer.spec')
249 if os.path.exists(spec):
250 defaults = {'orientation': 'landscape', 'icon': '',
251 'permissions': '', 'android.api': "18"}
252 bconfig = ConfigParser(defaults, allow_no_value=True)
254 package = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
255 version = bconfig.get('app', 'version')
258 logging.info("No android or kivy project could be found. Specify --subdir?")
261 # Make sure it's actually new...
263 if app['id'] == package:
264 logging.info("Package " + package + " already exists")
267 # Construct the metadata...
268 app = metadata.parse_metadata(None)
270 app['Web Site'] = website
271 app['Source Code'] = sourcecode
273 app['Issue Tracker'] = issuetracker
275 app['License'] = license
276 app['Repo Type'] = repotype
278 app['Update Check Mode'] = "Tags"
280 # Create a build line...
282 build['version'] = version if version else '?'
283 build['vercode'] = vercode if vercode else '?'
284 build['commit'] = '?'
285 build['disable'] = 'Generated by import.py - check/set version fields and commit id'
287 build['subdir'] = options.subdir
288 if os.path.exists(os.path.join(root_dir, 'jni')):
289 build['buildjni'] = ['yes']
290 app['builds'].append(build)
292 # Keep the repo directory to save bandwidth...
293 if not os.path.exists('build'):
295 shutil.move(src_dir, os.path.join('build', package))
296 with open('build/.fdroidvcs-' + package, 'w') as f:
297 f.write(repotype + ' ' + repo)
299 metafile = os.path.join('metadata', package + '.txt')
300 metadata.write_metadata(metafile, app)
301 logging.info("Wrote " + metafile)
304 if __name__ == "__main__":