首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOError:[Errno 2]使用绝对路径,linux上没有这样的文件或目录

IOError:[Errno 2]使用绝对路径,linux上没有这样的文件或目录
EN

Stack Overflow用户
提问于 2019-02-04 19:29:08
回答 1查看 313关注 0票数 1

我很难找到解决这个问题的办法。我使用的绝对路径绝对存在于文件系统中。在同一个脚本中使用相同的精确路径时,

代码语言:javascript
复制
if host.file(logPath).exists:
    print("Exists: " + logPath)

我得到了

代码语言:javascript
复制
Exists: /var/opt/jws/jws3.0/v7/testinfra_node1/logs/catalina.out

但在尝试时:

代码语言:javascript
复制
with open(logPath, "rt") as log:

我得到了:

代码语言:javascript
复制
>               with open(logPath, "rt") as log:
E               IOError: [Errno 2] No such file or directory: '/var/opt/jws/jws3.0/v7/testinfra_node1/logs/catalina.out'

下面是整个代码(这是一个用于测试JWT安装的testinfra脚本):

代码语言:javascript
复制
import pytest
import testinfra
import time
import os

@pytest.mark.parametrize("jws_version", [("3.0")])

def test_server_recycle(host, jws_version):
    instances = host.check_output("cat /var/opt/jws/jws" + jws_version + "/init/init_instances | grep -oP '(\/.*?\/)((?:[^\/]|\\\\/)+?)(?:(?<!\\\\)\s|$)'")
last = ""
for last in iter(instances.splitlines()):
    pass
last = last.strip().encode('ascii','ignore')
print(last)

instanceName = ""
for instanceName in iter(last.split("/")):
    pass
print(instanceName)

binPath = last + "/bin/"
logDir = last + "/logs/"
logPath = os.path.join(logDir, "catalina.out")
print(logPath)

runningAt0 = isInstanceRunning(host, instanceName)
if host.file(logPath).exists:
    print("Exists: " + logPath)

if (runningAt0):
    with host.sudo():
        host.run(os.path.join(binPath, "shutdown.sh"))    
        with open(logPath, "rt") as log:
            result = waitForEntry(log, "INFO  org.apache.catalina.core.StandardService- Stopping service Catalina","ERROR")
        assert not result.eq("ERROR")
        assert not isInstanceRunning(host, instanceName)
        host.run(os.path.join(binPath, "/bin/startup.sh"))
        with open(logPath, "rt") as log:
            result = waitForEntry(log, "INFO  org.apache.catalina.startup.Catalina- Server startup in","ERROR")
        assert not result.eq("ERROR")
        assert isInstanceRunning(host, instanceName)
else:
    with host.sudo():
        host.run(os.path.join(binPath, "startup.sh"))
        with open(logPath, "rt") as log:
            result = waitForEntry(log, "INFO  org.apache.catalina.core.StandardService- Stopping service Catalina","ERROR")
        assert not result.eq("ERROR")
        assert isInstanceRunning(host, instanceName)
        host.run(os.path.join(binPath, "shutdown.sh"))
        with open(logPath, "rt") as log:
            result = waitForEntry(log, "INFO  org.apache.catalina.startup.Catalina- Server startup in","ERROR")
        assert not result.eq("ERROR")
        assert not isInstanceRunning(host, instanceName)

def isInstanceRunning(host, instanceName):
    processes = host.check_output("ps auwwx | grep catalina.startup.Bootstrap")
    if  "-Dtomcat.inst.name=" + instanceName in processes:
        return True
    else:
        return False

def waitForEntry(file, entry1, entry2):
    while 1:
        file.seek(0,2)
        line = file.readline()
        if entry1 in line:
            return entry1
        else:
            if entry2 in line:
                return entry2
            else:
                time.sleep(0.1)

而不是

代码语言:javascript
复制
host.file(logPath).exists

我也一直在努力

代码语言:javascript
复制
print(host.check_output("cat " + logPath))

它很好地打印了文件内容。

对如何处理这件事有什么想法吗?事先非常感谢!

编辑:下面是我执行脚本的方式:

py.test -v -host=user@host tomcat_test_recycle.py --sudo

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-05 11:10:14

testinfra包似乎将测试远程服务器的状态集成到pytest中。因此

代码语言:javascript
复制
host.file(logPath).exists

代码语言:javascript
复制
host.check_output("cat " + logPath)

正在检查远程服务器上的存在和内容,而

代码语言:javascript
复制
open(logPath, "rb") 

本地机器上运行。文件不在本地机器上,而是在host上。

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

https://stackoverflow.com/questions/54523058

复制
相关文章

相似问题

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