]> begriffs open source - cmsis-freertos/blob - Test/litani/lib/output_artifact.py
Update README.md - branch main is now the base branch
[cmsis-freertos] / Test / litani / lib / output_artifact.py
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License").
4 # You may not use this file except in compliance with the License.
5 # A copy of the License is located at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # or in the "license" file accompanying this file. This file is distributed
10 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 # express or implied. See the License for the specific language governing
12 # permissions and limitations under the License.
13
14
15 import dataclasses
16 import logging
17 import os
18 import pathlib
19 import shutil
20
21
22
23 class MissingOutput(Exception):
24     pass
25
26
27
28 @dataclasses.dataclass
29 class Copier:
30     """Copy output artifacts to a directory, raising MissingOutput if they don't exist"""
31
32     artifacts_dir: pathlib.Path
33     job_args: dict
34
35
36     def copy_output_artifact(self, fyle):
37         try:
38             if os.path.isfile(fyle):
39                 shutil.copy(fyle, self.artifacts_dir)
40                 return
41             if os.path.isdir(fyle):
42                 shutil.copytree(fyle, self.artifacts_dir, dirs_exist_ok=True)
43                 return
44             raise FileNotFoundError
45         except FileNotFoundError as e:
46             if "phony_outputs" not in self.job_args:
47                 raise MissingOutput() from e
48
49             if self.job_args["phony_outputs"] is None:
50                 raise MissingOutput() from e
51
52             if not self.job_args["phony_outputs"]:
53                 # User supplied an empty list of phony outputs, so all outputs
54                 # are considered phony
55                 return
56
57             if fyle in self.job_args["phony_outputs"]:
58                 return
59
60             raise MissingOutput() from e