.gitlab-ci.yml: Use source distribution tarballs in all tests

This commit adds an initial stage to the pipeline to build a distribution
tarball and generate some helper scripts for later CI to use to unpack it
and install it.

Then, it makes sure that all pytest and integration test runs work from
the dist tarball instead of directly from the git repo, also the docs are
built from the dist tarball.

This ensures that everything continues to work with a dist tarball at all times.
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5c9f233..8a0862a 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -5,6 +5,7 @@
     - cache/buildstream/sources/
 
 stages:
+  - dist
   - test
   - coverage
   - docs
@@ -20,19 +21,64 @@
   - adduser -m buildstream
   - chown -R buildstream:buildstream .
 
-# Run premerge commits
+# Create a source distribution
 #
-pytest:
-  stage: test
+distcheck:
+  stage: dist
   script:
-  # We run as a simple user to test for permission issues
-  - su buildstream -c 'python3 setup.py test --index-url invalid://uri'
 
-  - mkdir -p coverage-pytest/
-  - cp .coverage.* coverage-pytest/coverage.pytest
+  # Generate the source distribution tarball
+  #
+  - python3 setup.py sdist
+  - tar -ztf dist/*
+  - tarball=$(cd dist && echo $(ls *))
+
+  # Create an installer script
+  - |
+    cat > dist/install.sh << EOF
+    #!/bin/sh
+    tar -zxf ${tarball}
+    cd ${tarball%.tar.gz}
+    pip3 install --no-index .
+    EOF
+
+  # unpack tarball as `dist/buildstream` directory
+  - |
+    cat > dist/unpack.sh << EOF
+    #!/bin/sh
+    tar -zxf ${tarball}
+    mv ${tarball%.tar.gz} buildstream
+    EOF
+
+  # Make our helpers executable
+  - chmod +x dist/install.sh
+  - chmod +x dist/unpack.sh
   artifacts:
     paths:
-    - coverage-pytest/
+    - dist/
+
+# Run premerge commits
+#
+pytest_linux:
+  stage: test
+  script:
+
+  # Unpack and get into dist/buildstream
+  - cd dist && ./unpack.sh
+  - chown -R buildstream:buildstream buildstream
+  - cd buildstream
+
+  # Run the tests from the source distribution, We run as a simple
+  # user to test for permission issues
+  - su buildstream -c 'python3 setup.py test --index-url invalid://uri'
+
+  # Go back to the toplevel and collect our reports
+  - cd ../..
+  - mkdir -p coverage-pytest-linux/
+  - cp dist/buildstream/.coverage.* coverage-pytest-linux/coverage.pytest-linux
+  artifacts:
+    paths:
+    - coverage-pytest-linux/
 
 # Run integration tests
 #
@@ -40,7 +86,7 @@
   stage: test
 
   script:
-    - pip3 install --no-index .
+    - cd dist && ./install.sh && cd ..
     - cd integration-tests
 
     # We run as a simple user to test for permission issues
@@ -56,6 +102,9 @@
     - coverage-linux/
     - logs-linux/
 
+  dependencies:
+  - distcheck
+
 pytest_unix:
   stage: test
   variables:
@@ -67,11 +116,16 @@
     - dnf mark install fuse-libs
     - dnf erase -y bubblewrap ostree
 
+    # Unpack and get into dist/buildstream
+    - cd dist && ./unpack.sh && cd buildstream
+
     # Since the unix platform is required to run as root, no user change required
     - python3 setup.py test --index-url invalid://uri
 
+    # Go back to the toplevel and collect our reports
+    - cd ../..
     - mkdir -p coverage-pytest-unix
-    - cp .coverage.* coverage-pytest-unix/coverage.pytest-unix
+    - cp dist/buildstream/.coverage.* coverage-pytest-unix/coverage.pytest-unix
   artifacts:
     paths:
       - coverage-pytest-unix/
@@ -81,7 +135,7 @@
   variables:
     BST_FORCE_BACKEND: "unix"
   script:
-    - pip3 install --no-index .
+    - cd dist && ./install.sh && cd ..
     - cd integration-tests
 
     # Since the unix platform is required to run as root, no user change required
@@ -97,6 +151,9 @@
     - coverage-unix/
     - logs-unix/
 
+  dependencies:
+  - distcheck
+
 # Collate coverage reports
 #
 coverage:
@@ -108,13 +165,13 @@
     - cp ../coverage-linux/coverage.linux .coverage
     - cp ../coverage-unix/coverage.unix .
     - coverage combine --rcfile=../.coveragerc -a ../coverage-unix/coverage.unix
-    - cp ../coverage-pytest/coverage.pytest .
-    - coverage combine --rcfile=../.coveragerc -a coverage.pytest
+    - cp ../coverage-pytest-linux/coverage.pytest-linux .
+    - coverage combine --rcfile=../.coveragerc -a coverage.pytest-linux
     - cp ../coverage-pytest-unix/coverage.pytest-unix .
     - coverage combine --rcfile=../.coveragerc -a coverage.pytest-unix
     - coverage report --rcfile=../.coveragerc -m
   dependencies:
-  - pytest
+  - pytest_linux
   - integration_linux
   - pytest_unix
   - integration_unix
@@ -131,11 +188,18 @@
   - dnf install -y python2
   - pip3 install sphinx
   - pip3 install sphinx-click
-  - pip3 install --user .
+  - cd dist && ./unpack.sh && cd buildstream
+  - pip3 install .
   - make -C doc
-  - mv doc/build/html public
+  - cd ../..
+  - mv dist/buildstream/doc/build/html public
   artifacts:
     paths:
     - public/
-  only:
-  - master
+
+  # XXX Testing
+  # only:
+  # - master
+
+  dependencies:
+  - distcheck