def testGetRemoteShakeMap(self): """Check that we can retrieve both input and output from ftp at once""" myShakeEvent = '20110413170148' myShakeData = ShakeData(myShakeEvent) myExpectedInpFile = os.path.join(shakemapZipDir(), myShakeEvent + '.inp.zip') myExpectedOutFile = os.path.join(shakemapZipDir(), myShakeEvent + '.out.zip') if os.path.exists(myExpectedInpFile): os.remove(myExpectedInpFile) if os.path.exists(myExpectedOutFile): os.remove(myExpectedOutFile) myInpFile, myOutFile = myShakeData.fetchEvent() myMessage = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (myExpectedOutFile, myOutFile)) self.assertEqual(myInpFile, myExpectedInpFile, myMessage) myMessage = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (myExpectedOutFile, myOutFile)) self.assertEqual(myOutFile, myExpectedOutFile, myMessage) assert os.path.exists(myExpectedInpFile) assert os.path.exists(myExpectedOutFile)
def setUp(self): """Copy our cached dataset from the fixture dir to the cache dir""" myOutFile = '20120726022003.out.zip' myInpFile = '20120726022003.inp.zip' myOutPath = os.path.abspath( os.path.join(os.path.dirname(__file__), 'fixtures', myOutFile)) myInpPath = os.path.abspath( os.path.join(os.path.dirname(__file__), 'fixtures', myInpFile)) shutil.copyfile(myOutPath, os.path.join(shakemapZipDir(), myOutFile)) shutil.copyfile(myInpPath, os.path.join(shakemapZipDir(), myInpFile))
def setUp(self): """Copy our cached dataset from the fixture dir to the cache dir""" myOutFile = '20120726022003.out.zip' myInpFile = '20120726022003.inp.zip' myOutPath = os.path.abspath(os.path.join(os.path.dirname(__file__), 'fixtures', myOutFile)) myInpPath = os.path.abspath(os.path.join(os.path.dirname(__file__), 'fixtures', myInpFile)) shutil.copyfile(myOutPath, os.path.join(shakemapZipDir(), myOutFile)) shutil.copyfile(myInpPath, os.path.join(shakemapZipDir(), myInpFile))
def cachePaths(self): """Return the paths to the inp and out files as expected locally. Args: None Returns: two tuple: str, str Consisting of inp and out local cache paths. Raises: None """ myInpFileName, myOutFileName = self.fileNames() myInpFilePath = os.path.join(shakemapZipDir(), myInpFileName) myOutFilePath = os.path.join(shakemapZipDir(), myOutFileName) return myInpFilePath, myOutFilePath
def testGetShakeMapOutput(self): """Check that we can retrieve a shakemap 'out' input file""" myEventId = '20110413170148' myShakeData = ShakeData(myEventId) myShakemapFile = myShakeData.fetchOutput() myExpectedFile = os.path.join(shakemapZipDir(), myEventId + '.out.zip') myMessage = 'Expected path for downloaded shakemap OUT not received' self.assertEqual(myShakemapFile, myExpectedFile, myMessage)
def testGetLatestShakeMap(self): """Check that we can retrieve the latest shake event""" # Simply dont set the event id in the ctor to get the latest myShakeData = ShakeData() myInpFile, myOutFile = myShakeData.fetchEvent() myEventId = myShakeData.eventId myExpectedInpFile = os.path.join(shakemapZipDir(), myEventId + '.inp.zip') myExpectedOutFile = os.path.join(shakemapZipDir(), myEventId + '.out.zip') myMessage = ('Expected path for downloaded shakemap INP not received' '\nExpected: %s\nGot: %s' % (myExpectedOutFile, myOutFile)) self.assertEqual(myInpFile, myExpectedInpFile, myMessage) myMessage = ('Expected path for downloaded shakemap OUT not received' '\nExpected: %s\nGot: %s' % (myExpectedOutFile, myOutFile)) self.assertEqual(myOutFile, myExpectedOutFile, myMessage)
def _fetchFile(self, theEventFile, theRetries=3): """Private helper to fetch a file from the ftp site. e.g. for event 20110413170148 this file would be fetched:: ftp://118.97.83.243/20110413170148.inp.zip and this local file created:: /tmp/realtime/20110413170148.inp.zip .. note:: If a cached copy of the file exits, the path to the cache copy will simply be returned without invoking any network requests. Args: * theEventFile: str - filename on server e.g.20110413170148.inp.zip * theRetries: int - number of reattempts that should be made in in case of network error etc. Returns: str: A string for the dataset path on the local storage system. Raises: EventUndefinedError, NetworkError """ # Return the cache copy if it exists myLocalPath = os.path.join(shakemapZipDir(), theEventFile) if os.path.exists(myLocalPath): return myLocalPath #Otherwise try to fetch it using ftp for myCounter in range(theRetries): myLastError = None try: myClient = FtpClient() myClient.getFile(theEventFile, myLocalPath) except NetworkError, e: myLastError = e except:
def test_shakemapZipDir(self): """Test we can get the shakemap zip dir""" myDir = shakemapZipDir() myExpectedDir = '/tmp/inasafe/realtime/shakemaps-zipped' assert os.path.exists(myExpectedDir) self.assertEqual(myDir, myExpectedDir)