Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Build graph dependency creation service. |
| 7 | |
| 8 | This service handles the creation of the portage build dependency graphs and the |
| 9 | graphs mapping from portage packages to the dependency source. |
| 10 | """ |
| 11 | |
| 12 | from __future__ import print_function |
| 13 | |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 14 | from chromite.api.gen import depgraph_pb2 |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 15 | from chromite.lib.api import dependency |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 16 | from chromite.lib import portage_util |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 17 | |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 18 | from google.protobuf import json_format |
| 19 | |
| 20 | |
| 21 | def CreateDepGraphProtoFromJsonMap(json_map): |
| 22 | """Return the depgraph proto message from package deps json map. |
| 23 | |
| 24 | Args: |
| 25 | json_map: the json object that stores the portage package. This is |
| 26 | generated from chromite.lib.api.dependency.GetBuildDependency() |
| 27 | """ |
| 28 | graph = depgraph_pb2.DepGraph() |
| 29 | graph.build_target.name = json_map['target_board'] |
| 30 | |
| 31 | for data in json_map['package_deps'].itervalues(): |
| 32 | package_dep_info = graph.package_deps.add() |
| 33 | package_info = package_dep_info.package_info |
| 34 | package_info.package_name = data['name'] |
| 35 | package_info.category = data['category'] |
| 36 | package_info.version = data['version'] |
| 37 | for dep in data['deps']: |
| 38 | cpv = portage_util.SplitCPV(dep, strict=False) |
| 39 | dep_package = package_dep_info.dependency_packages.add() |
| 40 | dep_package.package_name = cpv.package |
| 41 | dep_package.category = cpv.category |
| 42 | if cpv.version: |
| 43 | dep_package.version = cpv.version |
| 44 | |
Ned Nguyen | 8be1005 | 2019-02-26 08:58:45 -0700 | [diff] [blame^] | 45 | package_CPV = '%s/%s-%s' % ( |
| 46 | package_info.category, package_info.package_name, package_info.version) |
| 47 | for path in json_map['source_path_mapping'][package_CPV]: |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 48 | source_path = package_dep_info.dependency_source_paths.add() |
| 49 | source_path.path = path |
| 50 | |
| 51 | return graph |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 52 | |
| 53 | def GetBuildDependencyGraph(input_proto, output_proto): |
| 54 | """Create the build dependency graph. |
| 55 | |
| 56 | Args: |
| 57 | input_proto (GetBuildDependencyGraphRequest): The input arguments message. |
| 58 | output_proto (GetBuildDependencyGraphResponse): The empty output message. |
| 59 | """ |
| 60 | board = input_proto.build_target.name |
| 61 | output_path = input_proto.output_path |
| 62 | |
| 63 | assert board, 'Missing build target name' |
| 64 | assert output_path, 'Missing output file' |
| 65 | |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 66 | |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 67 | json_map = dependency.GetBuildDependency(board) |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 68 | graph = CreateDepGraphProtoFromJsonMap(json_map) |
| 69 | |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 70 | with open(output_path, 'w') as f: |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame] | 71 | f.write( |
| 72 | json_format.MessageToJson(graph, including_default_value_fields=True)) |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 73 | |
| 74 | output_proto.build_dependency_graph_file = output_path |