framework 19 KB

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