Search code examples
pythontarfile

Python tarfile fails


I am trying to write a script that tar a directory and scp's to a server which have lots of tar files. I am having trouble in creating tar of the directories, here is the complete script. Why is that happening?

Code:

#!/usr/bin/python
import json
from pprint import pprint
import subprocess
import os
from os.path import expanduser
import time
import os.path
import shutil
import tarfile
import smtplib
import zipfile
import glob

def checkFileDownload():
    os.system("scp ***@***.***.***.***:/var/log/apache2/access.log ~/pingMeServeraccess.log")

def sendNotificationText(server="smtp.gmail.com",userName="***@***.com",password="********",cellNumber="***********",testLink="Test"):
    server = smtplib.SMTP_SSL(server, ***)
    server.login(userName,password)
    server.sendmail(userName,cellNumber,testLink)

def sendTarFileToPingMeServer(locationOfTarFile="/home/autotest/tarPackage",nameOfTarFile=""):
    fullPathOfFile = nameOfTarFile
    scpCommand = "scp -r "+ fullPathOfFile +" ***@***.***.***.***:/home/autotest/untethered/"
    try:
        os.popen(scpCommand)
        testLink= "\nhttp://***.***.***.***/" + nameOfTarFile.split('/')[-1]
        sendNotificationText(testLink = testLink)
    except:
        print "something went wrong"

def makeTarFile(sourceDir):
    if os.path.exists(expanduser("~/tarPackage")):
        shutil.rmtree(expanduser("~/tarPackage"))
    else:
        pass
    dstFolder = expanduser('~/tarPackage')
    crtDstFolder = 'mkdir -p ' + dstFolder
    os.system(crtDstFolder)
    archiveName = str(time.time())+'.tar'
    print 'creating archive, '+archiveName
    out = tarfile.open(expanduser('~/tarPackage/'+archiveName), mode='w')
    try:
        out.add(sourceDir)
        sendTarFileToPingMeServer(nameOfTarFile=archiveName)
    finally:
        out.close()

    checkFileDownload()

def getTest(userName):
    testLoc = check(userName)
    gitList= [];TestList = []; packageDir = "mkdir ~/testPackageDir"
    if os.path.exists(expanduser("~/testPackageDir")):
        shutil.rmtree(expanduser("~/testPackageDir"))
    else:
        pass
    originalDirectory = os.getcwd()
    gitrepo = ""
    for test,gitLink in testLoc.items():
        if gitLink not in gitList:
            gitRepo = expanduser("~/tempGit_"+str(time.time()))
            p = subprocess.Popen(["git", "clone", gitLink,gitRepo], stdout=subprocess.PIPE)
            out,err = p.communicate()
            gitList.append(gitLink)
            testLink = gitRepo + test
            if os.path.isfile(testLink):
                os.system(packageDir)
                relPath = test.rstrip(test.split('/')[-1])
                x = "mkdir -p ~/testPackageDir"+relPath
                os.system(x)
                y = "~/testPackageDir" + relPath
                cpTest = "cp "+testLink+" "+ expanduser(y)
                os.system(cpTest)
        else:
            print "git link already cloned, skipping, checking for test cases."
            testLink = gitRepo + test
            if os.path.isfile(testLink):
                relPath = test.rstrip(test.split('/')[-1])
                x = "mkdir -p ~/testPackageDir"+relPath
                os.system(x)
                y = "~/testPackageDir" + relPath
                cpTest = "cp "+testLink+" "+ expanduser(y)
                os.system(cpTest)
    makeTarFile(expanduser("~/testPackageDir"))
    os.system("cd ~; rm -rf tempGit_*;cd -; rm -rf ~/testPackageDir")


def check(userName):
    p = subprocess.Popen(["ls", "/var/www/tempdata/testexec"], stdout=subprocess.PIPE)
    out,err = p.communicate()
    out = out.split('\n')[:-1]
    for fileName in out:
        if userName in fileName:
            filePath = "/var/www/tempdata/testexec/"+fileName
            json_data=open(filePath)
            data = json.load(json_data)
    testLoc = searchForGitTest(data)
    curDict = os.popen("pwd")
    os.system("cd ~")
    return testLoc

def searchForGitTest(data):
    aux = {};auxList= []
    for idx in range(len(data["rows"])):
        scriptPath = data["rows"][idx]["scriptPath"]
        gitPath = data["rows"][idx]["gitPath"]
        aux[scriptPath] = gitPath
return aux

if __name__ == "__main__":
    getTest("user")

Attaching the run:

autotest@batman007:/var/www$ python testPackageUploader.py 
remote: Counting objects: 38357, done
remote: Finding sources: 100% (38357/38357)
remote: Total 38357 (delta 15889), reused 36060 (delta 15889)
Receiving objects: 100% (38357/38357), 652.78 MiB | 17.08 MiB/s, done.
Resolving deltas: 100% (15889/15889), done.
git link already cloned, skipping, checking for test cases.
creating archive
1407871278.15.tar: No such file or directory
access.log                                                                                                                   100%   21KB  21.3KB/s   00:00    
/var/www

Solution

  • The problem in this script was I was not closing the file and sending it to the server. One of my colleagues helped me to figure out this problem.