Esempio n. 1
0
def loadSource(fileName):
    """
  loads source from fileName,
  we can't use tat function from utrunner, because of we
  store modules in global variable.
  """
    baseName = os.path.basename(fileName)
    moduleName = os.path.splitext(baseName)[0]

    # for users wanted to run simple doctests under django
    #because of django took advantage of module name
    settings_file = os.getenv('DJANGO_SETTINGS_MODULE')
    if settings_file and moduleName == "models":
        baseName = os.path.realpath(fileName)
        moduleName = ".".join((baseName.split(os.sep)[-2], "models"))

    if moduleName in modules:  # add unique number to prevent name collisions
        cnt = 2
        prefix = moduleName
        while getModuleName(prefix, cnt) in modules:
            cnt += 1
        moduleName = getModuleName(prefix, cnt)
    debug("/ Loading " + fileName + " as " + moduleName)
    module = _load_file(moduleName, fileName)
    modules[moduleName] = module
    return module
Esempio n. 2
0
def loadSource(fileName):
  """
  loads source from fileName,
  we can't use tat function from utrunner, because of we
  store modules in global variable.
  """
  baseName = os.path.basename(fileName)
  moduleName = os.path.splitext(baseName)[0]

  # for users wanted to run simple doctests under django
  #because of django took advantage of module name
  settings_file = os.getenv('DJANGO_SETTINGS_MODULE')
  if settings_file and moduleName=="models":
    baseName = os.path.realpath(fileName)
    moduleName = ".".join((baseName.split(os.sep)[-2], "models"))

  if moduleName in modules: # add unique number to prevent name collisions
    cnt = 2
    prefix = moduleName
    while getModuleName(prefix, cnt) in modules:
      cnt += 1
    moduleName = getModuleName(prefix, cnt)
  debug("/ Loading " + fileName + " as " + moduleName)
  module = imp.load_source(moduleName, fileName)
  modules[moduleName] = module
  return module
Esempio n. 3
0
def loadSource(fileName):
    baseName = os.path.basename(fileName)
    moduleName = os.path.splitext(baseName)[0]

    # for users wanted to run unittests under django
    #because of django took advantage of module name
    settings_file = os.getenv('DJANGO_SETTINGS_MODULE')
    if settings_file and moduleName == "models":
        baseName = os.path.realpath(fileName)
        moduleName = ".".join((baseName.split(os.sep)[-2], "models"))

    if moduleName in modules and len(
            sys.argv[1:-1]
    ) == 1:  # add unique number to prevent name collisions
        cnt = 2
        prefix = moduleName
        while getModuleName(prefix, cnt) in modules:
            cnt += 1
        moduleName = getModuleName(prefix, cnt)
    debug("/ Loading " + fileName + " as " + moduleName)
    if os.path.isdir(fileName):
        fileName = fileName + os.path.sep
    module = imp.load_source(moduleName, fileName)
    modules[moduleName] = module
    return module
Esempio n. 4
0
def loadSource(fileName):
    baseName = os.path.basename(fileName)
    moduleName = os.path.splitext(baseName)[0]

    if os.path.isdir(fileName):
        fileName = fileName.rstrip('/\\') + os.path.sep

    # for users wanted to run unittests under django
    # because of django took advantage of module name
    settings_file = os.getenv('DJANGO_SETTINGS_MODULE')

    if settings_file and moduleName == "models":
        baseName = os.path.realpath(fileName)
        moduleName = ".".join((baseName.split(os.sep)[-2], "models"))
    else:
        path = fileName
        for p in roots:
            # Python 2.6+
            try:
                rel_path = os.path.relpath(fileName, start=p)
                if rel_path.find('..') == -1 and len(rel_path) < len(path):
                    path = rel_path
            except:
                pass  # relpath can raise an error in case of different drives for a path and start on Windows

        if path.endswith('.py'):
            path = path[0:-3]

        moduleName = path.replace('/', '.').replace('\\', '.')

    if moduleName in modules and len(
            sys.argv[1:-1]
    ) == 1:  # add unique number to prevent name collisions
        cnt = 2
        prefix = moduleName
        while getModuleName(prefix, cnt) in modules:
            cnt += 1
        moduleName = getModuleName(prefix, cnt)

    debug("/ Loading " + fileName + " as " + moduleName)

    try:
        module = imp.load_source(moduleName, fileName)
    except SystemError:  # probably failed because of the relative imports
        # first we import module with all its parents
        __import__(moduleName)

        # then load it by filename to be sure it is the one we need
        module = imp.load_source(moduleName, fileName)

    modules[moduleName] = module
    return module
