Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 2 | # Copyright (C) 2012-2013, The CyanogenMod Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 16 | from __future__ import print_function |
| 17 | |
| 18 | import base64 |
Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 19 | import json |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 20 | import netrc |
| 21 | import os |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 22 | import re |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 23 | import sys |
| 24 | try: |
| 25 | # For python3 |
| 26 | import urllib.error |
| 27 | import urllib.parse |
| 28 | import urllib.request |
| 29 | except ImportError: |
| 30 | # For python2 |
| 31 | import imp |
| 32 | import urllib2 |
| 33 | import urlparse |
| 34 | urllib = imp.new_module('urllib') |
| 35 | urllib.error = urllib2 |
| 36 | urllib.parse = urlparse |
| 37 | urllib.request = urllib2 |
| 38 | |
Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 39 | from xml.etree import ElementTree |
| 40 | |
Anthony King | c713d76 | 2015-11-03 00:23:11 +0000 | [diff] [blame^] | 41 | product = sys.argv[1] |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 42 | |
| 43 | if len(sys.argv) > 2: |
| 44 | depsonly = sys.argv[2] |
| 45 | else: |
| 46 | depsonly = None |
| 47 | |
| 48 | try: |
| 49 | device = product[product.index("_") + 1:] |
| 50 | except: |
| 51 | device = product |
| 52 | |
| 53 | if not depsonly: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 54 | print("Device %s not found. Attempting to retrieve device repository from CyanogenMod Github (http://github.com/CyanogenMod)." % device) |
Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 55 | |
| 56 | repositories = [] |
| 57 | |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 58 | try: |
| 59 | authtuple = netrc.netrc().authenticators("api.github.com") |
| 60 | |
| 61 | if authtuple: |
Anthony King | c713d76 | 2015-11-03 00:23:11 +0000 | [diff] [blame^] | 62 | auth_string = ('%s:%s' % (authtuple[0], authtuple[2])).encode() |
| 63 | githubauth = base64.encodestring(auth_string).decode().replace('\n', '') |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 64 | else: |
| 65 | githubauth = None |
| 66 | except: |
| 67 | githubauth = None |
| 68 | |
| 69 | def add_auth(githubreq): |
| 70 | if githubauth: |
| 71 | githubreq.add_header("Authorization","Basic %s" % githubauth) |
| 72 | |
Matt Mower | ee84e3f | 2014-10-31 21:02:37 -0500 | [diff] [blame] | 73 | if not depsonly: |
Matt Mower | 28cb6ba | 2015-01-02 00:08:48 -0600 | [diff] [blame] | 74 | githubreq = urllib.request.Request("https://api.github.com/search/repositories?q=%s+user:CyanogenMod+in:name+fork:true" % device) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 75 | add_auth(githubreq) |
Matt Mower | ee84e3f | 2014-10-31 21:02:37 -0500 | [diff] [blame] | 76 | try: |
Anthony King | 365b1e2 | 2015-01-08 11:39:12 -0600 | [diff] [blame] | 77 | result = json.loads(urllib.request.urlopen(githubreq).read().decode()) |
| 78 | except urllib.error.URLError: |
| 79 | print("Failed to search GitHub") |
Matt Mower | ee84e3f | 2014-10-31 21:02:37 -0500 | [diff] [blame] | 80 | sys.exit() |
Anthony King | 365b1e2 | 2015-01-08 11:39:12 -0600 | [diff] [blame] | 81 | except ValueError: |
| 82 | print("Failed to parse return data from GitHub") |
Matt Mower | ee84e3f | 2014-10-31 21:02:37 -0500 | [diff] [blame] | 83 | sys.exit() |
Anthony King | 365b1e2 | 2015-01-08 11:39:12 -0600 | [diff] [blame] | 84 | for res in result.get('items', []): |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 85 | repositories.append(res) |
Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 86 | |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 87 | local_manifests = r'.repo/local_manifests' |
| 88 | if not os.path.exists(local_manifests): os.makedirs(local_manifests) |
Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 89 | |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 90 | def exists_in_tree(lm, repository): |
| 91 | for child in lm.getchildren(): |
| 92 | if child.attrib['name'].endswith(repository): |
| 93 | return True |
| 94 | return False |
| 95 | |
| 96 | # in-place prettyprint formatter |
| 97 | def indent(elem, level=0): |
| 98 | i = "\n" + level*" " |
| 99 | if len(elem): |
| 100 | if not elem.text or not elem.text.strip(): |
| 101 | elem.text = i + " " |
| 102 | if not elem.tail or not elem.tail.strip(): |
| 103 | elem.tail = i |
| 104 | for elem in elem: |
| 105 | indent(elem, level+1) |
| 106 | if not elem.tail or not elem.tail.strip(): |
| 107 | elem.tail = i |
| 108 | else: |
| 109 | if level and (not elem.tail or not elem.tail.strip()): |
| 110 | elem.tail = i |
| 111 | |
| 112 | def get_default_revision(): |
| 113 | m = ElementTree.parse(".repo/manifest.xml") |
| 114 | d = m.findall('default')[0] |
| 115 | r = d.get('revision') |
| 116 | return r.replace('refs/heads/', '').replace('refs/tags/', '') |
| 117 | |
| 118 | def get_from_manifest(devicename): |
| 119 | try: |
| 120 | lm = ElementTree.parse(".repo/local_manifests/roomservice.xml") |
| 121 | lm = lm.getroot() |
| 122 | except: |
| 123 | lm = ElementTree.Element("manifest") |
| 124 | |
| 125 | for localpath in lm.findall("project"): |
| 126 | if re.search("android_device_.*_%s$" % device, localpath.get("name")): |
| 127 | return localpath.get("path") |
| 128 | |
| 129 | # Devices originally from AOSP are in the main manifest... |
| 130 | try: |
| 131 | mm = ElementTree.parse(".repo/manifest.xml") |
| 132 | mm = mm.getroot() |
| 133 | except: |
| 134 | mm = ElementTree.Element("manifest") |
| 135 | |
| 136 | for localpath in mm.findall("project"): |
| 137 | if re.search("android_device_.*_%s$" % device, localpath.get("name")): |
| 138 | return localpath.get("path") |
| 139 | |
| 140 | return None |
| 141 | |
| 142 | def is_in_manifest(projectname): |
| 143 | try: |
| 144 | lm = ElementTree.parse(".repo/local_manifests/roomservice.xml") |
| 145 | lm = lm.getroot() |
| 146 | except: |
| 147 | lm = ElementTree.Element("manifest") |
| 148 | |
| 149 | for localpath in lm.findall("project"): |
| 150 | if localpath.get("name") == projectname: |
| 151 | return 1 |
| 152 | |
| 153 | ## Search in main manifest, too |
| 154 | try: |
| 155 | lm = ElementTree.parse(".repo/manifest.xml") |
| 156 | lm = lm.getroot() |
| 157 | except: |
| 158 | lm = ElementTree.Element("manifest") |
| 159 | |
| 160 | for localpath in lm.findall("project"): |
| 161 | if localpath.get("name") == projectname: |
| 162 | return 1 |
| 163 | |
| 164 | return None |
| 165 | |
| 166 | def add_to_manifest(repositories, fallback_branch = None): |
| 167 | try: |
| 168 | lm = ElementTree.parse(".repo/local_manifests/roomservice.xml") |
| 169 | lm = lm.getroot() |
| 170 | except: |
| 171 | lm = ElementTree.Element("manifest") |
| 172 | |
| 173 | for repository in repositories: |
| 174 | repo_name = repository['repository'] |
| 175 | repo_target = repository['target_path'] |
| 176 | if exists_in_tree(lm, repo_name): |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 177 | print('CyanogenMod/%s already exists' % (repo_name)) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 178 | continue |
| 179 | |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 180 | print('Adding dependency: CyanogenMod/%s -> %s' % (repo_name, repo_target)) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 181 | project = ElementTree.Element("project", attrib = { "path": repo_target, |
| 182 | "remote": "github", "name": "CyanogenMod/%s" % repo_name }) |
| 183 | |
| 184 | if 'branch' in repository: |
| 185 | project.set('revision',repository['branch']) |
| 186 | elif fallback_branch: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 187 | print("Using fallback branch %s for %s" % (fallback_branch, repo_name)) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 188 | project.set('revision', fallback_branch) |
| 189 | else: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 190 | print("Using default branch for %s" % repo_name) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 191 | |
Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 192 | lm.append(project) |
Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 193 | |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 194 | indent(lm, 0) |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 195 | raw_xml = ElementTree.tostring(lm).decode() |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 196 | raw_xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + raw_xml |
Koushik Dutta | 780fb5f | 2011-11-26 18:51:42 -0800 | [diff] [blame] | 197 | |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 198 | f = open('.repo/local_manifests/roomservice.xml', 'w') |
| 199 | f.write(raw_xml) |
| 200 | f.close() |
| 201 | |
| 202 | def fetch_dependencies(repo_path, fallback_branch = None): |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 203 | print('Looking for dependencies') |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 204 | dependencies_path = repo_path + '/cm.dependencies' |
| 205 | syncable_repos = [] |
| 206 | |
| 207 | if os.path.exists(dependencies_path): |
| 208 | dependencies_file = open(dependencies_path, 'r') |
| 209 | dependencies = json.loads(dependencies_file.read()) |
| 210 | fetch_list = [] |
| 211 | |
| 212 | for dependency in dependencies: |
| 213 | if not is_in_manifest("CyanogenMod/%s" % dependency['repository']): |
| 214 | fetch_list.append(dependency) |
| 215 | syncable_repos.append(dependency['target_path']) |
| 216 | |
| 217 | dependencies_file.close() |
| 218 | |
| 219 | if len(fetch_list) > 0: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 220 | print('Adding dependencies to manifest') |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 221 | add_to_manifest(fetch_list, fallback_branch) |
| 222 | else: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 223 | print('Dependencies file not found, bailing out.') |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 224 | |
| 225 | if len(syncable_repos) > 0: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 226 | print('Syncing dependencies') |
Brint E. Kriebel | 9129cb0 | 2015-08-12 14:05:03 -0700 | [diff] [blame] | 227 | os.system('repo sync --force-sync %s' % ' '.join(syncable_repos)) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 228 | |
| 229 | for deprepo in syncable_repos: |
| 230 | fetch_dependencies(deprepo) |
| 231 | |
| 232 | def has_branch(branches, revision): |
| 233 | return revision in [branch['name'] for branch in branches] |
| 234 | |
| 235 | if depsonly: |
| 236 | repo_path = get_from_manifest(device) |
| 237 | if repo_path: |
| 238 | fetch_dependencies(repo_path) |
| 239 | else: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 240 | print("Trying dependencies-only mode on a non-existing device tree?") |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 241 | |
| 242 | sys.exit() |
| 243 | |
| 244 | else: |
| 245 | for repository in repositories: |
| 246 | repo_name = repository['name'] |
| 247 | if repo_name.startswith("android_device_") and repo_name.endswith("_" + device): |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 248 | print("Found repository: %s" % repository['name']) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 249 | |
| 250 | manufacturer = repo_name.replace("android_device_", "").replace("_" + device, "") |
| 251 | |
| 252 | default_revision = get_default_revision() |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 253 | print("Default revision: %s" % default_revision) |
| 254 | print("Checking branch info") |
| 255 | githubreq = urllib.request.Request(repository['branches_url'].replace('{/branch}', '')) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 256 | add_auth(githubreq) |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 257 | result = json.loads(urllib.request.urlopen(githubreq).read().decode()) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 258 | |
| 259 | ## Try tags, too, since that's what releases use |
| 260 | if not has_branch(result, default_revision): |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 261 | githubreq = urllib.request.Request(repository['tags_url'].replace('{/tag}', '')) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 262 | add_auth(githubreq) |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 263 | result.extend (json.loads(urllib.request.urlopen(githubreq).read().decode())) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 264 | |
| 265 | repo_path = "device/%s/%s" % (manufacturer, device) |
| 266 | adding = {'repository':repo_name,'target_path':repo_path} |
| 267 | |
| 268 | fallback_branch = None |
| 269 | if not has_branch(result, default_revision): |
| 270 | if os.getenv('ROOMSERVICE_BRANCHES'): |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 271 | fallbacks = list(filter(bool, os.getenv('ROOMSERVICE_BRANCHES').split(' '))) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 272 | for fallback in fallbacks: |
| 273 | if has_branch(result, fallback): |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 274 | print("Using fallback branch: %s" % fallback) |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 275 | fallback_branch = fallback |
| 276 | break |
| 277 | |
| 278 | if not fallback_branch: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 279 | print("Default revision %s not found in %s. Bailing." % (default_revision, repo_name)) |
| 280 | print("Branches found:") |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 281 | for branch in [branch['name'] for branch in result]: |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 282 | print(branch) |
| 283 | print("Use the ROOMSERVICE_BRANCHES environment variable to specify a list of fallback branches.") |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 284 | sys.exit() |
| 285 | |
| 286 | add_to_manifest([adding], fallback_branch) |
| 287 | |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 288 | print("Syncing repository to retrieve project.") |
Brint E. Kriebel | 9129cb0 | 2015-08-12 14:05:03 -0700 | [diff] [blame] | 289 | os.system('repo sync --force-sync %s' % repo_path) |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 290 | print("Repository synced!") |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 291 | |
| 292 | fetch_dependencies(repo_path, fallback_branch) |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 293 | print("Done") |
Diogo Ferreira | 0d10917 | 2012-03-18 21:18:29 +0000 | [diff] [blame] | 294 | sys.exit() |
| 295 | |
Chirayu Desai | 55b9b64 | 2013-04-30 17:08:17 +0530 | [diff] [blame] | 296 | print("Repository for %s not found in the CyanogenMod Github repository list. If this is in error, you may need to manually add it to your local_manifests/roomservice.xml." % device) |