framework 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. elif [ -f ../aptconfig.conf ]; then
  64. APT_CONFIG=../aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  65. else
  66. LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  67. fi
  68. }
  69. aptconfig() { runapt apt-config $*; }
  70. aptcache() { runapt apt-cache $*; }
  71. aptget() { runapt apt-get $*; }
  72. aptftparchive() { runapt apt-ftparchive $*; }
  73. aptkey() { runapt apt-key $*; }
  74. dpkg() {
  75. $(which dpkg) --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log $*
  76. }
  77. aptitude() {
  78. if [ -f ./aptconfig.conf ]; then
  79. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  80. elif [ -f ../aptconfig.conf ]; then
  81. APT_CONFIG=../aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  82. else
  83. LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  84. fi
  85. }
  86. setupenvironment() {
  87. TMPWORKINGDIRECTORY=$(mktemp -d)
  88. local TESTDIR=$(readlink -f $(dirname $0))
  89. msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
  90. BUILDDIRECTORY="${TESTDIR}/../../build/bin"
  91. test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
  92. local OLDWORKINGDIRECTORY=$(pwd)
  93. CURRENTTRAP="cd /; rm -rf $TMPWORKINGDIRECTORY; cd $OLDWORKINGDIRECTORY"
  94. trap "$CURRENTTRAP" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  95. cd $TMPWORKINGDIRECTORY
  96. mkdir rootdir aptarchive keys
  97. cd rootdir
  98. mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
  99. mkdir -p var/cache var/lib var/log
  100. mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
  101. local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
  102. if [ -f "${TESTDIR}/${STATUSFILE}" ]; then
  103. cp "${TESTDIR}/${STATUSFILE}" var/lib/dpkg/status
  104. else
  105. touch var/lib/dpkg/status
  106. fi
  107. touch var/lib/dpkg/available
  108. mkdir -p usr/lib/apt
  109. ln -s ${BUILDDIRECTORY}/methods usr/lib/apt/methods
  110. cd ..
  111. local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
  112. if [ -f "${TESTDIR}/${PACKAGESFILE}" ]; then
  113. cp "${TESTDIR}/${PACKAGESFILE}" aptarchive/Packages
  114. fi
  115. local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
  116. if [ -f "${TESTDIR}/${SOURCESSFILE}" ]; then
  117. cp "${TESTDIR}/${SOURCESSFILE}" aptarchive/Sources
  118. fi
  119. cp $(find $TESTDIR -name '*.pub' -o -name '*.sec') keys/
  120. ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  121. echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
  122. echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
  123. echo "Debug::NoLocking \"true\";" >> aptconfig.conf
  124. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  125. echo "Dir::Bin::Methods \"${BUILDDIRECTORY}/methods\";" >> aptconfig.conf
  126. echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
  127. echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
  128. echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
  129. echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
  130. echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
  131. echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
  132. echo 'quiet::NoUpdate "true";' >> aptconfig.conf
  133. export LC_ALL=C
  134. msgdone "info"
  135. }
  136. configarchitecture() {
  137. local CONFFILE=rootdir/etc/apt/apt.conf.d/01multiarch.conf
  138. echo "APT::Architecture \"$1\";" > $CONFFILE
  139. shift
  140. while [ -n "$1" ]; do
  141. echo "APT::Architectures:: \"$1\";" >> $CONFFILE
  142. shift
  143. done
  144. }
  145. setupsimplenativepackage() {
  146. local NAME="$1"
  147. local ARCH="$2"
  148. local VERSION="$3"
  149. local RELEASE="${4:-unstable}"
  150. local DEPENDENCIES="$5"
  151. local DESCRIPTION="$6"
  152. local SECTION="${7:-others}"
  153. local DISTSECTION
  154. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  155. DISTSECTION="main"
  156. else
  157. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  158. fi
  159. local BUILDDIR=incoming/${NAME}-${VERSION}
  160. mkdir -p ${BUILDDIR}/debian/source
  161. cd ${BUILDDIR}
  162. echo "* most suckless software product ever" > FEATURES
  163. test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
  164. test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
  165. * Initial release
  166. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
  167. test -e debian/control || echo "Source: $NAME
  168. Section: $SECTION
  169. Priority: optional
  170. Maintainer: Joe Sixpack <joe@example.org>
  171. Build-Depends: debhelper (>= 7)
  172. Standards-Version: 3.9.1
  173. Package: $NAME
  174. Architecture: $ARCH" > debian/control
  175. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
  176. if [ -z "$DESCRIPTION" ]; then
  177. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  178. If you find such a package installed on your system,
  179. YOU did something horribly wrong! They are autogenerated
  180. und used only by testcases for APT and surf no other propose…" >> debian/control
  181. else
  182. echo "Description: $DESCRIPTION" >> debian/control
  183. fi
  184. test -e debian/compat || echo "7" > debian/compat
  185. test -e debian/source/format || echo "3.0 (native)" > debian/source/format
  186. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  187. cd - > /dev/null
  188. }
  189. buildsimplenativepackage() {
  190. local NAME="$1"
  191. local ARCH="$2"
  192. local VERSION="$3"
  193. local RELEASE="${4:-unstable}"
  194. local DEPENDENCIES="$5"
  195. local DESCRIPTION="$6"
  196. local SECTION="${7:-others}"
  197. local DISTSECTION
  198. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  199. DISTSECTION="main"
  200. else
  201. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  202. fi
  203. setupsimplenativepackage "$NAME" "$ARCH" "$VERSION" "$RELEASE" "$DEPENDENCIES" "$DESCRIPTION" "$SECTION"
  204. buildpackage "incoming/${NAME}-${VERSION}" "$RELEASE" "$DISTSECTION"
  205. rm -rf "incoming/${NAME}-${VERSION}"
  206. }
  207. buildpackage() {
  208. local BUILDDIR=$1
  209. local RELEASE=$2
  210. local SECTION=$3
  211. msgninfo "Build package $(echo "$BUILDDIR" | grep -o '[^/]*$') for ${RELEASE} in ${SECTION}… "
  212. cd $BUILDDIR
  213. if [ "$ARCH" = "all" ]; then
  214. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  215. fi
  216. local BUILT="$(dpkg-buildpackage -uc -us -a$ARCH 2> /dev/null)"
  217. local PKGS="$( echo "$BUILT" | grep '^dpkg-deb: building package' | cut -d'/' -f 2 | sed -e "s#'\.##")"
  218. local SRCS="$( echo "$BUILT" | grep '^dpkg-source: info: building' | grep -o '[a-z0-9._+~-]*$')"
  219. cd - > /dev/null
  220. for PKG in $PKGS; do
  221. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  222. done
  223. for SRC in $SRCS; do
  224. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  225. done
  226. msgdone "info"
  227. }
  228. buildaptarchive() {
  229. if [ -d incoming ]; then
  230. buildaptarchivefromincoming $*
  231. else
  232. buildaptarchivefromfiles $*
  233. fi
  234. }
  235. createaptftparchiveconfig() {
  236. 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' ' ')"
  237. if [ -z "$ARCHS" ]; then
  238. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  239. ARCHS="$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  240. fi
  241. echo -n 'Dir {
  242. ArchiveDir "' >> ftparchive.conf
  243. echo -n $(readlink -f .) >> ftparchive.conf
  244. echo -n '";
  245. CacheDir "' >> ftparchive.conf
  246. echo -n $(readlink -f ..) >> ftparchive.conf
  247. echo -n '";
  248. FileListDir "' >> ftparchive.conf
  249. echo -n $(readlink -f pool/) >> ftparchive.conf
  250. echo -n '";
  251. };
  252. Default {
  253. Packages::Compress ". gzip bzip2 lzma";
  254. Sources::Compress ". gzip bzip2 lzma";
  255. Contents::Compress ". gzip bzip2 lzma";
  256. };
  257. TreeDefault {
  258. Directory "pool/";
  259. SrcDirectory "pool/";
  260. };
  261. APT {
  262. FTPArchive {
  263. Release {
  264. Origin "joesixpack";
  265. Label "apttestcases";
  266. Suite "unstable";
  267. Description "repository with dummy packages";
  268. Architectures "' >> ftparchive.conf
  269. echo -n "$ARCHS" >> ftparchive.conf
  270. echo 'source";
  271. };
  272. };
  273. };' >> ftparchive.conf
  274. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  275. echo -n 'tree "dists/' >> ftparchive.conf
  276. echo -n "$DIST" >> ftparchive.conf
  277. echo -n '" {
  278. Architectures "' >> ftparchive.conf
  279. echo -n "$ARCHS" >> ftparchive.conf
  280. echo -n 'source";
  281. FileList "' >> ftparchive.conf
  282. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  283. echo -n '";
  284. SourceFileList "' >> ftparchive.conf
  285. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  286. echo -n '";
  287. Sections "' >> ftparchive.conf
  288. 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
  289. echo '";
  290. };' >> ftparchive.conf
  291. done
  292. }
  293. buildaptftparchivedirectorystructure() {
  294. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  295. for DIST in $DISTS; do
  296. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  297. for SECTION in $SECTIONS; do
  298. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  299. for ARCH in $ARCHS; do
  300. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  301. done
  302. mkdir -p dists/${DIST}/${SECTION}/source
  303. mkdir -p dists/${DIST}/${SECTION}/i18n
  304. done
  305. done
  306. }
  307. insertpackage() {
  308. local RELEASE="$1"
  309. local NAME="$2"
  310. local ARCH="$3"
  311. local VERSION="$4"
  312. local DEPENDENCIES="$5"
  313. local ARCHS="$ARCH"
  314. if [ "$ARCHS" = "all" ]; then
  315. ARCHS="$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  316. fi
  317. for BUILDARCH in $ARCHS; do
  318. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  319. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  320. touch aptarchive/dists/${RELEASE}/main/source/Sources
  321. local FILE="${PPATH}/Packages"
  322. echo "Package: $NAME
  323. Priority: optional
  324. Section: other
  325. Installed-Size: 42
  326. Maintainer: Joe Sixpack <joe@example.org>
  327. Architecture: $ARCH
  328. Version: $VERSION
  329. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${ARCH}.deb" >> $FILE
  330. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  331. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  332. If you find such a package installed on your system,
  333. YOU did something horribly wrong! They are autogenerated
  334. und used only by testcases for APT and surf no other propose…
  335. " >> $FILE
  336. done
  337. }
  338. buildaptarchivefromincoming() {
  339. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  340. cd aptarchive
  341. [ -e pool ] || ln -s ../incoming pool
  342. [ -e ftparchive.conf ] || createaptftparchiveconfig
  343. [ -e dists ] || buildaptftparchivedirectorystructure
  344. msgninfo "\tGenerate Packages, Sources and Contents files… "
  345. aptftparchive -qq generate ftparchive.conf
  346. cd - > /dev/null
  347. msgdone "info"
  348. generatereleasefiles
  349. }
  350. buildaptarchivefromfiles() {
  351. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  352. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  353. msgninfo "\t${line} file… "
  354. cat ${line} | gzip > ${line}.gz
  355. cat ${line} | bzip2 > ${line}.bz2
  356. cat ${line} | lzma > ${line}.lzma
  357. msgdone "info"
  358. done
  359. generatereleasefiles
  360. }
  361. generatereleasefiles() {
  362. msgninfo "\tGenerate Release files… "
  363. if [ -e aptarchive/dists ]; then
  364. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  365. local CODENAME="$(echo "$dir" | cut -d'/' -f 4)"
  366. aptftparchive -qq release $dir -o APT::FTPArchive::Release::Suite="${CODENAME}" -o APT::FTPArchive::Release::Codename="${CODENAME}" | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  367. if [ "$CODENAME" = "experimental" -o "$CODENAME" = "experimental2" ]; then
  368. sed -i '/^Date: / a\
  369. NotAutomatic: yes' $dir/Release
  370. fi
  371. done
  372. else
  373. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  374. fi
  375. msgdone "info"
  376. }
  377. setupdistsaptarchive() {
  378. local APTARCHIVE=$(readlink -f ./aptarchive)
  379. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  380. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  381. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  382. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  383. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  384. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  385. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  386. msgdone "info"
  387. done
  388. }
  389. setupflataptarchive() {
  390. local APTARCHIVE=$(readlink -f ./aptarchive)
  391. if [ -f ${APTARCHIVE}/Packages ]; then
  392. msgninfo "\tadd deb sources.list line… "
  393. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  394. msgdone "info"
  395. else
  396. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  397. fi
  398. if [ -f ${APTARCHIVE}/Sources ]; then
  399. msgninfo "\tadd deb-src sources.list line… "
  400. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  401. msgdone "info"
  402. else
  403. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  404. fi
  405. }
  406. setupaptarchive() {
  407. buildaptarchive
  408. if [ -e aptarchive/dists ]; then
  409. setupdistsaptarchive
  410. else
  411. setupflataptarchive
  412. fi
  413. signreleasefiles
  414. msgninfo "\tSync APT's cache with the archive… "
  415. aptget update -qq
  416. msgdone "info"
  417. }
  418. signreleasefiles() {
  419. local SIGNER="${1:-Joe Sixpack}"
  420. msgninfo "\tSign archive with $SIGNER key… "
  421. local SECKEYS=""
  422. for KEY in $(find keys/ -name '*.sec'); do
  423. SECKEYS="$SECKEYS --secret-keyring $KEY"
  424. done
  425. local PUBKEYS=""
  426. for KEY in $(find keys/ -name '*.pub'); do
  427. PUBKEYS="$PUBKEYS --keyring $KEY"
  428. done
  429. for RELEASE in $(find aptarchive/ -name Release); do
  430. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" -abs -o ${RELEASE}.gpg ${RELEASE}
  431. done
  432. msgdone "info"
  433. }
  434. changetowebserver() {
  435. if which weborf > /dev/null; then
  436. weborf -xb aptarchive/ 2>&1 > /dev/null &
  437. CURRENTTRAP="kill $!; $CURRENTTRAP"
  438. trap "$CURRENTTRAP" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  439. local APTARCHIVE="file://$(readlink -f ./aptarchive)"
  440. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  441. sed -i $LIST -e "s#$APTARCHIVE#http://localhost:8080/#"
  442. done
  443. return 0
  444. fi
  445. return 1
  446. }
  447. checkdiff() {
  448. local DIFFTEXT="$($(which diff) -u $* | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  449. if [ -n "$DIFFTEXT" ]; then
  450. echo
  451. echo "$DIFFTEXT"
  452. return 1
  453. else
  454. return 0
  455. fi
  456. }
  457. testfileequal() {
  458. local FILE="$1"
  459. shift
  460. msgtest "Test for correctness of file" "$FILE"
  461. if [ -z "$*" ]; then
  462. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  463. else
  464. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  465. fi
  466. }
  467. testequal() {
  468. local COMPAREFILE=$(mktemp)
  469. echo "$1" > $COMPAREFILE
  470. shift
  471. msgtest "Test for equality of" "$*"
  472. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  473. rm $COMPAREFILE
  474. }
  475. testequalor2() {
  476. local COMPAREFILE1=$(mktemp)
  477. local COMPAREFILE2=$(mktemp)
  478. local COMPAREAGAINST=$(mktemp)
  479. echo "$1" > $COMPAREFILE1
  480. echo "$2" > $COMPAREFILE2
  481. shift 2
  482. msgtest "Test for equality OR of" "$*"
  483. $* 2>&1 1> $COMPAREAGAINST
  484. (checkdiff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null ||
  485. checkdiff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null) && msgpass ||
  486. ( echo "\n${CINFO}Diff against OR 1${CNORMAL}" "$(checkdiff $COMPAREFILE1 $COMPAREAGAINST)" \
  487. "\n${CINFO}Diff against OR 2${CNORMAL}" "$(checkdiff $COMPAREFILE2 $COMPAREAGAINST)" &&
  488. msgfail )
  489. rm $COMPAREFILE1 $COMPAREFILE2 $COMPAREAGAINST
  490. }
  491. testshowvirtual() {
  492. local VIRTUAL="N: Can't select versions from package '$1' as it purely virtual"
  493. local PACKAGE="$1"
  494. shift
  495. while [ -n "$1" ]; do
  496. VIRTUAL="${VIRTUAL}
  497. N: Can't select versions from package '$1' as it purely virtual"
  498. PACKAGE="${PACKAGE} $1"
  499. shift
  500. done
  501. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  502. VIRTUAL="${VIRTUAL}
  503. N: No packages found"
  504. local COMPAREFILE=$(mktemp)
  505. local ARCH=$(dpkg-architecture -qDEB_HOST_ARCH_CPU)
  506. eval `apt-config shell ARCH APT::Architecture`
  507. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  508. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  509. rm $COMPAREFILE
  510. }
  511. testnopackage() {
  512. msgtest "Test for non-existent packages" "apt-cache show $*"
  513. local SHOWPKG="$(aptcache show $* 2>&1 | grep '^Package: ')"
  514. if [ -n "$SHOWPKG" ]; then
  515. echo
  516. echo "$SHOWPKG"
  517. msgfail
  518. return 1
  519. fi
  520. msgpass
  521. }
  522. testdpkginstalled() {
  523. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  524. local PKGS="$(dpkg -l $* | grep '^[a-z]' | grep '^[^i]' | wc -l)"
  525. if [ "$PKGS" != 0 ]; then
  526. echo $PKGS
  527. dpkg -l $* | grep '^[a-z]'
  528. msgfail
  529. return 1
  530. fi
  531. msgpass
  532. }
  533. testdpkgnoninstalled() {
  534. msgtest "Test for correctly non-installed package(s) with" "dpkg -l $*"
  535. local PKGS="$(dpkg -l $* | grep '^[a-z]' | grep '^[^u]' | wc -l)"
  536. if [ "$PKGS" != 0 ]; then
  537. echo
  538. dpkg -l $* | grep '^[a-z]'
  539. msgfail
  540. return 1
  541. fi
  542. msgpass
  543. }