framework 29 KB

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