longintrepr.h

Did your pip install fail with longintrepr.h: No such file or directory? The file likely is on your system, but it sometime or another it was moved, from /usr/include/python3.xx/longintrepr.h to /usr/include/python3.xx/cpython/longintrepr.h. The proper fix is to update the package in question with the new path, but if you’re installing an old version of something or a package that’s no longer maintained you can work around it like this:

ln -s /usr/include/python3.*/cpython/longintrepr.h .venv/include

Python antipattern: Close in finally

Don’t do this:

thing = Thing()
try:
    thing.do_stuff()
finally:
    thing.close()

Do do this:

from contextlib import closing

with closing(Thing()) as thing:
    thing.do_stuff()

Why is the second better? Using contextlib.closing() ties closing the item to its creation. These baby examples are about equally easy to reason about, with only a single line in the try block, but consider what happens ifwhen more lines get added in future? In the first example, the close moves away, potentially offscreen, but that doesn’t happen in the second.

GitLab YAML Docker Registry client

Have you written a Docker Registry API client in GitLab CI/CD YAML? I have.

# Delete candidate image from CI repository.
clean-image:
  stage: .post
  except:
    - main

  variables:
    AUTH_API: "$CI_SERVER_URL/jwt/auth"
    SCOPE: "repository:$CI_PROJECT_PATH"
    REGISTRY_API: "https://$CI_REGISTRY/v2/$CI_PROJECT_PATH"

  before_script:
    - >
      which jq >/dev/null
      || (sudo apt-get update
      && sudo apt-get -y install jq)

  script:
    - echo "Deleting $CANDIDATE_IMAGE"
    - >
      TOKEN=$(curl -s
      -u "$CI_REGISTRY_USER:$CI_REGISTRY_PASSWORD"
      "$AUTH_API?service=container_registry&scope=$SCOPE:delete,pull"
      | jq -r .token)
    - >
      DIGEST=$(curl -s -I
      -H "Authorization: Bearer $TOKEN"
      -H "Accept: application/vnd.docker.distribution.manifest.v2+json"
      "$REGISTRY_API/manifests/$CI_COMMIT_SHORT_SHA"
      | tr -d "\r"
      | grep -i "^docker-content-digest: "
      | sed "s/^[^:]*: *//")
    - >
      curl -s
      -X DELETE
      -H "Authorization: Bearer $TOKEN"
      "$REGISTRY_API/manifests/"$(echo $DIGEST | sed "s/:/%3A/g")