import os
import zipfile
from watcher import SonoSiteWatcher

SCANNER_OUTPUT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests', 'complete')

def get_subdirectories(directory):
    return [d for d in os.listdir(directory) if os.path.isdir(os.path.join(directory, d))]

def run():
    current_path = os.path.dirname(os.path.abspath(__file__))

    for session in get_subdirectories(SCANNER_OUTPUT_DIR):
        try:
            # each exam session has nested scan sessions
            # TODO this is "supposed to" only be one dir, but in theory
            # may be more
            session_dir = os.path.join(SCANNER_OUTPUT_DIR, session)
            scan_dir = os.path.join(session_dir, get_subdirectories(session_dir)[0])

            watcher = SonoSiteWatcher(session_dir)

            if not watcher.is_complete():
                next

            zip_file = zipfile.ZipFile('uploading.zip', 'w')

            zip_file.write(
                os.path.join(scan_dir, 'PT_PPS.XML'),
                os.path.join(os.path.split(scan_dir)[-1], 'PT_PPS.XML')
            )
            for media, xml in watcher.all_media():
                zip_file.write(
                    os.path.join(scan_dir, media),
                    os.path.join(
                        os.path.split(scan_dir)[-1],
                        xml.split('.')[0],
                        media)
                )
                zip_file.write(
                    os.path.join(scan_dir, xml),
                    os.path.join(
                        os.path.split(scan_dir)[-1],
                        xml.split('.')[0],
                        xml
                    )
                )

                # TODO do processing here
        finally:
            os.remove(os.path.join(current_path, 'uploading.zip'))


if __name__ == '__main__':
    run()

@@ -53,16 +53,87 @@
 eWatcher
+%0Aimport requests%0Afrom requests.auth import HTTPDigestAuth%0Aimport shutil
 %0A%0ASCANNE
@@ -152,32 +152,37 @@
  = os.path.join(
+%0A    
 os.path.dirname(
@@ -228,17 +228,19 @@
 omplete'
-)
+%0A)%0A
 %0A%0Adef ge
@@ -360,16 +360,17 @@
 , d))%5D%0A%0A
+%0A
 def run(
@@ -1771,54 +1771,829 @@
     
-    # TODO do processing here%0A        finally:
+zip_file.close()%0A%0A            with open(os.path.join(current_path, 'uploading.zip')) as f:%0A                r = requests.post(%0A                    url='http://localhost:8000/a/hello/sonosite_upload',%0A                    auth=HTTPDigestAuth('t@w.com', 'asdf'),%0A                    files=%7B'file': f%7D,%0A                    data=%7B%7D%0A                )%0A%0A            if r.status_code == 200 and r.json()%5B'result'%5D == 'uploaded':%0A                # TODO enable this after testing%0A                pass%0A                # shutil.move(%0A                #     session_dir,%0A                #     os.path.join(current_path, 'complete')%0A                # )%0A%0A            print r.status_code%0A            print r.text%0A        finally:%0A            # delete the temp zip no matter what happens since we keep%0A            # the original copy in tact
 %0A   

Refactor and handle file closing/moving