Skip to content

Commit

Permalink
Provide libvirt Python bindings for Python 2.6 and 2.7
Browse files Browse the repository at this point in the history
Instead of compiling for 2.6 and 2.7 just compile for 2.6 and rewrite
the the imports and symbols.
  • Loading branch information
photron committed Feb 12, 2011
1 parent e167074 commit 123c2ae
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
24 changes: 19 additions & 5 deletions gather_libvirt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions msys_setup.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
87 changes: 87 additions & 0 deletions rewritepython.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <malloc.h>
#include <string.h>

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 <libvirtmod.pyd>", 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;
}

0 comments on commit 123c2ae

Please sign in to comment.