Welcome to mirror list, hosted at ThFree Co, Russian Federation.

absolute_and_relative_filename.py « codesign « buildbot « build_files - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bea9ea7e8d0564945ee3a7b403bdcbe7c3ba0e7a (plain)
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# <pep8 compliant>

from dataclasses import dataclass
from pathlib import Path
from typing import List


@dataclass
class AbsoluteAndRelativeFileName:
    """
    Helper class which keeps track of absolute file path for a direct access and
    corresponding relative path against given base.

    The relative part is used to construct a file name within an archive which
    contains files which are to be signed or which has been signed already
    (depending on whether the archive is addressed to signing server or back
    to the buildbot worker).
    """

    # Base directory which is where relative_filepath is relative to.
    base_dir: Path

    # Full absolute path of the corresponding file.
    absolute_filepath: Path

    # Derived from full file path, contains part of the path which is relative
    # to a desired base path.
    relative_filepath: Path

    def __init__(self, base_dir: Path, filepath: Path):
        self.base_dir = base_dir
        self.absolute_filepath = filepath.resolve()
        self.relative_filepath = self.absolute_filepath.relative_to(
            self.base_dir)

    @classmethod
    def from_path(cls, path: Path) -> 'AbsoluteAndRelativeFileName':
        assert path.is_absolute()
        assert path.is_file()

        base_dir = path.parent
        return AbsoluteAndRelativeFileName(base_dir, path)

    @classmethod
    def recursively_from_directory(cls, base_dir: Path) \
            -> List['AbsoluteAndRelativeFileName']:
        """
        Create list of AbsoluteAndRelativeFileName for all the files in the
        given directory.
        """
        assert base_dir.is_absolute()
        assert base_dir.is_dir()

        result = []
        for filename in base_dir.glob('**/*'):
            if not filename.is_file():
                continue
            result.append(AbsoluteAndRelativeFileName(base_dir, filename))
        return result