aab210ee93916844f3e0d633985ef39ab5dd7530
[portage.git] / pym / portage / tests / bin / setup_env.py
1 # setup_env.py -- Make sure bin subdir has sane env for testing
2 # Copyright 2007 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Id$
5
6 from portage import os
7 from portage import shutil
8 from portage.tests import TestCase
9 from portage.process import spawn
10 from portage.const import PORTAGE_BIN_PATH
11
12 bindir = os.path.join(os.path.dirname(os.path.dirname(
13         os.path.abspath(__file__))),
14         "..", "..", "..", "bin", "ebuild-helpers")
15 basedir = os.path.join(os.path.dirname(os.path.dirname(
16         os.path.abspath(__file__))), "bin", "root")
17 os.environ["D"] = os.path.join(basedir, "image")
18 os.environ["T"] = os.path.join(basedir, "temp")
19 os.environ["S"] = os.path.join(basedir, "workdir")
20 os.environ["PF"] = "portage-tests-0.09-r1"
21 os.environ["PATH"] = bindir + ":" + os.environ["PATH"]
22 os.environ["PORTAGE_BIN_PATH"] = PORTAGE_BIN_PATH
23
24 def binTestsCleanup():
25         if os.access(basedir, os.W_OK):
26                 shutil.rmtree(basedir)
27 def binTestsInit():
28         binTestsCleanup()
29         os.mkdir(basedir)
30         os.mkdir(os.environ["D"])
31         os.mkdir(os.environ["T"])
32         os.mkdir(os.environ["S"])
33         os.chdir(os.environ["S"])
34
35 class BinTestCase(TestCase):
36         def __init__(self, methodName):
37                 TestCase.__init__(self, methodName)
38                 binTestsInit()
39         def __del__(self):
40                 binTestsCleanup()
41                 if hasattr(TestCase, "__del__"):
42                         TestCase.__del__(self)
43
44 def _exists_in_D(path):
45         # Note: do not use os.path.join() here, we assume D to end in /
46         return os.access(os.environ["D"] + path, os.W_OK)
47 def exists_in_D(path):
48         if not _exists_in_D(path):
49                 raise TestCase.failureException
50 def xexists_in_D(path):
51         if _exists_in_D(path):
52                 raise TestCase.failureException
53
54 def portage_func(func, args, exit_status=0):
55         # we don't care about the output of the programs,
56         # just their exit value and the state of $D
57         f = open('/dev/null', 'wb')
58         fd_pipes = {0:0,1:f.fileno(),2:f.fileno()}
59         spawn(func+" "+args, env=os.environ, fd_pipes=fd_pipes)
60         f.close()
61
62 def create_portage_wrapper(bin):
63         def derived_func(*args):
64                 newargs = list(args)
65                 newargs.insert(0, bin)
66                 return portage_func(*newargs)
67         return derived_func
68
69 for bin in os.listdir(bindir):
70         if bin.startswith("do") or \
71            bin.startswith("new") or \
72            bin.startswith("prep") or \
73            bin in ["ecompress","ecompressdir","fowners","fperms"]:
74                 globals()[bin] = create_portage_wrapper(bin)