Skip to content
Snippets Groups Projects

Resolve "Make ERS AppName more descriptive"

Merged Jonas Ladefoged Holm requested to merge 16-make-ers-appname-more-descriptive into master
All threads resolved!
Files
18
from subprocess import Popen, PIPE
import time
#For timing timeouts
from subprocess import Popen, PIPE
#For timing timeouts, timer starts with __init__ function
class Timer():
def __init__(self, seconds):
self.startTime = time.time()
@@ -19,8 +17,43 @@ class Timer():
def timesNotUp(self):
return not self.timesUp()
#For checking a dir
def createTmpDir(testLocation):
"""Creates the directories: tmp, des, src and log at the defined testLocation.
Testlocation is the relative path from the CastorScript folder to where the folders should be created.
returns a dictionary of paths to relevant directories: projectPath, configPath, tmpPath, srcPath, desPath and logPath."""
import os, shutil
dirs = {}
## Define folder structure
dirs["project"] = os.path.dirname(os.path.abspath(__name__))
thisDir = dirs["project"] + testLocation
dirs["tmp"] = thisDir + "/tmp"
dirs["config"] = dirs["project"] + "/Configs"
dirs["src"] = dirs["tmp"] + "/src"
dirs["des"] = dirs["tmp"] + "/des"
dirs["log"] = dirs["tmp"] + "/log"
# I create some folders
try:
os.makedirs(dirs["src"])
os.makedirs(dirs["des"])
os.makedirs(dirs["log"])
except:
shutil.rmtree(dirs["tmp"])
os.makedirs(dirs["src"])
os.makedirs(dirs["des"])
os.makedirs(dirs["log"])
return dirs
class DirectoryChecker():
"""For checking the contents of a directory either once, or until a condition is met or timeout is reached."""
def __init__(self, dirPath):
self.dirPath = dirPath
self.lsOutput = None
@@ -28,16 +61,20 @@ class DirectoryChecker():
self.timerUtil = None
def waitForAnyFile(self, seconds):
"Waits for the specified amount of time for the dir to be empty, returns a tuple (bool: timedout, str: lsOutput)"
timeToTimeout = seconds
self.timerUtil = Timer(timeToTimeout)
while self.checkIfEmpty() and self.timerUtil.timesNotUp():
pass
return (self.timerUtil.timedout, self.getLsOutput())
def waitForEmptyDir(self, seconds):
"Waits for the specified amount of time for the dir to contain files, returns a tuple (bool: timedout, str: lsOutput)"
timeToTimeout = seconds
self.timerUtil = Timer(timeToTimeout)
while self.checkIfNotEmpty() and self.timerUtil.timesNotUp():
pass
return (self.timerUtil.timedout, self.getLsOutput())
def checkIfEmpty(self):
if len(self.getLsOutput()) > 1:
@@ -58,29 +95,4 @@ class DirectoryChecker():
def getLsOutput(self):
self.lsOutput = str(Popen(["ls", self.dirPath],bufsize=-1, stdout=PIPE).communicate()[0])
return self.lsOutput
#Could be refactored
def findCastorScriptPids():
# first find all running instances with this command
# ps aux | grep CastorScript.py | grep -v grep | awk '{print $2}'
# it get list of processes
psCommand = Popen(["ps", "aux"], stdout=PIPE)
# finds castorscript processes
grepCommand = Popen(["grep", "CastorScript.py"], stdin=psCommand.stdout, stdout=PIPE)
psCommand.stdout.close()
# sorts grep out of the mix
grepvCommand = Popen(["grep", "-v", "grep"], stdin=grepCommand.stdout, stdout=PIPE)
grepCommand.stdout.close()
# and finally gets the PIDs
awkCommand = Popen(["awk", "{print $2}"], stdin=grepvCommand.stdout, stdout=PIPE)
grepvCommand.stdout.close()
pids = str(awkCommand.communicate()[0]).splitlines()
awkCommand.stdout.close()
return pids
\ No newline at end of file
return self.lsOutput
\ No newline at end of file
Loading