framework 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. #!/bin/sh -- # no runable script, just for vi
  2. EXIT_CODE=0
  3. # we all like colorful messages
  4. if [ "$MSGCOLOR" != 'NO' ]; then
  5. if [ ! -t 1 ]; then # but check that we output to a terminal
  6. export MSGCOLOR='NO'
  7. fi
  8. fi
  9. if [ "$MSGCOLOR" != 'NO' ]; then
  10. CERROR="\033[1;31m" # red
  11. CWARNING="\033[1;33m" # yellow
  12. CMSG="\033[1;32m" # green
  13. CINFO="\033[1;96m" # light blue
  14. CDEBUG="\033[1;94m" # blue
  15. CNORMAL="\033[0;39m" # default system console color
  16. CDONE="\033[1;32m" # green
  17. CPASS="\033[1;32m" # green
  18. CFAIL="\033[1;31m" # red
  19. CCMD="\033[1;35m" # pink
  20. fi
  21. msgdie() { printf "${CERROR}E: $1${CNORMAL}\n" >&2; exit 1; }
  22. msgwarn() { printf "${CWARNING}W: $1${CNORMAL}\n" >&2; }
  23. msgmsg() { printf "${CMSG}$1${CNORMAL}\n"; }
  24. msginfo() { printf "${CINFO}I: $1${CNORMAL}\n"; }
  25. msgdebug() { printf "${CDEBUG}D: $1${CNORMAL}\n"; }
  26. msgdone() { printf "${CDONE}DONE${CNORMAL}\n"; }
  27. msgnwarn() { printf "${CWARNING}W: $1${CNORMAL}" >&2; }
  28. msgnmsg() { printf "${CMSG}$1${CNORMAL}"; }
  29. msgninfo() { printf "${CINFO}I: $1${CNORMAL}"; }
  30. msgndebug() { printf "${CDEBUG}D: $1${CNORMAL}"; }
  31. msgtest() {
  32. while [ -n "$1" ]; do
  33. printf "${CINFO}$1${CCMD} "
  34. printf -- "$(echo "$2" | sed -e 's#^apt\([cgfs]\)#apt-\1#')${CINFO} "
  35. shift
  36. if [ -n "$1" ]; then shift; else break; fi
  37. done
  38. printf "…${CNORMAL} "
  39. }
  40. msgpass() { printf "${CPASS}PASS${CNORMAL}\n"; }
  41. msgskip() { printf "${CWARNING}SKIP${CNORMAL}\n" >&2; }
  42. msgfail() {
  43. if [ $# -gt 0 ]; then printf "${CFAIL}FAIL: $*${CNORMAL}\n" >&2;
  44. else printf "${CFAIL}FAIL${CNORMAL}\n" >&2; fi
  45. EXIT_CODE=$((EXIT_CODE+1));
  46. }
  47. # enable / disable Debugging
  48. MSGLEVEL=${MSGLEVEL:-3}
  49. if [ $MSGLEVEL -le 0 ]; then
  50. msgdie() { true; }
  51. fi
  52. if [ $MSGLEVEL -le 1 ]; then
  53. msgwarn() { true; }
  54. msgnwarn() { true; }
  55. fi
  56. if [ $MSGLEVEL -le 2 ]; then
  57. msgmsg() { true; }
  58. msgnmsg() { true; }
  59. msgtest() { true; }
  60. msgpass() { printf " ${CPASS}P${CNORMAL}"; }
  61. msgskip() { printf " ${CWARNING}S${CNORMAL}" >&2; }
  62. if [ -n "$CFAIL" ]; then
  63. msgfail() { printf " ${CFAIL}FAIL${CNORMAL}" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  64. else
  65. msgfail() { printf " ###FAILED###" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
  66. fi
  67. fi
  68. if [ $MSGLEVEL -le 3 ]; then
  69. msginfo() { true; }
  70. msgninfo() { true; }
  71. fi
  72. if [ $MSGLEVEL -le 4 ]; then
  73. msgdebug() { true; }
  74. msgndebug() { true; }
  75. fi
  76. msgdone() {
  77. if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
  78. [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
  79. [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
  80. [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
  81. [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
  82. true;
  83. else
  84. printf "${CDONE}DONE${CNORMAL}\n";
  85. fi
  86. }
  87. getaptconfig() {
  88. if [ -f ./aptconfig.conf ]; then
  89. echo "./aptconfig.conf"
  90. elif [ -f ../aptconfig.conf ]; then
  91. echo "../aptconfig.conf"
  92. fi
  93. }
  94. runapt() {
  95. msgdebug "Executing: ${CCMD}$*${CDEBUG} "
  96. local CMD="$1"
  97. shift
  98. case $CMD in
  99. sh|aptitude|*/*|command) ;;
  100. *) CMD="${BUILDDIRECTORY}/$CMD";;
  101. esac
  102. MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH=${LIBRARYPATH} $CMD "$@"
  103. }
  104. aptconfig() { runapt apt-config "$@"; }
  105. aptcache() { runapt apt-cache "$@"; }
  106. aptcdrom() { runapt apt-cdrom "$@"; }
  107. aptget() { runapt apt-get "$@"; }
  108. aptftparchive() { runapt apt-ftparchive "$@"; }
  109. aptkey() { runapt apt-key "$@"; }
  110. aptmark() { runapt apt-mark "$@"; }
  111. aptsortpkgs() { runapt apt-sortpkgs "$@"; }
  112. apt() { runapt apt "$@"; }
  113. apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
  114. aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
  115. aptitude() { runapt aptitude "$@"; }
  116. aptextracttemplates() { runapt apt-extracttemplates "$@"; }
  117. aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
  118. dpkg() {
  119. command dpkg --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log "$@"
  120. }
  121. dpkgcheckbuilddeps() {
  122. command dpkg-checkbuilddeps --admindir=${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg "$@"
  123. }
  124. gdb() {
  125. local CMD="$1"
  126. shift
  127. runapt command gdb --quiet -ex run "${BUILDDIRECTORY}/$CMD" --args "${BUILDDIRECTORY}/$CMD" "$@"
  128. }
  129. gpg() {
  130. # see apt-key for the whole trickery. Setup is done in setupenvironment
  131. command gpg --ignore-time-conflict --no-options --no-default-keyring \
  132. --homedir "${TMPWORKINGDIRECTORY}/gnupghome" \
  133. --no-auto-check-trustdb --trust-model always \
  134. "$@"
  135. }
  136. exitwithstatus() {
  137. # error if we about to overflow, but ...
  138. # "255 failures ought to be enough for everybody"
  139. if [ $EXIT_CODE -gt 255 ]; then
  140. msgdie "Total failure count $EXIT_CODE too big"
  141. fi
  142. exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
  143. }
  144. shellsetedetector() {
  145. local exit_status=$?
  146. if [ "$exit_status" != '0' ]; then
  147. printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
  148. if [ "$EXIT_CODE" = '0' ]; then
  149. EXIT_CODE="$exit_status"
  150. fi
  151. fi
  152. }
  153. addtrap() {
  154. if [ "$1" = 'prefix' ]; then
  155. CURRENTTRAP="$2 $CURRENTTRAP"
  156. else
  157. CURRENTTRAP="$CURRENTTRAP $1"
  158. fi
  159. trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  160. }
  161. setupenvironment() {
  162. TMPWORKINGDIRECTORY=$(mktemp -d)
  163. TESTDIRECTORY=$(readlink -f $(dirname $0))
  164. msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
  165. # allow overriding the default BUILDDIR location
  166. BUILDDIRECTORY=${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}
  167. LIBRARYPATH=${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}
  168. METHODSDIR=${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}
  169. APTHELPERBINDIR=${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}
  170. APTWEBSERVERBINDIR=${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}
  171. APTINTERNALSOLVER=${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}
  172. test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
  173. # -----
  174. addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY;"
  175. cd $TMPWORKINGDIRECTORY
  176. mkdir rootdir aptarchive keys
  177. cd rootdir
  178. mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
  179. mkdir -p var/cache var/lib/apt var/log tmp
  180. mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
  181. touch var/lib/dpkg/available
  182. mkdir -p usr/lib/apt
  183. ln -s ${METHODSDIR} usr/lib/apt/methods
  184. if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
  185. mkdir -p usr/lib/apt/solvers
  186. ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
  187. ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
  188. echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
  189. fi
  190. # use the autoremove from the BUILDDIRECTORY if its there, otherwise
  191. # system
  192. if [ -e ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove ]; then
  193. ln -s ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove etc/apt/apt.conf.d/01autoremove
  194. else
  195. ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
  196. fi
  197. cd ..
  198. local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
  199. if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
  200. cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
  201. fi
  202. local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
  203. if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
  204. cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
  205. fi
  206. cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
  207. ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  208. echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
  209. echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
  210. echo "Debug::NoLocking \"true\";" >> aptconfig.conf
  211. echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
  212. echo "Dir::Bin::Methods \"${METHODSDIR}\";" >> aptconfig.conf
  213. echo "Dir::Bin::apt-key \"${BUILDDIRECTORY}/apt-key\";" >> aptconfig.conf
  214. echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
  215. echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
  216. echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
  217. echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
  218. echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
  219. if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
  220. echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
  221. fi
  222. echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
  223. echo 'quiet::NoUpdate "true";' >> aptconfig.conf
  224. echo 'quiet::NoStatistic "true";' >> aptconfig.conf
  225. echo "Acquire::https::CaInfo \"${TESTDIR}/apt.pem\";" > rootdir/etc/apt/apt.conf.d/99https
  226. echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
  227. configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
  228. # gpg needs a trustdb to function, but it can't be invalid (not even empty)
  229. # see also apt-key where this trickery comes from:
  230. local TRUSTDBDIR="${TMPWORKINGDIRECTORY}/gnupghome"
  231. mkdir "$TRUSTDBDIR"
  232. chmod 700 "$TRUSTDBDIR"
  233. # We also don't use a secret keyring, of course, but gpg panics and
  234. # implodes if there isn't one available - and writeable for imports
  235. local SECRETKEYRING="${TRUSTDBDIR}/secring.gpg"
  236. touch $SECRETKEYRING
  237. # now create the trustdb with an (empty) dummy keyring
  238. # newer gpg versions are fine without it, but play it safe for now
  239. gpg --quiet --check-trustdb --secret-keyring $SECRETKEYRING --keyring $SECRETKEYRING >/dev/null 2>&1
  240. # cleanup the environment a bit
  241. # prefer our apt binaries over the system apt binaries
  242. export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
  243. export LC_ALL=C.UTF-8
  244. unset LANGUAGE APT_CONFIG
  245. unset GREP_OPTIONS DEB_BUILD_PROFILES
  246. msgdone "info"
  247. }
  248. getarchitecture() {
  249. if [ "$1" = "native" -o -z "$1" ]; then
  250. eval `aptconfig shell ARCH APT::Architecture`
  251. if [ -n "$ARCH" ]; then
  252. echo $ARCH
  253. else
  254. dpkg --print-architecture
  255. fi
  256. else
  257. echo $1
  258. fi
  259. }
  260. getarchitectures() {
  261. echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
  262. }
  263. getarchitecturesfromcommalist() {
  264. echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
  265. }
  266. configarchitecture() {
  267. {
  268. echo "APT::Architecture \"$(getarchitecture $1)\";"
  269. while [ -n "$1" ]; do
  270. echo "APT::Architectures:: \"$(getarchitecture $1)\";"
  271. shift
  272. done
  273. } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
  274. configdpkg
  275. }
  276. configdpkg() {
  277. if [ ! -e rootdir/var/lib/dpkg/status ]; then
  278. local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
  279. if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
  280. cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
  281. else
  282. echo -n > rootdir/var/lib/dpkg/status
  283. fi
  284. fi
  285. rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
  286. if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
  287. local ARCHS="$(getarchitectures)"
  288. if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
  289. DPKGARCH="$(dpkg --print-architecture)"
  290. for ARCH in ${ARCHS}; do
  291. if [ "${ARCH}" != "${DPKGARCH}" ]; then
  292. if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
  293. # old-style used e.g. in Ubuntu-P – and as it seems travis
  294. echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  295. echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
  296. fi
  297. fi
  298. done
  299. if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
  300. # dpkg doesn't really check the version as long as it is fully installed,
  301. # but just to be sure we choose one above the required version
  302. insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
  303. fi
  304. fi
  305. fi
  306. }
  307. configcompression() {
  308. while [ -n "$1" ]; do
  309. case "$1" in
  310. '.') printf ".\t.\tcat\n";;
  311. 'gz') printf "gzip\tgz\tgzip\n";;
  312. 'bz2') printf "bzip2\tbz2\tbzip2\n";;
  313. 'lzma') printf "lzma\tlzma\txz --format=lzma\n";;
  314. 'xz') printf "xz\txz\txz\n";;
  315. *) printf "$1\t$1\t$1\n";;
  316. esac
  317. shift
  318. done > ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf
  319. }
  320. setupsimplenativepackage() {
  321. local NAME="$1"
  322. local ARCH="$2"
  323. local VERSION="$3"
  324. local RELEASE="${4:-unstable}"
  325. local DEPENDENCIES="$5"
  326. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  327. If you find such a package installed on your system,
  328. something went horribly wrong! They are autogenerated
  329. und used only by testcases and surf no other propose…"}"
  330. local SECTION="${7:-others}"
  331. local DISTSECTION
  332. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  333. DISTSECTION="main"
  334. else
  335. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  336. fi
  337. local BUILDDIR=incoming/${NAME}-${VERSION}
  338. mkdir -p ${BUILDDIR}/debian/source
  339. cd ${BUILDDIR}
  340. echo "* most suckless software product ever" > FEATURES
  341. test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
  342. test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
  343. * Initial release
  344. -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
  345. test -e debian/control || echo "Source: $NAME
  346. Section: $SECTION
  347. Priority: optional
  348. Maintainer: Joe Sixpack <joe@example.org>
  349. Build-Depends: debhelper (>= 7)
  350. Standards-Version: 3.9.1
  351. Package: $NAME" > debian/control
  352. if [ "$ARCH" = 'all' ]; then
  353. echo "Architecture: all" >> debian/control
  354. else
  355. echo "Architecture: any" >> debian/control
  356. fi
  357. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
  358. echo "Description: $DESCRIPTION" >> debian/control
  359. test -e debian/compat || echo "7" > debian/compat
  360. test -e debian/source/format || echo "3.0 (native)" > debian/source/format
  361. test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
  362. cd - > /dev/null
  363. }
  364. buildsimplenativepackage() {
  365. local NAME="$1"
  366. local ARCH="$2"
  367. local VERSION="$3"
  368. local RELEASE="${4:-unstable}"
  369. local DEPENDENCIES="$5"
  370. local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  371. If you find such a package installed on your system,
  372. something went horribly wrong! They are autogenerated
  373. und used only by testcases and surf no other propose…"}"
  374. local SECTION="${7:-others}"
  375. local PRIORITY="${8:-optional}"
  376. local FILE_TREE="$9"
  377. local COMPRESS_TYPE="${10:-gzip}"
  378. local DISTSECTION
  379. if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
  380. DISTSECTION="main"
  381. else
  382. DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
  383. fi
  384. local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
  385. msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
  386. mkdir -p $BUILDDIR/debian/source
  387. echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
  388. echo "#!/bin/sh
  389. echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
  390. echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
  391. echo "$NAME ($VERSION) $RELEASE; urgency=low
  392. * Initial release
  393. -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
  394. echo "Source: $NAME
  395. Section: $SECTION
  396. Priority: $PRIORITY
  397. Maintainer: Joe Sixpack <joe@example.org>
  398. Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
  399. local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
  400. test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
  401. echo "
  402. Package: $NAME" >> ${BUILDDIR}/debian/control
  403. if [ "$ARCH" = 'all' ]; then
  404. echo "Architecture: all" >> ${BUILDDIR}/debian/control
  405. else
  406. echo "Architecture: any" >> ${BUILDDIR}/debian/control
  407. fi
  408. local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
  409. test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
  410. echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
  411. echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
  412. (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' \
  413. | while read SRC; do
  414. echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
  415. # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
  416. # gpg --yes --secret-keyring ./keys/joesixpack.sec \
  417. # --keyring ./keys/joesixpack.pub --default-key 'Joe Sixpack' \
  418. # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  419. # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
  420. # fi
  421. done
  422. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  423. rm -rf ${BUILDDIR}/debian/tmp
  424. mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
  425. cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
  426. cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
  427. if [ -n "$FILE_TREE" ]; then
  428. cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
  429. fi
  430. (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
  431. (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
  432. local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
  433. # ensure the right permissions as dpkg-deb ensists
  434. chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
  435. if ! dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. >$LOG 2>&1; then
  436. cat $LOG
  437. false
  438. fi
  439. rm $LOG
  440. echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
  441. done
  442. mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
  443. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
  444. cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
  445. rm -rf "${BUILDDIR}"
  446. msgdone "info"
  447. }
  448. buildpackage() {
  449. local BUILDDIR=$1
  450. local RELEASE=$2
  451. local SECTION=$3
  452. local ARCH=$(getarchitecture $4)
  453. local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
  454. local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
  455. msgninfo "Build package ${PKGNAME} for ${RELEASE} in ${SECTION}… "
  456. cd $BUILDDIR
  457. if [ "$ARCH" = "all" ]; then
  458. ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
  459. fi
  460. if ! dpkg-buildpackage -uc -us -a$ARCH >$BUILDLOG 2>&1 ; then
  461. cat $BUILDLOG
  462. false
  463. fi
  464. local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
  465. local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
  466. cd - > /dev/null
  467. for PKG in $PKGS; do
  468. echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
  469. done
  470. for SRC in $SRCS; do
  471. echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
  472. done
  473. msgdone "info"
  474. }
  475. buildaptarchive() {
  476. if [ -d incoming ]; then
  477. buildaptarchivefromincoming "$@"
  478. else
  479. buildaptarchivefromfiles "$@"
  480. fi
  481. }
  482. createaptftparchiveconfig() {
  483. local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
  484. COMPRESSORS="${COMPRESSORS%* }"
  485. 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' ' ')"
  486. if [ -z "$ARCHS" ]; then
  487. # the pool is empty, so we will operate on faked packages - let us use the configured archs
  488. ARCHS="$(getarchitectures)"
  489. fi
  490. echo -n 'Dir {
  491. ArchiveDir "' >> ftparchive.conf
  492. echo -n $(readlink -f .) >> ftparchive.conf
  493. echo -n '";
  494. CacheDir "' >> ftparchive.conf
  495. echo -n $(readlink -f ..) >> ftparchive.conf
  496. echo -n '";
  497. FileListDir "' >> ftparchive.conf
  498. echo -n $(readlink -f pool/) >> ftparchive.conf
  499. echo -n '";
  500. };
  501. Default {
  502. Packages::Compress "'"$COMPRESSORS"'";
  503. Sources::Compress "'"$COMPRESSORS"'";
  504. Contents::Compress "'"$COMPRESSORS"'";
  505. Translation::Compress "'"$COMPRESSORS"'";
  506. LongDescription "false";
  507. };
  508. TreeDefault {
  509. Directory "pool/";
  510. SrcDirectory "pool/";
  511. };
  512. APT {
  513. FTPArchive {
  514. Release {
  515. Origin "joesixpack";
  516. Label "apttestcases";
  517. Suite "unstable";
  518. Description "repository with dummy packages";
  519. Architectures "' >> ftparchive.conf
  520. echo -n "$ARCHS" >> ftparchive.conf
  521. echo 'source";
  522. };
  523. };
  524. };' >> ftparchive.conf
  525. for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
  526. echo -n 'tree "dists/' >> ftparchive.conf
  527. echo -n "$DIST" >> ftparchive.conf
  528. echo -n '" {
  529. Architectures "' >> ftparchive.conf
  530. echo -n "$ARCHS" >> ftparchive.conf
  531. echo -n 'source";
  532. FileList "' >> ftparchive.conf
  533. echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
  534. echo -n '";
  535. SourceFileList "' >> ftparchive.conf
  536. echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
  537. echo -n '";
  538. Sections "' >> ftparchive.conf
  539. 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
  540. echo '";
  541. };' >> ftparchive.conf
  542. done
  543. }
  544. buildaptftparchivedirectorystructure() {
  545. local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
  546. for DIST in $DISTS; do
  547. local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
  548. for SECTION in $SECTIONS; do
  549. local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
  550. for ARCH in $ARCHS; do
  551. mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
  552. done
  553. mkdir -p dists/${DIST}/${SECTION}/source
  554. mkdir -p dists/${DIST}/${SECTION}/i18n
  555. done
  556. done
  557. }
  558. insertpackage() {
  559. local RELEASE="$1"
  560. local NAME="$2"
  561. local ARCH="$3"
  562. local VERSION="$4"
  563. local DEPENDENCIES="$5"
  564. local PRIORITY="${6:-optional}"
  565. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
  566. If you find such a package installed on your system,
  567. something went horribly wrong! They are autogenerated
  568. und used only by testcases and surf no other propose…"}"
  569. local ARCHS=""
  570. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  571. if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
  572. ARCHS="$(getarchitectures)"
  573. else
  574. ARCHS="$arch"
  575. fi
  576. for BUILDARCH in $ARCHS; do
  577. local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
  578. mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
  579. touch aptarchive/dists/${RELEASE}/main/source/Sources
  580. local FILE="${PPATH}/Packages"
  581. echo "Package: $NAME
  582. Priority: $PRIORITY
  583. Section: other
  584. Installed-Size: 42
  585. Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
  586. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  587. echo "Version: $VERSION
  588. Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
  589. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  590. echo "Description: $DESCRIPTION" >> $FILE
  591. echo >> $FILE
  592. done
  593. done
  594. }
  595. insertsource() {
  596. local RELEASE="$1"
  597. local NAME="$2"
  598. local ARCH="$3"
  599. local VERSION="$4"
  600. local DEPENDENCIES="$5"
  601. local ARCHS=""
  602. local SPATH="aptarchive/dists/${RELEASE}/main/source"
  603. mkdir -p $SPATH
  604. local FILE="${SPATH}/Sources"
  605. echo "Package: $NAME
  606. Binary: $NAME
  607. Version: $VERSION
  608. Maintainer: Joe Sixpack <joe@example.org>
  609. Architecture: $ARCH" >> $FILE
  610. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  611. echo "Files:
  612. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
  613. d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
  614. " >> $FILE
  615. }
  616. insertinstalledpackage() {
  617. local NAME="$1"
  618. local ARCH="$2"
  619. local VERSION="$3"
  620. local DEPENDENCIES="$4"
  621. local PRIORITY="${5:-optional}"
  622. local STATUS="${6:-install ok installed}"
  623. local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
  624. If you find such a package installed on your system,
  625. something went horribly wrong! They are autogenerated
  626. und used only by testcases and surf no other propose…"}"
  627. local FILE='rootdir/var/lib/dpkg/status'
  628. local INFO='rootdir/var/lib/dpkg/info'
  629. for arch in $(getarchitecturesfromcommalist "$ARCH"); do
  630. echo "Package: $NAME
  631. Status: $STATUS
  632. Priority: $PRIORITY
  633. Section: other
  634. Installed-Size: 42
  635. Maintainer: Joe Sixpack <joe@example.org>
  636. Version: $VERSION" >> $FILE
  637. test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
  638. test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
  639. echo "Description: $DESCRIPTION" >> $FILE
  640. echo >> $FILE
  641. if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
  642. echo -n > ${INFO}/${NAME}:${arch}.list
  643. else
  644. echo -n > ${INFO}/${NAME}.list
  645. fi
  646. done
  647. }
  648. buildaptarchivefromincoming() {
  649. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
  650. cd aptarchive
  651. [ -e pool ] || ln -s ../incoming pool
  652. [ -e ftparchive.conf ] || createaptftparchiveconfig
  653. [ -e dists ] || buildaptftparchivedirectorystructure
  654. msgninfo "\tGenerate Packages, Sources and Contents files… "
  655. aptftparchive -qq generate ftparchive.conf
  656. cd - > /dev/null
  657. msgdone "info"
  658. generatereleasefiles
  659. }
  660. buildaptarchivefromfiles() {
  661. msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
  662. find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
  663. msgninfo "\t${line} file… "
  664. compressfile "$line" "$1"
  665. msgdone "info"
  666. done
  667. generatereleasefiles "$@"
  668. }
  669. compressfile() {
  670. cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
  671. if [ "$compressor" = '.' ]; then
  672. if [ -n "$2" ]; then
  673. touch -d "$2" "$1"
  674. fi
  675. continue
  676. fi
  677. cat "$1" | $command > "${1}.${extension}"
  678. if [ -n "$2" ]; then
  679. touch -d "$2" "${1}.${extension}"
  680. fi
  681. done
  682. }
  683. # can be overridden by testcases for their pleasure
  684. getcodenamefromsuite() {
  685. case "$1" in
  686. unstable) echo 'sid';;
  687. *) echo -n "$1";;
  688. esac
  689. }
  690. getreleaseversionfromsuite() { true; }
  691. getlabelfromsuite() { true; }
  692. generatereleasefiles() {
  693. # $1 is the Date header and $2 is the ValidUntil header to be set
  694. # both should be given in notation date/touch can understand
  695. msgninfo "\tGenerate Release files… "
  696. if [ -e aptarchive/dists ]; then
  697. for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
  698. local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
  699. local CODENAME="$(getcodenamefromsuite $SUITE)"
  700. local VERSION="$(getreleaseversionfromsuite $SUITE)"
  701. local LABEL="$(getlabelfromsuite $SUITE)"
  702. if [ -n "$VERSION" ]; then
  703. VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
  704. fi
  705. if [ -n "$LABEL" ]; then
  706. LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
  707. fi
  708. aptftparchive -qq release $dir \
  709. -o APT::FTPArchive::Release::Suite="${SUITE}" \
  710. -o APT::FTPArchive::Release::Codename="${CODENAME}" \
  711. ${LABEL} \
  712. ${VERSION} \
  713. | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
  714. if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
  715. sed -i '/^Date: / a\
  716. NotAutomatic: yes' $dir/Release
  717. fi
  718. if [ -n "$1" -a "$1" != "now" ]; then
  719. sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
  720. fi
  721. if [ -n "$2" ]; then
  722. sed -i "/^Date: / a\
  723. Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
  724. fi
  725. done
  726. else
  727. aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
  728. fi
  729. if [ -n "$1" -a "$1" != "now" ]; then
  730. for release in $(find ./aptarchive -name 'Release'); do
  731. touch -d "$1" $release
  732. done
  733. fi
  734. msgdone "info"
  735. }
  736. setupdistsaptarchive() {
  737. local APTARCHIVE=$(readlink -f ./aptarchive)
  738. rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
  739. rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
  740. for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
  741. SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
  742. msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
  743. echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
  744. echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
  745. msgdone "info"
  746. done
  747. }
  748. setupflataptarchive() {
  749. local APTARCHIVE=$(readlink -f ./aptarchive)
  750. if [ -f ${APTARCHIVE}/Packages ]; then
  751. msgninfo "\tadd deb sources.list line… "
  752. echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  753. msgdone "info"
  754. else
  755. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
  756. fi
  757. if [ -f ${APTARCHIVE}/Sources ]; then
  758. msgninfo "\tadd deb-src sources.list line… "
  759. echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  760. msgdone "info"
  761. else
  762. rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
  763. fi
  764. }
  765. setupaptarchive() {
  766. buildaptarchive
  767. if [ -e aptarchive/dists ]; then
  768. setupdistsaptarchive
  769. else
  770. setupflataptarchive
  771. fi
  772. signreleasefiles
  773. if [ "$1" != '--no-update' ]; then
  774. msgninfo "\tSync APT's cache with the archive… "
  775. aptget update -qq
  776. msgdone "info"
  777. fi
  778. }
  779. signreleasefiles() {
  780. local SIGNER="${1:-Joe Sixpack}"
  781. local GPG="gpg --batch --yes"
  782. msgninfo "\tSign archive with $SIGNER key… "
  783. local REXKEY='keys/rexexpired'
  784. local SECEXPIREBAK="${REXKEY}.sec.bak"
  785. local PUBEXPIREBAK="${REXKEY}.pub.bak"
  786. if [ "${SIGNER}" = 'Rex Expired' ]; then
  787. # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
  788. # option doesn't exist anymore (and using faketime would add a new obscure dependency)
  789. # therefore we 'temporary' make the key not expired and restore a backup after signing
  790. cp ${REXKEY}.sec $SECEXPIREBAK
  791. cp ${REXKEY}.pub $PUBEXPIREBAK
  792. local SECUNEXPIRED="${REXKEY}.sec.unexpired"
  793. local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
  794. if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
  795. cp $SECUNEXPIRED ${REXKEY}.sec
  796. cp $PUBUNEXPIRED ${REXKEY}.pub
  797. else
  798. printf "expire\n1w\nsave\n" | $GPG --keyring ${REXKEY}.pub --secret-keyring ${REXKEY}.sec --command-fd 0 --edit-key "${SIGNER}" >/dev/null 2>&1 || true
  799. cp ${REXKEY}.sec $SECUNEXPIRED
  800. cp ${REXKEY}.pub $PUBUNEXPIRED
  801. fi
  802. fi
  803. for KEY in $(find keys/ -name '*.sec'); do
  804. GPG="$GPG --secret-keyring $KEY"
  805. done
  806. for KEY in $(find keys/ -name '*.pub'); do
  807. GPG="$GPG --keyring $KEY"
  808. done
  809. for RELEASE in $(find aptarchive/ -name Release); do
  810. $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
  811. local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
  812. $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
  813. # we might have set a specific date for the Release file, so copy it
  814. touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
  815. done
  816. if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
  817. mv -f $SECEXPIREBAK ${REXKEY}.sec
  818. mv -f $PUBEXPIREBAK ${REXKEY}.pub
  819. fi
  820. msgdone "info"
  821. }
  822. webserverconfig() {
  823. msgtest "Set webserver config option '${1}' to" "$2"
  824. local DOWNLOG='rootdir/tmp/download-testfile.log'
  825. local STATUS='rootdir/tmp/webserverconfig.status'
  826. rm -f "$STATUS" "$DOWNLOG"
  827. if downloadfile "http://localhost:8080/_config/set/${1}/${2}" "$STATUS" > "$DOWNLOG"; then
  828. msgpass
  829. else
  830. cat "$DOWNLOG" "$STATUS"
  831. msgfail
  832. fi
  833. testwebserverlaststatuscode '200'
  834. }
  835. rewritesourceslist() {
  836. local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
  837. for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
  838. sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#http://localhost:4433/#${1}#"
  839. done
  840. }
  841. # wait for up to 10s for a pid file to appear to avoid possible race
  842. # when a helper is started and dosn't write the PID quick enough
  843. waitforpidfile() {
  844. local PIDFILE="$1"
  845. for i in $(seq 10); do
  846. if test -s "$PIDFILE"; then
  847. return 0
  848. fi
  849. sleep 1
  850. done
  851. msgdie "waiting for $PIDFILE failed"
  852. return 1
  853. }
  854. changetowebserver() {
  855. if [ "$1" != '--no-rewrite' ]; then
  856. rewritesourceslist 'http://localhost:8080/'
  857. else
  858. shift
  859. fi
  860. if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
  861. cd aptarchive
  862. local LOG="webserver.log"
  863. if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
  864. cat $LOG
  865. false
  866. fi
  867. waitforpidfile aptwebserver.pid
  868. local PID="$(cat aptwebserver.pid)"
  869. if [ -z "$PID" ]; then
  870. msgdie 'Could not fork aptwebserver successfully'
  871. fi
  872. addtrap "kill $PID;"
  873. cd - > /dev/null
  874. else
  875. msgdie 'You have to build aptwerbserver or install a webserver'
  876. fi
  877. }
  878. changetohttpswebserver() {
  879. if ! which stunnel4 >/dev/null; then
  880. msgdie 'You need to install stunnel4 for https testcases'
  881. fi
  882. if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
  883. changetowebserver --no-rewrite "$@"
  884. fi
  885. echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
  886. cert = ${TESTDIRECTORY}/apt.pem
  887. output = /dev/null
  888. [https]
  889. accept = 4433
  890. connect = 8080
  891. " > ${TMPWORKINGDIRECTORY}/stunnel.conf
  892. stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
  893. waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
  894. local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
  895. if [ -z "$PID" ]; then
  896. msgdie 'Could not fork stunnel4 successfully'
  897. fi
  898. addtrap 'prefix' "kill ${PID};"
  899. rewritesourceslist 'https://localhost:4433/'
  900. }
  901. changetocdrom() {
  902. mkdir -p rootdir/media/cdrom/.disk
  903. local CD="$(readlink -f rootdir/media/cdrom)"
  904. echo "acquire::cdrom::mount \"${CD}\";
  905. acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
  906. acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
  907. acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
  908. echo -n "$1" > ${CD}/.disk/info
  909. if [ ! -d aptarchive/dists ]; then
  910. msgdie 'Flat file archive cdroms can not be created currently'
  911. return 1
  912. fi
  913. mv aptarchive/dists "$CD"
  914. ln -s "$(readlink -f ./incoming)" $CD/pool
  915. find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
  916. # start with an unmounted disk
  917. mv "${CD}" "${CD}-unmounted"
  918. # we don't want the disk to be modifiable
  919. addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
  920. chmod -R -w rootdir/media/cdrom-unmounted/dists
  921. }
  922. downloadfile() {
  923. local PROTO="$(echo "$1" | cut -d':' -f 1 )"
  924. apthelper -o Debug::Acquire::${PROTO}=1 \
  925. download-file "$1" "$2" 2>&1 || true
  926. # only if the file exists the download was successful
  927. if [ -e "$2" ]; then
  928. return 0
  929. else
  930. return 1
  931. fi
  932. }
  933. checkdiff() {
  934. local DIFFTEXT="$(command diff -u "$@" | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
  935. if [ -n "$DIFFTEXT" ]; then
  936. echo >&2
  937. echo >&2 "$DIFFTEXT"
  938. return 1
  939. else
  940. return 0
  941. fi
  942. }
  943. testfileequal() {
  944. local FILE="$1"
  945. shift
  946. msgtest "Test for correctness of file" "$FILE"
  947. if [ -z "$*" ]; then
  948. echo -n "" | checkdiff $FILE - && msgpass || msgfail
  949. else
  950. echo "$*" | checkdiff $FILE - && msgpass || msgfail
  951. fi
  952. }
  953. testempty() {
  954. msgtest "Test for no output of" "$*"
  955. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
  956. if $* >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
  957. msgpass
  958. else
  959. cat $COMPAREFILE
  960. msgfail
  961. fi
  962. }
  963. testequal() {
  964. local MSG='Test of equality of'
  965. if [ "$1" = '--nomsg' ]; then
  966. MSG=''
  967. shift
  968. fi
  969. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
  970. echo "$1" > $COMPAREFILE
  971. shift
  972. if [ -n "$MSG" ]; then
  973. msgtest "$MSG" "$*"
  974. fi
  975. $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  976. }
  977. testequalor2() {
  978. local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
  979. local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
  980. local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
  981. echo "$1" > $COMPAREFILE1
  982. echo "$2" > $COMPAREFILE2
  983. shift 2
  984. msgtest "Test for equality OR of" "$*"
  985. $* >$COMPAREAGAINST 2>&1 || true
  986. if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
  987. checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
  988. then
  989. msgpass
  990. else
  991. echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
  992. checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
  993. echo -n "${CINFO}Diff against OR 2${CNORMAL}"
  994. checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
  995. msgfail
  996. fi
  997. }
  998. testshowvirtual() {
  999. local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
  1000. local PACKAGE="$1"
  1001. shift
  1002. while [ -n "$1" ]; do
  1003. VIRTUAL="${VIRTUAL}
  1004. N: Can't select versions from package '$1' as it is purely virtual"
  1005. PACKAGE="${PACKAGE} $1"
  1006. shift
  1007. done
  1008. msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
  1009. VIRTUAL="${VIRTUAL}
  1010. N: No packages found"
  1011. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
  1012. local ARCH="$(getarchitecture 'native')"
  1013. echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
  1014. aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1015. }
  1016. testnopackage() {
  1017. msgtest "Test for non-existent packages" "apt-cache show $*"
  1018. local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
  1019. if [ -n "$SHOWPKG" ]; then
  1020. echo >&2
  1021. echo >&2 "$SHOWPKG"
  1022. msgfail
  1023. else
  1024. msgpass
  1025. fi
  1026. }
  1027. testdpkginstalled() {
  1028. msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
  1029. local PKGS="$(dpkg -l "$@" 2>/dev/null | grep '^i' | wc -l)"
  1030. if [ "$PKGS" != $# ]; then
  1031. echo >&2 $PKGS
  1032. dpkg -l "$@" | grep '^[a-z]' >&2
  1033. msgfail
  1034. else
  1035. msgpass
  1036. fi
  1037. }
  1038. testdpkgnotinstalled() {
  1039. msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
  1040. local PKGS="$(dpkg -l "$@" 2> /dev/null | grep '^i' | wc -l)"
  1041. if [ "$PKGS" != 0 ]; then
  1042. echo
  1043. dpkg -l "$@" | grep '^[a-z]' >&2
  1044. msgfail
  1045. else
  1046. msgpass
  1047. fi
  1048. }
  1049. testmarkedauto() {
  1050. local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
  1051. if [ -n "$1" ]; then
  1052. msgtest 'Test for correctly marked as auto-installed' "$*"
  1053. while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
  1054. else
  1055. msgtest 'Test for correctly marked as auto-installed' 'no package'
  1056. echo -n > $COMPAREFILE
  1057. fi
  1058. aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
  1059. }
  1060. testsuccess() {
  1061. if [ "$1" = '--nomsg' ]; then
  1062. shift
  1063. else
  1064. msgtest 'Test for successful execution of' "$*"
  1065. fi
  1066. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
  1067. if $@ >${OUTPUT} 2>&1; then
  1068. msgpass
  1069. else
  1070. echo >&2
  1071. cat >&2 $OUTPUT
  1072. msgfail
  1073. fi
  1074. }
  1075. testfailure() {
  1076. if [ "$1" = '--nomsg' ]; then
  1077. shift
  1078. else
  1079. msgtest 'Test for failure in execution of' "$*"
  1080. fi
  1081. local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
  1082. if $@ >${OUTPUT} 2>&1; then
  1083. echo >&2
  1084. cat >&2 $OUTPUT
  1085. msgfail
  1086. else
  1087. msgpass
  1088. fi
  1089. }
  1090. testwebserverlaststatuscode() {
  1091. local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
  1092. local STATUS='rootdir/tmp/webserverstatus-statusfile.log'
  1093. rm -f "$DOWNLOG" "$STATUS"
  1094. msgtest 'Test last status code from the webserver was' "$1"
  1095. downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
  1096. if [ "$(cat "$STATUS")" = "$1" ]; then
  1097. msgpass
  1098. else
  1099. echo >&2
  1100. if [ -n "$2" ]; then
  1101. shift
  1102. echo >&2 '#### Additionally provided output files contain:'
  1103. cat >&2 "$@"
  1104. fi
  1105. echo >&2 '#### Download log of the status code:'
  1106. cat >&2 "$DOWNLOG"
  1107. msgfail "Status was $(cat "$STATUS")"
  1108. fi
  1109. }
  1110. pause() {
  1111. echo "STOPPED execution. Press enter to continue"
  1112. local IGNORE
  1113. read IGNORE
  1114. }