framework 26 KB

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