wraptool.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/usr/bin/env python3
  2. # Copyright 2015 The Meson development team
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. import urllib.request, json
  13. import sys, os
  14. import configparser
  15. import shutil
  16. try:
  17. import ssl
  18. has_ssl = True
  19. API_ROOT = 'https://wrapdb.mesonbuild.com/v1/'
  20. except ImportError:
  21. print('Warning: ssl not available, traffic not authenticated.')
  22. has_ssl = False
  23. API_ROOT = 'http://wrapdb.mesonbuild.com/v1/'
  24. from glob import glob
  25. wrapdb_certificate = '''-----BEGIN CERTIFICATE-----
  26. MIIFkzCCA3ugAwIBAgIJAIjVMWLmbJWUMA0GCSqGSIb3DQEBCwUAMGAxCzAJBgNV
  27. BAYTAkZJMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxGjAYBgNVBAoMEVRoZSBNZXNv
  28. biBQcm9qZWN0MR4wHAYDVQQDDBV3cmFwZGIubWVzb25idWlsZC5jb20wHhcNMTUw
  29. NzIxMTk0NjI1WhcNMTYwNzIwMTk0NjI1WjBgMQswCQYDVQQGEwJGSTEVMBMGA1UE
  30. BwwMRGVmYXVsdCBDaXR5MRowGAYDVQQKDBFUaGUgTWVzb24gUHJvamVjdDE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wTFY3ywsddWiomF
  47. 8kstor3103IEzPej9nNlQOht7+HKd1ggchji8+zFFGedmOxLweY5985Ze6TNaqVD
  48. ONZ2u7RmkfpgNUDoMsfHyRnENcsQrJXXS1Pp2TRhb/+0NrqrdSorIKYlt5FP/GkZ
  49. OBdm61RfwHLi72SmkeDGPeOYoS2b0SYNuoXHIX+fjVOOIES0A4jRXsQC10cKGZws
  50. IuXNVLrWaLQq874op0oVteR5guW7Rr0KGRNA6MJt67H2VxPtoyaxCXjygoX0+a92
  51. KlDBb8geKOkNfoXg4fRF2Qxh+j5VLBgJyR+x/YYUdG89kDc+Tb3By3PVWi5ypAPC
  52. UPYkc0F8hB9h9KYe78UnzqIRw+YjFN8bKJQS+DXBLyRmp35gn1yp/Vw2O7Vk+E7m
  53. SuYF28YTKF/woZWdJH1aQDO0erUBXdiycZVeKbdm3jenNPHTiF/Wt22CXIlGjj83
  54. G+eGrvfQVk3oXRn+YlypIbxkV8eI1wOina799oiflQmvV8EevAS4dkJObahV6rtZ
  55. qf3ZjWGS595JCwW0fq6AAtL+ygMSr6+DcjGibYbWTL3GmiMtUeWr
  56. -----END CERTIFICATE-----
  57. '''
  58. help_templ = '''This program allows you to manage your Wrap dependencies
  59. using the online wrap database http://wrapdb.mesonbuild.com.
  60. Run this command in your top level source directory.
  61. Usage:
  62. %s <command> [options]
  63. Commands:
  64. list - show all available projects
  65. search - search the db by name
  66. install - install the specified project
  67. update - update the project to its newest available release
  68. info - show available versions of a project
  69. status - show installed and available versions of your projects
  70. '''
  71. def print_help():
  72. print(help_templ % sys.argv[0])
  73. def build_ssl_context():
  74. ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
  75. ctx.options |= ssl.OP_NO_SSLv2
  76. ctx.options |= ssl.OP_NO_SSLv3
  77. ctx.verify_mode = ssl.CERT_REQUIRED
  78. ctx.load_verify_locations(cadata=wrapdb_certificate)
  79. return ctx
  80. def get_result(urlstring):
  81. if has_ssl:
  82. u = urllib.request.urlopen(urlstring, context=build_ssl_context())
  83. else:
  84. u = urllib.request.urlopen(urlstring)
  85. data = u.read().decode('utf-8')
  86. jd = json.loads(data)
  87. if jd['output'] != 'ok':
  88. print('Got bad output from server.')
  89. print(data)
  90. sys.exit(1)
  91. return jd
  92. def get_projectlist():
  93. jd = get_result(API_ROOT + 'projects')
  94. projects = jd['projects']
  95. return projects
  96. def list_projects():
  97. projects = get_projectlist()
  98. for p in projects:
  99. print(p)
  100. def search(name):
  101. jd = get_result(API_ROOT + 'query/byname/' + name)
  102. for p in jd['projects']:
  103. print(p)
  104. def get_latest_version(name):
  105. jd = get_result(API_ROOT + 'query/get_latest/' + name)
  106. branch = jd['branch']
  107. revision = jd['revision']
  108. return (branch, revision)
  109. def install(name):
  110. if not os.path.isdir('subprojects'):
  111. print('Subprojects dir not found. Run this script in your source root directory.')
  112. sys.exit(1)
  113. if os.path.isdir(os.path.join('subprojects', name)):
  114. print('Subproject directory for this project already exists.')
  115. sys.exit(1)
  116. wrapfile = os.path.join('subprojects', name + '.wrap')
  117. if os.path.exists(wrapfile):
  118. print('Wrap file already exists.')
  119. sys.exit(1)
  120. (branch, revision) = get_latest_version(name)
  121. u = urllib.request.urlopen(API_ROOT + 'projects/%s/%s/%s/get_wrap' % (name, branch, revision))
  122. data = u.read()
  123. open(wrapfile, 'wb').write(data)
  124. print('Installed', name, 'branch', branch, 'revision', revision)
  125. def get_current_version(wrapfile):
  126. cp = configparser.ConfigParser()
  127. cp.read(wrapfile)
  128. cp = cp['wrap-file']
  129. patch_url = cp['patch_url']
  130. arr = patch_url.split('/')
  131. branch = arr[-3]
  132. revision = int(arr[-2])
  133. return (branch, revision, cp['directory'], cp['source_filename'], cp['patch_filename'])
  134. def update(name):
  135. if not os.path.isdir('subprojects'):
  136. print('Subprojects dir not found. Run this command in your source root directory.')
  137. sys.exit(1)
  138. wrapfile = os.path.join('subprojects', name + '.wrap')
  139. if not os.path.exists(wrapfile):
  140. print('Project', name, 'is not in use.')
  141. sys.exit(1)
  142. (branch, revision, subdir, src_file, patch_file) = get_current_version(wrapfile)
  143. (new_branch, new_revision) = get_latest_version(name)
  144. if new_branch == branch and new_revision == revision:
  145. print('Project', name, 'is already up to date.')
  146. sys.exit(0)
  147. u = urllib.request.urlopen(API_ROOT + 'projects/%s/%s/%d/get_wrap' % (name, new_branch, new_revision))
  148. data = u.read()
  149. shutil.rmtree(os.path.join('subprojects', subdir), ignore_errors=True)
  150. try:
  151. os.unlink(os.path.join('subprojects/packagecache', src_file))
  152. except FileNotFoundError:
  153. pass
  154. try:
  155. os.unlink(os.path.join('subprojects/packagecache', patch_file))
  156. except FileNotFoundError:
  157. pass
  158. open(wrapfile, 'wb').write(data)
  159. print('Updated', name, 'to branch', new_branch, 'revision', new_revision)
  160. def info(name):
  161. jd = get_result(API_ROOT + 'projects/' + name)
  162. versions = jd['versions']
  163. if len(versions) == 0:
  164. print('No available versions of', name)
  165. sys.exit(0)
  166. print('Available versions of %s:' % name)
  167. for v in versions:
  168. print(' ', v['branch'], v['revision'])
  169. def status():
  170. print('Subproject status')
  171. for w in glob('subprojects/*.wrap'):
  172. name = os.path.split(w)[1][:-5]
  173. try:
  174. (latest_branch, latest_revision) = get_latest_version(name)
  175. except Exception:
  176. print('', name, 'not available in wrapdb.')
  177. continue
  178. try:
  179. (current_branch, current_revision, _, _, _) = get_current_version(w)
  180. except Exception:
  181. print('Wrap file not from wrapdb.')
  182. continue
  183. if current_branch == latest_branch and current_revision == latest_revision:
  184. print('', name, 'up to date. Branch %s, revision %d.' % (current_branch, current_revision))
  185. else:
  186. print('', name, 'not up to date. Have %s %d, but %s %d is available.' % (current_branch, current_revision, latest_branch, latest_revision))
  187. if __name__ == '__main__':
  188. if len(sys.argv) < 2 or sys.argv[1] == '-h' or sys.argv[1] == '--help':
  189. print_help()
  190. sys.exit(0)
  191. command = sys.argv[1]
  192. args = sys.argv[2:]
  193. if command == 'list':
  194. list_projects()
  195. elif command == 'search':
  196. if len(args) != 1:
  197. print('Search requires exactly one argument.')
  198. sys.exit(1)
  199. search(args[0])
  200. elif command == 'install':
  201. if len(args) != 1:
  202. print('Install requires exactly one argument.')
  203. sys.exit(1)
  204. install(args[0])
  205. elif command == 'update':
  206. if len(args) != 1:
  207. print('update requires exactly one argument.')
  208. sys.exit(1)
  209. update(args[0])
  210. elif command == 'info':
  211. if len(args) != 1:
  212. print('info requires exactly one argument.')
  213. sys.exit(1)
  214. info(args[0])
  215. elif command == 'status':
  216. status()
  217. else:
  218. print('Unknown command', command)
  219. sys.exit(1)