github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/bddtests/steps/contexthelper.py (about)

     1  # Copyright IBM Corp. 2017 All Rights Reserved.
     2  #
     3  # SPDX-License-Identifier: Apache-2.0
     4  #
     5  
     6  import uuid
     7  import os
     8  import shutil
     9  from slugify import slugify
    10  
    11  
    12  class Context(object):
    13      def __init__(self):
    14          pass
    15  
    16      def __setattr__(self, attr, value):
    17          self.__dict__[attr] = value
    18  
    19      def __getattr__(self, attr):
    20          return self.__dict__[attr]
    21          # raise AttributeError(e)
    22  
    23      def __getstate__(self):
    24          return self.__dict__
    25  
    26      def __setstate__(self, value):
    27          return self.__dict__.update(value)
    28  
    29      def __contains__(self, attr):
    30          return attr in self.__dict__
    31  
    32  class ContextHelper:
    33  
    34      @classmethod
    35      def GetHelper(cls, context):
    36          if not "contextHelper" in context:
    37              context.contextHelper = ContextHelper(context)
    38          return context.contextHelper
    39  
    40      def __init__(self, context):
    41          self.context = context
    42          self.guuid = str(uuid.uuid1()).replace('-','')
    43  
    44      def getBootrapHelper(self, chainId):
    45          import bootstrap_util
    46          return bootstrap_util.BootstrapHelper(chainId=chainId)
    47  
    48      def getGuuid(self):
    49          return self.guuid
    50  
    51      def getTmpPath(self):
    52          pathToReturn = "tmp"
    53          if not os.path.isdir(pathToReturn):
    54              os.makedirs(pathToReturn)
    55          return pathToReturn
    56  
    57      def getCachePath(self):
    58          pathToReturn = os.path.join(self.getTmpPath(), "cache")
    59          if not os.path.isdir(pathToReturn):
    60              os.makedirs(pathToReturn)
    61          return pathToReturn
    62  
    63  
    64      def getTmpProjectPath(self):
    65          pathToReturn = os.path.join(self.getTmpPath(), self.guuid)
    66          if not os.path.isdir(pathToReturn):
    67              os.makedirs(pathToReturn)
    68          return pathToReturn
    69  
    70      def getTmpPathForName(self, name, extension=None, copyFromCache=False, path_relative_to_tmp=''):
    71          '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'
    72          unicodeName = unicode(name)
    73          dir_path = os.path.join(self.getTmpProjectPath(), path_relative_to_tmp)
    74          if not os.path.isdir(dir_path):
    75              os.makedirs(dir_path)
    76          slugifiedName = ".".join([slugify(unicodeName), extension]) if extension else slugify(unicodeName)
    77          tmpPath = os.path.join(dir_path, slugifiedName)
    78          fileExists = False
    79          if os.path.isfile(tmpPath):
    80              # file already exists in tmp path, return path and exists flag
    81              fileExists = True
    82          elif copyFromCache:
    83              # See if the file exists in cache, and copy over to project folder.
    84              cacheFilePath = os.path.join(self.getCachePath(), slugifiedName)
    85              if os.path.isfile(cacheFilePath):
    86                  shutil.copy(cacheFilePath, tmpPath)
    87                  fileExists = True
    88          return (tmpPath, fileExists)
    89  
    90      def copyToCache(self, name):
    91          srcPath, fileExists = self.getTmpPathForName(name, copyFromCache=False)
    92          assert fileExists, "Tried to copy source file to cache, but file not found for: {0}".format(srcPath)
    93          # Now copy to the cache if it does not already exist
    94          cacheFilePath = os.path.join(self.getCachePath(), slugify(name))
    95          if not os.path.isfile(cacheFilePath):
    96              shutil.copy(srcPath, cacheFilePath)
    97  
    98  
    99      def isConfigEnabled(self, configName):
   100          return self.context.config.userdata.get(configName, "false") == "true"
   101  
   102      def before_scenario(self, scenario):
   103          # print("before_scenario: {0}".format(self))
   104          pass
   105  
   106      def after_scenario(self, scenario):
   107          # print("after_scenario: {0}".format(self))
   108          pass
   109  
   110      def before_step(self, step):
   111          # print("before_step: {0}".format(self))
   112          pass
   113  
   114      def after_step(self, step):
   115          # print("after_step: {0}".format(self))
   116          pass
   117  
   118      def registerComposition(self, composition):
   119          return composition
   120