From 3f17e033bd451d70a6703c99b728df018fc1d9e6 Mon Sep 17 00:00:00 2001 From: lmancini Date: Wed, 31 Dec 2008 13:45:09 +0000 Subject: [PATCH] Extract a helper function to spawn an external interpreter; useful for hooks that need to know informations about packages/modules without importing them from within the main running interpreter. git-svn-id: http://svn.pyinstaller.org/trunk@588 8dd32b29-ccff-0310-8a9a-9233e24343b1 --- hooks/hook-xml.py | 14 +++----------- hooks/hookutils.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 hooks/hookutils.py diff --git a/hooks/hook-xml.py b/hooks/hook-xml.py index 1c4e80a..95afa40 100644 --- a/hooks/hook-xml.py +++ b/hooks/hook-xml.py @@ -20,20 +20,12 @@ hiddenimports = ['xml.sax.xmlreader','xml.sax.expatreader'] def hook(mod): # This hook checks for the infamous _xmlcore hack # http://www.amk.ca/diary/2003/03/pythons__xmlplus_hack.html - import os, tempfile, sys, string, marshal - fnm = tempfile.mktemp() - exe = sys.executable + from hookutils import exec_statement + import string, marshal - # Using "echo on" as a workaround for a bug in NT4 shell - if os.name == "nt": - cmd = '"echo on && "%s" -c "import xml;print xml.__file__" > "%s""' % (exe, fnm) - else: - cmd = '"%s" -c "import xml;print xml.__file__" > "%s"' % (exe, fnm) - os.system(cmd) + txt = exec_statement("import xml;print xml.__file__") - txt = open(fnm, 'r').read()[:-1] - os.remove(fnm) if string.find(txt, '_xmlplus') > -1: if txt[:-3] == ".py": txt = txt + 'c' diff --git a/hooks/hookutils.py b/hooks/hookutils.py new file mode 100644 index 0000000..29cd8c3 --- /dev/null +++ b/hooks/hookutils.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +def exec_statement(stat): + """Executes a Python statement in an externally spawned interpreter, and + returns anything that was emitted in the standard output as a single string. + """ + + import os, tempfile, sys + + fnm = tempfile.mktemp() + exe = sys.executable + + # Using "echo on" as a workaround for a bug in NT4 shell + if os.name == "nt": + cmd = '"echo on && "%s" -c "%s" > "%s""' % (exe, stat, fnm) + else: + cmd = '"%s" -c "%s" > "%s"' % (exe, stat, fnm) + os.system(cmd) + + txt = open(fnm, 'r').read()[:-1] + os.remove(fnm) + return txt