add some useful options
[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: 144 $'
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 case ${NOCOLOR:-false} in
86         yes|true) unset_colors;;
87         no|false) set_colors;;
88 esac
89
90 # here we make a package given a little info
91 # $1 = package-name w/version
92 # $2 = category
93 do_pkg() {
94         mkdir -p "${PORTAGE_TMPDIR}/binpkgs" || exit 1
95         chmod 0750 "${PORTAGE_TMPDIR}/binpkgs"
96         MYDIR="${PORTAGE_TMPDIR}/binpkgs/$1"
97         SRCDIR="${PORTAGE_DB}/$2/$1"
98         LOG="${PORTAGE_TMPDIR}/binpkgs/$1-quickpkglog"
99
100         ebegin "Building package for $1"
101         (
102                 # clean up temp directory
103                 rm -rf "${MYDIR}"
104
105                 # get pkg info files
106                 mkdir -p "${MYDIR}"/temp
107                 cp "${SRCDIR}"/* "${MYDIR}"/temp/
108                 [ -d "${PKGDIR}"/All ] ||  mkdir -p "${PKGDIR}"/All
109                 local pkg_dest="${PKGDIR}/All/${1}.tbz2"
110                 local pkg_tmp="${PKGDIR}/All/${1}.tbz2.$$"
111
112                 # create filelist and a basic tbz2
113                 gawk '{
114                         if ($1 != "dir") {
115                                 if ($1 == "obj")
116                                         NF=NF-2
117                                 else if ($1 == "sym")
118                                         NF=NF-3
119                         }
120                         print
121                 }' "${SRCDIR}"/CONTENTS | cut -f2- -d" " - | sed -e 's:^/:./:' | \
122                 while read f; do
123                         [ -d "${ROOT}/${f}" ] && [ -h "${ROOT}/${f}" ] && continue
124                         echo "$f"
125                 done > "${MYDIR}"/filelist
126                 tar vjcf "${pkg_tmp}" -C "${ROOT}" --files-from="${MYDIR}"/filelist --no-recursion
127
128                 # join together the basic tbz2 and the pkg info files
129                 python -c "import xpak; t=xpak.tbz2('${pkg_tmp}'); t.recompose('${MYDIR}/temp')"
130
131                 # move the final binary package to PKGDIR
132                 mv -f "${pkg_tmp}" "${pkg_dest}"
133                 [ -d "${PKGDIR}/$2" ] || mkdir -p "${PKGDIR}/$2"
134                 ( cd "${PKGDIR}/$2" && ln -s ../All/$1.tbz2 )
135
136                 # cleanup again
137                 rm -rf "${MYDIR}"
138         ) >& "${LOG}"
139
140         if [ -e "${PKGDIR}/All/$1.tbz2" ] ; then
141                 rm -f "${LOG}"
142                 PKGSTATS="${PKGSTATS}"$'\n'"$(einfo $1: $(du -h "${PKGDIR}/All/$1.tbz2" | gawk '{print $1}'))"
143                 eend 0
144         else
145                 cat ${LOG}
146                 PKGSTATS="${PKGSTATS}"$'\n'"$(ewarn $1: not created)"
147                 eend 1
148         fi
149 }
150
151 # here we parse the parameters given to use on the cmdline
152 export PKGERROR=""
153 export PKGSTATS=""
154 for x in "$@" ; do
155
156         # they gave us full path
157         if [ -e "${x}"/CONTENTS ] ; then
158                 x=$(readlink -f $x)
159                 pkg=$(echo ${x} | cut -d/ -f6)
160                 cat=$(echo ${x} | cut -d/ -f5)
161                 do_pkg "${pkg}" "${cat}"
162
163         # lets figure out what they want
164         else
165                 DIRLIST=$(portageq match "${ROOT}" "${x}")
166                 if [ -z "${DIRLIST}" ] ; then
167                         eerror "Could not find anything to match '${x}'; skipping"
168                         export PKGERROR="${PKGERROR} ${x}"
169                         continue
170                 fi
171
172                 for d in ${DIRLIST} ; do
173                         pkg=$(echo ${d} | cut -d/ -f2)
174                         cat=$(echo ${d} | cut -d/ -f1)
175                         if [ -f "${PORTAGE_DB}/${cat}/${pkg}/CONTENTS" ] ; then
176                                 do_pkg ${pkg} ${cat}
177                         elif [ -d "${PORTAGE_DB}/${cat}/${pkg}" ] ; then
178                                 ewarn "Package '${cat}/${pkg}' was injected; skipping"
179                         else
180                                 eerror "Unhandled case (${cat}/${pkg}) !"
181                                 eerror "Please file a bug at http://bugs.gentoo.org/"
182                                 exit 10
183                         fi
184                 done
185         fi
186
187 done
188
189 if [ -z "${PKGSTATS}" ] ; then
190         eerror "No packages found"
191         exit 1
192 else
193         echo $'\n'"$(einfo Packages now in ${PKGDIR}:)${PKGSTATS}"
194 fi
195 if [ ! -z "${PKGERROR}" ] ; then
196         ewarn "The following packages could not be found:"
197         ewarn "${PKGERROR}"
198         exit 2
199 fi
200
201 exit 0