framework 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. local SRCS="$( (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | grep '^dpkg-source: info: building' | grep -o '[a-z0-9._+~-]*$')"
  320. for SRC in $SRCS; do
  321. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  322. done
  323. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  324. rm -rf ${BUILDDIR}/debian/tmp
  325. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  326. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  327. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  328. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  329. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  330. dpkg-deb --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. 2> /dev/null > /dev/null
  331. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  332. done
  333. mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
  334. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
  335. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
  336. rm -rf "${BUILDDIR}"
  337. msgdone "info"
  338. }
  339. buildpackage() {
  340. local BUILDDIR=$1
  341. local RELEASE=$2
  342. local SECTION=$3
  343. local ARCH=$(getarchitecture $4)
  344. msgninfo "Build package $(echo "$BUILDDIR" | grep -o '[^/]*$') for ${RELEASE} in ${SECTION}… "
  345. cd $BUILDDIR
  346. if [ "$ARCH" = "all" ]; then
  347. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  348. fi
  349. local BUILT="$(dpkg-buildpackage -uc -us -a$ARCH 2> /dev/null)"
  350. local PKGS="$( echo "$BUILT" | grep '^dpkg-deb: building package' | cut -d'/' -f 2 | sed -e "s#'\.##")"
  351. local SRCS="$( echo "$BUILT" | grep '^dpkg-source: info: building' | grep -o '[a-z0-9._+~-]*$')"
  352. cd - > /dev/null
  353. for PKG in $PKGS; do
  354. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  355. done
  356. for SRC in $SRCS; do
  357. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  358. done
  359. msgdone "info"
  360. }
  361. buildaptarchive() {
  362. if [ -d incoming ]; then
  363. buildaptarchivefromincoming $*
  364. else
  365. buildaptarchivefromfiles $*
  366. fi
  367. }
  368. createaptftparchiveconfig() {
  369. 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' ' ')"
  370. if [ -z "$ARCHS" ]; then
  371. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  372. ARCHS="$(getarchitectures)"
  373. fi
  374. echo -n 'Dir {
  375. ArchiveDir "' >> ftparchive.conf
  376. echo -n $(readlink -f .) >> ftparchive.conf
  377. echo -n '";
  378. CacheDir "' >> ftparchive.conf
  379. echo -n $(readlink -f ..) >> ftparchive.conf
  380. echo -n '";
  381. FileListDir "' >> ftparchive.conf
  382. echo -n $(readlink -f pool/) >> ftparchive.conf
  383. echo -n '";
  384. };
  385. Default {
  386. Packages::Compress ". gzip bzip2 lzma xz";
  387. Sources::Compress ". gzip bzip2 lzma xz";
  388. Contents::Compress ". gzip bzip2 lzma xz";
  389. Translation::Compress ". gzip bzip2 lzma xz";
  390. LongDescription "false";
  391. };
  392. TreeDefault {
  393. Directory "pool/";
  394. SrcDirectory "pool/";
  395. };
  396. APT {
  397. FTPArchive {
  398. Release {
  399. Origin "joesixpack";
  400. Label "apttestcases";
  401. Suite "unstable";
  402. Description "repository with dummy packages";
  403. Architectures "' >> ftparchive.conf
  404. echo -n "$ARCHS" >> ftparchive.conf
  405. echo 'source";
  406. };
  407. };
  408. };' >> ftparchive.conf
  409. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  410. echo -n 'tree "dists/' >> ftparchive.conf
  411. echo -n "$DIST" >> ftparchive.conf
  412. echo -n '" {
  413. Architectures "' >> ftparchive.conf
  414. echo -n "$ARCHS" >> ftparchive.conf
  415. echo -n 'source";
  416. FileList "' >> ftparchive.conf
  417. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  418. echo -n '";
  419. SourceFileList "' >> ftparchive.conf
  420. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  421. echo -n '";
  422. Sections "' >> ftparchive.conf
  423. 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
  424. echo '";
  425. };' >> ftparchive.conf
  426. done
  427. }
  428. buildaptftparchivedirectorystructure() {
  429. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  430. for DIST in $DISTS; do
  431. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  432. for SECTION in $SECTIONS; do
  433. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  434. for ARCH in $ARCHS; do
  435. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  436. done
  437. mkdir -p dists/${DIST}/${SECTION}/source
  438. mkdir -p dists/${DIST}/${SECTION}/i18n
  439. done
  440. done
  441. }
  442. insertpackage() {
  443. local RELEASE="$1"
  444. local NAME="$2"
  445. local ARCH="$3"
  446. local VERSION="$4"
  447. local DEPENDENCIES="$5"
  448. local PRIORITY="${6:-optional}"
  449. local ARCHS=""
  450. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  451. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  452. ARCHS="$(getarchitectures)"
  453. else
  454. ARCHS="$arch"
  455. fi
  456. for BUILDARCH in $ARCHS; do
  457. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  458. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  459. touch aptarchive/dists/${RELEASE}/main/source/Sources
  460. local FILE="${PPATH}/Packages"
  461. echo "Package: $NAME
  462. Priority: $PRIORITY
  463. Section: other
  464. Installed-Size: 42
  465. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  466. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  467. echo "Version: $VERSION
  468. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  469. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  470. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  471. If you find such a package installed on your system,
  472. YOU did something horribly wrong! They are autogenerated
  473. und used only by testcases for APT and surf no other propose…
  474. " >> $FILE
  475. done
  476. done
  477. }
  478. insertsource() {
  479. local RELEASE="$1"
  480. local NAME="$2"
  481. local ARCH="$3"
  482. local VERSION="$4"
  483. local DEPENDENCIES="$5"
  484. local ARCHS=""
  485. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  486. mkdir -p $SPATH
  487. local FILE="${SPATH}/Sources"
  488. echo "Package: $NAME
  489. Binary: $NAME
  490. Version: $VERSION
  491. Maintainer: Joe Sixpack <joe@example.org>
  492. Architecture: $ARCH" >> $FILE
  493. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  494. echo "Files:
  495. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  496. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  497. " >> $FILE
  498. }
  499. insertinstalledpackage() {
  500. local NAME="$1"
  501. local ARCH="$2"
  502. local VERSION="$3"
  503. local DEPENDENCIES="$4"
  504. local PRIORITY="${5:-optional}"
  505. local STATUS="${6:-install ok installed}"
  506. local FILE='rootdir/var/lib/dpkg/status'
  507. local INFO='rootdir/var/lib/dpkg/info'
  508. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  509. echo "Package: $NAME
  510. Status: $STATUS
  511. Priority: $PRIORITY
  512. Section: other
  513. Installed-Size: 42
  514. Maintainer: Joe Sixpack <joe@example.org>
  515. Version: $VERSION" >> $FILE
  516. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  517. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  518. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/installed
  519. If you find such a package installed on your system,
  520. YOU did something horribly wrong! They are autogenerated
  521. und used only by testcases for APT and surf no other propose…
  522. " >> $FILE
  523. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  524. echo -n > ${INFO}/${NAME}:${arch}.list
  525. else
  526. echo -n > ${INFO}/${NAME}.list
  527. fi
  528. done
  529. }
  530. buildaptarchivefromincoming() {
  531. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  532. cd aptarchive
  533. [ -e pool ] || ln -s ../incoming pool
  534. [ -e ftparchive.conf ] || createaptftparchiveconfig
  535. [ -e dists ] || buildaptftparchivedirectorystructure
  536. msgninfo "\tGenerate Packages, Sources and Contents files… "
  537. aptftparchive -qq generate ftparchive.conf
  538. cd - > /dev/null
  539. msgdone "info"
  540. generatereleasefiles
  541. }
  542. buildaptarchivefromfiles() {
  543. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  544. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  545. msgninfo "\t${line} file… "
  546. cat ${line} | gzip > ${line}.gz
  547. cat ${line} | bzip2 > ${line}.bz2
  548. cat ${line} | xz --format=lzma > ${line}.lzma
  549. cat ${line} | xz > ${line}.xz
  550. msgdone "info"
  551. done
  552. generatereleasefiles
  553. }
  554. # can be overridden by testcases for their pleasure
  555. getcodenamefromsuite() { echo -n "$1"; }
  556. getreleaseversionfromsuite() { true; }
  557. getlabelfromsuite() { true; }
  558. generatereleasefiles() {
  559. # $1 is the Date header and $2 is the ValidUntil header to be set
  560. # both should be given in notation date/touch can understand
  561. msgninfo "\tGenerate Release files… "
  562. if [ -e aptarchive/dists ]; then
  563. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  564. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  565. local CODENAME="$(getcodenamefromsuite $SUITE)"
  566. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  567. local LABEL="$(getlabelfromsuite $SUITE)"
  568. if [ -n "$VERSION" ]; then
  569. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  570. fi
  571. if [ -n "$LABEL" ]; then
  572. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  573. fi
  574. aptftparchive -qq release $dir \
  575. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  576. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  577. ${LABEL} \
  578. ${VERSION} \
  579. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  580. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  581. sed -i '/^Date: / a\
  582. NotAutomatic: yes' $dir/Release
  583. fi
  584. if [ -n "$1" -a "$1" != "now" ]; then
  585. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  586. fi
  587. if [ -n "$2" ]; then
  588. sed -i "/^Date: / a\
  589. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  590. fi
  591. done
  592. else
  593. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  594. fi
  595. if [ -n "$1" -a "$1" != "now" ]; then
  596. for release in $(find ./aptarchive -name 'Release'); do
  597. touch -d "$1" $release
  598. done
  599. fi
  600. msgdone "info"
  601. }
  602. setupdistsaptarchive() {
  603. local APTARCHIVE=$(readlink -f ./aptarchive)
  604. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  605. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  606. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  607. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  608. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  609. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  610. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  611. msgdone "info"
  612. done
  613. }
  614. setupflataptarchive() {
  615. local APTARCHIVE=$(readlink -f ./aptarchive)
  616. if [ -f ${APTARCHIVE}/Packages ]; then
  617. msgninfo "\tadd deb sources.list line… "
  618. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  619. msgdone "info"
  620. else
  621. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  622. fi
  623. if [ -f ${APTARCHIVE}/Sources ]; then
  624. msgninfo "\tadd deb-src sources.list line… "
  625. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  626. msgdone "info"
  627. else
  628. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  629. fi
  630. }
  631. setupaptarchive() {
  632. buildaptarchive
  633. if [ -e aptarchive/dists ]; then
  634. setupdistsaptarchive
  635. else
  636. setupflataptarchive
  637. fi
  638. signreleasefiles
  639. msgninfo "\tSync APT's cache with the archive… "
  640. aptget update -qq
  641. msgdone "info"
  642. }
  643. signreleasefiles() {
  644. local SIGNER="${1:-Joe Sixpack}"
  645. msgninfo "\tSign archive with $SIGNER key… "
  646. local SECKEYS=""
  647. for KEY in $(find keys/ -name '*.sec'); do
  648. SECKEYS="$SECKEYS --secret-keyring $KEY"
  649. done
  650. local PUBKEYS=""
  651. for KEY in $(find keys/ -name '*.pub'); do
  652. PUBKEYS="$PUBKEYS --keyring $KEY"
  653. done
  654. for RELEASE in $(find aptarchive/ -name Release); do
  655. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" -abs -o ${RELEASE}.gpg ${RELEASE}
  656. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" --clearsign -o "$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')" $RELEASE
  657. done
  658. msgdone "info"
  659. }
  660. changetowebserver() {
  661. if which weborf > /dev/null; then
  662. weborf -xb aptarchive/ 2>&1 > /dev/null &
  663. addtrap "kill $!;"
  664. elif which gatling > /dev/null; then
  665. cd aptarchive
  666. gatling -p 8080 -F -S 2>&1 > /dev/null &
  667. addtrap "kill $!;"
  668. cd - > /dev/null
  669. elif which lighttpd > /dev/null; then
  670. echo "server.document-root = \"$(readlink -f ./aptarchive)\"
  671. server.port = 8080
  672. server.stat-cache-engine = \"disable\"" > lighttpd.conf
  673. lighttpd -t -f lighttpd.conf >/dev/null || msgdie 'Can not change to webserver: our lighttpd config is invalid'
  674. lighttpd -D -f lighttpd.conf 2>/dev/null >/dev/null &
  675. addtrap "kill $!;"
  676. else
  677. msgdie 'You have to install weborf or lighttpd first'
  678. return 1
  679. fi
  680. local APTARCHIVE="file://$(readlink -f ./aptarchive)"
  681. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  682. sed -i $LIST -e "s#$APTARCHIVE#http://localhost:8080/#"
  683. done
  684. return 0
  685. }
  686. changetocdrom() {
  687. mkdir -p rootdir/media/cdrom/.disk
  688. local CD="$(readlink -f rootdir/media/cdrom)"
  689. echo "acquire::cdrom::mount \"${CD}\";" > rootdir/etc/apt/apt.conf.d/00cdrom
  690. echo 'acquire::cdrom::autodetect 0;' >> rootdir/etc/apt/apt.conf.d/00cdrom
  691. echo -n "$1" > ${CD}/.disk/info
  692. if [ ! -d aptarchive/dists ]; then
  693. msgdie 'Flat file archive cdroms can not be created currently'
  694. return 1
  695. fi
  696. mv aptarchive/dists $CD
  697. ln -s "$(readlink -f ./incoming)" $CD/pool
  698. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  699. }
  700. checkdiff() {
  701. local DIFFTEXT="$($(which diff) -u $* | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  702. if [ -n "$DIFFTEXT" ]; then
  703. echo
  704. echo "$DIFFTEXT"
  705. return 1
  706. else
  707. return 0
  708. fi
  709. }
  710. testfileequal() {
  711. local FILE="$1"
  712. shift
  713. msgtest "Test for correctness of file" "$FILE"
  714. if [ -z "$*" ]; then
  715. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  716. else
  717. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  718. fi
  719. }
  720. testempty() {
  721. msgtest "Test for no output of" "$*"
  722. test -z "$($* 2>&1)" && msgpass || msgfail
  723. }
  724. testequal() {
  725. local COMPAREFILE=$(mktemp)
  726. addtrap "rm $COMPAREFILE;"
  727. echo "$1" > $COMPAREFILE
  728. shift
  729. msgtest "Test for equality of" "$*"
  730. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  731. }
  732. testequalor2() {
  733. local COMPAREFILE1=$(mktemp)
  734. local COMPAREFILE2=$(mktemp)
  735. local COMPAREAGAINST=$(mktemp)
  736. addtrap "rm $COMPAREFILE1 $COMPAREFILE2 $COMPAREAGAINST;"
  737. echo "$1" > $COMPAREFILE1
  738. echo "$2" > $COMPAREFILE2
  739. shift 2
  740. msgtest "Test for equality OR of" "$*"
  741. $* 2>&1 1> $COMPAREAGAINST
  742. (checkdiff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null ||
  743. checkdiff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null) && msgpass ||
  744. ( echo "\n${CINFO}Diff against OR 1${CNORMAL}" "$(checkdiff $COMPAREFILE1 $COMPAREAGAINST)" \
  745. "\n${CINFO}Diff against OR 2${CNORMAL}" "$(checkdiff $COMPAREFILE2 $COMPAREAGAINST)" &&
  746. msgfail )
  747. }
  748. testshowvirtual() {
  749. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  750. local PACKAGE="$1"
  751. shift
  752. while [ -n "$1" ]; do
  753. VIRTUAL="${VIRTUAL}
  754. N: Can't select versions from package '$1' as it is purely virtual"
  755. PACKAGE="${PACKAGE} $1"
  756. shift
  757. done
  758. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  759. VIRTUAL="${VIRTUAL}
  760. N: No packages found"
  761. local COMPAREFILE=$(mktemp)
  762. addtrap "rm $COMPAREFILE;"
  763. local ARCH="$(getarchitecture 'native')"
  764. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  765. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  766. }
  767. testnopackage() {
  768. msgtest "Test for non-existent packages" "apt-cache show $*"
  769. local SHOWPKG="$(aptcache show $* 2>&1 | grep '^Package: ')"
  770. if [ -n "$SHOWPKG" ]; then
  771. echo
  772. echo "$SHOWPKG"
  773. msgfail
  774. return 1
  775. fi
  776. msgpass
  777. }
  778. testdpkginstalled() {
  779. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  780. local PKGS="$(dpkg -l $* 2>/dev/null | grep '^i' | wc -l)"
  781. if [ "$PKGS" != $# ]; then
  782. echo $PKGS
  783. dpkg -l $* | grep '^[a-z]'
  784. msgfail
  785. return 1
  786. fi
  787. msgpass
  788. }
  789. testdpkgnotinstalled() {
  790. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  791. local PKGS="$(dpkg -l $* 2> /dev/null | grep '^i' | wc -l)"
  792. if [ "$PKGS" != 0 ]; then
  793. echo
  794. dpkg -l $* | grep '^[a-z]'
  795. msgfail
  796. return 1
  797. fi
  798. msgpass
  799. }
  800. testmarkedauto() {
  801. local COMPAREFILE=$(mktemp)
  802. addtrap "rm $COMPAREFILE;"
  803. if [ -n "$1" ]; then
  804. msgtest 'Test for correctly marked as auto-installed' "$*"
  805. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  806. else
  807. msgtest 'Test for correctly marked as auto-installed' 'no package'
  808. echo -n > $COMPAREFILE
  809. fi
  810. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  811. }
  812. pause() {
  813. echo "STOPPED execution. Press enter to continue"
  814. local IGNORE
  815. read IGNORE
  816. }