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("-u", "--url", default=None,
98 help="Project URL to import from.")
99 parser.add_option("-s", "--subdir", default=None,
100 help="Path to main android project subdirectory, if not in root.")
101 parser.add_option("-r", "--repo", default=None,
102 help="Allows a different repo to be specified for a multi-repo google code project")
103 parser.add_option("--rev", default=None,
104 help="Allows a different revision (or git branch) to be specified for the initial import")
105 (options, args) = parser.parse_args()
107 config = common.read_config(options)
110 logging.info("Specify project url.")
115 if not os.path.isdir(tmp_dir):
116 logging.info("Creating temporary directory")
120 apps = metadata.read_metadata()
122 # Figure out what kind of project it is...
126 website = url #by default, we might override it
127 if url.startswith('git://'):
133 elif url.startswith('https://github.com'):
134 if url.endswith('/'):
136 if url.endswith('.git'):
137 logging.info("A github URL should point to the project, not the git repo")
139 projecttype = 'github'
143 issuetracker = url + '/issues'
144 elif url.startswith('https://gitorious.org/'):
145 projecttype = 'gitorious'
146 repo = 'https://git.gitorious.org/' + url[22:] + '.git'
149 elif url.startswith('https://bitbucket.org/'):
150 if url.endswith('/'):
152 projecttype = 'bitbucket'
153 sourcecode = url + '/src'
154 issuetracker = url + '/issues'
155 # Figure out the repo type and adddress...
156 repotype, repo = getrepofrompage(sourcecode)
158 logging.info("Unable to determine vcs type. " + repo)
160 elif url.startswith('http://code.google.com/p/'):
161 if not url.endswith('/'):
163 projecttype = 'googlecode'
164 sourcecode = url + 'source/checkout'
166 sourcecode += "?repo=" + options.repo
167 issuetracker = url + 'issues/list'
169 # Figure out the repo type and adddress...
170 repotype, repo = getrepofrompage(sourcecode)
172 logging.info("Unable to determine vcs type. " + repo)
175 # Figure out the license...
176 req = urllib.urlopen(url)
177 if req.getcode() != 200:
178 logging.info('Unable to find project page at ' + sourcecode + ' - return code ' + str(req.getcode()))
181 index = page.find('Code license')
183 logging.info("Couldn't find license data")
186 lprefix = 'rel="nofollow">'
187 index = ltext.find(lprefix)
189 logging.info("Couldn't find license text")
191 ltext = ltext[index + len(lprefix):]
192 index = ltext.find('<')
194 logging.info("License text not formatted as expected")
196 ltext = ltext[:index]
197 if ltext == 'GNU GPL v3':
199 elif ltext == 'GNU GPL v2':
201 elif ltext == 'Apache License 2.0':
203 elif ltext == 'MIT License':
205 elif ltext == 'GNU Lesser GPL':
207 elif ltext == 'Mozilla Public License 1.1':
209 elif ltext == 'New BSD License':
212 logging.info("License " + ltext + " is not recognised")
216 logging.info("Unable to determine the project type.")
217 logging.info("The URL you supplied was not in one of the supported formats. Please consult")
218 logging.info("the manual for a list of supported formats, and supply one of those.")
221 # Get a copy of the source so we can extract some info...
222 logging.info('Getting source from ' + repotype + ' repo at ' + repo)
223 src_dir = os.path.join(tmp_dir, 'importer')
224 if os.path.exists(src_dir):
225 shutil.rmtree(src_dir)
226 vcs = common.getvcs(repotype, repo, src_dir)
227 vcs.gotorevision(options.rev)
229 root_dir = os.path.join(src_dir, options.subdir)
233 # Extract some information...
234 paths = common.manifest_paths(root_dir, None)
237 version, vercode, package = common.parse_androidmanifests(paths)
239 logging.info("Couldn't find package ID")
242 logging.info("WARNING: Couldn't find latest version name")
244 logging.info("WARNING: Couldn't find latest version code")
246 spec = os.path.join(root_dir, 'buildozer.spec')
247 if os.path.exists(spec):
248 defaults = {'orientation': 'landscape', 'icon': '',
249 'permissions': '', 'android.api': "18"}
250 bconfig = ConfigParser(defaults, allow_no_value=True)
252 package = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
253 version = bconfig.get('app', 'version')
256 logging.info("No android or kivy project could be found. Specify --subdir?")
259 # Make sure it's actually new...
261 if app['id'] == package:
262 logging.info("Package " + package + " already exists")
265 # Construct the metadata...
266 app = metadata.parse_metadata(None)
268 app['Web Site'] = website
269 app['Source Code'] = sourcecode
271 app['Issue Tracker'] = issuetracker
273 app['License'] = license
274 app['Repo Type'] = repotype
276 app['Update Check Mode'] = "Tags"
278 # Create a build line...
280 build['version'] = version if version else '?'
281 build['vercode'] = vercode if vercode else '?'
282 build['commit'] = '?'
283 build['disable'] = 'Generated by import.py - check/set version fields and commit id'
285 build['subdir'] = options.subdir
286 if os.path.exists(os.path.join(root_dir, 'jni')):
287 build['buildjni'] = 'yes'
288 app['builds'].append(build)
290 # Keep the repo directory to save bandwidth...
291 if not os.path.exists('build'):
293 shutil.move(src_dir, os.path.join('build', package))
294 with open('build/.fdroidvcs-' + package, 'w') as f:
295 f.write(repotype + ' ' + repo)
297 metafile = os.path.join('metadata', package + '.txt')
298 metadata.write_metadata(metafile, app)
299 logging.info("Wrote " + metafile)
302 if __name__ == "__main__":