resolve.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. """
  18. Platform package dependency resolver for building Apache Hadoop.
  19. """
  20. import argparse
  21. import json
  22. import sys
  23. from check_platform import is_supported_platform
  24. def get_packages(platform, release=None):
  25. """
  26. Resolve and get the list of packages to install for the given platform.
  27. :param platform: The platform for which the packages needs to be resolved.
  28. :param release: An optional parameter that filters the packages of the given platform for the
  29. specified release.
  30. :return: A list of resolved packages to install.
  31. """
  32. with open('pkg-resolver/packages.json', encoding='utf-8', mode='r') as pkg_file:
  33. pkgs = json.loads(pkg_file.read())
  34. packages = []
  35. def process_package(package, in_release=False):
  36. """
  37. Processes the given package object that belongs to a platform and adds it to the packages
  38. list variable in the parent scope.
  39. In essence, this method recursively traverses the JSON structure defined in packages.json
  40. and performs the core filtering.
  41. :param package: The package object to process.
  42. :param in_release: A boolean that indicates whether the current travels belongs to a package
  43. that needs to be filtered for the given release label.
  44. """
  45. if isinstance(package, list):
  46. for entry in package:
  47. process_package(entry, in_release)
  48. elif isinstance(package, dict):
  49. if release is None:
  50. return
  51. for entry in package.get(release, []):
  52. process_package(entry, in_release=True)
  53. elif isinstance(package, str):
  54. # Filter out the package that doesn't belong to this release,
  55. # if a release label has been specified.
  56. if release is not None and not in_release:
  57. return
  58. packages.append(package)
  59. else:
  60. raise Exception('Unknown package of type: {}'.format(type(package)))
  61. for platforms in filter(lambda x: x.get(platform) is not None, pkgs.values()):
  62. process_package(platforms.get(platform))
  63. return packages
  64. if __name__ == '__main__':
  65. if len(sys.argv) < 2:
  66. print('ERROR: Need at least 1 argument, {} were provided'.format(len(sys.argv) - 1),
  67. file=sys.stderr)
  68. sys.exit(1)
  69. arg_parser = argparse.ArgumentParser(
  70. description='Platform package dependency resolver for building Apache Hadoop')
  71. arg_parser.add_argument('-r', '--release', nargs=1, type=str,
  72. help='The release label to filter the packages for the given platform')
  73. arg_parser.add_argument('platform', nargs=1, type=str,
  74. help='The name of the platform to resolve the dependencies for')
  75. args = arg_parser.parse_args()
  76. if not is_supported_platform(args.platform[0]):
  77. print(
  78. 'ERROR: The given platform {} is not supported. '
  79. 'Please refer to platforms.json for a list of supported platforms'.format(
  80. args.platform), file=sys.stderr)
  81. sys.exit(1)
  82. packages_to_install = get_packages(args.platform[0],
  83. args.release[0] if args.release is not None else None)
  84. print(' '.join(packages_to_install))