framework 28 KB

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