summaryrefslogtreecommitdiffstats
path: root/pym/portage/tests/bin/setup_env.py
blob: 086bcd5f5c71924be9dd359db74a69f6f8fee5c1 (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
# setup_env.py -- Make sure bin subdir has sane env for testing
# Copyright 2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

import tempfile

from portage import os
from portage import shutil
from portage.tests import TestCase
from portage.process import spawn
from portage.const import PORTAGE_BIN_PATH

bindir = os.path.join(os.path.dirname(os.path.dirname(
	os.path.abspath(__file__))),
	"..", "..", "..", "bin", "ebuild-helpers")
basedir = None
env = None

def binTestsCleanup():
	global basedir
	if basedir is None:
		return
	if os.access(basedir, os.W_OK):
		shutil.rmtree(basedir)
		basedir = None

def binTestsInit():
	binTestsCleanup()
	global basedir, env
	basedir = tempfile.mkdtemp()
	env = os.environ.copy()
	env["D"] = os.path.join(basedir, "image")
	env["T"] = os.path.join(basedir, "temp")
	env["S"] = os.path.join(basedir, "workdir")
	env["PF"] = "portage-tests-0.09-r1"
	env["PATH"] = bindir + ":" + env["PATH"]
	env["PORTAGE_BIN_PATH"] = PORTAGE_BIN_PATH
	os.mkdir(env["D"])
	os.mkdir(env["T"])
	os.mkdir(env["S"])
	os.chdir(env["S"])

class BinTestCase(TestCase):
	def __init__(self, methodName):
		TestCase.__init__(self, methodName)
		binTestsInit()
	def __del__(self):
		binTestsCleanup()
		if hasattr(TestCase, "__del__"):
			TestCase.__del__(self)

def _exists_in_D(path):
	# Note: do not use os.path.join() here, we assume D to end in /
	return os.access(env["D"] + path, os.W_OK)
def exists_in_D(path):
	if not _exists_in_D(path):
		raise TestCase.failureException
def xexists_in_D(path):
	if _exists_in_D(path):
		raise TestCase.failureException

def portage_func(func, args, exit_status=0):
	# we don't care about the output of the programs,
	# just their exit value and the state of $D
	global env
	f = open('/dev/null', 'wb')
	fd_pipes = {0:0,1:f.fileno(),2:f.fileno()}
	spawn([func] + args.split(), env=env, fd_pipes=fd_pipes)
	f.close()

def create_portage_wrapper(bin):
	def derived_func(*args):
		newargs = list(args)
		newargs.insert(0, bin)
		return portage_func(*newargs)
	return derived_func

for bin in os.listdir(bindir):
	if bin.startswith("do") or \
	   bin.startswith("new") or \
	   bin.startswith("prep") or \
	   bin in ["ecompress","ecompressdir","fowners","fperms"]:
		globals()[bin] = create_portage_wrapper(os.path.join(bindir, bin))