summaryrefslogtreecommitdiffstats
path: root/bin/ebuild
blob: 2a03e196a3c63e4de6fdeb1c506ba0db0ff80ea7 (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
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
#!/usr/bin/python -O
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-src/portage/bin/ebuild,v 1.18.2.3 2005/05/07 04:32:59 ferringb Exp $

import getopt, os, sys

if len(sys.argv) <= 2:
	print "Usage: ebuild <ebuild file> <command> [command] ..."
	print ""
	print "See the ebuild(1) man page for more info"
	sys.exit(1)


try:
	opts, pargs = getopt.getopt(sys.argv[1:], '', ['debug', 'force'])
except getopt.GetoptError, e:
	print e
	sys.exit(1)
debug = ("--debug",'') in opts
force = ("--force",'') in opts

if "merge" in pargs:
	print "Disabling noauto in features... merge disables it. (qmerge doesn't)"
	os.environ["FEATURES"] = os.environ.get("FEATURES", "") + " -noauto"

os.environ["PORTAGE_CALLER"]="ebuild"
try:
	import portage
except ImportError:
	from os import path as osp
	sys.path.insert(0, osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym"))
	import portage

import portage_util, portage_const
import portage_dep
portage_dep._dep_check_strict = True

# do this _after_ 'import portage' to prevent unnecessary tracing
if debug and "python-trace" in portage.features:
	import portage_debug
	portage_debug.set_trace(True)

if portage.settings["NOCOLOR"] in ("yes","true") or not sys.stdout.isatty():
	import output
	output.nocolor()

ebuild = pargs.pop(0)
if not os.path.isabs(ebuild):
	mycwd = os.getcwd()
	# Try to get the non-canonical path from the PWD evironment variable, since
	# the canonical path returned from os.getcwd() may may be unusable in
	# cases where the directory stucture is built from symlinks.
	if "PWD" in os.environ and os.environ["PWD"] != mycwd and \
		os.path.realpath(os.environ["PWD"]) == mycwd:
		mycwd = portage.normalize_path(os.environ["PWD"])
	ebuild = os.path.join(mycwd, ebuild)
ebuild = portage.normalize_path(ebuild)
# portdbapi uses the canonical path for the base of the portage tree, but
# subdirectories of the base can be built from symlinks (like crossdev does).
ebuild_portdir = os.path.realpath(os.path.dirname(os.path.dirname(ebuild)))
ebuild = os.path.join(ebuild_portdir, *ebuild.split(os.path.sep)[-2:])

if not os.path.exists(ebuild):
	print "'%s' does not exist." % ebuild
	sys.exit(1)

ebuild_split = ebuild.split("/")
del ebuild_split[-2]
cpv = "/".join(ebuild_split[-2:])[:-7]

if not portage.catpkgsplit(cpv):
	print "!!! %s does not follow correct package syntax." % (cpv)
	sys.exit(1)

if ebuild.startswith(portage.root + portage_const.VDB_PATH):
	mytree = "vartree"

	portage_ebuild = portage.db[portage.root][mytree].dbapi.findname(cpv)

	if os.path.realpath(portage_ebuild) != ebuild:
		print "!!! Portage seems to think that %s is at %s" % (cpv, portage_ebuild)
		sys.exit(1)

else:
	mytree = "porttree"

	portage_ebuild = portage.portdb.findname(cpv)

	if not portage_ebuild or portage_ebuild != ebuild:
		overlay = "/".join(ebuild_split[:-2])
		os.environ["PORTDIR_OVERLAY"] = os.environ.get("PORTDIR_OVERLAY","") + " " + overlay
		print "Appending %s to PORTDIR_OVERLAY..." % overlay
		portage.close_portdbapi_caches()
		reload(portage)
		portage_ebuild = portage.portdb.findname(cpv)

		if not portage_ebuild or portage_ebuild != ebuild:
			print "!!! %s does not seem to have a valid PORTDIR structure." % overlay
			sys.exit(1)


if len(pargs) > 1 and "config" in pargs:
	print "config must be called on it's own, not combined with any other phase"
	sys.exit(1)

def discard_digests(myebuild, mysettings, mydbapi):
	"""Discard all distfiles digests for the given ebuild.  This is useful when
	upstream has changed the identity of the distfiles and the user would
	otherwise have to manually remove the Manifest and files/digest-* files in
	order to ensure correct results."""
	try:
		portage._doebuild_manifest_exempt_depend += 1
		pkgdir = os.path.dirname(myebuild)
		fetchlist_dict = portage.FetchlistDict(pkgdir, mysettings, mydbapi)
		cat, pkg = pkgdir.split(os.sep)[-2:]
		cpv = cat + "/" + os.path.basename(myebuild)[:-7]
		from portage_manifest import Manifest
		mf = Manifest(pkgdir, mysettings["DISTDIR"],
			fetchlist_dict=fetchlist_dict)
		mf.create(requiredDistfiles=None,
			assumeDistHashesSometimes=True, assumeDistHashesAlways=True)
		distfiles = fetchlist_dict[cpv]
		for myfile in distfiles:
			try:
				del mf.fhashdict["DIST"][myfile]
			except KeyError:
				pass
		mf.write()
	finally:
		portage._doebuild_manifest_exempt_depend -= 1

tmpsettings = portage.config(clone=portage.settings)
if "test" in pargs and "test" not in tmpsettings.features:
	print "Forcing test."
	tmpsettings["EBUILD_FORCE_TEST"] = "1"
	tmpsettings.backup_changes("EBUILD_FORCE_TEST")
	tmpsettings.features.append("test")
	tmpsettings["FEATURES"] = " ".join(tmpsettings.features)
	tmpsettings.backup_changes("FEATURES")

build_dir_phases = set(["setup", "unpack", "compile",
	"test", "install", "package", "rpm"])

def stale_env_warning():
	if "clean" not in pargs and \
		"noauto" not in tmpsettings.features and \
		tmpsettings.get("PORTAGE_QUIET") != "1" and \
		build_dir_phases.intersection(pargs):
		portage.doebuild_environment(ebuild, "setup", portage.root,
			tmpsettings, debug, 1, portage.portdb)
		env_filename = os.path.join(tmpsettings["T"], "environment")
		if os.path.exists(env_filename):
			msg = ("Existing ${T}/environment for '%s' will be sourced. " + \
				"Run 'clean' to start with a fresh environment.") % \
				(tmpsettings["PF"], )
			from textwrap import wrap
			msg = wrap(msg, 70)
			for x in msg:
				portage.writemsg(">>> %s\n" % x)

checked_for_stale_env = False

for arg in pargs:
	if not checked_for_stale_env and arg not in ("digest","manifest"):
		# This has to go after manifest generation since otherwise
		# aux_get() might fail due to invalid ebuild digests.
		stale_env_warning()
		checked_for_stale_env = True
	try:
		if arg == "digest" and force:
			discard_digests(ebuild, tmpsettings, portage.portdb)
		a = portage.doebuild(ebuild, arg, portage.root, tmpsettings,
			debug=debug, tree=mytree)
	except KeyboardInterrupt:
		print "Interrupted."
		a = 1
	except KeyError:
		# aux_get error
		a = 1
	if a == None:
		print "Could not run the required binary?"
		a = 127
	if a:
		sys.exit(a)