Add back a missing `source isolated-functions.sh`. The color logic is handled intern...
[portage.git] / bin / quickpkg
1 #!/bin/bash
2 # Copyright 1999-2007 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Id$
5
6 # This script tries to quickly create a Gentoo binary package using the
7 # VDB_PATH/category/pkg/*  files
8 #
9 # Resulting tbz2 file will be created in ${PKGDIR} ...
10 # default is /usr/portage/packages/All/
11
12 if [[ ${UID} != "0" ]] ; then
13         echo "You must run this as root"
14         exit 1
15 fi
16
17 # We need to ensure a sane umask for the packages that will be created.
18 umask 022
19
20 # We need this portage cruft to be before --help output because
21 # we utilize some of these vars in the usage info :/
22 eval $(portageq envvar -v NOCOLOR PKGDIR PORTAGE_BIN_PATH PORTAGE_NICENESS \
23         PORTAGE_PYM_PATH PORTAGE_TMPDIR ROOT)
24 export PKGDIR PORTAGE_TMPDIR ROOT
25
26 [[ -n ${PORTAGE_NICENESS} ]] && renice $PORTAGE_NICENESS $$ > /dev/null
27
28 # Make sure the xpak module is in PYTHONPATH
29 export PYTHONPATH=${PORTAGE_PYM_PATH}
30 export PORTAGE_DB=$(portageq vdb_path)
31
32 version() {
33         local svnrev='$Rev$'
34         svnrev=${svnrev#* }
35         echo "quickpkg-${svnrev% *}"
36         exit 0
37 }
38 usage() {
39         cat <<-EOF
40         Usage: quickpkg [options] <list of pkgs>
41         
42         Options:
43           -C, --nocolor    Disable color output
44           -x, --debug      Run with shell debug turned on
45           -V, --version    Print version and exit
46           -h, --help       This cruft output
47         
48         A pkg can be of the form:
49           - ${PORTAGE_DB}/<CATEGORY>/<PKG-VERSION>/
50           - single depend-type atom ...
51               if portage can emerge it, quickpkg can make a package
52               for exact definitions of depend atoms, see ebuild(5)
53         
54         Examples:
55             quickpkg ${PORTAGE_DB}/net-www/apache-1.3.27-r1
56                 package up apache, just version 1.3.27-r1
57             quickpkg apache
58                 package up apache, all versions of apache installed
59             quickpkg =apache-1.3.27-r1
60                 quickpkg =apache-1.3.27-r1
61         EOF
62         if [[ -n $1 ]] ; then
63                 echo ""
64                 echo "Unknown arguments: $*" 1>&2
65                 exit 1
66         else
67                 exit 0
68         fi
69 }
70
71 SET_X="no"
72 while [[ -n $1 ]] ; do
73         case $1 in
74                 -C|--nocolor) export NOCOLOR="true";;
75                 -x|--debug)   SET_X="yes";;
76                 -V|--version) version;;
77                 -h|--help)    usage;;
78                 -*)           usage "$1";;
79                 *)            break;;
80         esac
81         shift
82 done
83 [[ ${SET_X} == "yes" ]] && set -x
84
85 source "${PORTAGE_BIN_PATH}/isolated-functions.sh"
86
87 # here we make a package given a little info
88 # $1 = package-name w/version
89 # $2 = category
90 do_pkg() {
91         mkdir -p "${PORTAGE_TMPDIR}/binpkgs" || exit 1
92         chmod 0750 "${PORTAGE_TMPDIR}/binpkgs"
93         MYDIR="${PORTAGE_TMPDIR}/binpkgs/$1"
94         SRCDIR="${PORTAGE_DB}/$2/$1"
95         LOG="${PORTAGE_TMPDIR}/binpkgs/$1-quickpkglog"
96
97         ebegin "Building package for $1"
98         (
99                 # clean up temp directory
100                 rm -rf "${MYDIR}"
101
102                 # get pkg info files
103                 mkdir -p "${MYDIR}"/temp
104                 cp "${SRCDIR}"/* "${MYDIR}"/temp/
105                 [ -d "${PKGDIR}"/All ] ||  mkdir -p "${PKGDIR}"/All
106                 local pkg_dest="${PKGDIR}/All/${1}.tbz2"
107                 local pkg_tmp="${PKGDIR}/All/${1}.tbz2.$$"
108
109                 # create filelist and a basic tbz2
110                 gawk '{
111                         if ($1 != "dir") {
112                                 if ($1 == "obj")
113                                         NF=NF-2
114                                 else if ($1 == "sym")
115                                         NF=NF-3
116                         }
117                         print
118                 }' "${SRCDIR}"/CONTENTS | cut -f2- -d" " - | sed -e 's:^/:./:' | \
119                 while read f; do
120                         [ -d "${ROOT}/${f}" ] && [ -h "${ROOT}/${f}" ] && continue
121                         echo "$f"
122                 done > "${MYDIR}"/filelist
123                 tar vjcf "${pkg_tmp}" -C "${ROOT}" --files-from="${MYDIR}"/filelist --no-recursion
124
125                 # join together the basic tbz2 and the pkg info files
126                 python -c "import xpak; t=xpak.tbz2('${pkg_tmp}'); t.recompose('${MYDIR}/temp')"
127
128                 # move the final binary package to PKGDIR
129                 mv -f "${pkg_tmp}" "${pkg_dest}"
130                 [ -d "${PKGDIR}/$2" ] || mkdir -p "${PKGDIR}/$2"
131                 ( cd "${PKGDIR}/$2" && ln -s ../All/$1.tbz2 )
132
133                 # cleanup again
134                 rm -rf "${MYDIR}"
135         ) >& "${LOG}"
136
137         if [ -e "${PKGDIR}/All/$1.tbz2" ] ; then
138                 rm -f "${LOG}"
139                 PKGSTATS="${PKGSTATS}"$'\n'"$(einfo $1: $(du -h "${PKGDIR}/All/$1.tbz2" | gawk '{print $1}'))"
140                 eend 0
141         else
142                 cat ${LOG}
143                 PKGSTATS="${PKGSTATS}"$'\n'"$(ewarn $1: not created)"
144                 eend 1
145         fi
146 }
147
148 # here we parse the parameters given to use on the cmdline
149 export PKGERROR=""
150 export PKGSTATS=""
151 for x in "$@" ; do
152
153         # they gave us full path
154         if [ -e "${x}"/CONTENTS ] ; then
155                 x=$(readlink -f $x)
156                 pkg=$(echo ${x} | cut -d/ -f6)
157                 cat=$(echo ${x} | cut -d/ -f5)
158                 do_pkg "${pkg}" "${cat}"
159
160         # lets figure out what they want
161         else
162                 DIRLIST=$(portageq match "${ROOT}" "${x}")
163                 if [ -z "${DIRLIST}" ] ; then
164                         eerror "Could not find anything to match '${x}'; skipping"
165                         export PKGERROR="${PKGERROR} ${x}"
166                         continue
167                 fi
168
169                 for d in ${DIRLIST} ; do
170                         pkg=$(echo ${d} | cut -d/ -f2)
171                         cat=$(echo ${d} | cut -d/ -f1)
172                         if [ -f "${PORTAGE_DB}/${cat}/${pkg}/CONTENTS" ] ; then
173                                 do_pkg ${pkg} ${cat}
174                         elif [ -d "${PORTAGE_DB}/${cat}/${pkg}" ] ; then
175                                 ewarn "Package '${cat}/${pkg}' was injected; skipping"
176                         else
177                                 eerror "Unhandled case (${cat}/${pkg}) !"
178                                 eerror "Please file a bug at http://bugs.gentoo.org/"
179                                 exit 10
180                         fi
181                 done
182         fi
183
184 done
185
186 if [ -z "${PKGSTATS}" ] ; then
187         eerror "No packages found"
188         exit 1
189 else
190         echo $'\n'"$(einfo Packages now in ${PKGDIR}:)${PKGSTATS}"
191 fi
192 if [ ! -z "${PKGERROR}" ] ; then
193         ewarn "The following packages could not be found:"
194         ewarn "${PKGERROR}"
195         exit 2
196 fi
197
198 exit 0