framework 25 KB

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