import base64
from subprocess import call
from os.path import abspath, dirname, join
import sys, os
import uuid
import shutil

# missing module fix
sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi'))
virtenv = os.environ['APPDIR']+'/virtenv'
os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python2.6/site-packages')
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except:
    pass

import untangle

# openshift env variables
# TODO: Move these settings to env variable
TMP = os.environ['OPENSHIFT_TMP_DIR']
PUBLIC = os.environ['OPENSHIFT_REPO_DIR']+'wsgi/static/download/'
DATA = os.environ['OPENSHIFT_DATA_DIR']
BIN = DATA+"texlive/bin/x86_64-linux/"

class clsi:
    def __init__(self):
        self.id = str(uuid.uuid4())
        self.tmp = TMP + self.id + "/"
        self.public = PUBLIC + self.id + "/"
        self.token = None
        self.format = "pdf"
        self.compiler = "pdflatex"
        
    def parse(self, data):
        req = untangle.parse(data)
        if req.compile.token:
            self.token = req.compile.token.cdata
        if req.compile.options.output_format:
            self.format = req.compile.options.output_format.cdata
        if req.compile.options.compiler:
            self.compiler = req.compile.options.compiler.cdata
        root = req.compile.resources['root-resource-path']
        to_compile = self.tmp + root
        # Writes the files to disk
        for file in req.compile.resources.resource:
            if not os.path.exists(self.tmp):
                os.makedirs(self.tmp)
            f = open(self.tmp+file['path'], 'w')
            if file['encoding'] == 'base64':
                f.write(base64.b64decode(file.cdata))
            elif file['encoding'] == 'utf-8':
		data = file.cdata
                f.write(data.encode('utf-8'))
            else:
                print 'Error in file encoding'
        return(to_compile)

    def run(self, file):
        dir = os.path.dirname(file)+"/"


        # Change PATH and run latexmk
        # Adjust compiler option to latexmk
        if self.compiler == 'standalone':
            call("PATH=${PATH}:"+ BIN +" && cd "+ dir +" &&  latex -shell-escape -output-directory="+ dir +" "+ file, shell=True)
        elif self.compiler == "pdflatex":
            call("PATH=${PATH}:"+ BIN +" && latexmk -"+ self.format +" -outdir="+ dir +" "+ file, shell=True)
        elif self.compiler == "latex":
            call("PATH=${PATH}:"+ BIN +" && latexmk -"+ self.format +" -outdir="+ dir +" "+ file, shell=True)
        else:
            call("PATH=${PATH}:"+ BIN +" && latexmk -"+ self.compiler +" -"+ self.format +" -outdir="+ dir +" "+ file, shell=True)

        log, out = self._move_results(file)
        self._rm_tmp()
        return [log, out]


    def _move_results(self, file):
        dir = os.path.dirname(file)+'/'
        base = os.path.basename(file)
        name = os.path.splitext(base)[0]
        out = dir+name+'.'+self.format
        log = dir+name+'.log'
        if not os.path.exists(self.public):
            os.makedirs(self.public)
        
        if os.path.exists(out):
            shutil.move(out, self.public + name + '.' + self.format)
        if os.path.exists(log):
            shutil.move(log, self.public + name +'.log')
            
        if os.path.exists(self.public + name + '.' + self.format):
            return([self.id+'/'+name+'.log', self.id+'/'+name+'.'+self.format])
        else:
            return([self.id+'/'+name+'.log', None])
    
    def _rm_tmp(self):
        if os.path.isdir(self.tmp):
            shutil.rmtree(self.tmp)
        

@@ -937,35 +937,8 @@
 /%22%0D%0A
-        self.token = None%0D%0A
     
@@ -1084,35 +1084,23 @@
     
-if req.compile.
+# Check 
 token
-:
 %0D%0A
-    
     
@@ -1112,16 +1112,21 @@
 elf.
+_check_
 token
- = 
+(
 req.
@@ -1140,24 +1140,25 @@
 .token.cdata
+)
 %0D%0A        if
@@ -2077,47 +2077,8 @@
 %0D%0A%0D%0A
-        # Change PATH and run latexmk%0D%0A
     
@@ -3695,20 +3695,518 @@
 elf.tmp)%0D%0A        %0D%0A
+    def _check_token(self, token):%0D%0A        # TODO: Clean up this hack and find a better way to%0D%0A        # keep track of the authorized users%0D%0A        try:%0D%0A            with open(DATA+'tokens') as users:%0D%0A                for user in users:%0D%0A                    if token == user.rstrip(): # Removes the newline at the end of %0D%0A                        return True %0D%0A        except IOError:%0D%0A            return True%0D%0A        print(%22User %22+ token +%22 not found in database%22)%0D%0A        return sys.exit()%0D%0A

Add token check