framework 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. #!/bin/sh -- # no runable script, just for vi
  2. # we all like colorful messages
  3. if expr match "$(readlink -f /proc/$$/fd/1)" '/dev/pts/[0-9]\+' > /dev/null && \
  4. expr match "$(readlink -f /proc/$$/fd/2)" '/dev/pts/[0-9]\+' > /dev/null; then
  5. CERROR="" # red
  6. CWARNING="" # yellow
  7. CMSG="" # green
  8. CINFO="" # light blue
  9. CDEBUG="" # blue
  10. CNORMAL="" # default system console color
  11. CDONE="" # green
  12. CPASS="" # green
  13. CFAIL="" # red
  14. CCMD="" # pink
  15. fi
  16. msgdie() { echo "${CERROR}E: $1${CNORMAL}" >&2; exit 1; }
  17. msgwarn() { echo "${CWARNING}W: $1${CNORMAL}" >&2; }
  18. msgmsg() { echo "${CMSG}$1${CNORMAL}" >&2; }
  19. msginfo() { echo "${CINFO}I: $1${CNORMAL}" >&2; }
  20. msgdebug() { echo "${CDEBUG}D: $1${CNORMAL}" >&2; }
  21. msgdone() { echo "${CDONE}DONE${CNORMAL}" >&2; }
  22. msgnwarn() { echo -n "${CWARNING}W: $1${CNORMAL}" >&2; }
  23. msgnmsg() { echo -n "${CMSG}$1${CNORMAL}" >&2; }
  24. msgninfo() { echo -n "${CINFO}I: $1${CNORMAL}" >&2; }
  25. msgndebug() { echo -n "${CDEBUG}D: $1${CNORMAL}" >&2; }
  26. msgtest() {
  27. while [ -n "$1" ]; do
  28. echo -n "${CINFO}$1${CCMD} " >&2;
  29. echo -n "$(echo "$2" | sed -e 's/^aptc/apt-c/' -e 's/^aptg/apt-g/' -e 's/^aptf/apt-f/')${CINFO} " >&2;
  30. shift 2
  31. done
  32. echo -n "…${CNORMAL} " >&2;
  33. }
  34. msgpass() { echo "${CPASS}PASS${CNORMAL}" >&2; }
  35. msgskip() { echo "${CWARNING}SKIP${CNORMAL}" >&2; }
  36. msgfail() { echo "${CFAIL}FAIL${CNORMAL}" >&2; }
  37. # enable / disable Debugging
  38. MSGLEVEL=${MSGLEVEL:-3}
  39. if [ $MSGLEVEL -le 0 ]; then
  40. msgdie() { true; }
  41. fi
  42. if [ $MSGLEVEL -le 1 ]; then
  43. msgwarn() { true; }
  44. msgnwarn() { true; }
  45. fi
  46. if [ $MSGLEVEL -le 2 ]; then
  47. msgmsg() { true; }
  48. msgnmsg() { true; }
  49. msgtest() { true; }
  50. msgpass() { echo -n " ${CPASS}P${CNORMAL}" >&2; }
  51. msgskip() { echo -n " ${CWARNING}S${CNORMAL}" >&2; }
  52. if [ -n "$CFAIL" ]; then
  53. msgfail() { echo -n " ${CFAIL}FAIL${CNORMAL}" >&2; }
  54. else
  55. msgfail() { echo -n " ###FAILED###" >&2; }
  56. fi
  57. fi
  58. if [ $MSGLEVEL -le 3 ]; then
  59. msginfo() { true; }
  60. msgninfo() { true; }
  61. fi
  62. if [ $MSGLEVEL -le 4 ]; then
  63. msgdebug() { true; }
  64. msgndebug() { true; }
  65. fi
  66. msgdone() {
  67. if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
  68. [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
  69. [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
  70. [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
  71. [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
  72. true;
  73. else
  74. echo "${CDONE}DONE${CNORMAL}" >&2;
  75. fi
  76. }
  77. runapt() {
  78. msgdebug "Executing: ${CCMD}$*${CDEBUG} "
  79. if [ -f ./aptconfig.conf ]; then
  80. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  81. elif [ -f ../aptconfig.conf ]; then
  82. APT_CONFIG=../aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  83. else
  84. LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
  85. fi
  86. }
  87. aptconfig() { runapt apt-config $*; }
  88. aptcache() { runapt apt-cache $*; }
  89. aptget() { runapt apt-get $*; }
  90. aptftparchive() { runapt apt-ftparchive $*; }
  91. aptkey() { runapt apt-key $*; }
  92. aptmark() { runapt apt-mark $*; }
  93. dpkg() {
  94. $(which dpkg) --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log $*
  95. }
  96. aptitude() {
  97. if [ -f ./aptconfig.conf ]; then
  98. APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  99. elif [ -f ../aptconfig.conf ]; then
  100. APT_CONFIG=../aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  101. else
  102. LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
  103. fi
  104. }
  105. addtrap() {
  106. CURRENTTRAP="$CURRENTTRAP $1"
  107. trap "$CURRENTTRAP exit;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  108. }
  109. setupenvironment() {
  110. TMPWORKINGDIRECTORY=$(mktemp -d)
  111. TESTDIRECTORY=$(readlink -f $(dirname $0))
  112. msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
  113. BUILDDIRECTORY="${TESTDIRECTORY}/../../build/bin"
  114. test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
  115. local OLDWORKINGDIRECTORY=$(pwd)
  116. addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY; cd $OLDWORKINGDIRECTORY;"
  117. cd $TMPWORKINGDIRECTORY
  118. mkdir rootdir aptarchive keys
  119. cd rootdir
  120. mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
  121. mkdir -p var/cache var/lib var/log
  122. mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
  123. touch var/lib/dpkg/available
  124. mkdir -p usr/lib/apt
  125. ln -s ${BUILDDIRECTORY}/methods usr/lib/apt/methods
  126. cd ..
  127. local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
  128. if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
  129. cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
  130. fi
  131. local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
  132. if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
  133. cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
  134. fi
  135. cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
  136. ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  137. echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
  138. echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
  139. echo "Debug::NoLocking \"true\";" >> aptconfig.conf
  140. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  141. echo "Dir::Bin::Methods \"${BUILDDIRECTORY}/methods\";" >> aptconfig.conf
  142. echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
  143. echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
  144. echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
  145. echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
  146. echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
  147. if ! $(which dpkg) --assert-multi-arch; then
  148. echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
  149. fi
  150. echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
  151. echo 'quiet::NoUpdate "true";' >> aptconfig.conf
  152. export LC_ALL=C
  153. export PATH="${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
  154. msgdone "info"
  155. }
  156. getarchitecture() {
  157. if [ "$1" = "native" -o -z "$1" ]; then
  158. eval `aptconfig shell ARCH APT::Architecture`
  159. if [ -n "$ARCH" ]; then
  160. echo $ARCH
  161. else
  162. dpkg-architecture -qDEB_BUILD_ARCH
  163. fi
  164. else
  165. echo $1
  166. fi
  167. }
  168. getarchitectures() {
  169. echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  170. }
  171. configarchitecture() {
  172. local CONFFILE=rootdir/etc/apt/apt.conf.d/01multiarch.conf
  173. rm -f $CONFFILE
  174. echo "APT::Architecture \"$(getarchitecture $1)\";" > $CONFFILE
  175. shift
  176. while [ -n "$1" ]; do
  177. echo "APT::Architectures:: \"$(getarchitecture $1)\";" >> $CONFFILE
  178. shift
  179. done
  180. configdpkg
  181. }
  182. configdpkg() {
  183. if [ ! -e rootdir/var/lib/dpkg/status ]; then
  184. local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
  185. if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
  186. cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
  187. else
  188. echo -n > rootdir/var/lib/dpkg/status
  189. fi
  190. fi
  191. if $(which dpkg) --assert-multi-arch; then
  192. local ARCHS="$(getarchitectures)"
  193. if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
  194. DPKGARCH="$(dpkg --print-architecture)"
  195. for ARCH in ${ARCHS}; do
  196. if [ "${ARCH}" != "${DPKGARCH}" ]; then dpkg --add-architecture ${ARCH}; fi
  197. done
  198. if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
  199. insertinstalledpackage 'dpkg' "all" '1.16.2~wipmultiarch~fake'
  200. fi
  201. fi
  202. fi
  203. }
  204. setupsimplenativepackage() {
  205. local NAME="$1"
  206. local ARCH="$2"
  207. local VERSION="$3"
  208. local RELEASE="${4:-unstable}"
  209. local DEPENDENCIES="$5"
  210. local DESCRIPTION="$6"
  211. local SECTION="${7:-others}"
  212. local DISTSECTION
  213. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  214. DISTSECTION="main"
  215. else
  216. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  217. fi
  218. local BUILDDIR=incoming/${NAME}-${VERSION}
  219. mkdir -p ${BUILDDIR}/debian/source
  220. cd ${BUILDDIR}
  221. echo "* most suckless software product ever" > FEATURES
  222. test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
  223. test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
  224. * Initial release
  225. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
  226. test -e debian/control || echo "Source: $NAME
  227. Section: $SECTION
  228. Priority: optional
  229. Maintainer: Joe Sixpack <joe@example.org>
  230. Build-Depends: debhelper (>= 7)
  231. Standards-Version: 3.9.1
  232. Package: $NAME" > debian/control
  233. if [ "$ARCH" = 'all' ]; then
  234. echo "Architecture: all" >> debian/control
  235. else
  236. echo "Architecture: any" >> debian/control
  237. fi
  238. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> 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…" >> debian/control
  244. else
  245. echo "Description: $DESCRIPTION" >> debian/control
  246. fi
  247. test -e debian/compat || echo "7" > debian/compat
  248. test -e debian/source/format || echo "3.0 (native)" > debian/source/format
  249. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  250. cd - > /dev/null
  251. }
  252. buildsimplenativepackage() {
  253. local NAME="$1"
  254. local ARCH="$2"
  255. local VERSION="$3"
  256. local RELEASE="${4:-unstable}"
  257. local DEPENDENCIES="$5"
  258. local DESCRIPTION="$6"
  259. local SECTION="${7:-others}"
  260. local PRIORITY="${8:-optional}"
  261. local DISTSECTION
  262. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  263. DISTSECTION="main"
  264. else
  265. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  266. fi
  267. local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
  268. msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
  269. mkdir -p $BUILDDIR/debian/source
  270. echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
  271. echo "#!/bin/sh
  272. echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
  273. echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
  274. echo "$NAME ($VERSION) $RELEASE; urgency=low
  275. * Initial release
  276. -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
  277. echo "Source: $NAME
  278. Section: $SECTION
  279. Priority: $PRIORITY
  280. Maintainer: Joe Sixpack <joe@example.org>
  281. Standards-Version: 3.9.1
  282. Package: $NAME" > ${BUILDDIR}/debian/control
  283. if [ "$ARCH" = 'all' ]; then
  284. echo "Architecture: all" >> ${BUILDDIR}/debian/control
  285. else
  286. echo "Architecture: any" >> ${BUILDDIR}/debian/control
  287. fi
  288. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> ${BUILDDIR}/debian/control
  289. if [ -z "$DESCRIPTION" ]; then
  290. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  291. If you find such a package installed on your system,
  292. YOU did something horribly wrong! They are autogenerated
  293. und used only by testcases for APT and surf no other propose…" >> ${BUILDDIR}/debian/control
  294. else
  295. echo "Description: $DESCRIPTION" >> ${BUILDIR}/debian/control
  296. fi
  297. echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
  298. local SRCS="$( (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | grep '^dpkg-source: info: building' | grep -o '[a-z0-9._+~-]*$')"
  299. for SRC in $SRCS; do
  300. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  301. done
  302. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  303. rm -rf ${BUILDDIR}/debian/tmp
  304. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  305. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  306. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  307. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  308. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  309. dpkg-deb --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. 2> /dev/null > /dev/null
  310. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  311. done
  312. mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
  313. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
  314. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
  315. rm -rf "${BUILDDIR}"
  316. msgdone "info"
  317. }
  318. buildpackage() {
  319. local BUILDDIR=$1
  320. local RELEASE=$2
  321. local SECTION=$3
  322. local ARCH=$(getarchitecture $4)
  323. msgninfo "Build package $(echo "$BUILDDIR" | grep -o '[^/]*$') for ${RELEASE} in ${SECTION}… "
  324. cd $BUILDDIR
  325. if [ "$ARCH" = "all" ]; then
  326. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  327. fi
  328. local BUILT="$(dpkg-buildpackage -uc -us -a$ARCH 2> /dev/null)"
  329. local PKGS="$( echo "$BUILT" | grep '^dpkg-deb: building package' | cut -d'/' -f 2 | sed -e "s#'\.##")"
  330. local SRCS="$( echo "$BUILT" | grep '^dpkg-source: info: building' | grep -o '[a-z0-9._+~-]*$')"
  331. cd - > /dev/null
  332. for PKG in $PKGS; do
  333. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  334. done
  335. for SRC in $SRCS; do
  336. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  337. done
  338. msgdone "info"
  339. }
  340. buildaptarchive() {
  341. if [ -d incoming ]; then
  342. buildaptarchivefromincoming $*
  343. else
  344. buildaptarchivefromfiles $*
  345. fi
  346. }
  347. createaptftparchiveconfig() {
  348. 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' ' ')"
  349. if [ -z "$ARCHS" ]; then
  350. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  351. ARCHS="$(getarchitectures)"
  352. fi
  353. echo -n 'Dir {
  354. ArchiveDir "' >> ftparchive.conf
  355. echo -n $(readlink -f .) >> ftparchive.conf
  356. echo -n '";
  357. CacheDir "' >> ftparchive.conf
  358. echo -n $(readlink -f ..) >> ftparchive.conf
  359. echo -n '";
  360. FileListDir "' >> ftparchive.conf
  361. echo -n $(readlink -f pool/) >> ftparchive.conf
  362. echo -n '";
  363. };
  364. Default {
  365. Packages::Compress ". gzip bzip2 lzma xz";
  366. Sources::Compress ". gzip bzip2 lzma xz";
  367. Contents::Compress ". gzip bzip2 lzma xz";
  368. Translation::Compress ". gzip bzip2 lzma xz";
  369. LongDescription "false";
  370. };
  371. TreeDefault {
  372. Directory "pool/";
  373. SrcDirectory "pool/";
  374. };
  375. APT {
  376. FTPArchive {
  377. Release {
  378. Origin "joesixpack";
  379. Label "apttestcases";
  380. Suite "unstable";
  381. Description "repository with dummy packages";
  382. Architectures "' >> ftparchive.conf
  383. echo -n "$ARCHS" >> ftparchive.conf
  384. echo 'source";
  385. };
  386. };
  387. };' >> ftparchive.conf
  388. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  389. echo -n 'tree "dists/' >> ftparchive.conf
  390. echo -n "$DIST" >> ftparchive.conf
  391. echo -n '" {
  392. Architectures "' >> ftparchive.conf
  393. echo -n "$ARCHS" >> ftparchive.conf
  394. echo -n 'source";
  395. FileList "' >> ftparchive.conf
  396. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  397. echo -n '";
  398. SourceFileList "' >> ftparchive.conf
  399. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  400. echo -n '";
  401. Sections "' >> ftparchive.conf
  402. 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
  403. echo '";
  404. };' >> ftparchive.conf
  405. done
  406. }
  407. buildaptftparchivedirectorystructure() {
  408. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  409. for DIST in $DISTS; do
  410. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  411. for SECTION in $SECTIONS; do
  412. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  413. for ARCH in $ARCHS; do
  414. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  415. done
  416. mkdir -p dists/${DIST}/${SECTION}/source
  417. mkdir -p dists/${DIST}/${SECTION}/i18n
  418. done
  419. done
  420. }
  421. insertpackage() {
  422. local RELEASE="$1"
  423. local NAME="$2"
  424. local ARCH="$3"
  425. local VERSION="$4"
  426. local DEPENDENCIES="$5"
  427. local PRIORITY="${6:-optional}"
  428. local ARCHS=""
  429. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  430. if [ "$arch" = "all" ]; then
  431. ARCHS="$(getarchitectures)"
  432. else
  433. ARCHS="$arch"
  434. fi
  435. for BUILDARCH in $ARCHS; do
  436. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  437. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  438. touch aptarchive/dists/${RELEASE}/main/source/Sources
  439. local FILE="${PPATH}/Packages"
  440. echo "Package: $NAME
  441. Priority: $PRIORITY
  442. Section: other
  443. Installed-Size: 42
  444. Maintainer: Joe Sixpack <joe@example.org>
  445. Architecture: $arch
  446. Version: $VERSION
  447. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  448. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  449. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  450. If you find such a package installed on your system,
  451. YOU did something horribly wrong! They are autogenerated
  452. und used only by testcases for APT and surf no other propose…
  453. " >> $FILE
  454. done
  455. done
  456. }
  457. insertsource() {
  458. local RELEASE="$1"
  459. local NAME="$2"
  460. local ARCH="$3"
  461. local VERSION="$4"
  462. local DEPENDENCIES="$5"
  463. local ARCHS=""
  464. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  465. mkdir -p $SPATH
  466. local FILE="${SPATH}/Sources"
  467. echo "Package: $NAME
  468. Binary: $NAME
  469. Version: $VERSION
  470. Maintainer: Joe Sixpack <joe@example.org>
  471. Architecture: $ARCH" >> $FILE
  472. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  473. echo "Files:
  474. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  475. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz" >> $FILE
  476. }
  477. insertinstalledpackage() {
  478. local NAME="$1"
  479. local ARCH="$2"
  480. local VERSION="$3"
  481. local DEPENDENCIES="$4"
  482. local PRIORITY="${5:-optional}"
  483. local FILE='rootdir/var/lib/dpkg/status'
  484. local INFO='rootdir/var/lib/dpkg/info'
  485. for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
  486. echo "Package: $NAME
  487. Status: install ok installed
  488. Priority: $PRIORITY
  489. Section: other
  490. Installed-Size: 42
  491. Maintainer: Joe Sixpack <joe@example.org>
  492. Architecture: $arch
  493. Version: $VERSION" >> $FILE
  494. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  495. echo "Description: an autogenerated dummy ${NAME}=${VERSION}/installed
  496. If you find such a package installed on your system,
  497. YOU did something horribly wrong! They are autogenerated
  498. und used only by testcases for APT and surf no other propose…
  499. " >> $FILE
  500. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  501. echo -n > ${INFO}/${NAME}:${arch}.list
  502. else
  503. echo -n > ${INFO}/${NAME}.list
  504. fi
  505. done
  506. }
  507. buildaptarchivefromincoming() {
  508. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  509. cd aptarchive
  510. [ -e pool ] || ln -s ../incoming pool
  511. [ -e ftparchive.conf ] || createaptftparchiveconfig
  512. [ -e dists ] || buildaptftparchivedirectorystructure
  513. msgninfo "\tGenerate Packages, Sources and Contents files… "
  514. aptftparchive -qq generate ftparchive.conf
  515. cd - > /dev/null
  516. msgdone "info"
  517. generatereleasefiles
  518. }
  519. buildaptarchivefromfiles() {
  520. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  521. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  522. msgninfo "\t${line} file… "
  523. cat ${line} | gzip > ${line}.gz
  524. cat ${line} | bzip2 > ${line}.bz2
  525. cat ${line} | lzma > ${line}.lzma
  526. cat ${line} | xz > ${line}.xz
  527. msgdone "info"
  528. done
  529. generatereleasefiles
  530. }
  531. # can be overridden by testcases for their pleasure
  532. getcodenamefromsuite() { echo -n "$1"; }
  533. getreleaseversionfromsuite() { true; }
  534. getlabelfromsuite() { true; }
  535. generatereleasefiles() {
  536. # $1 is the Date header and $2 is the ValidUntil header to be set
  537. # both should be given in notation date/touch can understand
  538. msgninfo "\tGenerate Release files… "
  539. if [ -e aptarchive/dists ]; then
  540. for dir in $(find ./aptarchive/dists -mindepth 3 -maxdepth 3 -type d -name 'i18n'); do
  541. aptftparchive -qq release $dir -o APT::FTPArchive::Release::Patterns::='Translation-*' > $dir/Index
  542. done
  543. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  544. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  545. local CODENAME="$(getcodenamefromsuite $SUITE)"
  546. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  547. local LABEL="$(getlabelfromsuite $SUITE)"
  548. if [ -n "$VERSION" ]; then
  549. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  550. fi
  551. if [ -n "$LABEL" ]; then
  552. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  553. fi
  554. aptftparchive -qq release $dir \
  555. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  556. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  557. ${LABEL} \
  558. ${VERSION} \
  559. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  560. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  561. sed -i '/^Date: / a\
  562. NotAutomatic: yes' $dir/Release
  563. fi
  564. if [ -n "$1" -a "$1" != "now" ]; then
  565. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  566. fi
  567. if [ -n "$2" ]; then
  568. sed -i "/^Date: / a\
  569. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  570. fi
  571. done
  572. else
  573. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  574. fi
  575. if [ -n "$1" -a "$1" != "now" ]; then
  576. for release in $(find ./aptarchive -name 'Release'); do
  577. touch -d "$1" $release
  578. done
  579. fi
  580. msgdone "info"
  581. }
  582. setupdistsaptarchive() {
  583. local APTARCHIVE=$(readlink -f ./aptarchive)
  584. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  585. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  586. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  587. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  588. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  589. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  590. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  591. msgdone "info"
  592. done
  593. }
  594. setupflataptarchive() {
  595. local APTARCHIVE=$(readlink -f ./aptarchive)
  596. if [ -f ${APTARCHIVE}/Packages ]; then
  597. msgninfo "\tadd deb sources.list line… "
  598. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  599. msgdone "info"
  600. else
  601. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  602. fi
  603. if [ -f ${APTARCHIVE}/Sources ]; then
  604. msgninfo "\tadd deb-src sources.list line… "
  605. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  606. msgdone "info"
  607. else
  608. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  609. fi
  610. }
  611. setupaptarchive() {
  612. buildaptarchive
  613. if [ -e aptarchive/dists ]; then
  614. setupdistsaptarchive
  615. else
  616. setupflataptarchive
  617. fi
  618. signreleasefiles
  619. msgninfo "\tSync APT's cache with the archive… "
  620. aptget update -qq
  621. msgdone "info"
  622. }
  623. signreleasefiles() {
  624. local SIGNER="${1:-Joe Sixpack}"
  625. msgninfo "\tSign archive with $SIGNER key… "
  626. local SECKEYS=""
  627. for KEY in $(find keys/ -name '*.sec'); do
  628. SECKEYS="$SECKEYS --secret-keyring $KEY"
  629. done
  630. local PUBKEYS=""
  631. for KEY in $(find keys/ -name '*.pub'); do
  632. PUBKEYS="$PUBKEYS --keyring $KEY"
  633. done
  634. for RELEASE in $(find aptarchive/ -name Release); do
  635. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" -abs -o ${RELEASE}.gpg ${RELEASE}
  636. gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" --clearsign -o "$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')" $RELEASE
  637. done
  638. msgdone "info"
  639. }
  640. changetowebserver() {
  641. if which weborf > /dev/null; then
  642. weborf -xb aptarchive/ 2>&1 > /dev/null &
  643. addtrap "kill $!;"
  644. elif which gatling > /dev/null; then
  645. cd aptarchive
  646. gatling -p 8080 -F -S 2>&1 > /dev/null &
  647. addtrap "kill $!;"
  648. cd - > /dev/null
  649. elif which lighttpd > /dev/null; then
  650. echo "server.document-root = \"$(readlink -f ./aptarchive)\"
  651. server.port = 8080
  652. server.stat-cache-engine = \"disable\"" > lighttpd.conf
  653. lighttpd -t -f lighttpd.conf >/dev/null || msgdie 'Can not change to webserver: our lighttpd config is invalid'
  654. lighttpd -D -f lighttpd.conf 2>/dev/null >/dev/null &
  655. addtrap "kill $!;"
  656. else
  657. msgdie 'You have to install weborf or lighttpd first'
  658. return 1
  659. fi
  660. local APTARCHIVE="file://$(readlink -f ./aptarchive)"
  661. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  662. sed -i $LIST -e "s#$APTARCHIVE#http://localhost:8080/#"
  663. done
  664. return 0
  665. }
  666. checkdiff() {
  667. local DIFFTEXT="$($(which diff) -u $* | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  668. if [ -n "$DIFFTEXT" ]; then
  669. echo
  670. echo "$DIFFTEXT"
  671. return 1
  672. else
  673. return 0
  674. fi
  675. }
  676. testfileequal() {
  677. local FILE="$1"
  678. shift
  679. msgtest "Test for correctness of file" "$FILE"
  680. if [ -z "$*" ]; then
  681. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  682. else
  683. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  684. fi
  685. }
  686. testequal() {
  687. local COMPAREFILE=$(mktemp)
  688. addtrap "rm $COMPAREFILE;"
  689. echo "$1" > $COMPAREFILE
  690. shift
  691. msgtest "Test for equality of" "$*"
  692. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  693. }
  694. testequalor2() {
  695. local COMPAREFILE1=$(mktemp)
  696. local COMPAREFILE2=$(mktemp)
  697. local COMPAREAGAINST=$(mktemp)
  698. addtrap "rm $COMPAREFILE1 $COMPAREFILE2 $COMPAREAGAINST;"
  699. echo "$1" > $COMPAREFILE1
  700. echo "$2" > $COMPAREFILE2
  701. shift 2
  702. msgtest "Test for equality OR of" "$*"
  703. $* 2>&1 1> $COMPAREAGAINST
  704. (checkdiff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null ||
  705. checkdiff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null) && msgpass ||
  706. ( echo "\n${CINFO}Diff against OR 1${CNORMAL}" "$(checkdiff $COMPAREFILE1 $COMPAREAGAINST)" \
  707. "\n${CINFO}Diff against OR 2${CNORMAL}" "$(checkdiff $COMPAREFILE2 $COMPAREAGAINST)" &&
  708. msgfail )
  709. }
  710. testshowvirtual() {
  711. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  712. local PACKAGE="$1"
  713. shift
  714. while [ -n "$1" ]; do
  715. VIRTUAL="${VIRTUAL}
  716. N: Can't select versions from package '$1' as it is purely virtual"
  717. PACKAGE="${PACKAGE} $1"
  718. shift
  719. done
  720. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  721. VIRTUAL="${VIRTUAL}
  722. N: No packages found"
  723. local COMPAREFILE=$(mktemp)
  724. addtrap "rm $COMPAREFILE;"
  725. local ARCH="$(getarchitecture 'native')"
  726. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  727. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  728. }
  729. testnopackage() {
  730. msgtest "Test for non-existent packages" "apt-cache show $*"
  731. local SHOWPKG="$(aptcache show $* 2>&1 | grep '^Package: ')"
  732. if [ -n "$SHOWPKG" ]; then
  733. echo
  734. echo "$SHOWPKG"
  735. msgfail
  736. return 1
  737. fi
  738. msgpass
  739. }
  740. testdpkginstalled() {
  741. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  742. local PKGS="$(dpkg -l $* | grep '^i' | wc -l)"
  743. if [ "$PKGS" != $# ]; then
  744. echo $PKGS
  745. dpkg -l $* | grep '^[a-z]'
  746. msgfail
  747. return 1
  748. fi
  749. msgpass
  750. }
  751. testdpkgnotinstalled() {
  752. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  753. local PKGS="$(dpkg -l $* 2> /dev/null | grep '^i' | wc -l)"
  754. if [ "$PKGS" != 0 ]; then
  755. echo
  756. dpkg -l $* | grep '^[a-z]'
  757. msgfail
  758. return 1
  759. fi
  760. msgpass
  761. }
  762. testmarkedauto() {
  763. local COMPAREFILE=$(mktemp)
  764. addtrap "rm $COMPAREFILE;"
  765. if [ -n "$1" ]; then
  766. msgtest 'Test for correctly marked as auto-installed' "$*"
  767. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  768. else
  769. msgtest 'Test for correctly marked as auto-installed' 'no package'
  770. echo -n > $COMPAREFILE
  771. fi
  772. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  773. }
  774. pause() {
  775. echo "STOPPED execution. Press enter to continue"
  776. local IGNORE
  777. read IGNORE
  778. }