Python tarfile失败

时间:2014-08-12 19:54:07

标签: python tarfile

我正在尝试编写一个tar目录和scp的脚本到一个有很多tar文件的服务器上。我在创建目录的tar时遇到了麻烦,这是完整的脚本。为什么会这样?

代码:

#!/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")

附加运行:

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

1 个答案:

答案 0 :(得分:0)

此脚本中的问题是我没有关闭文件并将其发送到服务器。我的一位同事帮我解决了这个问题。