xref: /aosp_15_r20/external/pigweed/pw_package/py/pw_package/packages/crlset.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Install and check status of CRLSet and download chromium CRLSet."""
15
16import os
17import pathlib
18import subprocess
19from typing import Sequence
20import pw_package.git_repo
21import pw_package.package_manager
22
23
24def crlset_tools_repo_path(path: pathlib.Path) -> pathlib.Path:
25    return path / 'crlset-tools'
26
27
28def crlset_exec_path(path: pathlib.Path) -> pathlib.Path:
29    return path / 'crlset_exec'
30
31
32def crlset_file_path(path: pathlib.Path) -> pathlib.Path:
33    return path / 'crlset'
34
35
36class CRLSet(pw_package.package_manager.Package):
37    """Install and check status of CRLSet and downloaded CLRSet file."""
38
39    def __init__(self, *args, **kwargs):
40        super().__init__(*args, name='crlset', **kwargs)
41        self._crlset_tools = pw_package.git_repo.GitRepo(
42            name='crlset-tools',
43            url='https://github.com/agl/crlset-tools.git',
44            commit='1a1019bb500f93bc2b847a57cdbaede847649b99',
45        )
46
47    def status(self, path: pathlib.Path) -> bool:
48        if not self._crlset_tools.status(crlset_tools_repo_path(path)):
49            return False
50
51        # The executable should have been built and exist.
52        if not os.path.exists(crlset_exec_path(path)):
53            return False
54
55        # A crlset has been downloaded
56        if not os.path.exists(crlset_file_path(path)):
57            return False
58
59        return True
60
61    def install(self, path: pathlib.Path) -> None:
62        self._crlset_tools.install(crlset_tools_repo_path(path))
63
64        # Build the go tool
65        subprocess.run(
66            ['go', 'build', '-o', crlset_exec_path(path), 'crlset.go'],
67            check=True,
68            cwd=crlset_tools_repo_path(path),
69        )
70
71        crlset_tools_exec = crlset_exec_path(path)
72        if not os.path.exists(crlset_tools_exec):
73            raise FileNotFoundError('Fail to find crlset executable')
74
75        # Download the latest CRLSet with the go tool
76        with open(crlset_file_path(path), 'wb') as crlset_file:
77            fetched = subprocess.run(
78                [crlset_exec_path(path), 'fetch'],
79                capture_output=True,
80                check=True,
81            ).stdout
82            crlset_file.write(fetched)
83
84    def info(self, path: pathlib.Path) -> Sequence[str]:
85        return (
86            f'{self.name} installed in: {path}',
87            "Enable by running 'gn args out' and adding this line:",
88            f'  pw_tls_client_CRLSET_PATH = "{crlset_file_path(path)}"',
89        )
90
91
92pw_package.package_manager.register(CRLSet)
93