3 # import.py - part of the FDroid server tools
4 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
5 # Copyright (C) 2013-2014 Daniel Martà <mvdan@mvdan.cc>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from argparse import ArgumentParser
26 from configparser import ConfigParser
30 from . import metadata
33 # Get the repo type and address from the given web page. The page is scanned
34 # in a rather naive manner for 'git clone xxxx', 'hg clone xxxx', etc, and
35 # when one of these is found it's assumed that's the information we want.
36 # Returns repotype, address, or None, reason
37 def getrepofrompage(url):
39 req = urllib.request.urlopen(url)
40 if req.getcode() != 200:
41 return (None, 'Unable to get ' + url + ' - return code ' + str(req.getcode()))
45 index = page.find('hg clone')
48 repo = page[index + 9:]
49 index = repo.find('<')
51 return (None, "Error while getting repo address")
53 repo = repo.split('"')[0]
54 return (repotype, repo)
57 index = page.find('git clone')
60 repo = page[index + 10:]
61 index = repo.find('<')
63 return (None, "Error while getting repo address")
65 repo = repo.split('"')[0]
66 return (repotype, repo)
68 return (None, "No information found." + page)
75 def get_metadata_from_url(app, url):
78 if not os.path.isdir(tmp_dir):
79 logging.info("Creating temporary directory")
82 # Figure out what kind of project it is...
84 app.WebSite = url # by default, we might override it
85 if url.startswith('git://'):
91 elif url.startswith('https://github.com'):
92 projecttype = 'github'
96 app.IssueTracker = url + '/issues'
98 elif url.startswith('https://gitlab.com/'):
99 projecttype = 'gitlab'
100 # git can be fussy with gitlab URLs unless they end in .git
101 if url.endswith('.git'):
106 app.SourceCode = url + '/tree/HEAD'
107 app.IssueTracker = url + '/issues'
108 elif url.startswith('https://bitbucket.org/'):
109 if url.endswith('/'):
111 projecttype = 'bitbucket'
112 app.SourceCode = url + '/src'
113 app.IssueTracker = url + '/issues'
114 # Figure out the repo type and adddress...
115 repotype, repo = getrepofrompage(app.SourceCode)
117 logging.error("Unable to determine vcs type. " + repo)
119 elif url.startswith('https://') and url.endswith('.git'):
126 logging.error("Unable to determine the project type.")
127 logging.error("The URL you supplied was not in one of the supported formats. Please consult")
128 logging.error("the manual for a list of supported formats, and supply one of those.")
131 # Ensure we have a sensible-looking repo address at this point. If not, we
132 # might have got a page format we weren't expecting. (Note that we
133 # specifically don't want git@...)
134 if ((repotype != 'bzr' and (not repo.startswith('http://') and
135 not repo.startswith('https://') and
136 not repo.startswith('git://'))) or
138 logging.error("Repo address '{0}' does not seem to be valid".format(repo))
141 # Get a copy of the source so we can extract some info...
142 logging.info('Getting source from ' + repotype + ' repo at ' + repo)
143 build_dir = os.path.join(tmp_dir, 'importer')
144 if os.path.exists(build_dir):
145 shutil.rmtree(build_dir)
146 vcs = common.getvcs(repotype, repo, build_dir)
147 vcs.gotorevision(options.rev)
148 root_dir = get_subdir(build_dir)
150 app.RepoType = repotype
153 return root_dir, build_dir
160 def get_subdir(build_dir):
162 return os.path.join(build_dir, options.subdir)
169 global config, options
171 # Parse command line...
172 parser = ArgumentParser()
173 common.setup_global_opts(parser)
174 parser.add_argument("-u", "--url", default=None,
175 help="Project URL to import from.")
176 parser.add_argument("-s", "--subdir", default=None,
177 help="Path to main android project subdirectory, if not in root.")
178 parser.add_argument("--rev", default=None,
179 help="Allows a different revision (or git branch) to be specified for the initial import")
180 metadata.add_metadata_arguments(parser)
181 options = parser.parse_args()
182 metadata.warnings_action = options.W
184 config = common.read_config(options)
186 apps = metadata.read_metadata()
188 app.UpdateCheckMode = "Tags"
193 local_metadata_files = common.get_local_metadata_files()
194 if local_metadata_files != []:
195 logging.error("This repo already has local metadata: %s" % local_metadata_files[0])
198 if options.url is None and os.path.isdir('.git'):
199 app.AutoName = os.path.basename(os.getcwd())
203 root_dir = get_subdir(os.getcwd())
204 if os.path.exists('build.gradle'):
205 build.gradle = ['yes']
208 repo = git.repo.Repo(root_dir) # git repo
209 for remote in git.Remote.iter_items(repo):
210 if remote.name == 'origin':
211 url = repo.remotes.origin.url
212 if url.startswith('https://git'): # github, gitlab
213 app.SourceCode = url.rstrip('.git')
216 # repo.head.commit.binsha is a bytearray stored in a str
217 build.commit = binascii.hexlify(bytearray(repo.head.commit.binsha))
218 write_local_file = True
220 root_dir, build_dir = get_metadata_from_url(app, options.url)
221 build = metadata.Build()
223 build.disable = 'Generated by import.py - check/set version fields and commit id'
224 write_local_file = False
226 logging.error("Specify project url.")
229 # Extract some information...
230 paths = common.manifest_paths(root_dir, [])
233 versionName, versionCode, package = common.parse_androidmanifests(paths, app)
235 logging.error("Couldn't find package ID")
238 logging.warn("Couldn't find latest version name")
240 logging.warn("Couldn't find latest version code")
242 spec = os.path.join(root_dir, 'buildozer.spec')
243 if os.path.exists(spec):
244 defaults = {'orientation': 'landscape', 'icon': '',
245 'permissions': '', 'android.api': "18"}
246 bconfig = ConfigParser(defaults, allow_no_value=True)
248 package = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
249 versionName = bconfig.get('app', 'version')
252 logging.error("No android or kivy project could be found. Specify --subdir?")
255 # Make sure it's actually new...
257 logging.error("Package " + package + " already exists")
260 # Create a build line...
261 build.versionName = versionName or '?'
262 build.versionCode = versionCode or '?'
264 build.subdir = options.subdir
265 if os.path.exists(os.path.join(root_dir, 'jni')):
266 build.buildjni = ['yes']
268 metadata.post_metadata_parse(app)
270 app.builds.append(build)
273 metadata.write_metadata('.fdroid.yml', app)
275 # Keep the repo directory to save bandwidth...
276 if not os.path.exists('build'):
278 if build_dir is not None:
279 shutil.move(build_dir, os.path.join('build', package))
280 with open('build/.fdroidvcs-' + package, 'w') as f:
281 f.write(app.RepoType + ' ' + app.Repo)
283 metadatapath = os.path.join('metadata', package + '.txt')
284 metadata.write_metadata(metadatapath, app)
285 logging.info("Wrote " + metadatapath)
288 if __name__ == "__main__":