2017-01-12 2 views
0

디렉토리가 존재하는지 확인하는 동안 문제가 있습니다.Dir.exist? 예상대로 작동하지 않습니다.

저장소에서 svn 체크 아웃을 수행하는 작은 Ruby 앱을 작성한 다음 작업 사본을보고 특정 디렉토리가 있는지 확인합니다.

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 

이 클래스는 다음과 같이 사용됩니다

나는 (내가 SVN과 함께 작업 할 수있는 루비 보석을 발견하지 않았습니다) open3.popen3을 사용하여 명령 행 클라이언트를 호출의 SVN 작업을 수행하는 SVNClient 클래스가 있습니다 :

 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? 호출은 false를 반환하며 (또한 File.directory?File.exist?) "... 존재하지 않습니다"라는 로그 메시지가 나타납니다.
내가 여기에 명백한 것을 놓치고 있습니까? 디렉토리에 대한 사용 권한을 확인한 결과 좋아 보인다.

그건 그렇고, 코드는 레일 애플 리케이션 내에서 실행됩니다.

답변

1

솔루션은 명령이 디렉토리의 존재를 확인하기 전에 끝나는 확인하기 위해, 내 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에 대한 호출은 체크 아웃 후 발생 종료되었으므로 디렉토리가 올바르게 발견되었습니다.

관련 문제