framework 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #!/bin/sh -- # no runable script, just for vi
  2. # we all like colorful messages
  3. CERROR="" # red
  4. CWARNING="" # yellow
  5. CMSG="" # green
  6. CINFO="" # light blue
  7. CDEBUG="" # blue
  8. CNORMAL="" # default system console color
  9. CDONE="" # green
  10. CPASS="" # green
  11. CFAIL="" # red
  12. CCMD="" # pink
  13. msgdie() { echo "${CERROR}E: $1${CNORMAL}" >&2; exit 1; }
  14. msgwarn() { echo "${CWARNING}W: $1${CNORMAL}" >&2; }
  15. msgmsg() { echo "${CMSG}$1${CNORMAL}" >&2; }
  16. msginfo() { echo "${CINFO}I: $1${CNORMAL}" >&2; }
  17. msgdebug() { echo "${CDEBUG}D: $1${CNORMAL}" >&2; }
  18. msgdone() { echo "${CDONE}DONE${CNORMAL}" >&2; }
  19. msgnwarn() { echo -n "${CWARNING}W: $1${CNORMAL}" >&2; }
  20. msgnmsg() { echo -n "${CMSG}$1${CNORMAL}" >&2; }
  21. msgninfo() { echo -n "${CINFO}I: $1${CNORMAL}" >&2; }
  22. msgndebug() { echo -n "${CDEBUG}D: $1${CNORMAL}" >&2; }
  23. msgtest() { echo -n "${CINFO}$1 ${CCMD}$(echo "$2" | sed -e 's/^aptc/apt-c/' -e 's/^aptg/apt-g/' -e 's/^aptf/apt-f/')${CINFO} …${CNORMAL} " >&2; }
  24. msgpass() { echo "${CPASS}PASS${CNORMAL}" >&2; }
  25. msgskip() { echo "${CWARNING}SKIP${CNORMAL}" >&2; }
  26. msgfail() { echo "${CFAIL}FAIL${CNORMAL}" >&2; }
  27. # enable / disable Debugging
  28. MSGLEVEL=${MSGLEVEL:-3}
  29. if [ $MSGLEVEL -le 0 ]; then
  30. msgdie() { true; }
  31. fi
  32. if [ $MSGLEVEL -le 1 ]; then
  33. msgwarn() { true; }
  34. msgnwarn() { true; }
  35. fi
  36. if [ $MSGLEVEL -le 2 ]; then
  37. msgmsg() { true; }
  38. msgnmsg() { true; }
  39. fi
  40. if [ $MSGLEVEL -le 3 ]; then
  41. msginfo() { true; }
  42. msgninfo() { true; }
  43. fi
  44. if [ $MSGLEVEL -le 4 ]; then
  45. msgdebug() { true; }
  46. msgndebug() { true; }
  47. fi
  48. msgdone() {
  49. if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
  50. [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
  51. [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
  52. [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
  53. [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
  54. true;
  55. else
  56. echo "${CDONE}DONE${CNORMAL}" >&2;
  57. fi
  58. }
  59. runapt() {
  60. msgdebug "Executing: ${CCMD}$*${CDEBUG} "
  61. if [ -f ./aptconfig.conf ]; then
  62. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  63. else
  64. LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  65. fi
  66. }
  67. aptconfig() { runapt apt-config $*; }
  68. aptcache() { runapt apt-cache $*; }
  69. aptget() { runapt apt-get $*; }
  70. aptftparchive() { runapt apt-ftparchive $*; }
  71. setupenvironment() {
  72. local TMPWORKINGDIRECTORY=$(mktemp -d)
  73. local TESTDIR=$(readlink -f $(dirname $0))
  74. msgninfo "Preparing environment for ${CCMD}$0${CINFO} in ${TMPWORKINGDIRECTORY}… "
  75. BUILDDIRECTORY="${TESTDIR}/../../build/bin"
  76. test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
  77. local OLDWORKINGDIRECTORY=$(pwd)
  78. trap "cd /; rm -rf $TMPWORKINGDIRECTORY; cd $OLDWORKINGDIRECTORY" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  79. cd $TMPWORKINGDIRECTORY
  80. mkdir rootdir aptarchive
  81. cd rootdir
  82. mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d var/cache var/lib/dpkg
  83. mkdir -p var/cache/apt/archives/partial var/lib/apt/lists/partial
  84. local STATUSFILE=$(echo "$(basename $0)" | sed 's/^test-/status-/')
  85. if [ -f "${TESTDIR}/${STATUSFILE}" ]; then
  86. cp "${TESTDIR}/${STATUSFILE}" var/lib/dpkg/status
  87. else
  88. touch var/lib/dpkg/status
  89. fi
  90. mkdir -p usr/lib/apt
  91. ln -s ${BUILDDIRECTORY}/methods usr/lib/apt/methods
  92. cd ..
  93. local PACKAGESFILE=$(echo "$(basename $0)" | sed 's/^test-/Packages-/')
  94. if [ -f "${TESTDIR}/${PACKAGESFILE}" ]; then
  95. cp "${TESTDIR}/${PACKAGESFILE}" aptarchive/Packages
  96. else
  97. touch aptarchive/Packages
  98. fi
  99. echo "RootDir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
  100. echo "Debug::NoLocking \"true\";" >> aptconfig.conf
  101. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  102. export LC_ALL=C
  103. msgdone "info"
  104. }
  105. configarchitecture() {
  106. local CONFFILE=rootdir/etc/apt/apt.conf.d/01multiarch.conf
  107. echo "APT::Architecture \"$1\";" > $CONFFILE
  108. shift
  109. while [ -n "$1" ]; do
  110. echo "APT::Architectures:: \"$1\";" >> $CONFFILE
  111. shift
  112. done
  113. }
  114. buildsimplenativepackage() {
  115. local NAME="$1"
  116. local ARCH="$2"
  117. local VERSION="$3"
  118. local RELEASE="${4:-unstable}"
  119. local DEPENDENCIES="$5"
  120. local DESCRIPTION="$6"
  121. msgndebug "Build package ${CCMD}${NAME}=${VERSION}/${RELEASE}${CDEBUG}… "
  122. local BUILDDIR=incoming/${NAME}-${VERSION}
  123. mkdir -p ${BUILDDIR}/debian/source
  124. cd ${BUILDDIR}
  125. echo "* most suckless software product ever" > FEATURES
  126. test -e debian/copyright || (echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright)
  127. test -e debian/changelog || (echo "$NAME ($VERSION) $RELEASE; urgency=low
  128. * Initial release
  129. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog)
  130. test -e debian/control || (echo "Source: $NAME
  131. Section: admin
  132. Priority: optional
  133. Maintainer: Joe Sixpack <joe@example.org>
  134. Build-Depends: debhelper (>= 7)
  135. Standards-Version: 3.9.1
  136. Package: $NAME
  137. Architecture: $ARCH" > debian/control)
  138. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
  139. if [ -z "$DESCRIPTION" ]; then
  140. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  141. If you find such a package installed on your system,
  142. YOU did something horribly wrong! They are autogenerated
  143. und used only by testcases for APT and surf no other propose…" >> debian/control
  144. else
  145. echo "Description: $DESCRIPTION" >> debian/control
  146. fi
  147. test -e debian/compat || (echo "7" > debian/compat)
  148. test -e debian/source/format || (echo "3.0 (native)" > debian/source/format)
  149. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  150. dpkg-buildpackage -uc -us -a$ARCH > /dev/null 2> /dev/null
  151. cd - > /dev/null
  152. rm -rf $BUILDDIR
  153. msgdone "debug"
  154. }
  155. buildaptarchive() {
  156. msgninfo "Build APT archive for ${CCMD}$0${CINFO} based on "
  157. if [ -d incoming ]; then
  158. buildaptarchivefromincoming $*
  159. else
  160. buildaptarchivefromfiles $*
  161. fi
  162. }
  163. createaptftparchiveconfig() {
  164. 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' ' ')"
  165. echo -n 'Dir {
  166. ArchiveDir "' >> ftparchive.conf
  167. echo -n $(readlink -f .) >> ftparchive.conf
  168. echo -n '";
  169. CacheDir "' >> ftparchive.conf
  170. echo -n $(readlink -f ..) >> ftparchive.conf
  171. echo -n '";
  172. };
  173. TreeDefault {
  174. Directory "pool/";
  175. SrcDirectory "pool/";
  176. };
  177. APT {
  178. FTPArchive {
  179. Release {
  180. Origin "joesixpack";
  181. Label "apttestcases";
  182. Suite "unstable";
  183. Description "repository with dummy packages";
  184. Architectures "' >> ftparchive.conf
  185. echo -n "$ARCHS" >> ftparchive.conf
  186. echo 'source";
  187. };
  188. };
  189. };' >> ftparchive.conf
  190. if [ -z "$1" ]; then
  191. echo -n 'tree "dists/unstable" {
  192. Architectures "' >> ftparchive.conf
  193. echo -n "$ARCHS" >> ftparchive.conf
  194. echo 'source";
  195. Sections "main";
  196. };' >> ftparchive.conf
  197. fi
  198. }
  199. buildaptftparchivedirectorystructure() {
  200. 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#')"
  201. for arch in $ARCHS; do
  202. mkdir -p dists/unstable/main/binary-${arch}
  203. done
  204. mkdir -p dists/unstable/main/source
  205. mkdir -p dists/unstable/main/i18n
  206. }
  207. buildaptarchivefromincoming() {
  208. msginfo "incoming packages…"
  209. cd aptarchive
  210. [ -e pool ] || ln -s ../incoming pool
  211. [ -e ftparchive.conf ] || createaptftparchiveconfig
  212. [ -e dists ] || buildaptftparchivedirectorystructure
  213. aptftparchive -qq generate ftparchive.conf
  214. for dir in $(find ./dists -mindepth 1 -maxdepth 1 -type d); do
  215. aptftparchive -qq release $dir > $dir/Release
  216. sed -i -e '/0 Release$/ d' $dir/Release # remove the self reference
  217. done
  218. cd - > /dev/null
  219. msgdone "info"
  220. }
  221. buildaptarchivefromfiles() {
  222. msginfo "prebuild files…"
  223. cd aptarchive
  224. if [ -f Packages ]; then
  225. msgninfo "\tPackages file… "
  226. cat Packages | gzip > Packages.gz
  227. cat Packages | bzip2 > Packages.bz2
  228. cat Packages | lzma > Packages.lzma
  229. msgdone "info"
  230. fi
  231. if [ -f Sources ]; then
  232. msgninfo "\tSources file… "
  233. cat Sources | gzip > Sources.gz
  234. cat Sources | bzip2 > Sources.bz2
  235. cat Sources | lzma > Sources.lzma
  236. msgdone "info"
  237. fi
  238. aptftparchive -qq release . > Release
  239. sed -i -e '/0 Release$/ d' Release # remove the self reference
  240. cd ..
  241. }
  242. setupaptarchive() {
  243. buildaptarchive
  244. local APTARCHIVE=$(readlink -f ./aptarchive)
  245. if [ -f ${APTARCHIVE}/Packages ]; then
  246. msgninfo "\tadd deb sources.list line… "
  247. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  248. msgdone "info"
  249. else
  250. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  251. fi
  252. if [ -f ${APTARCHIVE}/Sources ]; then
  253. msgninfo "\tadd deb-src sources.list line… "
  254. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  255. msgdone "info"
  256. else
  257. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  258. fi
  259. aptget update -qq
  260. }
  261. diff() {
  262. local DIFFTEXT="$($(which diff) -u $* | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  263. if [ -n "$DIFFTEXT" ]; then
  264. echo
  265. echo "$DIFFTEXT"
  266. return 1
  267. else
  268. return 0
  269. fi
  270. }
  271. testequal() {
  272. local COMPAREFILE=$(mktemp)
  273. echo "$1" > $COMPAREFILE
  274. shift
  275. msgtest "Test for equality of" "$*"
  276. $* 2>&1 | diff $COMPAREFILE - && msgpass || msgfail
  277. rm $COMPAREFILE
  278. }
  279. testequalor2() {
  280. local COMPAREFILE1=$(mktemp)
  281. local COMPAREFILE2=$(mktemp)
  282. local COMPAREAGAINST=$(mktemp)
  283. echo "$1" > $COMPAREFILE1
  284. echo "$2" > $COMPAREFILE2
  285. shift 2
  286. msgtest "Test for equality OR of" "$*"
  287. $* 2>&1 1> $COMPAREAGAINST
  288. (diff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null ||
  289. diff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null) && msgpass ||
  290. ( echo "\n${CINFO}Diff against OR 1${CNORMAL}" "$(diff $COMPAREFILE1 $COMPAREAGAINST)" \
  291. "\n${CINFO}Diff against OR 2${CNORMAL}" "$(diff $COMPAREFILE2 $COMPAREAGAINST)" &&
  292. msgfail )
  293. rm $COMPAREFILE1 $COMPAREFILE2 $COMPAREAGAINST
  294. }
  295. testshowvirtual() {
  296. local VIRTUAL="N: Can't select versions from package '$1' as it purely virtual"
  297. local PACKAGE="$1"
  298. shift
  299. while [ -n "$1" ]; do
  300. VIRTUAL="${VIRTUAL}
  301. N: Can't select versions from package '$1' as it purely virtual"
  302. PACKAGE="${PACKAGE} $1"
  303. shift
  304. done
  305. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  306. VIRTUAL="${VIRTUAL}
  307. N: No packages found"
  308. local COMPAREFILE=$(mktemp)
  309. local ARCH=$(dpkg-architecture -qDEB_HOST_ARCH_CPU)
  310. eval `apt-config shell ARCH APT::Architecture`
  311. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  312. aptcache show $PACKAGE 2>&1 | diff $COMPAREFILE - && msgpass || msgfail
  313. rm $COMPAREFILE
  314. }
  315. testnopackage() {
  316. msgtest "Test for non-existent packages" "apt-cache show $*"
  317. local SHOWPKG="$(aptcache show $* 2>&1 | grep '^Package: ')"
  318. if [ -n "$SHOWPKG" ]; then
  319. echo
  320. echo "$SHOWPKG"
  321. msgfail
  322. return 1
  323. fi
  324. msgpass
  325. }