Skip to content

Commit

Permalink
Calculate version using setuptools scm (#534)
Browse files Browse the repository at this point in the history
* Migrate setup.py -> pyproject.toml

* Update mk-venv to use pyproject.toml

* Migrate setup.py -> pyproject.py

* Minor: Added missing fields to metadata

* Migrate somekeys from pyproject.toml to setup.cfg

This was done because on older versions of pip the previous setup
does not work (even though it does not report any error). The
previous version seems to work on `pip 23.1.2` but it does not
install correctly at all (mainly: missing bins and missing package name)

Also: sign was imported from main

* Updated metabox config checking for source

* Add isolated_build to tox.ini

* Fixed install in tox.ini and updated run command

* Removed unecessary editable install

* Fixed install and run scripts

* Avoid scm versioning in tox runs

* Move scm bodge to the correct python version

* Port dependencies to setup.cfg as well

* Bodge: Install as dep requests-unixsocked in py3.6

* Port bodge to 3.5

* Use checkbox_ng.__version__ without hacking path

* Fix relative install requirements.txt

* Avoid dumping the full version number in the doc header

* Remove outdated references to setup.py

* Support multidigits tags

* Re-include setup.cfg options field lost in rebase

* Updated authors

* Added pybuild plugin pyproject to build-req

* Fix snap builds scm (#578)

* Duplicated dependencies in pyproject.toml

* Avoid copying by symlink in prepare.sh

* Revert "Avoid copying by symlink in prepare.sh"

This reverts commit 4812099.

* Dump version in text file within series dir

* Added bodges for setup.py and pretend version

This was done to be backward compatible with any version that does
not support pyproject.toml. Also, when building a snap via snapcraft
we have no .git, so we need to dump the version before and load it
via the file "pretending it"

* Missing pipe concats commands breaking build

* Fix version path

* Fix setup scripts from setup.cfg -> pyproject.toml

* Escape scripts name

* Recovered all entrypoints not just scripts

* Removed dynamic from startpoints

* Quote names with spaces

* Ported fix to series20

* Fixed definition of stress-ng and updated var export

Amend: recovered tag, git init

* Better setup_scm bodge

* Backported to core16

* Ported to core18

* Fixed moved lib in series22

* Bodge 18 to support pyproject

* Make dependencies local to jobs

* Full build for series16 snap

* Small guide on how to build checkbox-core-snap

* Adopt version from version file

* Fixed scm version of libraries

* Update grep to support semantic versioning

* Removed version calculation from github job

* Dump version using setuptools_scm

* adopt-info for checkbox-snap and checkbox-classic-snap

* Minor: Info about operation being done in prepare.sh

* Xenial support

* Setuptools scm deb daily (#588)

* Remove bumpversion configs and workflows

* Pass the version generated by setuptools_scm to deb daily build recipes

* Fixed line len to max 80

* Explained how to use the lxd backend and reordering

* Updated requirements

* Ensure that setup.py has the SETUPTOOLS_PRETEND_VERSION envvar

* Ported envvar fix to 20

* Updated README.md as per feedback

* Document versioning via setuptools_scm in prepare.sh

* Minor: call launcher checkbox snap frontend and not user

* Removed hack in requirements.txt for readthedocs.yaml

Minor: Fixed typo in README.md

---------

Co-authored-by: Sylvain Pineau <sylvain.pineau@canonical.com>
  • Loading branch information
Hook25 and yphus committed Jun 30, 2023
1 parent bcfeae5 commit 1c4748b
Show file tree
Hide file tree
Showing 47 changed files with 668 additions and 499 deletions.
43 changes: 0 additions & 43 deletions .bumpversion.cfg

This file was deleted.

30 changes: 0 additions & 30 deletions .github/workflows/bumpversion.yml

This file was deleted.

8 changes: 1 addition & 7 deletions .github/workflows/checkbox-core-snap-beta-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@ jobs:
- name: Copy over the common files for series${{ matrix.releases }}
run: |
cd checkbox-core-snap/
apt install python3-setuptools-scm
./prepare.sh $SERIES
- name: Remove '.dev' suffix from the version name
run: |
cd checkbox-core-snap
# in the following sed onliner the '0,/^version/' bit specifies the range
# of lines it should act upon, this way only the first occurence of '^version'
# is getting affected.
sed -i '0,/^version/s/\(^version\:.*\).dev/\1/' $SERIES/snap/snapcraft.yaml
- name: add LP credentials
run: |
mkdir -p ~/.local/share/snapcraft/provider/launchpad/
Expand Down
9 changes: 1 addition & 8 deletions .github/workflows/checkbox-core-snap-daily-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@ jobs:
- name: Copy over the common files for series ${{ matrix.releases }}
run: |
cd checkbox-core-snap/
apt install python3-setuptools-scm
./prepare.sh $SERIES
- name: Add git short sha suffix to the version name
run: |
cd checkbox-core-snap
# in the following sed onliner the '0,/^version/' bit specifies the range
# of lines it should act upon, this way only the first occurence of '^version'
# is getting affected.
sed -i '0,/^version/s/\(^version\:.*\).dev/\1/' $SERIES/snap/snapcraft.yaml
sed -i "0,/^version/s/\(^version\:.*\)'/\1+git$(git rev-parse --short HEAD)'/" $SERIES/snap/snapcraft.yaml
- name: add LP credentials
run: |
mkdir -p ~/.local/share/snapcraft/provider/launchpad/
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deb-daily-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt update -qq
sudo apt install -qq -y python3-launchpadlib
sudo apt install -qq -y python3-launchpadlib python3-setuptools-scm
git clone -b main https://git.launchpad.net/~hook25/ppa-dev-tools /tmp/ppa-dev-tools
- name: Checkout checkbox monorepo
uses: actions/checkout@v3
Expand Down
6 changes: 6 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ build:
os: ubuntu-22.04
tools:
python: "3.11"
jobs:
post_checkout:
- git fetch --unshallow || true
post_create_environment:
- pip install --upgrade pip
- pip install checkbox-ng/

# Build documentation in the docs/ directory with Sphinx
sphinx:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Install the Checkbox support library in the virtual environment:

``` bash
(venv) $ cd ~/checkbox/checkbox-support
(venv) $ ./setup.py install
(venv) $ python3 -m pip install -e .
```

You should now be able to run checkbox, select a test plan and run it:
Expand Down
147 changes: 147 additions & 0 deletions checkbox-core-snap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Building checkbox-core-snap
This snap is the core reusable part of checkbox, it includes all the utilities
and some core providers. The snap can be built for multiple
[base snaps](https://snapcraft.io/docs/base-snaps). Each recipe is in a `seriesXX`
directory where `XX` is the base snap. For instance `series22` contains
the `core22 or jammy` snap.

## Building guide
This section covers how to build the snap via either the `multipass` or the
`lxd` backend. This guide will focus on the latter
but with small adjustments can be used for the former. Also, this guide will
build the `series22` snap, adjust the commands to build any other.

In order to build the snap you are going to need the following:
1. **snapcraft:** Installed via `snap install snapcraft --classic` or
`snap install snapcraft --classic --channel=4.x` for series16
2. **python3:** Installed via `apt` or any other packaging solution
3. **setuptools_scm:** Installed via pip
4. **rsync:** Installed via apt or any other packaging solution

After installing all the dependencies do the following:
```
> git clone https://github.com/canonical/checkbox
> cd checkbox/checkbox-core-snap/
> ./prepare.sh series22
> cd series22
```

> Note: `prepare.sh` will prepare the series creating the build environment,
> refer to its output for further detail
Finally build the snap with:
```
> snapcraft --use-lxd
```

If the build has failed, check either the **Building guide for debugging**, if
it completed succesfully, refer to **Testing the build**.

## Testing the build
To test the build one must install it and see if the content is correct.
One way to do it is the following:

```bash
> snap install checkbox22_(version)_(arch).snap --dangerous
```
Now we have installed the core snap, but we cannnot use it directly. To use it
we need a frontend snap.
```bash
# Note: install the correct channel for your series
> snap install checkbox --classic --channel=22.04
```
To test the checkbox snap, you can try to run test plans as follows.
For example, you may want to run the smoke test plan.
```
> checkbox.checkbox_cli
```

## Building for debugging
To build the checkbox core snap for debugging you might find
`snapcraft --destructive-mode` executed in a single-use container a handy trick
(see [Faster snap development – additional tips and tricks](https://snapcraft.io/blog/faster-snap-development-additional-tips-and-tricks)
for more info).
Below you will find build steps for the series22 snap as an example
(changes needed for earlier series pointed out where applicable).

The destructive mode is useful for debugging a build because of incremental
build time: with direct host system one can quickly iterate on the environment
installing packages, changing versions or editing the recipe, without restarting
every build from scratch afterwards.

### Container configuration
Let's begin by creating the container and installing the needed packages.
```bash
# First, create the container
# Note: Always use the version of ubuntu for the snap you
# are building, series22 -> ubuntu22.04 (jammy)
(host) > lxd launch ubuntu:22.04 jammy
(host) > lxd exec jammy bash
# Once in the container install snapcraft.
# Note: For series16 you will need snapcraft4.x, to install
# it use run:
# snap install snapcraft --classic --channel=4.x
(jammy)> snap install snapcraft --classic
# Now clone the checkbox repository
(jammy)> git clone https://github.com/canonical/checkbox
(jammy)> apt install python3-setuptools-scm git snapd
```
If you are debugging a build, this is a good step to make a backup of the
environment, so that you don't need to repeat the above steps if something
goes wrong.
```bash
(jammy)> exit
(host) > lxd snapshot jammy backup
```
If at any point you need to rollback to this backup run
```bash
(host) > lxd restore jammy backup
(host) > lxd exec jammy bash
# This you may want to do if you made any updates to the repo
# in the meantime
(jammy)> (cd checkbox && git pull)
```
### Running the build
Now let's launch the actual build
```bash
(jammy)> cd checkbox/checkbox-core-snap
# For another series, change the parameter accordingly
(jammy)> ./prepare.sh series22
(jammy)> snapcraft --destructive-mode
```
The previous will either create a `checkbox22_(version)_(arch).snap` file or
yield an error.

If the build fails, refer to the **Recovering from a failure** chapter for some
tips!

If the build completes, refer to the **Testing the build**, remember to follow
that guide from within the container you have created in this chapter!

### Recovering from a failure

Recovering from a failure is tricky, which is why you should create a
snapshot. Most of the time you will not need it, but sometimes it will be
necessary. Depending on what failed you may want to follow these strategies
re-run the build:

**Just re-run it:** Snapcraft will try to use cached steps for what you have
done and re-do things that you have changed in the recipe.

**Clean parts (partial):** You can try to remove a `part` cache. To do so run
`rm -rf parts/(part_name)`. This will many problems like the part being dirty
and snapcraft being unable to overwrite what is there.

**Clean parts (all)**: Sometimes a parts dir in unrecoverably dirty and you
may be unable to clean specifically what is broken, a symptom of this is
snapcraft skipping a part thinking that it is up-to-date when it was actually
changed. To do this `rm -rf parts`.

**Clean all:** Sometimes cleaning `parts` is not enough, you can run
`git clean -xfd`, resetting the repo to a clean state. If you do this remember
you need to run `prepare.sh` again.

**Reset to the snapshot:** This is also necessary sometimes, you can find the
command above!


9 changes: 9 additions & 0 deletions checkbox-core-snap/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ echo "Copying over checkbox-ng to $series"
rsync -r --links ../checkbox-ng $series/
echo "Copying over checkbox-support to $series"
rsync -r --links ../checkbox-support $series
echo "Dumping version in version file for $series"
# setuptools_scm fetches the version calculating it from the latest tag.
# We use the default setuptools_scm schema refert to:
# https://pypi.org/project/setuptools-scm/ for more infos. Generally:
# no distance and clean: {tag}
# distance and clean: {next_version}.dev{distance}+{scm letter}{revision hash}
# no distance and not clean: {tag}+dYYYYMMDD
# distance and not clean: {next_version}.dev{distance}+{scm letter}{revision hash}.dYYYYMMDD
(cd .. && python3 -m setuptools_scm | grep -oP "\S+$") 2>/dev/null 1>$series/version.txt
42 changes: 37 additions & 5 deletions checkbox-core-snap/series16/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: "Checkbox runtime and public providers"
grade: stable
confinement: strict

version: '2.8'
adopt-info: version-calculator

base: core

Expand Down Expand Up @@ -57,6 +57,14 @@ package-repositories:
ppa: hardware-certification/public

parts:
version-calculator:
plugin: dump
source: .
override-pull: |
# version.txt created by prepare.sh. Refer to it for more info
export version=`cat $SNAPCRAFT_PROJECT_DIR/version.txt`
[ $version ] || exit 1
snapcraftctl set-version $version
################################################################################
# Upstream: https://kernel.ubuntu.com/git/hwe/fwts.git/plain/snapcraft.yaml
fwts:
Expand All @@ -81,6 +89,7 @@ parts:
- libglib2.0-dev
- libfdt-dev
- libbsd-dev
after: [version-calculator]
################################################################################
# Upstream: https://kernel.ubuntu.com/git/cking/stress-ng.git/plain/snap/snapcraft.yaml
stress-ng:
Expand All @@ -90,6 +99,7 @@ parts:
source: https://github.com/ColinIanKing/stress-ng.git
make-parameters:
- STATIC=1
- LDFLAGS=-lrt
build-packages:
- gcc
- make
Expand Down Expand Up @@ -141,14 +151,25 @@ parts:
# when running lsb_release on ubuntu core
# The fix was finally released with the distro 1.6 release available as of
# 22.04 (i.e base: core22)
sed -i 's|except OSError: # Command not found|except subprocess.CalledProcessError: # Command not found|g' lib/python3.*/site-packages/distro.py
sed -i 's|except OSError: # Command not found|except subprocess.CalledProcessError: # Command not found|g' lib/python3.*/site-packages/**/**/distro.py
override-pull: |
PYTHONHOME=/usr PYTHONUSERBASE=$SNAPCRAFT_PART_INSTALL $SNAPCRAFT_PART_INSTALL/usr/bin/python3 -m pip install --upgrade 'pip; python_version >= "3.6"' 'pip<21; python_version < "3.6"' --user
snapcraftctl pull
build-packages:
- libbluetooth-dev
- python3-dev
- python3-pip
override-pull: |
PYTHONHOME=/usr PYTHONUSERBASE=$SNAPCRAFT_PART_INSTALL $SNAPCRAFT_PART_INSTALL/usr/bin/python3 -m pip install --upgrade 'pip; python_version >= "3.6"' 'pip<21; python_version < "3.6"' --user
snapcraftctl pull
override-build: |
export SETUPTOOLS_SCM_PRETEND_VERSION=`cat $SNAPCRAFT_PROJECT_DIR/version.txt`
# this is a bodge to be backward compatible
snapcraftctl build
# also use build to ensure install (pip is not compinat to pyproject)
# on this version
echo "from setuptools import setup; setup()" > setup.py
python3 -m pip install "build<0.4" "setuptools>=42,<51" setuptools>=42 setuptools_scm[toml]>=3.4 --force-reinstall
python3 -m build --no-isolation -x
# also, call pip again, this installs the module itself
python3 -m pip install .
################################################################################
checkbox-ng:
plugin: python
Expand All @@ -171,6 +192,17 @@ parts:
after: [checkbox-support]
build-environment:
- PYTHONPATH: $SNAPCRAFT_PART_INSTALL/usr/lib/python3/dist-packages:$PYTHONPATH
override-build: |
export SETUPTOOLS_SCM_PRETEND_VERSION=`cat $SNAPCRAFT_PROJECT_DIR/version.txt`
# this is a bodge to be backward compatible
snapcraftctl build
# also use build to ensure install (pip is not compinat to pyproject)
# on this version
echo "from setuptools import setup; setup()" > setup.py
python3 -m pip install "build<0.4" "setuptools>=42,<51" setuptools>=42 setuptools_scm[toml]>=3.4 --force-reinstall
python3 -m build --no-isolation -x
# also, call pip again, this installs the module itself
python3 -m pip install .
################################################################################
checkbox-provider-resource:
plugin: dump
Expand Down
1 change: 0 additions & 1 deletion checkbox-core-snap/series18/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ stage/
prime/

# following files are populated by the prepare.sh script and should be ignored
.bumpversion.cfg
config/

0 comments on commit 1c4748b

Please sign in to comment.