def loadSource(fileName):
  baseName = os.path.basename(fileName)
  moduleName = os.path.splitext(baseName)[0]

  if moduleName in modules:
    cnt = 2
    prefix = moduleName
    while getModuleName(prefix, cnt) in modules:
      cnt += 1
    moduleName = getModuleName(prefix, cnt)
  debug("/ Loading " + fileName + " as " + moduleName)
  module = imp.load_source(moduleName, fileName)
  modules[moduleName] = module
  return module
Esempio n. 6
0
def loadSource(fileName):
    baseName = os.path.basename(fileName)
    moduleName = os.path.splitext(baseName)[0]

    if moduleName in modules:
        cnt = 2
        prefix = moduleName
        while getModuleName(prefix, cnt) in modules:
            cnt += 1
        moduleName = getModuleName(prefix, cnt)
    debug("/ Loading " + fileName + " as " + moduleName)
    module = imp.load_source(moduleName, fileName)
    modules[moduleName] = module
    return module
Esempio n. 7
0
def loadSource(fileName):
    baseName = os.path.basename(fileName)
    moduleName = os.path.splitext(baseName)[0]

    if os.path.isdir(fileName):
        fileName = fileName.rstrip("/\\") + os.path.sep

    # for users wanted to run unittests under django
    # because of django took advantage of module name
    settings_file = os.getenv("DJANGO_SETTINGS_MODULE")

    if settings_file and moduleName == "models":
        baseName = os.path.realpath(fileName)
        moduleName = ".".join((baseName.split(os.sep)[-2], "models"))
    else:
        path = fileName
        for p in roots:
            # Python 2.6+
            try:
                rel_path = os.path.relpath(fileName, start=p)
                if rel_path.find("..") == -1 and len(rel_path) < len(path):
                    path = rel_path
            except:
                pass  # relpath can raise an error in case of different drives for a path and start on Windows

        if path.endswith(".py"):
            path = path[0:-3]

        moduleName = path.replace("/", ".").replace("\\", ".")

    if moduleName in modules and len(sys.argv[1:-1]) == 1:  # add unique number to prevent name collisions
        cnt = 2
        prefix = moduleName
        while getModuleName(prefix, cnt) in modules:
            cnt += 1
        moduleName = getModuleName(prefix, cnt)

    debug("/ Loading " + fileName + " as " + moduleName)

    try:
        module = imp.load_source(moduleName, fileName)
    except SystemError:  # probably failed because of the relative imports
        # first we import module with all its parents
        __import__(moduleName)

        # then load it by filename to be sure it is the one we need
        module = imp.load_source(moduleName, fileName)

    modules[moduleName] = module
    return module
Esempio n. 8
0
def loadSource(fileName):
  baseName = os.path.basename(fileName)
  moduleName = os.path.splitext(baseName)[0]

  # for users wanted to run unittests under django
  #because of django took advantage of module name
  settings_file = os.getenv('DJANGO_SETTINGS_MODULE')
  if settings_file and moduleName == "models":
    baseName = os.path.realpath(fileName)
    moduleName = ".".join((baseName.split(os.sep)[-2], "models"))

  if moduleName in modules and len(sys.argv[1:-1]) == 1: # add unique number to prevent name collisions
    cnt = 2
    prefix = moduleName
    while getModuleName(prefix, cnt) in modules:
      cnt += 1
    moduleName = getModuleName(prefix, cnt)
  debug("/ Loading " + fileName + " as " + moduleName)
  module = imp.load_source(moduleName, fileName)
  modules[moduleName] = module
  return module