#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import cgi
import cgitb
import json
import logging
import logging.handlers
from Errors import *
from TaskConfig import *
CGILOG=logging.getLogger('BackVersionCGI')
CGILOG.setLevel(logging.DEBUG)
#fh=logging.FileHandler('BackVersionCGI.log')
fh=logging.handlers.RotatingFileHandler('BackVersionCGI.log',maxBytes=1048576)
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s,%(levelname)s, %(message)s",
"%Y-%m-%d %H:%M:%S")
fh.setFormatter(formatter)
CGILOG.addHandler(fh)
RESTVerb = ''
def ReplyHTML(arguments):
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print ""
print "
CGI script output"
print "This CGI is used by File Versioning Backup Restore Function
"
print "Internal usage only!
"
print "-------- arguments -------
"
for i in arguments.keys():
print "%s : %s
" % (i, arguments[i])
print "------- os.environ --------
"
for param in os.environ.keys():
print "%20s: %s
" % (param, os.environ[param])
print "----------------
"
print "VERB: %s
" % RESTVerb
print ""
def DumpDebugInfo(arguments):
CGILOG.debug("==== os.environ ====")
for param in os.environ.keys():
CGILOG.debug("%s : %s" % (param, os.environ[param]))
CGILOG.debug("---- arguments ----")
if arguments:
for arg in arguments.keys():
CGILOG.debug("%s : %s" % (arg, arguments[arg].value))
#cgitb.enable(display=0, logdir="/log")
cgitb.enable()
if 'REQUEST_METHOD' in os.environ:
RESTVerb = os.environ['REQUEST_METHOD']
CGILOG.debug("####################################### %s" % RESTVerb)
inputLen = 0
inputData = ""
if 'CONTENT_LENGTH' in os.environ:
inputLen = int(os.environ['CONTENT_LENGTH'])
if inputLen > 0:
inputData = sys.stdin.read()
if (inputData[0] == '\'' or inputData[0] == '\"') and (inputData[-1] =='\'' or inputData[-1] == '\"'):
inputData = inputData[1:-1]
inputData = inputData.replace('\\n', '')
inputData = inputData.replace('\\r', '')
inputData = inputData.replace('\\t', '')
inputData = inputData.replace('\\"', '"')
CGILOG.debug("stdin: %s" % inputData)
arguments = cgi.FieldStorage()
ArgLen = 0
if arguments:
ArgLen = len(arguments)
CGILOG.debug("ArgLen : %s" % ArgLen)
#if RESTVerb == 'GET' and ArgLen <= 0:
if 'QUERY_STRING' in os.environ:
qs = os.environ['QUERY_STRING']
if ArgLen <= 0:
arguments = dict([x.split("=") for x in qs.split("&")])
#ReplyHTML(arguments)
else:
DumpDebugInfo(arguments)
fv = FileVersionBackup(RESTVerb, inputData, arguments)
resultJSON = fv.Process()
CGILOG.debug('Return: %s', json.dumps(resultJSON))
sys.stdout.write("Content-Type: application/json\n\n")
sys.stdout.write(json.dumps(resultJSON))
sys.stdout.write("\n")
sys.stdout.close()