From 123c2ae18d9690a52daec2d413f30b751d9c6aad Mon Sep 17 00:00:00 2001 From: Matthias Bolte Date: Sat, 12 Feb 2011 19:03:11 +0100 Subject: [PATCH] Provide libvirt Python bindings for Python 2.6 and 2.7 Instead of compiling for 2.6 and 2.7 just compile for 2.6 and rewrite the the imports and symbols. --- gather_libvirt.sh | 24 ++++++++++--- msys_setup.bat | 1 + rewritepython.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 rewritepython.c diff --git a/gather_libvirt.sh b/gather_libvirt.sh index 946673a..1de31bd 100644 --- a/gather_libvirt.sh +++ b/gather_libvirt.sh @@ -4,12 +4,14 @@ dst=/gather/libvirt bin=$dst/bin lib=$dst/lib include=$dst/include -python=$dst/python +python26=$dst/python26 +python27=$dst/python27 mkdir -p $bin mkdir -p $lib mkdir -p $include -mkdir -p $python +mkdir -p $python26 +mkdir -p $python27 # bin cp /bin/virsh.exe $bin @@ -38,9 +40,21 @@ cp /lib/libvirt-qemu.dll.a $lib # include cp -R /include/libvirt $include -# python -cp /python/Lib/site-packages/libvirt.py $python -cp /python/Lib/site-packages/libvirtmod.dll $python/libvirtmod.pyd +# python26 +cp /python/Lib/site-packages/libvirt.py $python26 +cp /python/Lib/site-packages/libvirtmod.dll $python26/libvirtmod.pyd + +# python27 +cp /python/Lib/site-packages/libvirt.py $python27 +cp /python/Lib/site-packages/libvirtmod.dll $python27/libvirtmod.pyd + +pushd $python27 + +gcc /src/rewritepython.c -o rewritepython.exe +rewritepython $python27/libvirtmod.pyd +rm rewritepython.exe + +popd # rewrite imports pushd $bin diff --git a/msys_setup.bat b/msys_setup.bat index dc02506..157dc9c 100644 --- a/msys_setup.bat +++ b/msys_setup.bat @@ -564,6 +564,7 @@ call %tmp%\install_patch.bat libvirt-git-snapshot-mingw.patch call %tmp%\install_file.bat %base_dir%\gather_libvirt.sh %msys_dir%\bin\gather_libvirt.sh call %tmp%\install_file.bat %base_dir%\rewriteimports.c %msys_dir%\src\rewriteimports.c +call %tmp%\install_file.bat %base_dir%\rewritepython.c %msys_dir%\src\rewritepython.c call %tmp%\install_file.bat %base_dir%\download_libvirt-fedora.sh %msys_dir%\bin\download_libvirt-fedora.sh diff --git a/rewritepython.c b/rewritepython.c new file mode 100644 index 0000000..fadec93 --- /dev/null +++ b/rewritepython.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include + +struct rewrite { + const char *from; + const char *to; + size_t length; +}; + +struct rewrite rewrites[] = { + { "python26", "python27", 8 }, + { "_26_python_", "_27_python_", 11 } +}; + +int +main(int argc, char **argv) +{ + char *path; + struct stat st; + char *buffer; + FILE *fp; + int i, k; + int rewrites_count = sizeof (rewrites) / sizeof (struct rewrite); + + if (argc != 2) { + printf("usage: %s ", argv[0]); + return 1; + } + + path = argv[1]; + + if (stat(path, &st) < 0) { + printf("error: could not stat '%s'\n", path); + return 1; + } + + buffer = malloc(st.st_size + 1024); + fp = fopen(path, "rb"); + + if (fp == NULL) { + printf("error: could not open '%s' for reading\n", path); + return 1; + } + + if (fread(buffer, 1, st.st_size, fp) != st.st_size) { + fclose(fp); + free(buffer); + printf("error: could not read from '%s'\n", path); + return 1; + } + + fclose(fp); + + for (i = 0; i < st.st_size - 100; ++i) { + for (k = 0; k < rewrites_count; ++k) { + if (memcmp(buffer + i, rewrites[k].from, rewrites[k].length) == 0) { + printf("rewriting '%s' at 0x%08x\n", rewrites[k].from, i); + + memcpy(buffer + i, rewrites[k].to, rewrites[k].length); + i += rewrites[k].length; + } + } + } + + fp = fopen(path, "wb"); + + if (fp == NULL) { + free(buffer); + printf("error: could not open '%s' for writing\n", path); + return 1; + } + + if (fwrite(buffer, 1, st.st_size, fp) != st.st_size) { + fclose(fp); + free(buffer); + printf("error: could not write to '%s'\n", path); + return 1; + } + + fclose(fp); + free(buffer); + + return 0; +}