framework 24 KB

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