framework 26 KB

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