From: W. Trevor King Date: Thu, 23 Aug 2012 17:35:58 +0000 (-0400) Subject: libbe:util:plugin: cleanup recent zip/egg handling code. X-Git-Tag: 1.1.0~136 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=831691bd9aa1ea4092f718fa5ce4da1ce96d06db;p=be.git libbe:util:plugin: cleanup recent zip/egg handling code. To test this, you'll need to build an egg. You can use setuptools to do this, by applying: --- diff --git a/libbe/util/plugin.py b/libbe/util/plugin.py index 63715a0..ba563eb 100644 --- a/libbe/util/plugin.py +++ b/libbe/util/plugin.py @@ -53,14 +53,13 @@ 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 zip_listdir(path, components): + """Lists items in a directory contained in a zip file + """ + dirpath = '/'.join(components) + with zipfile.ZipFile(path, 'r') as f: + return [os.path.relpath(f, dirpath) for f in f.namelist() + if f.startswith(dirpath)] def modnames(prefix): """ @@ -70,31 +69,15 @@ def modnames(prefix): True """ components = prefix.split('.') - modfilespath=os.path.join(_PLUGIN_PATH, *components) - # Cope if we are executing from inside a zip archive full of precompiled .pyc's - inside_zip='.zip' in modfilespath - modfiles=ziplistdir(modfilespath) if inside_zip else os.listdir(modfilespath) - modfiles.sort() - # Eliminate .py/.pyc duplicates, preferring .pyc if we're in a zip archive - if inside_zip: - x=1 - while x