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/>.
24 from argparse import ArgumentParser
25 from ConfigParser import ConfigParser
31 # Get the repo type and address from the given web page. The page is scanned
32 # in a rather naive manner for 'git clone xxxx', 'hg clone xxxx', etc, and
33 # when one of these is found it's assumed that's the information we want.
34 # Returns repotype, address, or None, reason
35 def getrepofrompage(url):
37 req = urllib.urlopen(url)
38 if req.getcode() != 200:
39 return (None, 'Unable to get ' + url + ' - return code ' + str(req.getcode()))
43 index = page.find('hg clone')
46 repo = page[index + 9:]
47 index = repo.find('<')
49 return (None, "Error while getting repo address")
51 repo = repo.split('"')[0]
52 return (repotype, repo)
55 index = page.find('git clone')
58 repo = page[index + 10:]
59 index = repo.find('<')
61 return (None, "Error while getting repo address")
63 repo = repo.split('"')[0]
64 return (repotype, repo)
66 return (None, "No information found." + page)
72 def get_metadata_from_url(app, url):
75 if not os.path.isdir(tmp_dir):
76 logging.info("Creating temporary directory")
79 # Figure out what kind of project it is...
81 app.WebSite = url # by default, we might override it
82 if url.startswith('git://'):
88 elif url.startswith('https://github.com'):
89 projecttype = 'github'
93 app.IssueTracker = url + '/issues'
95 elif url.startswith('https://gitlab.com/'):
96 projecttype = 'gitlab'
97 # git can be fussy with gitlab URLs unless they end in .git
98 if url.endswith('.git'):
103 app.SourceCode = url + '/tree/HEAD'
104 app.IssueTracker = url + '/issues'
105 elif url.startswith('https://bitbucket.org/'):
106 if url.endswith('/'):
108 projecttype = 'bitbucket'
109 app.SourceCode = url + '/src'
110 app.IssueTracker = url + '/issues'
111 # Figure out the repo type and adddress...
112 repotype, repo = getrepofrompage(app.SourceCode)
114 logging.error("Unable to determine vcs type. " + repo)
117 logging.error("Unable to determine the project type.")
118 logging.error("The URL you supplied was not in one of the supported formats. Please consult")
119 logging.error("the manual for a list of supported formats, and supply one of those.")
122 # Ensure we have a sensible-looking repo address at this point. If not, we
123 # might have got a page format we weren't expecting. (Note that we
124 # specifically don't want git@...)
125 if ((repotype != 'bzr' and (not repo.startswith('http://') and
126 not repo.startswith('https://') and
127 not repo.startswith('git://'))) or
129 logging.error("Repo address '{0}' does not seem to be valid".format(repo))
132 # Get a copy of the source so we can extract some info...
133 logging.info('Getting source from ' + repotype + ' repo at ' + repo)
134 build_dir = os.path.join(tmp_dir, 'importer')
135 if os.path.exists(build_dir):
136 shutil.rmtree(build_dir)
137 vcs = common.getvcs(repotype, repo, build_dir)
138 vcs.gotorevision(options.rev)
139 root_dir = get_subdir(build_dir)
141 app.RepoType = repotype
144 return root_dir, build_dir
151 def get_subdir(build_dir):
153 return os.path.join(build_dir, options.subdir)
160 global config, options
162 # Parse command line...
163 parser = ArgumentParser()
164 common.setup_global_opts(parser)
165 parser.add_argument("-u", "--url", default=None,
166 help="Project URL to import from.")
167 parser.add_argument("-s", "--subdir", default=None,
168 help="Path to main android project subdirectory, if not in root.")
169 parser.add_argument("--rev", default=None,
170 help="Allows a different revision (or git branch) to be specified for the initial import")
171 options = parser.parse_args()
173 config = common.read_config(options)
175 apps = metadata.read_metadata()
177 app.UpdateCheckMode = "Tags"
183 root_dir, build_dir = get_metadata_from_url(app, options.url)
184 elif os.path.isdir('.git'):
186 app.WebSite = options.url
187 root_dir = get_subdir(os.getcwd())
189 logging.error("Specify project url.")
192 # Extract some information...
193 paths = common.manifest_paths(root_dir, [])
196 version, vercode, package = common.parse_androidmanifests(paths, app)
198 logging.error("Couldn't find package ID")
201 logging.warn("Couldn't find latest version name")
203 logging.warn("Couldn't find latest version code")
205 spec = os.path.join(root_dir, 'buildozer.spec')
206 if os.path.exists(spec):
207 defaults = {'orientation': 'landscape', 'icon': '',
208 'permissions': '', 'android.api': "18"}
209 bconfig = ConfigParser(defaults, allow_no_value=True)
211 package = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
212 version = bconfig.get('app', 'version')
215 logging.error("No android or kivy project could be found. Specify --subdir?")
218 # Make sure it's actually new...
220 logging.error("Package " + package + " already exists")
223 # Create a build line...
224 build = metadata.Build()
225 build.version = version or '?'
226 build.vercode = vercode or '?'
228 build.disable = 'Generated by import.py - check/set version fields and commit id'
230 build.subdir = options.subdir
231 if os.path.exists(os.path.join(root_dir, 'jni')):
232 build.buildjni = ['yes']
234 app.builds.append(build)
236 # Keep the repo directory to save bandwidth...
237 if not os.path.exists('build'):
239 if build_dir is not None:
240 shutil.move(build_dir, os.path.join('build', package))
241 with open('build/.fdroidvcs-' + package, 'w') as f:
242 f.write(app.RepoType + ' ' + app.Repo)
244 metadatapath = os.path.join('metadata', package + '.txt')
245 with open(metadatapath, 'w') as f:
246 metadata.write_metadata('txt', f, app)
247 logging.info("Wrote " + metadatapath)
250 if __name__ == "__main__":