build_virtualenv 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/bin/bash
  2. #
  3. # runs dh_virtualenv to build the virtualenv in the build directory,
  4. # and then runs the trial tests against the installed synapse.
  5. set -e
  6. export DH_VIRTUALENV_INSTALL_ROOT=/opt/venvs
  7. # make sure that the virtualenv links to the specific version of python, by
  8. # dereferencing the python3 symlink.
  9. #
  10. # Otherwise, if somebody tries to install (say) the stretch package on buster,
  11. # they will get a confusing error about "No module named 'synapse'", because
  12. # python won't look in the right directory. At least this way, the error will
  13. # be a *bit* more obvious.
  14. #
  15. SNAKE=`readlink -e /usr/bin/python3`
  16. # try to set the CFLAGS so any compiled C extensions are compiled with the most
  17. # generic as possible x64 instructions, so that compiling it on a new Intel chip
  18. # doesn't enable features not available on older ones or AMD.
  19. #
  20. # TODO: add similar things for non-amd64, or figure out a more generic way to
  21. # do this.
  22. case `dpkg-architecture -q DEB_HOST_ARCH` in
  23. amd64)
  24. export CFLAGS=-march=x86-64
  25. ;;
  26. esac
  27. # Use --builtin-venv to use the better `venv` module from CPython 3.4+ rather
  28. # than the 2/3 compatible `virtualenv`.
  29. dh_virtualenv \
  30. --install-suffix "matrix-synapse" \
  31. --builtin-venv \
  32. --python "$SNAKE" \
  33. --upgrade-pip \
  34. --preinstall="lxml" \
  35. --preinstall="mock" \
  36. --extra-pip-arg="--no-cache-dir" \
  37. --extra-pip-arg="--compile" \
  38. --extras="all,systemd,test"
  39. PACKAGE_BUILD_DIR="debian/matrix-synapse-py3"
  40. VIRTUALENV_DIR="${PACKAGE_BUILD_DIR}${DH_VIRTUALENV_INSTALL_ROOT}/matrix-synapse"
  41. TARGET_PYTHON="${VIRTUALENV_DIR}/bin/python"
  42. case "$DEB_BUILD_OPTIONS" in
  43. *nocheck*)
  44. # Skip running tests if "nocheck" present in $DEB_BUILD_OPTIONS
  45. ;;
  46. *)
  47. # Copy tests to a temporary directory so that we can put them on the
  48. # PYTHONPATH without putting the uninstalled synapse on the pythonpath.
  49. tmpdir=`mktemp -d`
  50. trap "rm -r $tmpdir" EXIT
  51. cp -r tests "$tmpdir"
  52. PYTHONPATH="$tmpdir" \
  53. "${TARGET_PYTHON}" -m twisted.trial --reporter=text -j2 tests
  54. ;;
  55. esac
  56. # build the config file
  57. "${TARGET_PYTHON}" "${VIRTUALENV_DIR}/bin/generate_config" \
  58. --config-dir="/etc/matrix-synapse" \
  59. --data-dir="/var/lib/matrix-synapse" |
  60. perl -pe '
  61. # tweak the paths to the tls certs and signing keys
  62. /^tls_.*_path:/ and s/SERVERNAME/homeserver/;
  63. /^signing_key_path:/ and s/SERVERNAME/homeserver/;
  64. # tweak the pid file location
  65. /^pid_file:/ and s#:.*#: "/var/run/matrix-synapse.pid"#;
  66. # tweak the path to the log config
  67. /^log_config:/ and s/SERVERNAME\.log\.config/log.yaml/;
  68. # tweak the path to the media store
  69. /^media_store_path:/ and s#/media_store#/media#;
  70. # remove the server_name setting, which is set in a separate file
  71. /^server_name:/ and $_ = "#\n# This is set in /etc/matrix-synapse/conf.d/server_name.yaml for Debian installations.\n# $_";
  72. # remove the report_stats setting, which is set in a separate file
  73. /^# report_stats:/ and $_ = "";
  74. ' > "${PACKAGE_BUILD_DIR}/etc/matrix-synapse/homeserver.yaml"
  75. # build the log config file
  76. "${TARGET_PYTHON}" "${VIRTUALENV_DIR}/bin/generate_log_config" \
  77. --output-file="${PACKAGE_BUILD_DIR}/etc/matrix-synapse/log.yaml"
  78. # add a dependency on the right version of python to substvars.
  79. PYPKG=`basename $SNAKE`
  80. echo "synapse:pydepends=$PYPKG" >> debian/matrix-synapse-py3.substvars
  81. # add a couple of triggers. This is needed so that dh-virtualenv can rebuild
  82. # the venv when the system python changes (see
  83. # https://dh-virtualenv.readthedocs.io/en/latest/tutorial.html#step-2-set-up-packaging-for-your-project)
  84. #
  85. # we do it here rather than the more conventional way of just adding it to
  86. # debian/matrix-synapse-py3.triggers, because we need to add a trigger on the
  87. # right version of python.
  88. cat >>"debian/.debhelper/generated/matrix-synapse-py3/triggers" <<EOF
  89. # triggers for dh-virtualenv
  90. interest-noawait $SNAKE
  91. interest dh-virtualenv-interpreter-update
  92. EOF