Skip to content

Instantly share code, notes, and snippets.

@silviot
Created November 4, 2020 10:57
Show Gist options
  • Save silviot/ceaf42b2b55d0d6ba85265781ee2df71 to your computer and use it in GitHub Desktop.
Save silviot/ceaf42b2b55d0d6ba85265781ee2df71 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Script to migrate a zodb from python 2 to python 3
# Needs to be run inside a buildout directory
# It is meant to automate the procedure documented here:
# https://docs.plone.org/manage/upgrading/version_specific_migration/upgrade_zodb_to_python3.html
BUILDOUT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
[ ! -f "${BUILDOUT_DIR}/buildout.cfg" ] && {
echo "${BUILDOUT_DIR}/buildout.cfg" not found
echo This script needs to be run in a buildout directory
exit 1
}
# Setup a trap to cleanup our temporary files
VENV27=$(mktemp -d --suffix=-virtualenv-2.7)
PACK_SCRIPT_PY=$(mktemp --suffix=-pack.py)
trap 'rm -rf "${PACK_SCRIPT_PY}" "${VENV27}" "${PACK_SCRIPT_PY}" "${BUILDOUT_DIR}/pack-zodb.cfg"' EXIT INT HUP
echo Creating python 2 virtualenv
virtualenv -p python2.7 "${VENV27}"
"${VENV27}/bin/pip" install zc.buildout
echo Prepare python 2 interpreter to pack and verify the database
# Create a buildout configuration script to create a python 2 interpreter
# and the tools to verify and update the zodb
cat > "${BUILDOUT_DIR}/pack-zodb.cfg" << EOF
[buildout]
extends =
buildout.cfg
parts = zopepy-2
[zopepy-2]
recipe = zc.recipe.egg
eggs =
zodbverify
zodbupdate
\${buildout:eggs}
interpreter = zopepy2
scripts =
zopepy2
zodbverify
zodbupdate
EOF
"${VENV27}/bin/buildout" -c "${BUILDOUT_DIR}/pack-zodb.cfg" install zopepy-2
# Create a python script that will pack the database
cat > "${PACK_SCRIPT_PY}" << EOF
import time
import ZODB.FileStorage
import ZODB.serialize
import sys
import logging
logging.basicConfig(level=logging.DEBUG)
print("Packing file " + sys.argv[1])
storage=ZODB.FileStorage.FileStorage(sys.argv[1])
storage.pack(time.time(), ZODB.serialize.referencesf)
EOF
echo Packing database. Current status:
ls -l var/filestorage/Data.fs* -ltrh
"${BUILDOUT_DIR}/bin/zopepy2" "${PACK_SCRIPT_PY}" var/filestorage/Data.fs
echo Database packed:
ls -l var/filestorage/Data.fs* -ltrh
"${BUILDOUT_DIR}/bin/zodbverify" -f var/filestorage/Data.fs
echo "The database has been packed and you can see the result above. Continue with migration? [Y/n]"
read -r response
if ! echo "${response}" |grep -E "^(y|yes| |)$"; then
echo aborting
exit 0
fi
"${BUILDOUT_DIR}/bin/zodbupdate" --convert-py3 --file="${BUILDOUT_DIR}/var/filestorage/Data.fs"
echo Done. Now you can use python 3 on this Data.fs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment