framework 27 KB

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