import cPickle
import mimetypes
import os
import sys
import socket
from os import path

import httplib2
import simplejson

socket.setdefaulttimeout(60)

HERE_DIR = os.getcwd()
BUILD_DIR = path.join(HERE_DIR, '_build')

#host = 'http://localhost:25050'
host = 'http://beta.pylonshq.com'

post_uri = '%s/docs/upload' % host
image_uri = '%s/docs/upload_image' % host
delete_uri = '%s/docs/delete_revision' % host


def scan_dir(parent, directory):
    files = []
    for name in os.listdir(directory):
        full_name = path.join(directory, name)
        if name in ['_sources', '_static']:
            continue
        if path.isdir(full_name):
            new_parent = parent + '/' + name
            files.extend(scan_dir(new_parent, full_name))
        else:
            if name.endswith('.fpickle') or name.endswith('.pickle'):
                data = cPickle.load(open(full_name, 'r'))
                files.append(
                    ('/'.join([parent, name]), data)
                )
    return files

def scan_images(directory):
    files = []
    for name in os.listdir(directory):
        if name[-4:] in ['.png', '.jpg', 'gif']:
            files.append(name)
    return files

files = scan_dir('', path.join(BUILD_DIR, 'web'))

basedata = {}
# Find the metadata about versions and such
for filename, filedoc in files:
    if 'globalcontext.pickle' in filename:
        basedata['version'] = filedoc['version']
        basedata['project'] = filedoc['project']
        basedata['shorttitle'] = filedoc['shorttitle']

if len(sys.argv) < 2:
    raise Exception('Failed to specify doc-key')

dockey = sys.argv[1]
headers = {}
headers.setdefault('Accept', 'application/json')
headers.setdefault('User-Agent', 'Doc Uploader')
headers.setdefault('Content-Type', 'application/json')
headers.setdefault('Authkey', dockey)

language = os.path.split(HERE_DIR)[-1]

# Delete this revision, just in case
http = httplib2.Http(timeout=60)
del_uri = '%s/%s/%s' % (delete_uri, basedata['project'], basedata['version'])
resp, data = http.request(del_uri, 'GET', headers=headers)

for filename, filedoc in files:
    http = httplib2.Http(timeout=60)
    if not isinstance(filedoc, dict):
        continue
    filedoc['filename'] = filename
    filedoc['language'] = language
    filedoc.update(basedata)
    content = simplejson.dumps(filedoc, ensure_ascii=False).encode('utf-8')
    resp, data = http.request(post_uri, 'POST', body=content, headers=headers)
    status_code = int(resp.status)
    if status_code == 200:
        print "Uploaded %s" % filename
    else:
        print "FAILED: %s" % filename

for filename in scan_images(path.join(BUILD_DIR, 'web', '_images')):
    http = httplib2.Http(timeout=60)
    headers['Content-Type'] = mimetypes.guess_type(filename)
    file_content = open(path.join(BUILD_DIR, 'web', '_images', filename), 'r').read()
    resp, data = http.request(image_uri + '?version=%s&project=%s&name=%s' %
                              (basedata['version'], basedata['project'], filename),
                              body=file_content, headers=headers)
    status_code = int(resp.status)
    if status_code == 200:
        print "Uploaded %s" % filename
    else:
        print "FAILED: %s" % filename

@@ -845,48 +845,99 @@
     
-data = cPickle.load(open(full_name, 'r')
+fp = open(full_name, 'r')%0A                data = cPickle.load(fp)%0A                fp.close(
 )%0A  
@@ -1282,16 +1282,110 @@
  'web'))
+%0Aimages = scan_images(path.join(BUILD_DIR, 'web', '_images'))%0Ahttp = httplib2.Http(timeout=60)
 %0A%0Abaseda
@@ -2044,41 +2044,8 @@
 ase%0A
-http = httplib2.Http(timeout=60)%0A
 del_
@@ -2214,45 +2214,8 @@
 es:%0A
-    http = httplib2.Http(timeout=60)%0A
     
@@ -2440,16 +2440,66 @@
 utf-8')%0A
+    headers%5B'Content-Length'%5D = str(len(content))%0A
     resp
@@ -2739,97 +2739,15 @@
  in 
-scan_
 images
-(path.join(BUILD_DIR, 'web', '_images')):%0A    http = httplib2.Http(timeout=60)
+:
 %0A   
@@ -2809,27 +2809,17 @@
 e)%0A    f
-ile_content
+p
  = open(
@@ -2876,14 +2876,106 @@
 'r')
-.read(
+%0A    file_content = fp.read()%0A    fp.close()%0A    headers%5B'Content-Length'%5D = str(len(file_content)
 )%0A  

add content-length to image uploads