aboutsummaryrefslogtreecommitdiff
path: root/cli/lib/environment/sysroot_stub.py
blob: 059d552c2a9884295c60ced7e09f9a4ff97395b8 (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
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
#
# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


"""A stub for the Sysroot class."""


import os

import error


class Error(error.Error):
    pass


class StubSysroot(object):
    """Replacement for Sysroots."""

    def __init__(self, path):
        self.path = path
        self.should_write = []
        self.last_written = ''
        self.written = []
        self.should_add_file = []
        self.should_add_dir = []
        self.should_add_glob = []
        self.should_pass_filter = []
        self.should_fail_filter = []
        self.should_makedirs = []

    def Path(self, *path):
        return os.path.join(self.path, *path)

    def WriteFile(self, path, contents):
        if path in self.should_write:
            self.last_written = contents
            self.written.append(contents)
            self.should_write.remove(path)
        else:
            raise IOError('not supposed to write {0}'.format(path))

    # pylint: disable=unused-argument
    def AddFile(self, src, dest='', overwrite=False):
        if not (src, dest) in self.should_add_file:
            raise IOError('Not supposed to add file {0} to {1}'.format(src,
                                                                       dest))
        self.should_add_file.remove((src, dest))

    # pylint: disable=unused-argument
    def AddDir(self, src, dest=None, recurse=True, filter_func=None,
               symlinks=None):
        dest = dest or ''
        proper_filter = True
        if filter_func:
            proper_filter = (
                [f for f in self.should_pass_filter
                 if filter_func(f)] == self.should_pass_filter and
                [f for f in self.should_fail_filter if filter_func(f)] == [])

        if not proper_filter:
            raise Error('AddDir called with improper filter')
        if not (src, dest, recurse) in self.should_add_dir:
            raise IOError(('Not supposed to add dir "{}" to "{}", '
                           'recursive: {}').format(src, dest, recurse))

        self.should_add_dir.remove((src, dest, recurse))
        # This should walk path.should_exist if recurse.
        return [dest]

    # pylint: disable=unused-argument
    def AddGlob(self, src, dest=None, recurse=True, filter_func=None,
                symlinks=None):
        dest = dest or ''
        proper_filter = True
        if filter_func:
            proper_filter = (
                [f for f in self.should_pass_filter
                 if filter_func(f)] == self.should_pass_filter and
                [f for f in self.should_fail_filter if filter_func(f)] == [])

        if not proper_filter:
            raise Error('AddGlob called with improper filter')
        if not (src, dest, recurse) in self.should_add_glob:
            raise IOError(('Not supposed to add glob "{}" to "{}", '
                           'recursive: {}').format(src, dest, recurse))

        self.should_add_glob.remove((src, dest, recurse))
        # This should walk path.should_exist if recurse.
        return [dest]

    def Makedirs(self, dirs):
        if not dirs in self.should_makedirs:
            raise IOError('Could not make dirs {0}'.format(dirs))

    def Destroy(self):
        pass

class StubSysrootGenerator(object):
    """Generates stub sysroots."""

    def __init__(self):
        self.should_write = []
        self.last_written = ''
        self.should_add_file = []
        self.should_add_dir = []
        self.should_add_glob = []
        self.should_pass_filter = []
        self.should_fail_filter = []
        self.should_makedirs = []
        self.sysroots = []

    # pylint: disable=unused-argument
    def Sysroot(self, path, copy_newer_only=False):
        gen = StubSysroot(path)
        self.sysroots.append(gen)
        gen.should_write = self.should_write
        gen.last_written = self.last_written
        gen.should_add_file = self.should_add_file
        gen.should_add_dir = self.should_add_dir
        gen.should_add_glob = self.should_add_glob
        gen.should_pass_filter = self.should_pass_filter
        gen.should_fail_filter = self.should_fail_filter
        gen.should_makedirs = self.should_makedirs
        return gen