From e55730c6b0737e3edb4ff407c12b34b5390ef4a1 Mon Sep 17 00:00:00 2001 From: "Niall Douglas (a [underscors] sourceforge {at} nedprod [dot] com)" Date: Fri, 24 Feb 2012 14:35:04 -0500 Subject: [PATCH] Cherrypick initial zipfile support from Niall Douglas. WTK: This is a portion of Niall's commit 7f7a7738bcbcfd06a026f2985c1823a4ba5eb55b Author: Niall Douglas ... Date: Tue Feb 21 20:35:28 2012 +0000 Several hacks to make BE compatible with bbfreeze and therefore compilable into a self contained directory --- libbe/util/plugin.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/libbe/util/plugin.py b/libbe/util/plugin.py index 211e608..1fee690 100644 --- a/libbe/util/plugin.py +++ b/libbe/util/plugin.py @@ -27,6 +27,7 @@ submodules (i.e. "plugins"). import os import os.path import sys +import zipfile _PLUGIN_PATH = os.path.realpath( @@ -52,6 +53,15 @@ def import_by_name(modname): module = getattr(module, comp) return module +def ziplistdir(path): + """Lists items in a directory contained in a zip file""" + zipidx=path.find('.zip') + zippath=path[:4+zipidx] + path=path[5+zipidx:].replace(os.sep, '/') + with zipfile.ZipFile(zippath, 'r') as zf: + files=[f[len(path)+1:] for f in zf.namelist() if f[:len(path)]==path and '/' not in f[len(path)+1:]] + return files + def modnames(prefix): """ >>> 'list' in [n for n in modnames('libbe.command')] @@ -60,10 +70,25 @@ def modnames(prefix): True """ components = prefix.split('.') - modfiles = os.listdir(os.path.join(_PLUGIN_PATH, *components)) + modfilespath=os.path.join(_PLUGIN_PATH, *components) + # Cope if we are executing from inside a zip archive full of precompiled .pyc's + modfiles=ziplistdir(modfilespath) if '.zip' in modfilespath else os.listdir(modfilespath) modfiles.sort() + # Eliminate .py/.pyc duplicates + print modfiles + x=1 + while x