compatible_utils.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2022 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Functions used in both v1 and v2 scripts."""
  5. import os
  6. import re
  7. from typing import Tuple
  8. # File indicating version of an image downloaded to the host
  9. _BUILD_ARGS = "buildargs.gn"
  10. _FILTER_DIR = 'testing/buildbot/filters'
  11. class VersionNotFoundError(Exception):
  12. """Thrown when version info cannot be retrieved from device."""
  13. def parse_host_port(host_port_pair: str) -> Tuple[str, int]:
  14. """Parses a host name or IP address and a port number from a string of
  15. any of the following forms:
  16. - hostname:port
  17. - IPv4addy:port
  18. - [IPv6addy]:port
  19. Returns:
  20. A tuple of the string host name/address and integer port number.
  21. Raises:
  22. ValueError if `host_port_pair` does not contain a colon or if the
  23. substring following the last colon cannot be converted to an int.
  24. """
  25. host, port = host_port_pair.rsplit(':', 1)
  26. # Strip the brackets if the host looks like an IPv6 address.
  27. if len(host) >= 4 and host[0] == '[' and host[-1] == ']':
  28. host = host[1:-1]
  29. return (host, int(port))
  30. # TODO(crbug.com/1279803): Until one can send files to the device when running
  31. # a test, filter files must be read from the test package.
  32. def map_filter_file_to_package_file(filter_file: str) -> str:
  33. """Returns the path to |filter_file| within the test component's package."""
  34. if not _FILTER_DIR in filter_file:
  35. raise ValueError('CFv2 tests only support registered filter files '
  36. 'present in the test package')
  37. return '/pkg/' + filter_file[filter_file.index(_FILTER_DIR):]
  38. def get_sdk_hash(system_image_dir: str) -> Tuple[str, str]:
  39. """Read version of hash in pre-installed package directory.
  40. Returns:
  41. Tuple of (product, version) of image to be installed.
  42. Raises:
  43. VersionNotFoundError: if contents of buildargs.gn cannot be found or the
  44. version number cannot be extracted.
  45. """
  46. # TODO(crbug.com/1261961): Stop processing buildargs.gn directly.
  47. with open(os.path.join(system_image_dir, _BUILD_ARGS)) as f:
  48. contents = f.readlines()
  49. if not contents:
  50. raise VersionNotFoundError('Could not retrieve %s' % _BUILD_ARGS)
  51. version_key = 'build_info_version'
  52. product_key = 'build_info_product'
  53. info_keys = [product_key, version_key]
  54. version_info = {}
  55. for line in contents:
  56. for key in info_keys:
  57. match = re.match(r'%s = "(.*)"' % key, line)
  58. if match:
  59. version_info[key] = match.group(1)
  60. if not (version_key in version_info and product_key in version_info):
  61. raise VersionNotFoundError(
  62. 'Could not extract version info from %s. Contents: %s' %
  63. (_BUILD_ARGS, contents))
  64. return (version_info[product_key], version_info[version_key])