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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# Copyright 2012-2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
import subprocess
import sys
import textwrap
import portage
from portage import os
from portage import _unicode_decode
from portage.const import BASH_BINARY, PORTAGE_PYM_PATH, USER_CONFIG_PATH
from portage.tests import TestCase
from portage.tests.resolver.ResolverPlayground import ResolverPlayground
from portage.util import ensure_dirs
class PortdbCacheTestCase(TestCase):
def testPortdbCache(self):
debug = False
ebuilds = {
"dev-libs/A-1": {},
"dev-libs/A-2": {},
"sys-apps/B-1": {},
"sys-apps/B-2": {},
}
playground = ResolverPlayground(ebuilds=ebuilds, debug=debug)
settings = playground.settings
eprefix = settings["EPREFIX"]
test_repo_location = settings.repositories["test_repo"].location
user_config_dir = os.path.join(eprefix, USER_CONFIG_PATH)
metadata_dir = os.path.join(test_repo_location, "metadata")
md5_cache_dir = os.path.join(metadata_dir, "md5-cache")
pms_cache_dir = os.path.join(metadata_dir, "cache")
layout_conf_path = os.path.join(metadata_dir, "layout.conf")
portage_python = portage._python_interpreter
egencache_cmd = (
portage_python,
"-b",
"-Wd",
os.path.join(self.bindir, "egencache"),
"--update-manifests",
"--sign-manifests=n",
"--repo",
"test_repo",
"--repositories-configuration",
settings.repositories.config_string(),
)
python_cmd = (portage_python, "-b", "-Wd", "-c")
test_commands = (
(lambda: not os.path.exists(pms_cache_dir),),
(lambda: not os.path.exists(md5_cache_dir),),
python_cmd
+ (
textwrap.dedent(
"""
import os, sys, portage
if portage.portdb.repositories['test_repo'].location in portage.portdb._pregen_auxdb:
sys.exit(1)
"""
),
),
egencache_cmd + ("--update",),
(lambda: not os.path.exists(pms_cache_dir),),
(lambda: os.path.exists(md5_cache_dir),),
python_cmd
+ (
textwrap.dedent(
"""
import os, sys, portage
if portage.portdb.repositories['test_repo'].location not in portage.portdb._pregen_auxdb:
sys.exit(1)
"""
),
),
python_cmd
+ (
textwrap.dedent(
"""
import os, sys, portage
from portage.cache.flat_hash import md5_database
if not isinstance(portage.portdb._pregen_auxdb[portage.portdb.repositories['test_repo'].location], md5_database):
sys.exit(1)
"""
),
),
(
BASH_BINARY,
"-c",
"echo %s > %s"
% tuple(
map(
portage._shell_quote,
(
"cache-formats = md5-dict pms",
layout_conf_path,
),
)
),
),
egencache_cmd + ("--update",),
(lambda: os.path.exists(md5_cache_dir),),
python_cmd
+ (
textwrap.dedent(
"""
import os, sys, portage
if portage.portdb.repositories['test_repo'].location not in portage.portdb._pregen_auxdb:
sys.exit(1)
"""
),
),
python_cmd
+ (
textwrap.dedent(
"""
import os, sys, portage
from portage.cache.flat_hash import md5_database
if not isinstance(portage.portdb._pregen_auxdb[portage.portdb.repositories['test_repo'].location], md5_database):
sys.exit(1)
"""
),
),
# Disable DeprecationWarnings, since the pms format triggers them
# in portdbapi._create_pregen_cache().
(
BASH_BINARY,
"-c",
"echo %s > %s"
% tuple(
map(
portage._shell_quote,
(
"cache-formats = pms md5-dict",
layout_conf_path,
),
)
),
),
(portage_python, "-b", "-Wd", "-Wi::DeprecationWarning", "-c")
+ (
textwrap.dedent(
"""
import os, sys, portage
if portage.portdb.repositories['test_repo'].location not in portage.portdb._pregen_auxdb:
sys.exit(1)
"""
),
),
(portage_python, "-b", "-Wd", "-Wi::DeprecationWarning", "-c")
+ (
textwrap.dedent(
"""
import os, sys, portage
from portage.cache.metadata import database as pms_database
if not isinstance(portage.portdb._pregen_auxdb[portage.portdb.repositories['test_repo'].location], pms_database):
sys.exit(1)
"""
),
),
# Test auto-detection and preference for md5-cache when both
# cache formats are available but layout.conf is absent.
(BASH_BINARY, "-c", "rm %s" % portage._shell_quote(layout_conf_path)),
python_cmd
+ (
textwrap.dedent(
"""
import os, sys, portage
if portage.portdb.repositories['test_repo'].location not in portage.portdb._pregen_auxdb:
sys.exit(1)
"""
),
),
python_cmd
+ (
textwrap.dedent(
"""
import os, sys, portage
from portage.cache.flat_hash import md5_database
if not isinstance(portage.portdb._pregen_auxdb[portage.portdb.repositories['test_repo'].location], md5_database):
sys.exit(1)
"""
),
),
)
pythonpath = os.environ.get("PYTHONPATH")
if pythonpath is not None and not pythonpath.strip():
pythonpath = None
if pythonpath is not None and pythonpath.split(":")[0] == PORTAGE_PYM_PATH:
pass
else:
if pythonpath is None:
pythonpath = ""
else:
pythonpath = ":" + pythonpath
pythonpath = PORTAGE_PYM_PATH + pythonpath
env = {
"PATH": os.environ.get("PATH", ""),
"PORTAGE_OVERRIDE_EPREFIX": eprefix,
"PORTAGE_PYTHON": portage_python,
"PORTAGE_REPOSITORIES": settings.repositories.config_string(),
"PYTHONDONTWRITEBYTECODE": os.environ.get("PYTHONDONTWRITEBYTECODE", ""),
"PYTHONPATH": pythonpath,
}
if "__PORTAGE_TEST_HARDLINK_LOCKS" in os.environ:
env["__PORTAGE_TEST_HARDLINK_LOCKS"] = os.environ[
"__PORTAGE_TEST_HARDLINK_LOCKS"
]
dirs = [user_config_dir]
try:
for d in dirs:
ensure_dirs(d)
if debug:
# The subprocess inherits both stdout and stderr, for
# debugging purposes.
stdout = None
else:
# The subprocess inherits stderr so that any warnings
# triggered by python -Wd will be visible.
stdout = subprocess.PIPE
for i, args in enumerate(test_commands):
if hasattr(args[0], "__call__"):
self.assertTrue(args[0](), "callable at index %s failed" % (i,))
continue
proc = subprocess.Popen(args, env=env, stdout=stdout)
if debug:
proc.wait()
else:
output = proc.stdout.readlines()
proc.wait()
proc.stdout.close()
if proc.returncode != os.EX_OK:
for line in output:
sys.stderr.write(_unicode_decode(line))
self.assertEqual(
os.EX_OK,
proc.returncode,
"command %d failed with args %s"
% (
i,
args,
),
)
finally:
playground.cleanup()
|