github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bddtests/steps/contexthelper.py (about)

     1  
     2  import uuid
     3  import os
     4  import shutil
     5  from slugify import slugify
     6  
     7  class ContextHelper:
     8  
     9      @classmethod
    10      def GetHelper(cls, context):
    11          if not "contextHelper" in context:
    12              context.contextHelper = ContextHelper(context)
    13          return context.contextHelper
    14  
    15      def __init__(self, context):
    16          self.context = context
    17          self.guuid = str(uuid.uuid1()).replace('-','')
    18  
    19      def getBootrapHelper(self, chainId):
    20          import bootstrap_util
    21          return bootstrap_util.BootstrapHelper(chainId=chainId)
    22  
    23      def getGuuid(self):
    24          return self.guuid
    25  
    26      def getTmpPath(self):
    27          pathToReturn = "tmp"
    28          if not os.path.isdir(pathToReturn):
    29              os.makedirs(pathToReturn)
    30          return pathToReturn
    31  
    32      def getCachePath(self):
    33          pathToReturn = os.path.join(self.getTmpPath(), "cache")
    34          if not os.path.isdir(pathToReturn):
    35              os.makedirs(pathToReturn)
    36          return pathToReturn
    37  
    38  
    39      def getTmpProjectPath(self):
    40          pathToReturn = os.path.join(self.getTmpPath(), self.guuid)
    41          if not os.path.isdir(pathToReturn):
    42              os.makedirs(pathToReturn)
    43          return pathToReturn
    44  
    45      def getTmpPathForName(self, name, extension=None, copyFromCache=False):
    46          'Returns the tmp path for a file, and a flag indicating if the file exists. Will also check in the cache and copy to tmp if copyFromCache==True'
    47          unicodeName = unicode(name)
    48          slugifiedName = ".".join([slugify(unicodeName), extension]) if extension else slugify(unicodeName)
    49          tmpPath = os.path.join(self.getTmpProjectPath(), slugifiedName)
    50          fileExists = False
    51          if os.path.isfile(tmpPath):
    52              # file already exists in tmp path, return path and exists flag
    53              fileExists = True
    54          elif copyFromCache:
    55              # See if the file exists in cache, and copy over to project folder.
    56              cacheFilePath = os.path.join(self.getCachePath(), slugifiedName)
    57              if os.path.isfile(cacheFilePath):
    58                  shutil.copy(cacheFilePath, tmpPath)
    59                  fileExists = True
    60          return (tmpPath, fileExists)
    61  
    62      def copyToCache(self, name):
    63          srcPath, fileExists = self.getTmpPathForName(name, copyFromCache=False)
    64          assert fileExists, "Tried to copy source file to cache, but file not found for: {0}".format(srcPath)
    65          # Now copy to the cache if it does not already exist
    66          cacheFilePath = os.path.join(self.getCachePath(), slugify(name))
    67          if not os.path.isfile(cacheFilePath):
    68              shutil.copy(srcPath, cacheFilePath)
    69  
    70  
    71      def isConfigEnabled(self, configName):
    72          return self.context.config.userdata.get(configName, "false") == "true"
    73  
    74      def before_scenario(self, scenario):
    75          # print("before_scenario: {0}".format(self))
    76          pass
    77  
    78      def after_scenario(self, scenario):
    79          # print("after_scenario: {0}".format(self))
    80          pass
    81  
    82      def before_step(self, step):
    83          # print("before_step: {0}".format(self))
    84          pass
    85  
    86      def after_step(self, step):
    87          # print("after_step: {0}".format(self))
    88          pass
    89  
    90      def registerComposition(self, composition):
    91          return composition
    92