framework 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 DESCRIPTION="${7}"
  456. local ARCHS=""
  457. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  458. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  459. ARCHS="$(getarchitectures)"
  460. else
  461. ARCHS="$arch"
  462. fi
  463. for BUILDARCH in $ARCHS; do
  464. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  465. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  466. touch aptarchive/dists/${RELEASE}/main/source/Sources
  467. local FILE="${PPATH}/Packages"
  468. echo "Package: $NAME
  469. Priority: $PRIORITY
  470. Section: other
  471. Installed-Size: 42
  472. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  473. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  474. echo "Version: $VERSION
  475. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  476. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  477. echo -n 'Description: ' >> $FILE
  478. if [ -z "$DESCRIPTION" ]; then
  479. echo "an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  480. If you find such a package installed on your system,
  481. YOU did something horribly wrong! They are autogenerated
  482. und used only by testcases for APT and surf no other propose…" >> $FILE
  483. else
  484. echo "$DESCRIPTION" >> $FILE
  485. fi
  486. echo >> $FILE
  487. done
  488. done
  489. }
  490. insertsource() {
  491. local RELEASE="$1"
  492. local NAME="$2"
  493. local ARCH="$3"
  494. local VERSION="$4"
  495. local DEPENDENCIES="$5"
  496. local ARCHS=""
  497. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  498. mkdir -p $SPATH
  499. local FILE="${SPATH}/Sources"
  500. echo "Package: $NAME
  501. Binary: $NAME
  502. Version: $VERSION
  503. Maintainer: Joe Sixpack <joe@example.org>
  504. Architecture: $ARCH" >> $FILE
  505. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  506. echo "Files:
  507. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  508. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  509. " >> $FILE
  510. }
  511. insertinstalledpackage() {
  512. local NAME="$1"
  513. local ARCH="$2"
  514. local VERSION="$3"
  515. local DEPENDENCIES="$4"
  516. local PRIORITY="${5:-optional}"
  517. local STATUS="${6:-install ok installed}"
  518. local FILE='rootdir/var/lib/dpkg/status'
  519. local INFO='rootdir/var/lib/dpkg/info'
  520. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  521. echo "Package: $NAME
  522. Status: $STATUS
  523. Priority: $PRIORITY
  524. Section: other
  525. Installed-Size: 42
  526. Maintainer: Joe Sixpack <joe@example.org>
  527. Version: $VERSION" >> $FILE
  528. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  529. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  530. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/installed
  531. If you find such a package installed on your system,
  532. YOU did something horribly wrong! They are autogenerated
  533. und used only by testcases for APT and surf no other propose…
  534. " >> $FILE
  535. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  536. echo -n > ${INFO}/${NAME}:${arch}.list
  537. else
  538. echo -n > ${INFO}/${NAME}.list
  539. fi
  540. done
  541. }
  542. buildaptarchivefromincoming() {
  543. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  544. cd aptarchive
  545. [ -e pool ] || ln -s ../incoming pool
  546. [ -e ftparchive.conf ] || createaptftparchiveconfig
  547. [ -e dists ] || buildaptftparchivedirectorystructure
  548. msgninfo "\tGenerate Packages, Sources and Contents files… "
  549. aptftparchive -qq generate ftparchive.conf
  550. cd - > /dev/null
  551. msgdone "info"
  552. generatereleasefiles
  553. }
  554. buildaptarchivefromfiles() {
  555. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  556. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  557. msgninfo "\t${line} file… "
  558. cat ${line} | gzip > ${line}.gz
  559. cat ${line} | bzip2 > ${line}.bz2
  560. cat ${line} | xz --format=lzma > ${line}.lzma
  561. cat ${line} | xz > ${line}.xz
  562. msgdone "info"
  563. done
  564. generatereleasefiles
  565. }
  566. # can be overridden by testcases for their pleasure
  567. getcodenamefromsuite() { echo -n "$1"; }
  568. getreleaseversionfromsuite() { true; }
  569. getlabelfromsuite() { true; }
  570. generatereleasefiles() {
  571. # $1 is the Date header and $2 is the ValidUntil header to be set
  572. # both should be given in notation date/touch can understand
  573. msgninfo "\tGenerate Release files… "
  574. if [ -e aptarchive/dists ]; then
  575. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  576. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  577. local CODENAME="$(getcodenamefromsuite $SUITE)"
  578. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  579. local LABEL="$(getlabelfromsuite $SUITE)"
  580. if [ -n "$VERSION" ]; then
  581. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  582. fi
  583. if [ -n "$LABEL" ]; then
  584. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  585. fi
  586. aptftparchive -qq release $dir \
  587. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  588. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  589. ${LABEL} \
  590. ${VERSION} \
  591. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  592. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  593. sed -i '/^Date: / a\
  594. NotAutomatic: yes' $dir/Release
  595. fi
  596. if [ -n "$1" -a "$1" != "now" ]; then
  597. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  598. fi
  599. if [ -n "$2" ]; then
  600. sed -i "/^Date: / a\
  601. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  602. fi
  603. done
  604. else
  605. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  606. fi
  607. if [ -n "$1" -a "$1" != "now" ]; then
  608. for release in $(find ./aptarchive -name 'Release'); do
  609. touch -d "$1" $release
  610. done
  611. fi
  612. msgdone "info"
  613. }
  614. setupdistsaptarchive() {
  615. local APTARCHIVE=$(readlink -f ./aptarchive)
  616. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  617. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  618. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  619. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  620. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  621. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  622. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  623. msgdone "info"
  624. done
  625. }
  626. setupflataptarchive() {
  627. local APTARCHIVE=$(readlink -f ./aptarchive)
  628. if [ -f ${APTARCHIVE}/Packages ]; then
  629. msgninfo "\tadd deb sources.list line… "
  630. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  631. msgdone "info"
  632. else
  633. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  634. fi
  635. if [ -f ${APTARCHIVE}/Sources ]; then
  636. msgninfo "\tadd deb-src sources.list line… "
  637. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  638. msgdone "info"
  639. else
  640. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  641. fi
  642. }
  643. setupaptarchive() {
  644. buildaptarchive
  645. if [ -e aptarchive/dists ]; then
  646. setupdistsaptarchive
  647. else
  648. setupflataptarchive
  649. fi
  650. signreleasefiles
  651. msgninfo "\tSync APT's cache with the archive… "
  652. aptget update -qq
  653. msgdone "info"
  654. }
  655. signreleasefiles() {
  656. local SIGNER="${1:-Joe Sixpack}"
  657. msgninfo "\tSign archive with $SIGNER key… "
  658. local SECKEYS=""
  659. for KEY in $(find keys/ -name '*.sec'); do
  660. SECKEYS="$SECKEYS --secret-keyring $KEY"
  661. done
  662. local PUBKEYS=""
  663. for KEY in $(find keys/ -name '*.pub'); do
  664. PUBKEYS="$PUBKEYS --keyring $KEY"
  665. done
  666. for RELEASE in $(find aptarchive/ -name Release); do
  667. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" -abs -o ${RELEASE}.gpg ${RELEASE}
  668. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" --clearsign -o "$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')" $RELEASE
  669. done
  670. msgdone "info"
  671. }
  672. changetowebserver() {
  673. if which weborf > /dev/null; then
  674. weborf -xb aptarchive/ 2>&1 > /dev/null &
  675. addtrap "kill $!;"
  676. elif which gatling > /dev/null; then
  677. cd aptarchive
  678. gatling -p 8080 -F -S 2>&1 > /dev/null &
  679. addtrap "kill $!;"
  680. cd - > /dev/null
  681. elif which lighttpd > /dev/null; then
  682. echo "server.document-root = \"$(readlink -f ./aptarchive)\"
  683. server.port = 8080
  684. server.stat-cache-engine = \"disable\"" > lighttpd.conf
  685. lighttpd -t -f lighttpd.conf >/dev/null || msgdie 'Can not change to webserver: our lighttpd config is invalid'
  686. lighttpd -D -f lighttpd.conf 2>/dev/null >/dev/null &
  687. addtrap "kill $!;"
  688. else
  689. msgdie 'You have to install weborf or lighttpd first'
  690. return 1
  691. fi
  692. local APTARCHIVE="file://$(readlink -f ./aptarchive)"
  693. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  694. sed -i $LIST -e "s#$APTARCHIVE#http://localhost:8080/#"
  695. done
  696. return 0
  697. }
  698. changetocdrom() {
  699. mkdir -p rootdir/media/cdrom/.disk
  700. local CD="$(readlink -f rootdir/media/cdrom)"
  701. echo "acquire::cdrom::mount \"${CD}\";" > rootdir/etc/apt/apt.conf.d/00cdrom
  702. echo 'acquire::cdrom::autodetect 0;' >> rootdir/etc/apt/apt.conf.d/00cdrom
  703. echo -n "$1" > ${CD}/.disk/info
  704. if [ ! -d aptarchive/dists ]; then
  705. msgdie 'Flat file archive cdroms can not be created currently'
  706. return 1
  707. fi
  708. mv aptarchive/dists $CD
  709. ln -s "$(readlink -f ./incoming)" $CD/pool
  710. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  711. }
  712. checkdiff() {
  713. local DIFFTEXT="$($(which diff) -u $* | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  714. if [ -n "$DIFFTEXT" ]; then
  715. echo
  716. echo "$DIFFTEXT"
  717. return 1
  718. else
  719. return 0
  720. fi
  721. }
  722. testfileequal() {
  723. local FILE="$1"
  724. shift
  725. msgtest "Test for correctness of file" "$FILE"
  726. if [ -z "$*" ]; then
  727. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  728. else
  729. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  730. fi
  731. }
  732. testempty() {
  733. msgtest "Test for no output of" "$*"
  734. test -z "$($* 2>&1)" && msgpass || msgfail
  735. }
  736. testequal() {
  737. local COMPAREFILE=$(mktemp)
  738. addtrap "rm $COMPAREFILE;"
  739. echo "$1" > $COMPAREFILE
  740. shift
  741. msgtest "Test for equality of" "$*"
  742. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  743. }
  744. testequalor2() {
  745. local COMPAREFILE1=$(mktemp)
  746. local COMPAREFILE2=$(mktemp)
  747. local COMPAREAGAINST=$(mktemp)
  748. addtrap "rm $COMPAREFILE1 $COMPAREFILE2 $COMPAREAGAINST;"
  749. echo "$1" > $COMPAREFILE1
  750. echo "$2" > $COMPAREFILE2
  751. shift 2
  752. msgtest "Test for equality OR of" "$*"
  753. $* 2>&1 1> $COMPAREAGAINST
  754. (checkdiff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null ||
  755. checkdiff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null) && msgpass ||
  756. ( echo "\n${CINFO}Diff against OR 1${CNORMAL}" "$(checkdiff $COMPAREFILE1 $COMPAREAGAINST)" \
  757. "\n${CINFO}Diff against OR 2${CNORMAL}" "$(checkdiff $COMPAREFILE2 $COMPAREAGAINST)" &&
  758. msgfail )
  759. }
  760. testshowvirtual() {
  761. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  762. local PACKAGE="$1"
  763. shift
  764. while [ -n "$1" ]; do
  765. VIRTUAL="${VIRTUAL}
  766. N: Can't select versions from package '$1' as it is purely virtual"
  767. PACKAGE="${PACKAGE} $1"
  768. shift
  769. done
  770. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  771. VIRTUAL="${VIRTUAL}
  772. N: No packages found"
  773. local COMPAREFILE=$(mktemp)
  774. addtrap "rm $COMPAREFILE;"
  775. local ARCH="$(getarchitecture 'native')"
  776. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  777. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  778. }
  779. testnopackage() {
  780. msgtest "Test for non-existent packages" "apt-cache show $*"
  781. local SHOWPKG="$(aptcache show $* 2>&1 | grep '^Package: ')"
  782. if [ -n "$SHOWPKG" ]; then
  783. echo
  784. echo "$SHOWPKG"
  785. msgfail
  786. return 1
  787. fi
  788. msgpass
  789. }
  790. testdpkginstalled() {
  791. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  792. local PKGS="$(dpkg -l $* 2>/dev/null | grep '^i' | wc -l)"
  793. if [ "$PKGS" != $# ]; then
  794. echo $PKGS
  795. dpkg -l $* | grep '^[a-z]'
  796. msgfail
  797. return 1
  798. fi
  799. msgpass
  800. }
  801. testdpkgnotinstalled() {
  802. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  803. local PKGS="$(dpkg -l $* 2> /dev/null | grep '^i' | wc -l)"
  804. if [ "$PKGS" != 0 ]; then
  805. echo
  806. dpkg -l $* | grep '^[a-z]'
  807. msgfail
  808. return 1
  809. fi
  810. msgpass
  811. }
  812. testmarkedauto() {
  813. local COMPAREFILE=$(mktemp)
  814. addtrap "rm $COMPAREFILE;"
  815. if [ -n "$1" ]; then
  816. msgtest 'Test for correctly marked as auto-installed' "$*"
  817. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  818. else
  819. msgtest 'Test for correctly marked as auto-installed' 'no package'
  820. echo -n > $COMPAREFILE
  821. fi
  822. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  823. }
  824. pause() {
  825. echo "STOPPED execution. Press enter to continue"
  826. local IGNORE
  827. read IGNORE
  828. }