framework 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. #!/bin/sh -- # no runable script, just for vi
  2. EXIT_CODE=0
  3. # we all like colorful messages
  4. if expr match "$(readlink -f /proc/$$/fd/1)" '/dev/pts/[0-9]\+' > /dev/null && \
  5. expr match "$(readlink -f /proc/$$/fd/2)" '/dev/pts/[0-9]\+' > /dev/null; then
  6. CERROR="" # red
  7. CWARNING="" # yellow
  8. CMSG="" # green
  9. CINFO="" # light blue
  10. CDEBUG="" # blue
  11. CNORMAL="" # default system console color
  12. CDONE="" # green
  13. CPASS="" # green
  14. CFAIL="" # red
  15. CCMD="" # pink
  16. fi
  17. msgdie() { echo "${CERROR}E: $1${CNORMAL}" >&2; exit 1; }
  18. msgwarn() { echo "${CWARNING}W: $1${CNORMAL}" >&2; }
  19. msgmsg() { echo "${CMSG}$1${CNORMAL}" >&2; }
  20. msginfo() { echo "${CINFO}I: $1${CNORMAL}" >&2; }
  21. msgdebug() { echo "${CDEBUG}D: $1${CNORMAL}" >&2; }
  22. msgdone() { echo "${CDONE}DONE${CNORMAL}" >&2; }
  23. msgnwarn() { echo -n "${CWARNING}W: $1${CNORMAL}" >&2; }
  24. msgnmsg() { echo -n "${CMSG}$1${CNORMAL}" >&2; }
  25. msgninfo() { echo -n "${CINFO}I: $1${CNORMAL}" >&2; }
  26. msgndebug() { echo -n "${CDEBUG}D: $1${CNORMAL}" >&2; }
  27. msgtest() {
  28. while [ -n "$1" ]; do
  29. echo -n "${CINFO}$1${CCMD} " >&2;
  30. echo -n "$(echo "$2" | sed -e 's/^aptc/apt-c/' -e 's/^aptg/apt-g/' -e 's/^aptf/apt-f/')${CINFO} " >&2;
  31. shift
  32. if [ -n "$1" ]; then shift; else break; fi
  33. done
  34. echo -n "…${CNORMAL} " >&2;
  35. }
  36. msgpass() { echo "${CPASS}PASS${CNORMAL}" >&2; }
  37. msgskip() { echo "${CWARNING}SKIP${CNORMAL}" >&2; }
  38. msgfail() {
  39. if [ $# -gt 0 ]; then echo "${CFAIL}FAIL: $*${CNORMAL}" >&2;
  40. else echo "${CFAIL}FAIL${CNORMAL}" >&2; fi
  41. EXIT_CODE=$((EXIT_CODE+1));
  42. }
  43. # enable / disable Debugging
  44. MSGLEVEL=${MSGLEVEL:-3}
  45. if [ $MSGLEVEL -le 0 ]; then
  46. msgdie() { true; }
  47. fi
  48. if [ $MSGLEVEL -le 1 ]; then
  49. msgwarn() { true; }
  50. msgnwarn() { true; }
  51. fi
  52. if [ $MSGLEVEL -le 2 ]; then
  53. msgmsg() { true; }
  54. msgnmsg() { true; }
  55. msgtest() { true; }
  56. msgpass() { echo -n " ${CPASS}P${CNORMAL}" >&2; }
  57. msgskip() { echo -n " ${CWARNING}S${CNORMAL}" >&2; }
  58. if [ -n "$CFAIL" ]; then
  59. msgfail() { echo -n " ${CFAIL}FAIL${CNORMAL}" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  60. else
  61. msgfail() { echo -n " ###FAILED###" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  62. fi
  63. fi
  64. if [ $MSGLEVEL -le 3 ]; then
  65. msginfo() { true; }
  66. msgninfo() { true; }
  67. fi
  68. if [ $MSGLEVEL -le 4 ]; then
  69. msgdebug() { true; }
  70. msgndebug() { true; }
  71. fi
  72. msgdone() {
  73. if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
  74. [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
  75. [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
  76. [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
  77. [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
  78. true;
  79. else
  80. echo "${CDONE}DONE${CNORMAL}" >&2;
  81. fi
  82. }
  83. runapt() {
  84. msgdebug "Executing: ${CCMD}$*${CDEBUG} "
  85. if [ -f ./aptconfig.conf ]; then
  86. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  87. elif [ -f ../aptconfig.conf ]; then
  88. APT_CONFIG=../aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  89. else
  90. LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  91. fi
  92. }
  93. aptconfig() { runapt apt-config $*; }
  94. aptcache() { runapt apt-cache $*; }
  95. aptcdrom() { runapt apt-cdrom $*; }
  96. aptget() { runapt apt-get $*; }
  97. aptftparchive() { runapt apt-ftparchive $*; }
  98. aptkey() { runapt apt-key $*; }
  99. aptmark() { runapt apt-mark $*; }
  100. dpkg() {
  101. $(which dpkg) --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log $*
  102. }
  103. aptitude() {
  104. if [ -f ./aptconfig.conf ]; then
  105. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  106. elif [ -f ../aptconfig.conf ]; then
  107. APT_CONFIG=../aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  108. else
  109. LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  110. fi
  111. }
  112. gdb() {
  113. echo "gdb: run »$*«"
  114. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which gdb) ${BUILDDIRECTORY}/$1
  115. }
  116. exitwithstatus() {
  117. # error if we about to overflow, but ...
  118. # "255 failures ought to be enough for everybody"
  119. if [ $EXIT_CODE -gt 255 ]; then
  120. msgdie "Total failure count $EXIT_CODE too big"
  121. fi
  122. exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
  123. }
  124. addtrap() {
  125. if [ "$1" = 'prefix' ]; then
  126. CURRENTTRAP="$2 $CURRENTTRAP"
  127. else
  128. CURRENTTRAP="$CURRENTTRAP $1"
  129. fi
  130. trap "$CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  131. }
  132. setupenvironment() {
  133. TMPWORKINGDIRECTORY=$(mktemp -d)
  134. TESTDIRECTORY=$(readlink -f $(dirname $0))
  135. msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
  136. BUILDDIRECTORY="${TESTDIRECTORY}/../../build/bin"
  137. test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
  138. addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY;"
  139. cd $TMPWORKINGDIRECTORY
  140. mkdir rootdir aptarchive keys
  141. cd rootdir
  142. mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
  143. mkdir -p var/cache var/lib var/log
  144. mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
  145. touch var/lib/dpkg/available
  146. mkdir -p usr/lib/apt
  147. ln -s ${BUILDDIRECTORY}/methods usr/lib/apt/methods
  148. cd ..
  149. local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
  150. if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
  151. cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
  152. fi
  153. local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
  154. if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
  155. cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
  156. fi
  157. cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
  158. ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  159. echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
  160. echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
  161. echo "Debug::NoLocking \"true\";" >> aptconfig.conf
  162. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  163. echo "Dir::Bin::Methods \"${BUILDDIRECTORY}/methods\";" >> aptconfig.conf
  164. echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
  165. echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
  166. echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
  167. echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
  168. echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
  169. if ! $(which dpkg) --assert-multi-arch 2>&1 > /dev/null; then
  170. echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
  171. fi
  172. echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
  173. echo 'quiet::NoUpdate "true";' >> aptconfig.conf
  174. export LC_ALL=C
  175. export PATH="${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
  176. msgdone "info"
  177. }
  178. getarchitecture() {
  179. if [ "$1" = "native" -o -z "$1" ]; then
  180. eval `aptconfig shell ARCH APT::Architecture`
  181. if [ -n "$ARCH" ]; then
  182. echo $ARCH
  183. else
  184. dpkg --print-architecture
  185. fi
  186. else
  187. echo $1
  188. fi
  189. }
  190. getarchitectures() {
  191. echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  192. }
  193. configarchitecture() {
  194. local CONFFILE=rootdir/etc/apt/apt.conf.d/01multiarch.conf
  195. rm -f $CONFFILE
  196. echo "APT::Architecture \"$(getarchitecture $1)\";" > $CONFFILE
  197. shift
  198. while [ -n "$1" ]; do
  199. echo "APT::Architectures:: \"$(getarchitecture $1)\";" >> $CONFFILE
  200. shift
  201. done
  202. configdpkg
  203. }
  204. configdpkg() {
  205. if [ ! -e rootdir/var/lib/dpkg/status ]; then
  206. local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
  207. if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
  208. cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
  209. else
  210. echo -n > rootdir/var/lib/dpkg/status
  211. fi
  212. fi
  213. if $(which dpkg) --assert-multi-arch 2>&1 > /dev/null; then
  214. local ARCHS="$(getarchitectures)"
  215. if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
  216. DPKGARCH="$(dpkg --print-architecture)"
  217. for ARCH in ${ARCHS}; do
  218. if [ "${ARCH}" != "${DPKGARCH}" ]; then dpkg --add-architecture ${ARCH}; fi
  219. done
  220. if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
  221. # dpkg doesn't really check the version as long as it is fully installed,
  222. # but just to be sure we choose one above the required version
  223. insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
  224. fi
  225. fi
  226. fi
  227. }
  228. setupsimplenativepackage() {
  229. local NAME="$1"
  230. local ARCH="$2"
  231. local VERSION="$3"
  232. local RELEASE="${4:-unstable}"
  233. local DEPENDENCIES="$5"
  234. local DESCRIPTION="$6"
  235. local SECTION="${7:-others}"
  236. local DISTSECTION
  237. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  238. DISTSECTION="main"
  239. else
  240. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  241. fi
  242. local BUILDDIR=incoming/${NAME}-${VERSION}
  243. mkdir -p ${BUILDDIR}/debian/source
  244. cd ${BUILDDIR}
  245. echo "* most suckless software product ever" > FEATURES
  246. test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
  247. test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
  248. * Initial release
  249. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
  250. test -e debian/control || echo "Source: $NAME
  251. Section: $SECTION
  252. Priority: optional
  253. Maintainer: Joe Sixpack <joe@example.org>
  254. Build-Depends: debhelper (>= 7)
  255. Standards-Version: 3.9.1
  256. Package: $NAME" > debian/control
  257. if [ "$ARCH" = 'all' ]; then
  258. echo "Architecture: all" >> debian/control
  259. else
  260. echo "Architecture: any" >> debian/control
  261. fi
  262. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
  263. if [ -z "$DESCRIPTION" ]; then
  264. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  265. If you find such a package installed on your system,
  266. YOU did something horribly wrong! They are autogenerated
  267. und used only by testcases for APT and surf no other propose…" >> debian/control
  268. else
  269. echo "Description: $DESCRIPTION" >> debian/control
  270. fi
  271. test -e debian/compat || echo "7" > debian/compat
  272. test -e debian/source/format || echo "3.0 (native)" > debian/source/format
  273. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  274. cd - > /dev/null
  275. }
  276. buildsimplenativepackage() {
  277. local NAME="$1"
  278. local ARCH="$2"
  279. local VERSION="$3"
  280. local RELEASE="${4:-unstable}"
  281. local DEPENDENCIES="$5"
  282. local DESCRIPTION="$6"
  283. local SECTION="${7:-others}"
  284. local PRIORITY="${8:-optional}"
  285. local DISTSECTION
  286. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  287. DISTSECTION="main"
  288. else
  289. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  290. fi
  291. local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
  292. msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
  293. mkdir -p $BUILDDIR/debian/source
  294. echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
  295. echo "#!/bin/sh
  296. echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
  297. echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
  298. echo "$NAME ($VERSION) $RELEASE; urgency=low
  299. * Initial release
  300. -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
  301. echo "Source: $NAME
  302. Section: $SECTION
  303. Priority: $PRIORITY
  304. Maintainer: Joe Sixpack <joe@example.org>
  305. Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
  306. local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
  307. test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
  308. echo "
  309. Package: $NAME" >> ${BUILDDIR}/debian/control
  310. if [ "$ARCH" = 'all' ]; then
  311. echo "Architecture: all" >> ${BUILDDIR}/debian/control
  312. else
  313. echo "Architecture: any" >> ${BUILDDIR}/debian/control
  314. fi
  315. local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
  316. test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
  317. if [ -z "$DESCRIPTION" ]; then
  318. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  319. If you find such a package installed on your system,
  320. YOU did something horribly wrong! They are autogenerated
  321. und used only by testcases for APT and surf no other propose…" >> ${BUILDDIR}/debian/control
  322. else
  323. echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
  324. fi
  325. echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
  326. (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' \
  327. | while read SRC; do
  328. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  329. # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
  330. # gpg --yes --no-default-keyring --secret-keyring ./keys/joesixpack.sec \
  331. # --keyring ./keys/joesixpack.pub --default-key 'Joe Sixpack' \
  332. # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  333. # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  334. # fi
  335. done
  336. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  337. rm -rf ${BUILDDIR}/debian/tmp
  338. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  339. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  340. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  341. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  342. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  343. dpkg-deb --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. 2> /dev/null > /dev/null
  344. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  345. done
  346. mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
  347. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
  348. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
  349. rm -rf "${BUILDDIR}"
  350. msgdone "info"
  351. }
  352. buildpackage() {
  353. local BUILDDIR=$1
  354. local RELEASE=$2
  355. local SECTION=$3
  356. local ARCH=$(getarchitecture $4)
  357. msgninfo "Build package $(echo "$BUILDDIR" | grep -o '[^/]*$') for ${RELEASE} in ${SECTION}… "
  358. cd $BUILDDIR
  359. if [ "$ARCH" = "all" ]; then
  360. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  361. fi
  362. local BUILT="$(dpkg-buildpackage -uc -us -a$ARCH 2> /dev/null)"
  363. local PKGS="$( echo "$BUILT" | grep '^dpkg-deb: building package' | cut -d'/' -f 2 | sed -e "s#'\.##")"
  364. local SRCS="$( echo "$BUILT" | grep '^dpkg-source: info: building' | grep -o '[a-z0-9._+~-]*$')"
  365. cd - > /dev/null
  366. for PKG in $PKGS; do
  367. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  368. done
  369. for SRC in $SRCS; do
  370. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  371. done
  372. msgdone "info"
  373. }
  374. buildaptarchive() {
  375. if [ -d incoming ]; then
  376. buildaptarchivefromincoming $*
  377. else
  378. buildaptarchivefromfiles $*
  379. fi
  380. }
  381. createaptftparchiveconfig() {
  382. local ARCHS="$(find pool/ -name '*.deb' | grep -oE '_[a-z0-9-]+\.deb$' | sort | uniq | sed -e '/^_all.deb$/ d' -e 's#^_\([a-z0-9-]*\)\.deb$#\1#' | tr '\n' ' ')"
  383. if [ -z "$ARCHS" ]; then
  384. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  385. ARCHS="$(getarchitectures)"
  386. fi
  387. echo -n 'Dir {
  388. ArchiveDir "' >> ftparchive.conf
  389. echo -n $(readlink -f .) >> ftparchive.conf
  390. echo -n '";
  391. CacheDir "' >> ftparchive.conf
  392. echo -n $(readlink -f ..) >> ftparchive.conf
  393. echo -n '";
  394. FileListDir "' >> ftparchive.conf
  395. echo -n $(readlink -f pool/) >> ftparchive.conf
  396. echo -n '";
  397. };
  398. Default {
  399. Packages::Compress ". gzip bzip2 lzma xz";
  400. Sources::Compress ". gzip bzip2 lzma xz";
  401. Contents::Compress ". gzip bzip2 lzma xz";
  402. Translation::Compress ". gzip bzip2 lzma xz";
  403. LongDescription "false";
  404. };
  405. TreeDefault {
  406. Directory "pool/";
  407. SrcDirectory "pool/";
  408. };
  409. APT {
  410. FTPArchive {
  411. Release {
  412. Origin "joesixpack";
  413. Label "apttestcases";
  414. Suite "unstable";
  415. Description "repository with dummy packages";
  416. Architectures "' >> ftparchive.conf
  417. echo -n "$ARCHS" >> ftparchive.conf
  418. echo 'source";
  419. };
  420. };
  421. };' >> ftparchive.conf
  422. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  423. echo -n 'tree "dists/' >> ftparchive.conf
  424. echo -n "$DIST" >> ftparchive.conf
  425. echo -n '" {
  426. Architectures "' >> ftparchive.conf
  427. echo -n "$ARCHS" >> ftparchive.conf
  428. echo -n 'source";
  429. FileList "' >> ftparchive.conf
  430. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  431. echo -n '";
  432. SourceFileList "' >> ftparchive.conf
  433. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  434. echo -n '";
  435. Sections "' >> ftparchive.conf
  436. echo -n "$(find ./pool/ -maxdepth 1 -name "${DIST}.*.pkglist" -type f | cut -d'/' -f 3 | cut -d'.' -f 2 | sort | uniq | tr '\n' ' ')" >> ftparchive.conf
  437. echo '";
  438. };' >> ftparchive.conf
  439. done
  440. }
  441. buildaptftparchivedirectorystructure() {
  442. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  443. for DIST in $DISTS; do
  444. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  445. for SECTION in $SECTIONS; do
  446. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  447. for ARCH in $ARCHS; do
  448. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  449. done
  450. mkdir -p dists/${DIST}/${SECTION}/source
  451. mkdir -p dists/${DIST}/${SECTION}/i18n
  452. done
  453. done
  454. }
  455. insertpackage() {
  456. local RELEASE="$1"
  457. local NAME="$2"
  458. local ARCH="$3"
  459. local VERSION="$4"
  460. local DEPENDENCIES="$5"
  461. local PRIORITY="${6:-optional}"
  462. local DESCRIPTION="${7}"
  463. local ARCHS=""
  464. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  465. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  466. ARCHS="$(getarchitectures)"
  467. else
  468. ARCHS="$arch"
  469. fi
  470. for BUILDARCH in $ARCHS; do
  471. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  472. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  473. touch aptarchive/dists/${RELEASE}/main/source/Sources
  474. local FILE="${PPATH}/Packages"
  475. echo "Package: $NAME
  476. Priority: $PRIORITY
  477. Section: other
  478. Installed-Size: 42
  479. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  480. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  481. echo "Version: $VERSION
  482. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  483. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  484. echo -n 'Description: ' >> $FILE
  485. if [ -z "$DESCRIPTION" ]; then
  486. echo "an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  487. If you find such a package installed on your system,
  488. YOU did something horribly wrong! They are autogenerated
  489. und used only by testcases for APT and surf no other propose…" >> $FILE
  490. else
  491. echo "$DESCRIPTION" >> $FILE
  492. fi
  493. echo >> $FILE
  494. done
  495. done
  496. }
  497. insertsource() {
  498. local RELEASE="$1"
  499. local NAME="$2"
  500. local ARCH="$3"
  501. local VERSION="$4"
  502. local DEPENDENCIES="$5"
  503. local ARCHS=""
  504. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  505. mkdir -p $SPATH
  506. local FILE="${SPATH}/Sources"
  507. echo "Package: $NAME
  508. Binary: $NAME
  509. Version: $VERSION
  510. Maintainer: Joe Sixpack <joe@example.org>
  511. Architecture: $ARCH" >> $FILE
  512. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  513. echo "Files:
  514. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  515. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  516. " >> $FILE
  517. }
  518. insertinstalledpackage() {
  519. local NAME="$1"
  520. local ARCH="$2"
  521. local VERSION="$3"
  522. local DEPENDENCIES="$4"
  523. local PRIORITY="${5:-optional}"
  524. local STATUS="${6:-install ok installed}"
  525. local FILE='rootdir/var/lib/dpkg/status'
  526. local INFO='rootdir/var/lib/dpkg/info'
  527. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  528. echo "Package: $NAME
  529. Status: $STATUS
  530. Priority: $PRIORITY
  531. Section: other
  532. Installed-Size: 42
  533. Maintainer: Joe Sixpack <joe@example.org>
  534. Version: $VERSION" >> $FILE
  535. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  536. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  537. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/installed
  538. If you find such a package installed on your system,
  539. YOU did something horribly wrong! They are autogenerated
  540. und used only by testcases for APT and surf no other propose…
  541. " >> $FILE
  542. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  543. echo -n > ${INFO}/${NAME}:${arch}.list
  544. else
  545. echo -n > ${INFO}/${NAME}.list
  546. fi
  547. done
  548. }
  549. buildaptarchivefromincoming() {
  550. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  551. cd aptarchive
  552. [ -e pool ] || ln -s ../incoming pool
  553. [ -e ftparchive.conf ] || createaptftparchiveconfig
  554. [ -e dists ] || buildaptftparchivedirectorystructure
  555. msgninfo "\tGenerate Packages, Sources and Contents files… "
  556. aptftparchive -qq generate ftparchive.conf
  557. cd - > /dev/null
  558. msgdone "info"
  559. generatereleasefiles
  560. }
  561. buildaptarchivefromfiles() {
  562. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  563. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  564. msgninfo "\t${line} file… "
  565. cat ${line} | gzip > ${line}.gz
  566. cat ${line} | bzip2 > ${line}.bz2
  567. cat ${line} | xz --format=lzma > ${line}.lzma
  568. cat ${line} | xz > ${line}.xz
  569. msgdone "info"
  570. done
  571. generatereleasefiles
  572. }
  573. # can be overridden by testcases for their pleasure
  574. getcodenamefromsuite() { echo -n "$1"; }
  575. getreleaseversionfromsuite() { true; }
  576. getlabelfromsuite() { true; }
  577. generatereleasefiles() {
  578. # $1 is the Date header and $2 is the ValidUntil header to be set
  579. # both should be given in notation date/touch can understand
  580. msgninfo "\tGenerate Release files… "
  581. if [ -e aptarchive/dists ]; then
  582. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  583. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  584. local CODENAME="$(getcodenamefromsuite $SUITE)"
  585. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  586. local LABEL="$(getlabelfromsuite $SUITE)"
  587. if [ -n "$VERSION" ]; then
  588. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  589. fi
  590. if [ -n "$LABEL" ]; then
  591. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  592. fi
  593. aptftparchive -qq release $dir \
  594. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  595. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  596. ${LABEL} \
  597. ${VERSION} \
  598. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  599. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  600. sed -i '/^Date: / a\
  601. NotAutomatic: yes' $dir/Release
  602. fi
  603. if [ -n "$1" -a "$1" != "now" ]; then
  604. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  605. fi
  606. if [ -n "$2" ]; then
  607. sed -i "/^Date: / a\
  608. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  609. fi
  610. done
  611. else
  612. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  613. fi
  614. if [ -n "$1" -a "$1" != "now" ]; then
  615. for release in $(find ./aptarchive -name 'Release'); do
  616. touch -d "$1" $release
  617. done
  618. fi
  619. msgdone "info"
  620. }
  621. setupdistsaptarchive() {
  622. local APTARCHIVE=$(readlink -f ./aptarchive)
  623. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  624. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  625. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  626. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  627. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  628. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  629. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  630. msgdone "info"
  631. done
  632. }
  633. setupflataptarchive() {
  634. local APTARCHIVE=$(readlink -f ./aptarchive)
  635. if [ -f ${APTARCHIVE}/Packages ]; then
  636. msgninfo "\tadd deb sources.list line… "
  637. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  638. msgdone "info"
  639. else
  640. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  641. fi
  642. if [ -f ${APTARCHIVE}/Sources ]; then
  643. msgninfo "\tadd deb-src sources.list line… "
  644. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  645. msgdone "info"
  646. else
  647. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  648. fi
  649. }
  650. setupaptarchive() {
  651. buildaptarchive
  652. if [ -e aptarchive/dists ]; then
  653. setupdistsaptarchive
  654. else
  655. setupflataptarchive
  656. fi
  657. signreleasefiles
  658. if [ "$1" != '--no-update' ]; then
  659. msgninfo "\tSync APT's cache with the archive… "
  660. aptget update -qq
  661. msgdone "info"
  662. fi
  663. }
  664. signreleasefiles() {
  665. local SIGNER="${1:-Joe Sixpack}"
  666. msgninfo "\tSign archive with $SIGNER key… "
  667. local SECKEYS=""
  668. for KEY in $(find keys/ -name '*.sec'); do
  669. SECKEYS="$SECKEYS --secret-keyring $KEY"
  670. done
  671. local PUBKEYS=""
  672. for KEY in $(find keys/ -name '*.pub'); do
  673. PUBKEYS="$PUBKEYS --keyring $KEY"
  674. done
  675. for RELEASE in $(find aptarchive/ -name Release); do
  676. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" -abs -o ${RELEASE}.gpg ${RELEASE}
  677. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" --clearsign -o "$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')" $RELEASE
  678. done
  679. msgdone "info"
  680. }
  681. changetowebserver() {
  682. local LOG='/dev/null'
  683. if test -x ${BUILDDIRECTORY}/aptwebserver; then
  684. cd aptarchive
  685. LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/aptwebserver "$@" >$LOG 2>&1 &
  686. addtrap "kill $!;"
  687. cd - > /dev/null
  688. elif [ $# -gt 0 ]; then
  689. msgdie 'Need the aptwebserver when passing arguments for the webserver'
  690. elif which weborf > /dev/null; then
  691. weborf -xb aptarchive/ >$LOG 2>&1 &
  692. addtrap "kill $!;"
  693. elif which gatling > /dev/null; then
  694. cd aptarchive
  695. gatling -p 8080 -F -S >$LOG 2>&1 &
  696. addtrap "kill $!;"
  697. cd - > /dev/null
  698. elif which lighttpd > /dev/null; then
  699. echo "server.document-root = \"$(readlink -f ./aptarchive)\"
  700. server.port = 8080
  701. server.stat-cache-engine = \"disable\"" > lighttpd.conf
  702. lighttpd -t -f lighttpd.conf >/dev/null || msgdie 'Can not change to webserver: our lighttpd config is invalid'
  703. lighttpd -D -f lighttpd.conf >$LOG 2>&1 &
  704. addtrap "kill $!;"
  705. else
  706. msgdie 'You have to build aptwerbserver or install a webserver'
  707. fi
  708. local APTARCHIVE="file://$(readlink -f ./aptarchive)"
  709. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  710. sed -i $LIST -e "s#$APTARCHIVE#http://localhost:8080/#"
  711. done
  712. return 0
  713. }
  714. changetocdrom() {
  715. mkdir -p rootdir/media/cdrom/.disk
  716. local CD="$(readlink -f rootdir/media/cdrom)"
  717. echo "acquire::cdrom::mount \"${CD}\";" > rootdir/etc/apt/apt.conf.d/00cdrom
  718. echo 'acquire::cdrom::autodetect 0;' >> rootdir/etc/apt/apt.conf.d/00cdrom
  719. echo -n "$1" > ${CD}/.disk/info
  720. if [ ! -d aptarchive/dists ]; then
  721. msgdie 'Flat file archive cdroms can not be created currently'
  722. return 1
  723. fi
  724. mv aptarchive/dists $CD
  725. ln -s "$(readlink -f ./incoming)" $CD/pool
  726. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  727. }
  728. checkdiff() {
  729. local DIFFTEXT="$($(which diff) -u $* | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  730. if [ -n "$DIFFTEXT" ]; then
  731. echo
  732. echo "$DIFFTEXT"
  733. return 1
  734. else
  735. return 0
  736. fi
  737. }
  738. testfileequal() {
  739. local FILE="$1"
  740. shift
  741. msgtest "Test for correctness of file" "$FILE"
  742. if [ -z "$*" ]; then
  743. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  744. else
  745. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  746. fi
  747. }
  748. testempty() {
  749. msgtest "Test for no output of" "$*"
  750. test -z "$($* 2>&1)" && msgpass || msgfail
  751. }
  752. testequal() {
  753. local COMPAREFILE=$(mktemp)
  754. addtrap "rm $COMPAREFILE;"
  755. echo "$1" > $COMPAREFILE
  756. shift
  757. msgtest "Test for equality of" "$*"
  758. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  759. }
  760. testequalor2() {
  761. local COMPAREFILE1=$(mktemp)
  762. local COMPAREFILE2=$(mktemp)
  763. local COMPAREAGAINST=$(mktemp)
  764. addtrap "rm $COMPAREFILE1 $COMPAREFILE2 $COMPAREAGAINST;"
  765. echo "$1" > $COMPAREFILE1
  766. echo "$2" > $COMPAREFILE2
  767. shift 2
  768. msgtest "Test for equality OR of" "$*"
  769. $* 2>&1 1> $COMPAREAGAINST
  770. (checkdiff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null ||
  771. checkdiff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null) && msgpass ||
  772. ( echo "\n${CINFO}Diff against OR 1${CNORMAL}" "$(checkdiff $COMPAREFILE1 $COMPAREAGAINST)" \
  773. "\n${CINFO}Diff against OR 2${CNORMAL}" "$(checkdiff $COMPAREFILE2 $COMPAREAGAINST)" &&
  774. msgfail )
  775. }
  776. testshowvirtual() {
  777. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  778. local PACKAGE="$1"
  779. shift
  780. while [ -n "$1" ]; do
  781. VIRTUAL="${VIRTUAL}
  782. N: Can't select versions from package '$1' as it is purely virtual"
  783. PACKAGE="${PACKAGE} $1"
  784. shift
  785. done
  786. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  787. VIRTUAL="${VIRTUAL}
  788. N: No packages found"
  789. local COMPAREFILE=$(mktemp)
  790. addtrap "rm $COMPAREFILE;"
  791. local ARCH="$(getarchitecture 'native')"
  792. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  793. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  794. }
  795. testnopackage() {
  796. msgtest "Test for non-existent packages" "apt-cache show $*"
  797. local SHOWPKG="$(aptcache show $* 2>&1 | grep '^Package: ')"
  798. if [ -n "$SHOWPKG" ]; then
  799. echo
  800. echo "$SHOWPKG"
  801. msgfail
  802. return 1
  803. fi
  804. msgpass
  805. }
  806. testdpkginstalled() {
  807. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  808. local PKGS="$(dpkg -l $* 2>/dev/null | grep '^i' | wc -l)"
  809. if [ "$PKGS" != $# ]; then
  810. echo $PKGS
  811. dpkg -l $* | grep '^[a-z]'
  812. msgfail
  813. return 1
  814. fi
  815. msgpass
  816. }
  817. testdpkgnotinstalled() {
  818. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  819. local PKGS="$(dpkg -l $* 2> /dev/null | grep '^i' | wc -l)"
  820. if [ "$PKGS" != 0 ]; then
  821. echo
  822. dpkg -l $* | grep '^[a-z]'
  823. msgfail
  824. return 1
  825. fi
  826. msgpass
  827. }
  828. testmarkedauto() {
  829. local COMPAREFILE=$(mktemp)
  830. addtrap "rm $COMPAREFILE;"
  831. if [ -n "$1" ]; then
  832. msgtest 'Test for correctly marked as auto-installed' "$*"
  833. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  834. else
  835. msgtest 'Test for correctly marked as auto-installed' 'no package'
  836. echo -n > $COMPAREFILE
  837. fi
  838. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  839. }
  840. pause() {
  841. echo "STOPPED execution. Press enter to continue"
  842. local IGNORE
  843. read IGNORE
  844. }