import urlparse 
import subprocess
import time
import logging
import os

def runpreloadcommand(cmd,timeout,preload = 60):
    newenv = dict(os.environ)
    newenv["LD_PRELOAD"] = preload
    process = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,env=newenv)
    processRc = None
    handleprocess = True
    counter = 0
    stdout = ''
    stderr = ''
    while handleprocess:
        counter += 1
        time.sleep(1)
        cout,cerr = process.communicate()
        stdout += cout
        stderr += cerr
        process.poll()
        processRc = process.returncode
        if processRc != None:
            break
        if counter == timeout:
            os.kill(process.pid, signal.SIGQUIT)
        if counter > timeout:
            os.kill(process.pid, signal.SIGKILL)
            processRc = -9
            break
    
    return (processRc,stdout,stderr)

def gsiDcapCopy(src,dest,timeout = 60):
    cmd = "dccp -C 3000 -d 2 -A %s %s" % (src,dest)
    process = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    processRc = None
    handleprocess = True
    counter = 0
    stdout = ''
    stderr = ''
    while handleprocess:
        counter += 1
        time.sleep(1)
        cout,cerr = process.communicate()
        stdout += cout
        stderr += cerr
        process.poll()
        processRc = process.returncode
        if processRc != None:
            break
        if counter == timeout:
            os.kill(process.pid, signal.SIGQUIT)
        if counter > timeout:
            os.kill(process.pid, signal.SIGKILL)
            processRc = -9
            break
    if processRc != 0:
        log = logging.getLogger("gsiDcapCopy")
        log.error("failed to execute command '%s'" % (cmd))
    return (processRc,stdout,stderr)



class uploaderDcap:
    def __init__(self):
        self.remotePrefix = None
        self.log = logging.getLogger("uploaderGsiDcap")
    def _getfilepath(self,remotePath):
        if self.remotePrefix != None:
            return self.remotePrefix + remotePath
        else:
            return remotePath

    def delete(self,remotePath):
        cmd = "unlink %s" % (self._getfilepath(remotePath))
        timeout = 10
        preload = "/usr/lib64/libpdcap.so.1"
        return runpreloadcommand(cmd,timeout,preload)
        
    def upload(self,localpath,remotePath):
        return gsiDcapCopy(localpath,self._getfilepath(remotePath))

    def replace(localpath,remotePath):
        (rc,stdout,stderr) = self.delete(remotePath)
        if rc != 0:
            print stderr
            return (rc,stdout,stderr)
        rc,stdout,stderr = gsiDcapCopy(localpath,gsipath,timeout)
        print rc
        if rc != 0:
            print stderr
            return (rc,stdout,stderr)
        return (rc,stdout,stderr)
    def download(self,remotePath,localpath):
        rc,stdout,stderr = gsiDcapCopy(self._getfilepath(remotePath),localpath)
        if rc != 0:
            for errorLine in stderr.split('\n'):
                self.log.error("stderr:'%s'" % (errorLine))
        return rc,stdout,stderr

@@ -66,16 +66,19 @@
 ort os%0A%0A
+%0A%0A%0A
 def runp
@@ -2127,16 +2127,235 @@
 otePath%0A
+    def exists(self,remotePath):%0A        cmd = %22stat %25s%22 %25 (self._getfilepath(remotePath))%0A        timeout = 10%0A        preload = %22/usr/lib64/libpdcap.so.1%22%0A        return runpreloadcommand(cmd,timeout,preload)%0A        
 %0A    def
@@ -2624,37 +2624,148 @@
     
-return gsiDcapCopy(localpath,
+path = self._getfilepath(remotePath)%0A        return gsiDcapCopy(localpath,path)%0A%0A    def replace(self,localpath,remotePath):%0A        path = 
 self
@@ -2793,50 +2793,81 @@
 ath)
-)%0A
 %0A    
-def replace(localpath,remotePath):%0A
+    (rc,stdout,stderr) =  self.exists(path)%0A        if rc == 0:%0A    
     
@@ -2903,28 +2903,26 @@
 .delete(
-remoteP
+p
 ath)%0A
+    
         
@@ -2937,32 +2937,36 @@
  0:%0A            
+    
 print stderr%0A   
@@ -2966,32 +2966,36 @@
 err%0A            
+   
+ 
 return (rc,stdou
@@ -3057,41 +3057,13 @@
 ath,
-gsi
 path
-,timeout)%0A        print rc
+)
 %0A   

make the replace option work correctly