Created
November 28, 2016 03:30
-
-
Save amosshapira/9619ea2ab32f2e50cdab57eda4c43e80 to your computer and use it in GitHub Desktop.
sample code for generating a zip file in memory using Python. Useful for AWS Lambda Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StringIO | |
import zipfile | |
import os | |
import os.path | |
def append_requests_module_code(zf): | |
requests_path = os.path.dirname(os.path.realpath(requests.__file__)) | |
for root, dirs, files in os.walk(requests_path): | |
ziproot = os.path.join('requests', root[len(requests_path)+1:]) | |
for fn in files: | |
if not fn.endswith('.pyc'): | |
zf.write(os.path.join(root, fn), os.path.join(ziproot, fn)) | |
def append_delta_function_code(zf): | |
zf.write('path/to/pythonfile.py', 'pythonfile.py') | |
def get_zip_file(): | |
zipfile_content = StringIO.StringIO() | |
zf = zipfile.ZipFile(zipfile_content, mode='w', | |
compression=zipfile.ZIP_DEFLATED) | |
append_requests_module_code(zf) | |
append_delta_function_code(zf) | |
zf.close() | |
return zipfile_content.getvalue() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment