[IMP] Add a new method: get_module_as_zip_from_module_directory(module_directory, b64enc, src)

This function compress a module directory as zip file

bzr revid: stephane@tinyerp.com-20090122152536-c9wb2fogcocn7sha
This commit is contained in:
Stephane Wirtel 2009-01-22 16:25:36 +01:00
parent 9ef4ac36fe
commit 3268209d8c
1 changed files with 34 additions and 10 deletions

View File

@ -185,8 +185,15 @@ def get_module_filetree(module, dir='.'):
return tree
def get_module_as_zip_from_module_directory(module_directory, b64enc=True, src=True):
"""Compress a module directory
def get_module_as_zip(modulename, b64enc=True, src=True):
@param module_directory: The module directory
@param base64enc: if True the function will encode the zip file with base64
@param src: Integrate the source files
@return: a stream to store in a file-like object
"""
RE_exclude = re.compile('(?:^\..+\.swp$)|(?:\.py[oc]$)|(?:\.bak$)|(?:\.~.~$)', re.I)
@ -198,6 +205,29 @@ def get_module_as_zip(modulename, b64enc=True, src=True):
if not RE_exclude.search(bf) and (src or bf == '__terp__.py' or not bf.endswith('.py')):
archive.write(os.path.join(path, f), os.path.join(base, f))
archname = StringIO()
archive = PyZipFile(archname, "w", ZIP_DEFLATED)
archive.writepy(module_directory)
_zippy(archive, module_directory, src=src)
archive.close()
val = archname.getvalue()
archname.close()
if b64enc:
val = base64.encodestring(val)
return val
def get_module_as_zip(modulename, b64enc=True, src=True):
"""Generate a module as zip file with the source or not and can do a base64 encoding
@param modulename: The module name
@param b64enc: if True the function will encode the zip file with base64
@param src: Integrate the source files
@return: a stream to store in a file-like object
"""
ap = get_module_path(str(modulename))
if not ap:
raise Exception('Unable to find path for module %s' % modulename)
@ -205,17 +235,11 @@ def get_module_as_zip(modulename, b64enc=True, src=True):
ap = ap.encode('utf8')
if os.path.isfile(ap + '.zip'):
val = file(ap + '.zip', 'rb').read()
if b64enc:
val = base64.encodestring(val)
else:
archname = StringIO()
archive = PyZipFile(archname, "w", ZIP_DEFLATED)
archive.writepy(ap)
_zippy(archive, ap, src=src)
archive.close()
val = archname.getvalue()
archname.close()
val = get_module_as_zip_from_module_directory(ap, b64enc, src)
if b64enc:
val = base64.encodestring(val)
return val