• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2025 Northeastern University
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# 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
19from typing import List, Dict, Any, Optional
20
21from ohos.sbom.data.file_dependence import File
22from ohos.sbom.data.manifest import Project
23from ohos.sbom.sbom.metadata.sbom_meta_data import RelationshipType
24
25
26class ProjectDependence:
27    __slots__ = ('_source_project', '_dependencies')
28
29    def __init__(self, source: Project):
30        self._source_project = source
31        self._dependencies = {
32            type: set()
33            for type in RelationshipType
34        }
35
36    @property
37    def source_project(self) -> Project:
38        return self._source_project
39
40    def add_dependency(self, dep_type: RelationshipType, other) -> None:
41        if dep_type not in self._dependencies:
42            raise ValueError(f"Invalid dependency type: {dep_type}")
43        self._dependencies[dep_type].add(other)
44
45    def add_dependency_list(self, dep_type: RelationshipType, others: List) -> None:
46        for other in others:
47            self.add_dependency(dep_type, other)
48
49    def get_dependencies(self, dep_type: Optional[RelationshipType] = None):
50        if dep_type:
51            return self._dependencies.get(dep_type, set())
52        return self._dependencies
53
54    def to_dict(self) -> Dict[str, Any]:
55        deps_dict = {}
56
57        for rel_type in RelationshipType:
58            items = []
59            for obj in self._dependencies[rel_type]:
60                if isinstance(obj, File):
61                    items.append(obj.relative_path)
62                else:
63                    items.append(getattr(obj, "name", str(obj)))
64            deps_dict[rel_type.value] = sorted(set(items))
65
66        return {
67            "source_project": self.source_project.name,
68            "dependencies": deps_dict
69        }
70