import boto3
import filecmp
import os


UPDATED_SITE_DIR = os.environ.get('UPDATED_SITE_DIR')
CURRENT_SITE_DIR = os.environ.get('CURRENT_SITE_DIR')
BUCKET_NAME = os.environ.get('S3_BUCKET_NAME')


def upload_with_content_type(file, full_key, auto):
    if ".html" in file:
        print("uploading \"{}\" as html".format(full_key))
        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',
                          ContentType="text/html")
    elif ".css" in file:
        print("uploading \"{}\" as css".format(full_key))
        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',
                          ContentType="text/css")
    elif ".jpg" in file:
        print("uploading \"{}\" as jpeg".format(full_key))
        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',
                          ContentType="image/jpeg")
    elif ".png" in file:
        print("uploading \"{}\" as png".format(full_key))
        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',
                          ContentType="image/png")
    elif ".xml" in file:
        print("uploading \"{}\" as xml".format(full_key))
        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',
                          ContentType="application/xml")
    else:
        print("uploading \"{}\"".format(full_key))
        bucket.put_object(Key=full_key, Body=auto,
                          ACL='public-read')


if __name__ == "__main__":
    s3 = boto3.resource('s3')
    bucket = s3.Bucket(BUCKET_NAME)
    for root, dirs, files in os.walk(os.getcwd() + UPDATED_SITE_DIR):
        subdir = root[(len(os.getcwd())+len(UPDATED_SITE_DIR)):len(root)]
        if subdir.startswith('/'):
            subdir = subdir[1:]
        for file in files:
            current_file = str(os.path.join(root, file)).\
                           replace(UPDATED_SITE_DIR, CURRENT_SITE_DIR)
            updated_file = str(os.path.join(root, file))
            files_match = False
            try:
                files_match = filecmp.cmp(current_file, updated_file,
                                          shallow=False)
            except FileNotFoundError:
                pass # files_match already set to False
            if not files_match and file is not ".DS_Store":
                with open(os.path.join(root, file), "rb") as auto:
                    full_key = file
                    if subdir:
                        full_key = subdir + '/' + file
                    upload_with_content_type(file, full_key, auto)

@@ -250,195 +250,87 @@
     
-if %22.html%22 in file:%0A        print(%22uploading %5C%22%7B%7D%5C%22 as html%22.format(full_key))%0A        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',%0A                          C
+content_type = %22default%22%0A    if file.endswith(%22.html%22):%0A        c
 ontent
-Type=
+_type = 
 %22tex
@@ -336,17 +336,16 @@
 xt/html%22
-)
 %0A    eli
@@ -350,190 +350,54 @@
 lif 
-%22.css%22 in file:%0A        print(%22uploading %5C%22%7B%7D%5C%22 as css%22.format(full_key))%0A        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',%0A                          ContentType=
+file.endswith(%22.css%22):%0A        content_type = 
 %22tex
@@ -402,17 +402,16 @@
 ext/css%22
-)
 %0A    eli
@@ -416,191 +416,54 @@
 lif 
-%22.jpg%22 in file:%0A        print(%22uploading %5C%22%7B%7D%5C%22 as 
+file.endswith(%22.
 jp
-e
 g%22
-.format(full_key))%0A        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',%0A                          ContentType=
+):%0A        content_type = 
 %22ima
@@ -470,17 +470,16 @@
 ge/jpeg%22
-)
 %0A    eli
@@ -484,178 +484,106 @@
 lif 
-%22.png%22 in file:%0A        print(%22uploading %5C%22%7B%7D%5C%22 as png%22.format(full_key))%0A        bucket.put_object(Key=full_key, Body=auto, ACL='public-read',%0A                  
+file.endswith(%22.png%22):%0A        content_type = %22image/png%22%0A    elif file.endswith(%22.xml%22):%0A
         
 Cont
@@ -582,62 +582,42 @@
     
-C
+c
 ontent
-Type=%22image/png%22)%0A    elif %22.xml%22 in file:%0A    
+_type = %22application/xml%22%0A%0A
     
@@ -643,19 +643,18 @@
 %7B%7D%5C%22 as 
-xml
+%7B%7D
 %22.format
@@ -654,34 +654,82 @@
 .format(full_key
-))
+, content_type))%0A    if content_type is %22default%22:
 %0A        bucket.
@@ -767,27 +767,8 @@
 uto,
- ACL='public-read',
 %0A   
@@ -794,99 +794,36 @@
     
-ContentType=%22application/xml%22)%0A    else:%0A        print(%22uploading %5C%22%7B%7D%5C%22%22.format(full_key))
+ACL='public-read')%0A    else:
 %0A   
@@ -861,32 +861,51 @@
 _key, Body=auto,
+ ACL='public-read',
 %0A               
@@ -915,33 +915,40 @@
         
-ACL='public-read'
+ContentType=content_type
 )%0A%0A%0Aif _

clean up update script