framework 26 KB

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