summaryrefslogtreecommitdiffstats
path: root/bin/quickpkg
blob: 7121d65d08bcaaf986a61ecb08e942a7d26cca43 (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
#!/bin/bash
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

# This script tries to quickly create a Gentoo binary package using the
# VDB_PATH/category/pkg/*  files
#
# Resulting tbz2 file will be created in ${PKGDIR} ...
# default is /usr/portage/packages/All/

if [ "${UID}" != "0" ] ; then
	echo "You must run this as root"
	exit 1
fi

# We need to ensure a sane umask for the packages that will be created.
umask 022

eval $(portageq envvar -v NOCOLOR PKGDIR PORTAGE_BIN_PATH PORTAGE_NICENESS \
	PORTAGE_PYM_PATH PORTAGE_TMPDIR ROOT)
export PKGDIR PORTAGE_TMPDIR ROOT

[ -n "${PORTAGE_NICENESS}" ] && renice $PORTAGE_NICENESS $$ > /dev/null

# Make sure the xpak module is in PYTHONPATH
export PYTHONPATH=${PORTAGE_PYM_PATH}
export PORTAGE_DB=$(portageq vdb_path)

if [ -z "$1" ] || [ $1 == "-h" ] || [ $1 == "--help" ] ; then
	echo "QUICKPKG ver 1.2"
	echo "USAGE: quickpkg <list of pkgs>"
	echo "    a pkg can be of the form:"
	echo "        - ${PORTAGE_DB}/<CATEGORY>/<PKG-VERSION>/"
	echo "        - single depend-type atom ..."
	echo "              if portage can emerge it, quickpkg can make a package"
	echo "              for exact definitions of depend atoms, see ebuild(5)"
	echo
	echo "EXAMPLE:"
	echo "    quickpkg ${PORTAGE_DB}/net-www/apache-1.3.27-r1"
	echo "        package up apache, just version 1.3.27-r1"
	echo "    quickpkg apache"
	echo "        package up apache, all versions of apache installed"
	echo "    quickpkg =apache-1.3.27-r1"
	echo "        package up apache, just version 1.3.27-r1"
	exit 1
fi

source "${PORTAGE_BIN_PATH}/isolated-functions.sh"
case "${NOCOLOR:-false}" in
	yes|true)
		unset_colors
		;;
	no|false)
		set_colors
		;;
esac

# here we make a package given a little info
# $1 = package-name w/version
# $2 = category
do_pkg() {
	mkdir -p "${PORTAGE_TMPDIR}/binpkgs" || exit 1
	chmod 0750 "${PORTAGE_TMPDIR}/binpkgs"
	MYDIR="${PORTAGE_TMPDIR}/binpkgs/$1"
	SRCDIR="${PORTAGE_DB}/$2/$1"
	LOG="${PORTAGE_TMPDIR}/binpkgs/$1-quickpkglog"

	ebegin "Building package for $1"
	(
		# clean up temp directory
		rm -rf "${MYDIR}"

		# get pkg info files
		mkdir -p "${MYDIR}"/temp
		cp "${SRCDIR}"/* "${MYDIR}"/temp/
		[ -d "${PKGDIR}"/All ] ||  mkdir -p "${PKGDIR}"/All
		local pkg_dest="${PKGDIR}/All/${1}.tbz2"
		local pkg_tmp="${PKGDIR}/All/${1}.tbz2.$$"

		# create filelist and a basic tbz2
		gawk '{
			if ($1 != "dir") {
				if ($1 == "obj")
					NF=NF-2
				else if ($1 == "sym")
					NF=NF-3
			}
			print
		}' "${SRCDIR}"/CONTENTS | cut -f2- -d" " - | sed -e 's:^/:./:' | \
		while read f; do
			[ -d "${ROOT}/${f}" ] && [ -h "${ROOT}/${f}" ] && continue
			echo "$f"
		done > "${MYDIR}"/filelist
		tar vjcf "${pkg_tmp}" -C "${ROOT}" --files-from="${MYDIR}"/filelist --no-recursion

		# join together the basic tbz2 and the pkg info files
		python -c "import xpak; t=xpak.tbz2('${pkg_tmp}'); t.recompose('${MYDIR}/temp')"

		# move the final binary package to PKGDIR
		mv -f "${pkg_tmp}" "${pkg_dest}"
		[ -d "${PKGDIR}/$2" ] || mkdir -p "${PKGDIR}/$2"
		( cd "${PKGDIR}/$2" && ln -s ../All/$1.tbz2 )

		# cleanup again
		rm -rf "${MYDIR}"
	) >& "${LOG}"

	if [ -e "${PKGDIR}/All/$1.tbz2" ] ; then
		rm -f "${LOG}"
		PKGSTATS="${PKGSTATS}"$'\n'"$(einfo $1: $(du -h "${PKGDIR}/All/$1.tbz2" | gawk '{print $1}'))"
		eend 0
	else
		cat ${LOG}
		PKGSTATS="${PKGSTATS}"$'\n'"$(ewarn $1: not created)"
		eend 1
	fi
}

# here we parse the parameters given to use on the cmdline
export PKGERROR=""
export PKGSTATS=""
for x in "$@" ; do

	# they gave us full path
	if [ -e "${x}"/CONTENTS ] ; then
		x=$(readlink -f $x)
		pkg=$(echo ${x} | cut -d/ -f6)
		cat=$(echo ${x} | cut -d/ -f5)
		do_pkg "${pkg}" "${cat}"

	# lets figure out what they want
	else
		DIRLIST=$(portageq match "${ROOT}" "${x}")
		if [ -z "${DIRLIST}" ] ; then
			eerror "Could not find anything to match '${x}'; skipping"
			export PKGERROR="${PKGERROR} ${x}"
			continue
		fi

		for d in ${DIRLIST} ; do
			pkg=$(echo ${d} | cut -d/ -f2)
			cat=$(echo ${d} | cut -d/ -f1)
			if [ -f "${PORTAGE_DB}/${cat}/${pkg}/CONTENTS" ] ; then
				do_pkg ${pkg} ${cat}
			elif [ -d "${PORTAGE_DB}/${cat}/${pkg}" ] ; then
				ewarn "Package '${cat}/${pkg}' was injected; skipping"
			else
				eerror "Unhandled case (${cat}/${pkg}) !"
				eerror "Please file a bug at http://bugs.gentoo.org/"
				exit 10
			fi
		done
	fi

done

if [ -z "${PKGSTATS}" ] ; then
	eerror "No packages found"
	exit 1
else
	echo $'\n'"$(einfo Packages now in ${PKGDIR}:)${PKGSTATS}"
fi
if [ ! -z "${PKGERROR}" ] ; then
	ewarn "The following packages could not be found:"
	ewarn "${PKGERROR}"
	exit 2
fi

exit 0