File: ProjectMap.py

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (208 lines) | stat: -rw-r--r-- 6,828 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import json
import os

from enum import auto, Enum
from typing import Any, Dict, List, NamedTuple, Optional, Tuple


JSON = Dict[str, Any]


DEFAULT_MAP_FILE = "projects.json"


class DownloadType(str, Enum):
    GIT = "git"
    ZIP = "zip"
    SCRIPT = "script"


class Size(int, Enum):
    """
    Size of the project.

    Sizes do not directly correspond to the number of lines or files in the
    project.  The key factor that is important for the developers of the
    analyzer is the time it takes to analyze the project.  Here is how
    the following sizes map to times:

    TINY:  <1min
    SMALL: 1min-10min
    BIG:   10min-1h
    HUGE:  >1h

    The borders are a bit of a blur, especially because analysis time varies
    from one machine to another.  However, the relative times will stay pretty
    similar, and these groupings will still be helpful.

    UNSPECIFIED is a very special case, which is intentionally last in the list
    of possible sizes.  If the user wants to filter projects by one of the
    possible sizes, we want projects with UNSPECIFIED size to be filtered out
    for any given size.
    """
    TINY = auto()
    SMALL = auto()
    BIG = auto()
    HUGE = auto()
    UNSPECIFIED = auto()

    @staticmethod
    def from_str(raw_size: Optional[str]) -> "Size":
        """
        Construct a Size object from an optional string.

        :param raw_size: optional string representation of the desired Size
                         object.  None will produce UNSPECIFIED size.

        This method is case-insensitive, so raw sizes 'tiny', 'TINY', and
        'TiNy' will produce the same result.
        """
        if raw_size is None:
            return Size.UNSPECIFIED

        raw_size_upper = raw_size.upper()
        # The implementation is decoupled from the actual values of the enum,
        # so we can easily add or modify it without bothering about this
        # function.
        for possible_size in Size:
            if possible_size.name == raw_size_upper:
                return possible_size

        possible_sizes = [size.name.lower() for size in Size
                          # no need in showing our users this size
                          if size != Size.UNSPECIFIED]
        raise ValueError(f"Incorrect project size '{raw_size}'. "
                         f"Available sizes are {possible_sizes}")


class ProjectInfo(NamedTuple):
    """
    Information about a project to analyze.
    """
    name: str
    mode: int
    source: DownloadType = DownloadType.SCRIPT
    origin: str = ""
    commit: str = ""
    enabled: bool = True
    size: Size = Size.UNSPECIFIED

    def with_fields(self, **kwargs) -> "ProjectInfo":
        """
        Create a copy of this project info with customized fields.
        NamedTuple is immutable and this is a way to create modified copies.

          info.enabled = True
          info.mode = 1

        can be done as follows:

          modified = info.with_fields(enbled=True, mode=1)
        """
        return ProjectInfo(**{**self._asdict(), **kwargs})


class ProjectMap:
    """
    Project map stores info about all the "registered" projects.
    """
    def __init__(self, path: Optional[str] = None, should_exist: bool = True):
        """
        :param path: optional path to a project JSON file, when None defaults
                     to DEFAULT_MAP_FILE.
        :param should_exist: flag to tell if it's an exceptional situation when
                             the project file doesn't exist, creates an empty
                             project list instead if we are not expecting it to
                             exist.
        """
        if path is None:
            path = os.path.join(os.path.abspath(os.curdir), DEFAULT_MAP_FILE)

        if not os.path.exists(path):
            if should_exist:
                raise ValueError(
                    f"Cannot find the project map file {path}"
                    f"\nRunning script for the wrong directory?\n")
            else:
                self._create_empty(path)

        self.path = path
        self._load_projects()

    def save(self):
        """
        Save project map back to its original file.
        """
        self._save(self.projects, self.path)

    def _load_projects(self):
        with open(self.path) as raw_data:
            raw_projects = json.load(raw_data)

            if not isinstance(raw_projects, list):
                raise ValueError(
                    "Project map should be a list of JSON objects")

            self.projects = self._parse(raw_projects)

    @staticmethod
    def _parse(raw_projects: List[JSON]) -> List[ProjectInfo]:
        return [ProjectMap._parse_project(raw_project)
                for raw_project in raw_projects]

    @staticmethod
    def _parse_project(raw_project: JSON) -> ProjectInfo:
        try:
            name: str = raw_project["name"]
            build_mode: int = raw_project["mode"]
            enabled: bool = raw_project.get("enabled", True)
            source: DownloadType = raw_project.get("source", "zip")
            size = Size.from_str(raw_project.get("size", None))

            if source == DownloadType.GIT:
                origin, commit = ProjectMap._get_git_params(raw_project)
            else:
                origin, commit = "", ""

            return ProjectInfo(name, build_mode, source, origin, commit,
                               enabled, size)

        except KeyError as e:
            raise ValueError(
                f"Project info is required to have a '{e.args[0]}' field")

    @staticmethod
    def _get_git_params(raw_project: JSON) -> Tuple[str, str]:
        try:
            return raw_project["origin"], raw_project["commit"]
        except KeyError as e:
            raise ValueError(
                f"Profect info is required to have a '{e.args[0]}' field "
                f"if it has a 'git' source")

    @staticmethod
    def _create_empty(path: str):
        ProjectMap._save([], path)

    @staticmethod
    def _save(projects: List[ProjectInfo], path: str):
        with open(path, "w") as output:
            json.dump(ProjectMap._convert_infos_to_dicts(projects),
                      output, indent=2)

    @staticmethod
    def _convert_infos_to_dicts(projects: List[ProjectInfo]) -> List[JSON]:
        return [ProjectMap._convert_info_to_dict(project)
                for project in projects]

    @staticmethod
    def _convert_info_to_dict(project: ProjectInfo) -> JSON:
        whole_dict = project._asdict()
        defaults = project._field_defaults

        # there is no need in serializing fields with default values
        for field, default_value in defaults.items():
            if whole_dict[field] == default_value:
                del whole_dict[field]

        return whole_dict