linuxman94

Untitled

Nov 2nd, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. #!/usr/bin/env python
  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.  
  16. from __future__ import print_function
  17.  
  18. import base64
  19. import json
  20. import netrc
  21. import os
  22. import re
  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.  
  39. from xml.etree import ElementTree
  40.  
  41. product = sys.argv[1];
  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:
  54. print("Device %s not found. Attempting to retrieve device repository from AOSPA Github (http://github.com/AOSPA)." % device)
  55.  
  56. repositories = []
  57.  
  58. try:
  59. authtuple = netrc.netrc().authenticators("api.github.com")
  60.  
  61. if authtuple:
  62. githubauth = base64.encodestring('%s:%s' % (authtuple[0], authtuple[2])).replace('\n', '')
  63. else:
  64. githubauth = None
  65. except:
  66. githubauth = None
  67.  
  68. def add_auth(githubreq):
  69. if githubauth:
  70. githubreq.add_header("Authorization","Basic %s" % githubauth)
  71.  
  72. page = 1
  73. while not depsonly:
  74. githubreq = urllib.request.Request("https://api.github.com/users/AOSPA/repos?per_page=200&page=%d" % page)
  75. add_auth(githubreq)
  76. result = json.loads(urllib.request.urlopen(githubreq).read().decode())
  77. if len(result) == 0:
  78. break
  79. for res in result:
  80. repositories.append(res)
  81. page = page + 1
  82.  
  83. local_manifests = r'.repo/local_manifests'
  84. if not os.path.exists(local_manifests): os.makedirs(local_manifests)
  85.  
  86. def exists_in_tree(lm, repository):
  87. for child in lm.getchildren():
  88. if child.attrib['name'].endswith(repository):
  89. return True
  90. return False
  91.  
  92. # in-place prettyprint formatter
  93. def indent(elem, level=0):
  94. i = "\n" + level*" "
  95. if len(elem):
  96. if not elem.text or not elem.text.strip():
  97. elem.text = i + " "
  98. if not elem.tail or not elem.tail.strip():
  99. elem.tail = i
  100. for elem in elem:
  101. indent(elem, level+1)
  102. if not elem.tail or not elem.tail.strip():
  103. elem.tail = i
  104. else:
  105. if level and (not elem.tail or not elem.tail.strip()):
  106. elem.tail = i
  107.  
  108. def get_default_revision():
  109. # m = ElementTree.parse(".repo/manifest.xml")
  110. # d = m.findall('default')[0]
  111. # r = d.get('revision')
  112. # return r.split('/')[-1]
  113. return "kitkat"
  114.  
  115. def get_from_manifest(devicename):
  116. try:
  117. lm = ElementTree.parse(".repo/local_manifests/roomservice.xml")
  118. lm = lm.getroot()
  119. except:
  120. lm = ElementTree.Element("manifest")
  121.  
  122. for localpath in lm.findall("project"):
  123. if re.search("android_device_.*_%s$" % device, localpath.get("name")):
  124. return localpath.get("path")
  125.  
  126. # Devices originally from AOSP are in the main manifest...
  127. try:
  128. mm = ElementTree.parse(".repo/manifest.xml")
  129. mm = mm.getroot()
  130. except:
  131. mm = ElementTree.Element("manifest")
  132.  
  133. for localpath in mm.findall("project"):
  134. if re.search("android_device_.*_%s$" % device, localpath.get("name")):
  135. return localpath.get("path")
  136.  
  137. return None
  138.  
  139. def is_in_manifest(projectname):
  140. try:
  141. lm = ElementTree.parse(".repo/local_manifests/roomservice.xml")
  142. lm = lm.getroot()
  143. except:
  144. lm = ElementTree.Element("manifest")
  145.  
  146. for localpath in lm.findall("project"):
  147. if localpath.get("name") == projectname:
  148. return 1
  149.  
  150. ## Search in main manifest, too
  151. try:
  152. lm = ElementTree.parse(".repo/manifest.xml")
  153. lm = lm.getroot()
  154. except:
  155. lm = ElementTree.Element("manifest")
  156.  
  157. for localpath in lm.findall("project"):
  158. if localpath.get("name") == projectname:
  159. return 1
  160.  
  161. return None
  162.  
  163. def add_to_manifest(repositories, fallback_branch = None, is_dependency = False):
  164. try:
  165. lm = ElementTree.parse(".repo/local_manifests/roomservice.xml")
  166. lm = lm.getroot()
  167. except:
  168. lm = ElementTree.Element("manifest")
  169.  
  170. for repository in repositories:
  171. repo_name = repository['repository']
  172. repo_target = repository['target_path']
  173.  
  174. if is_dependency:
  175. try:
  176. repo_remote = repository['remote']
  177. except:
  178. repo_remote = "github"
  179. try:
  180. repo_revision = repository['revision']
  181. except:
  182. repo_revision = "kitkat"
  183.  
  184. try:
  185. repo_account = repository['account']
  186. repo_full = repo_account + '/' + repo_name
  187. except:
  188. repo_full = repo_name
  189.  
  190. if exists_in_tree(lm, repo_name):
  191. print('AOSPA/%s already exists' % (repo_name))
  192. continue
  193.  
  194. print ('Adding dependecy: %s -> %s' % (repo_full, repo_target))
  195.  
  196. if is_dependency:
  197. project = ElementTree.Element("project", attrib = { "path": repo_target,
  198. "remote": repo_remote, "name": repo_full, "revision": repo_revision })
  199. else:
  200. project = ElementTree.Element("project", attrib = { "path": repo_target,
  201. "remote": "github", "name": "AOSPA/%s" % repo_name })
  202.  
  203. if 'branch' in repository:
  204. project.set('revision',repository['branch'])
  205. elif fallback_branch:
  206. print("Using fallback branch %s for %s" % (fallback_branch, repo_name))
  207. project.set('revision', fallback_branch)
  208. else:
  209. print("Using default branch for %s" % repo_name)
  210.  
  211. lm.append(project)
  212.  
  213. indent(lm, 0)
  214. raw_xml = ElementTree.tostring(lm).decode()
  215. raw_xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + raw_xml
  216.  
  217. f = open('.repo/local_manifests/roomservice.xml', 'w')
  218. f.write(raw_xml)
  219. f.close()
  220.  
  221. def fetch_dependencies(repo_path, fallback_branch = None):
  222. print('Looking for dependencies')
  223. dependencies_path = repo_path + '/pa.dependencies'
  224. syncable_repos = []
  225.  
  226. if os.path.exists(dependencies_path):
  227. dependencies_file = open(dependencies_path, 'r')
  228. dependencies = json.loads(dependencies_file.read())
  229. fetch_list = []
  230.  
  231. for dependency in dependencies:
  232. if not is_in_manifest("AOSPA/%s" % dependency['repository']):
  233. fetch_list.append(dependency)
  234. syncable_repos.append(dependency['target_path'])
  235.  
  236. dependencies_file.close()
  237.  
  238. if len(fetch_list) > 0:
  239. print('Adding dependencies to manifest')
  240. add_to_manifest(fetch_list, fallback_branch,True)
  241.  
  242. if len(syncable_repos) > 0:
  243. print('Syncing dependencies')
  244. os.system('repo sync %s' % ' '.join(syncable_repos))
  245.  
  246. def has_branch(branches, revision):
  247. return revision in [branch['name'] for branch in branches]
  248.  
  249. if depsonly:
  250. repo_path = get_from_manifest(device)
  251. if repo_path:
  252. fetch_dependencies(repo_path)
  253. else:
  254. print("Device tree not found on git using local tree")
  255.  
  256. sys.exit()
  257.  
  258. else:
  259. for repository in repositories:
  260. repo_name = repository['name']
  261. if repo_name.startswith("android_device_") and repo_name.endswith("_" + device):
  262. print("Found repository: %s" % repository['name'])
  263.  
  264. manufacturer = repo_name.replace("android_device_", "").replace("_" + device, "")
  265.  
  266. default_revision = get_default_revision()
  267. print("Default revision: %s" % default_revision)
  268. print("Checking branch info")
  269. githubreq = urllib.request.Request(repository['branches_url'].replace('{/branch}', ''))
  270. add_auth(githubreq)
  271. result = json.loads(urllib.request.urlopen(githubreq).read().decode())
  272.  
  273. ## Try tags, too, since that's what releases use
  274. if not has_branch(result, default_revision):
  275. githubreq = urllib.request.Request(repository['tags_url'].replace('{/tag}', ''))
  276. add_auth(githubreq)
  277. result.extend (json.loads(urllib.request.urlopen(githubreq).read().decode()))
  278.  
  279. repo_path = "device/%s/%s" % (manufacturer, device)
  280. adding = {'repository':repo_name,'target_path':repo_path}
  281.  
  282. fallback_branch = None
  283. if not has_branch(result, default_revision):
  284. if os.getenv('ROOMSERVICE_BRANCHES'):
  285. fallbacks = list(filter(bool, os.getenv('ROOMSERVICE_BRANCHES').split(' ')))
  286. for fallback in fallbacks:
  287. if has_branch(result, fallback):
  288. print("Using fallback branch: %s" % fallback)
  289. fallback_branch = fallback
  290. break
  291.  
  292. if not fallback_branch:
  293. print("Default revision %s not found in %s. Bailing." % (default_revision, repo_name))
  294. print("Branches found:")
  295. for branch in [branch['name'] for branch in result]:
  296. print(branch)
  297. print("Use the ROOMSERVICE_BRANCHES environment variable to specify a list of fallback branches.")
  298. sys.exit()
  299.  
  300. add_to_manifest([adding], fallback_branch)
  301.  
  302. print("Syncing repository to retrieve project.")
  303. os.system('repo sync %s' % repo_path)
  304. print("Repository synced!")
  305.  
  306. fetch_dependencies(repo_path, fallback_branch)
  307. print("Done")
  308. sys.exit()
  309.  
  310. print("Repository for %s not found in the AOSPA Github repository list. If this is in error, you may need to manually add it to your local_manifests/roomservice.xml." % device)
Add Comment
Please, Sign In to add comment