framework 29 KB

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