"""
Packages the files into a folder ready to upload to AWS.

This involves:

 (a) Copy all the files into the folder.
 (b) Run pip install -r requirements.txt -t /path/to/folder
 (c) Zip it all up
"""

from distutils import dir_util
import os
import pip
import re
import shutil
import sys
import tempfile
import zipfile

def package(source_folder, requirements_file, target):
    """
    Creates a package ready to upload to AWS Lambda.

    @param source_folder
        The source code folder to upload.
    @param requirements_file
        The requirements file to use to specify the dependencies.
    @param target
        The file in which to save the target
    """
    bundle = tempfile.mkdtemp()
    try:
        dir_util.copy_tree(source_folder, bundle)
        if requirements_file:
            with open(requirements_file) as f:
                with tempfile.NamedTemporaryFile(mode='w+t') as t:
                    for line in f:
                        s = line.strip()
                        s = re.sub(r'^-e\s+', '', s)
                        t.file.write(s + os.linesep)
                    t.flush()
                    pip.main(['install', '-r', t.name, '-t', bundle])

        dirname = os.path.dirname(target)
        os.makedirs(dirname, exist_ok=True)
        base_name, fmt = os.path.splitext(target)
        fmt = fmt.replace(os.path.extsep, '') or 'zip'

        shutil.make_archive(base_name, fmt, bundle, './', True)

    finally:
        shutil.rmtree(bundle)


@@ -237,16 +237,31 @@
 port os%0A
+import os.path%0A
 import p
@@ -295,17 +295,24 @@
 import s
-y
+ubproces
 s%0Aimport
@@ -324,23 +324,8 @@
 file
-%0Aimport zipfile
 %0A%0Ade
@@ -374,16 +374,33 @@
 , target
+, use_docker=True
 ):%0A    %22
@@ -677,24 +677,439 @@
  the target%0A
+    @param use_docker%0A        True to use Docker (if available) to create the bundle, otherwise False.%0A        You will need to use Docker if you are running on OSX or Windows and%0A        one or more of your dependencies includes C source code which has to be%0A        compiled. This is because binaries compiled on OSX or Windows will not%0A        run on the Linux instances that are used to run Lambda code in AWS.%0A
     %22%22%22%0A    
@@ -1274,30 +1274,9 @@
 as f
-:%0A                with
+,
  tem
@@ -1334,20 +1334,16 @@
         
-  
-  
 for line
@@ -1349,20 +1349,16 @@
 e in f:%0A
-    
         
@@ -1402,28 +1402,24 @@
             
- 
-   
 s = re.sub(r
@@ -1435,20 +1435,16 @@
  '', s)%0A
-    
         
@@ -1500,20 +1500,16 @@
         
-    
 t.flush(
@@ -1509,16 +1509,485 @@
 .flush()
+%0A                if use_docker and shutil.which('docker'):%0A                    subprocess.run(%5B%0A                        'docker', 'run',%0A                        '-v', os.path.realpath(t.name) + ':/requirements.txt',%0A                        '-v', os.path.realpath(bundle) + ':/bundle',%0A                        '-it', '--rm', 'python:3.6.3',%0A                        'pip', 'install', '-r', '/requirements.txt', '-t', '/bundle'%0A                    %5D)%0A                else:
 %0A       
@@ -2350,9 +2350,8 @@
 bundle)%0A
-%0A

Use Docker to install pip packages where available.