首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python tarfile失败

Python tarfile失败
EN

Stack Overflow用户
提问于 2014-08-12 19:54:07
回答 1查看 133关注 0票数 0

我正在尝试编写一个脚本,将tar、一个目录和scp写到有大量tar文件的服务器上。我在创建目录的tar时遇到了困难,这是完整的脚本。为什么会发生这种事?

代码:

代码语言:javascript
复制
#!/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")

附加运行:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-25 14:49:01

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25272954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档