Ejemplo n.º 1
0
def import_path(path, name=None, dont_write_bytecode=None):
    _dont_write_bytecode = sys.dont_write_bytecode
    try:
        path = str(path)
        if name:
            name = str(name)
        assert_exists(path)
        if not name:
            name = re.sub("[^a-zA-Z0-9]", '_', path)
            if os.path.isfile(path):
                name = name.replace(".pyc", "").replace(".py", "")
        if name in ["__init__.py", "__init__.pyc"]:
            name = os.path.basename(os.path.dirname(path))
        # if exists(path):
            # if isdir(path):
            # fullname = basename(path)
            # path = dirname(path)
        if dont_write_bytecode is None:
            dont_write_bytecode = _dont_write_bytecode
        sys.dont_write_bytecode = dont_write_bytecode
        warnings.simplefilter("ignore", RuntimeWarning)
        warnings.simplefilter("ignore", ImportWarning)
        ext = os.path.splitext(path)[1]
        if os.path.isfile(path) and ext not in [".py", ".pyc"]:
            mod = imp.load_source(name, path)
        else:
            mod = load_module(name, path)
        return mod
    finally:
        sys.dont_write_bytecode = _dont_write_bytecode
Ejemplo n.º 2
0
def cp(source, target, force=True):
    if isinstance(source, list):  # list
        # copy files to dir
        targets = []
        for s in source:
            t = cp(s, target, force)
            targets.append(t)
        return targets
    assert_exists(source)  # assert exists
    if not force and os.path.exists(target):
        return
    if source == target:
        return target
    if os.path.isfile(source) and os.path.isdir(target):
        # target is DIR
        target = os.path.join(target, os.path.basename(source))
    if os.path.isfile(source) or os.path.islink(source):
        if (os.path.exists(target) or os.path.lexists(target)):
            if os.path.isfile(source) != os.path.isfile(target):
                os.unlink(target)
        shutil.copy(source, target)
    if os.path.isdir(source):
        # first create dirs
        if not os.path.exists(target):
            os.makedirs(target)
        dir_util.copy_tree(source, target)
    return target
Ejemplo n.º 3
0
def comment_get(path):
    assert_exists(path)
    try:
        comment = xattr.getxattr(path, kMDItemFinderComment)
        # str/bytes by default
        return comment.decode("utf-8")  # convert to unicode
    except:
        pass
Ejemplo n.º 4
0
 def __init__(self, path, parser=None, autosave=True):
     self.path = fullpath(path)
     self.autosave = autosave
     if not parser:
         parser = RawConfigParser()
         path = fullpath(path)
         assert_exists(path)
         parser.read(path)
     self._parser = parser
Ejemplo n.º 5
0
def comment_set(path, comment=None):
    assert_exists(path)
    if comment is None:
        xattr.removexattr(path, kMDItemFinderComment)
        return
    old = comment_get(path)
    if comment != old:
        if comment is not None and hasattr(comment, "encode"):
            comment = comment.encode("utf-8")  # str/bytes required
        xattr.setxattr(path, kMDItemFinderComment, comment)
Ejemplo n.º 6
0
def dirpath(path):
    """return directory path
    todo
    """
    if path is None:
        return None
    path = fullpath(path)
    if not cached(isdir)(path):
        assert_exists(path)
        path = dirname(path)
    return path
def ispythonsource(path):
    if path is None:
        return False
    assert_exists(path)
    if splitext(path)[1] == ".py":
        return True
    binary = isbinaryfile(path)
    if not binary:
        _shebang = shebang(path)
        if not _shebang:
            return False
        return "python" in _shebang
    return False
def ispythoncompiled(path):
    """return True if file compiled"""
    if not path:
        return False
    if os.path.splitext(path)[1] == ".py":
        return False
    if path[-1] != "c":
        return False
    if os.path.splitext(path)[1] == ".pyc":
        return True
    assert_exists(path)
    try:
        code(path)
        return True
    except Exception:
        return False
Ejemplo n.º 9
0
def load_module(name, path=None):
    """imp.load_module replacement
    todo: isfile, isdir
    """
    if name:
        name = str(name)
    asname = name
    if name in sys.modules.keys():  # already loaded
        return sys.modules[name]
    if path:
        if isinstance(path, list):
            pass
            # modpath = path
        else:
            path = str(path)
            assert_exists(path)
            if os.path.isdir(path):
                path = [path]
            if not isinstance(path, list) and os.path.isfile(path):
                asname = os.path.basename(path).replace(".py", "")
                path = [os.path.dirname(path)]
    else:
        path = [os.getcwd()]
    # path = [] # python2 default
    # path = None # python3

    try:
        t = imp.find_module(asname, path)
    except Exception:
        return
    warnings.simplefilter("ignore", RuntimeWarning)
    warnings.simplefilter("ignore", ImportWarning)
    syspath = sys.path
    if path:
        sys.path += tolist(path)
    module = imp.load_module(name, *t)
    sys.path = syspath
    return module