Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.apo.nmsu.edu/Telescopes/TCC/html/test_utils_8py_source.html
Дата изменения: Tue Sep 15 02:25:37 2015
Дата индексирования: Sun Apr 10 01:51:35 2016
Кодировка:

Поисковые слова: п п п п п п п п п п
lsst.tcc: python/tcc/base/testUtils.py Source File
lsst.tcc  1.2.2-3-g89ecb63
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
testUtils.py
Go to the documentation of this file.
1 from __future__ import division, absolute_import
2 """Utilities to aid unit tests
3 """
4 import os
5 import shutil
6 import stat
7 import tempfile
8 import time
9 
10 import eups
11 from RO.StringUtil import strFromException
12 from twistedActor import testUtils
13 
14 from .tccLib import tai
15 
16 _MaxEarthAgeSec = 3600
17 _ModeRWRWR = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH
18 _DaysPerSec = 1.0 / (24.0 * 3600.0)
19 
20 def getTestDir():
21  """!Return path to unit tests
22  """
23  return os.path.join(eups.productDir("tcc"), "tests")
24 
25 def setEnviron():
26  """!Set TCC environment variables appropriately for running unit tests
27  """
28  testDir = getTestDir()
29  os.environ["TCC_DATA_DIR"] = os.path.join(testDir, "data")
30 
32  """!Create a new earth prediction file, so the TCC can be started
33 
34  The file is: <getTestDir()>/data/earthpred.dat and any existing copy is overwritten.
35 
36  Note that this intentionally does not use os.environ["TCC_DATA_DIR"], for fear of
37  accidentally overwriting the real earth orientation prediction file.
38  """
39  testDir = getTestDir()
40  tempEarthPath = None
41  earthPath = os.path.join(testDir, "data", "earthpred.dat")
42  try:
43  mtime = os.path.getmtime(earthPath)
44  if time.time() - mtime < _MaxEarthAgeSec:
45  return
46  except OSError:
47  pass # file does not exist; create it
48 
49  currTAI = tai()
50  startMJD = int(currTAI * _DaysPerSec) - 1
51  print "Creating new %r" % (earthPath,)
52  try:
53  with tempfile.NamedTemporaryFile(mode="w", dir=testDir, delete=False) as tempEarthFile:
54  tempEarthPath = tempEarthFile.name
55  print "Created temporary earthpred file %r" % (tempEarthPath,)
56  tempEarthFile.write("""! Earth Orientation Predictions (in TCC format)
57 
58 ! UTC UTC-TAI UT1-TAI dX dY
59 ! days sec sec arcsec arcsec
60 """)
61  for i in range(7):
62  utcDays = startMJD + i - 1
63  tempEarthFile.write("%d -35 -34.8 0.1 0.3\n" % (utcDays,))
64  except Exception as e:
65  print "Failed to create %s: %s; trying to remove temp file %r" % \
66  (earthPath, strFromException(e), tempEarthPath)
67  os.remove(tempEarthPath)
68  return
69  os.chmod(tempEarthPath, _ModeRWRWR)
70 
71  # check file date again in case another process already replaced it
72  try:
73  mtime = os.path.getmtime(earthPath)
74  if time.time() - mtime < _MaxEarthAgeSec:
75  print "Another thread already replaced %r; removing temp file %r" % (earthPath, tempEarthPath)
76  os.remove(tempEarthPath)
77  return
78  except OSError:
79  pass # file does not exist; create it
80  shutil.move(tempEarthPath, earthPath)
81  print "Updated %r" % (earthPath,)
82 
83 def init(filePath=None):
84  """!Prepare for a unit test to run that starts an actor
85 
86  @param[in] filePath path of file being tested (e.g. __file__), or None:
87  - If supplied must be in subdir tests of your package (NOT deeper in the hierarchy)
88  in order to determine where the log file should go.
89  - If None, no log file is created.
90  """
91  testUtils.startLogging(filePath)
92  setEnviron()
def setEnviron
Set TCC environment variables appropriately for running unit tests.
Definition: testUtils.py:25
def getTestDir
Return path to unit tests.
Definition: testUtils.py:20
def init
Prepare for a unit test to run that starts an actor.
Definition: testUtils.py:83
def makeEarthPred
Create a new earth prediction file, so the TCC can be started.
Definition: testUtils.py:31
double tai()
Definition: tai.cc:7