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 | |
| 45 | package_CP = '%s/%s' % (package_info.category, package_info.package_name) |
| 46 | for path in json_map['source_path_mapping'][package_CP]: |
| 47 | source_path = package_dep_info.dependency_source_paths.add() |
| 48 | source_path.path = path |
| 49 | |
| 50 | return graph |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 51 | |
| 52 | def GetBuildDependencyGraph(input_proto, output_proto): |
| 53 | """Create the build dependency graph. |
| 54 | |
| 55 | Args: |
| 56 | input_proto (GetBuildDependencyGraphRequest): The input arguments message. |
| 57 | output_proto (GetBuildDependencyGraphResponse): The empty output message. |
| 58 | """ |
| 59 | board = input_proto.build_target.name |
| 60 | output_path = input_proto.output_path |
| 61 | |
| 62 | assert board, 'Missing build target name' |
| 63 | assert output_path, 'Missing output file' |
| 64 | |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame^] | 65 | |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 66 | json_map = dependency.GetBuildDependency(board) |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame^] | 67 | graph = CreateDepGraphProtoFromJsonMap(json_map) |
| 68 | |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 69 | with open(output_path, 'w') as f: |
Ned Nguyen | e5d0485 | 2019-02-19 16:33:15 -0700 | [diff] [blame^] | 70 | f.write( |
| 71 | json_format.MessageToJson(graph, including_default_value_fields=True)) |
Ned Nguyen | 9a7a905 | 2019-02-05 11:04:03 -0700 | [diff] [blame] | 72 | |
| 73 | output_proto.build_dependency_graph_file = output_path |