Dir.exist?没有按预期工作
Dir.exist? does not work as expected
我在检查目录是否存在时遇到问题。
我正在编写一个小型 Ruby 应用程序,它从存储库执行 svn 检出,然后查看工作副本以查看某个目录是否存在。
我有一个执行 svn 操作的 SVNClient class,使用 open3.popen3
调用命令行客户端(我还没有找到任何 ruby gem 可以使用svn):
class SVNClient
require 'open3'
def initialize(repoUrl, wcPath, username, password)
@repoUrl = repoUrl
@wcPath = wcPath
@username = username
@password = password
end
def checkout
cmd = "svn co %s %s --non-interactive --trust-server-cert --username %s --password %s" % [@repoUrl, @wcPath, @username, @password]
stdin, stdout, stderr = Open3.popen3(cmd)
end
def createDirIfNotExists(dirname)
@subdirPath = @wcPath + "/" + dirname
a = File.directory? @subdirPath
b = File.exist? @subdirPath
c = Dir.exist? @subdirPath
Rails.logger.debug("#{a}")
Rails.logger.debug("#{b}")
Rails.logger.debug("#{c}")
if !Dir.exist? @subdirPath
Rails.logger.debug("#{@subdirPath} does not exist")
Dir.mkdir @subdirPath
end
end
end
这个class是这样用的:
wcDir = "/the/workingcopy/dir"
logger.debug("#{wcDir}")
urlToCkeckout = "http://somerepo/path
client = SVNClient.new(urlToCkeckout, wcDir, username, password)
client.checkout
client.createDirIfNotExists("subdir")
现在,我正在做一些测试,我确信当我结帐时,"subdir"
目录在工作副本目录中。但是在 createDirIfNotExists
方法中,Dir.exist?
调用 returns false(还有 File.directory?
和 File.exist?
),我得到“...不存在" 日志消息。
我在这里遗漏了一些明显的东西吗?我检查了目录的权限,它们看起来不错。
顺便说一下,代码是 运行 在 Rails 应用程序中。
解决方案是在我的 checkout
方法中使用 open3.popen3
的块版本,以确保命令在检查目录存在之前结束:
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
pid = wait_thr.pid # pid of the started process.
Rails.logger.debug("#{pid}")
end
现在,对 createDirIfNotExists
的调用发生在结帐终止之后,因此可以正确找到目录。
我在检查目录是否存在时遇到问题。
我正在编写一个小型 Ruby 应用程序,它从存储库执行 svn 检出,然后查看工作副本以查看某个目录是否存在。
我有一个执行 svn 操作的 SVNClient class,使用 open3.popen3
调用命令行客户端(我还没有找到任何 ruby gem 可以使用svn):
class SVNClient
require 'open3'
def initialize(repoUrl, wcPath, username, password)
@repoUrl = repoUrl
@wcPath = wcPath
@username = username
@password = password
end
def checkout
cmd = "svn co %s %s --non-interactive --trust-server-cert --username %s --password %s" % [@repoUrl, @wcPath, @username, @password]
stdin, stdout, stderr = Open3.popen3(cmd)
end
def createDirIfNotExists(dirname)
@subdirPath = @wcPath + "/" + dirname
a = File.directory? @subdirPath
b = File.exist? @subdirPath
c = Dir.exist? @subdirPath
Rails.logger.debug("#{a}")
Rails.logger.debug("#{b}")
Rails.logger.debug("#{c}")
if !Dir.exist? @subdirPath
Rails.logger.debug("#{@subdirPath} does not exist")
Dir.mkdir @subdirPath
end
end
end
这个class是这样用的:
wcDir = "/the/workingcopy/dir"
logger.debug("#{wcDir}")
urlToCkeckout = "http://somerepo/path
client = SVNClient.new(urlToCkeckout, wcDir, username, password)
client.checkout
client.createDirIfNotExists("subdir")
现在,我正在做一些测试,我确信当我结帐时,"subdir"
目录在工作副本目录中。但是在 createDirIfNotExists
方法中,Dir.exist?
调用 returns false(还有 File.directory?
和 File.exist?
),我得到“...不存在" 日志消息。
我在这里遗漏了一些明显的东西吗?我检查了目录的权限,它们看起来不错。
顺便说一下,代码是 运行 在 Rails 应用程序中。
解决方案是在我的 checkout
方法中使用 open3.popen3
的块版本,以确保命令在检查目录存在之前结束:
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
pid = wait_thr.pid # pid of the started process.
Rails.logger.debug("#{pid}")
end
现在,对 createDirIfNotExists
的调用发生在结帐终止之后,因此可以正确找到目录。