Esempio n. 1
0
def adrm(dirs, options, excludes1 = [], tn = ""):
    # Get a list of all files inside the directory
    files = listdir(dirs, {"recurse": True, "dirs": True, "symlinks": False}, tn)
    excludes = []
    # Exclude the files listed to exclude
    if options["excludes"] and len(excludes1) > 0:
        excludes = exclude(files, excludes1)
    # Remove the wanted files
    for file_ in files:
        file__ = utilities.utf8(file_)
        file_ = utilities.utf8(os.path.basename(file__))
        # Make sure we don't remove files that are listed to exclude from removal
        if file__ in excludes:
            logger.logVV(tn, logger.I, utilities.utf8all(file_, " ",
                                                         _("is to be excluded. Skipping a CPU cycle")))
            continue
        fullpath = file__
        dfile = delink(fullpath)
        if dfile == None:
            if os.path.isfile(fullpath):
                rm(fullpath)
            elif os.path.isdir(fullpath) and options["remdirs"]:
                rm(fullpath)
        else:
            if options["remsymlink"]:
                logger.logVV(tn, logger.I, utilities.utf8all(_("Removing symlink"), " ", fullpath))
                rm(fullpath)
            if options["remfullpath"]:
                logger.logVV(tn, logger.I, utilities.utf8all(_("Removing"), " ", dfile, " (",
                                                   _("directed by symlink"), fullpath, ")"))
                rm(dfile)
    if options["remdirs"] is True:
        logger.logVV(tn, logger.I, utilities.utf8all(_("Removing source directory"), " ", dirs))
        rm(dirs)
Esempio n. 2
0
def fscopy(src, dst, excludes1, tn=""):
    src1 = re.sub(r"/+$", "", src)
    src = src1
    # Get a list of all files
    files = listdir(src, {
        "recurse": True,
        "dirs": True,
        "symlinks": False
    }, tn)
    # Exclude the files that are not wanted
    excludes = []
    if len(excludes1) > 0:
        excludes = exclude(files, excludes1)
    makedir(dst)
    # Copy the files
    for file__ in files:
        file_ = utilities.utf8(os.path.basename(utilities.utf8(file__)))
        # Make sure we don't copy files that are supposed to be excluded
        if file_ in excludes:
            #logger.logVV(tn, logger.W, utilities.utf8all(file_, " ", _("is to be excluded. Skipping a CPU cycle")))
            continue
        fullpath = utilities.utf8(file__)
        #print(dst + " " + file__[len(src):])
        temp = re.sub(r"^/+", "", file__[len(src):])
        newpath = utilities.utf8(os.path.join(dst, temp))
        # Save some valuable time
        if os.path.exists(newpath):
            fpmd5 = genMD5(fullpath)
            npmd5 = genMD5(newpath)
            if fpmd5 == npmd5:
                continue
        dfile = delink(fullpath)
        if dfile is not None:
            logger.logVV(
                tn, logger.I,
                utilities.utf8all(
                    file_, " ",
                    _("is a symlink. Creating an identical symlink at"), " ",
                    newpath))
            symlink(dfile, newpath)
        elif os.path.isdir(fullpath):
            logger.logVV(
                tn, logger.I,
                utilities.utf8all(_("Creating directory"), " ", file_))
            makedir(newpath)
            logger.logVV(tn, logger.I, _("Setting permissions"))
            copystat(fullpath, newpath)
        else:
            logger.logVV(
                tn, logger.I,
                utilities.utf8all(_("Copying"), " ", fullpath, " ", _("to"),
                                  " ", newpath))
            shutil.copy2(fullpath, newpath)
    logger.logVV(tn, logger.I, _("Setting permissions"))
    copystat(src, dst)
Esempio n. 3
0
def adrm(dirs, **options):
    utilities.setDefault(options,
                         excludes=[],
                         remdirs=True,
                         remsymlink=True,
                         remfullpath=False,
                         remoriginal=True,
                         tn="",
                         progressfunc=None)
    # Get a list of all files inside the directory
    files = list(
        listdir(dirs,
                recurse=True,
                dirs=True,
                symlinks=False,
                tn=options["tn"]))
    excludes = []
    # Exclude the files listed to exclude
    if len(options["excludes"]) > 0:
        excludes = exclude(files, options["excludes"])
    c = 0
    l = len(files)
    # Remove the wanted files
    for file_ in files:
        file__ = utilities.utf8(file_)
        file_ = utilities.utf8(os.path.basename(file__))
        if options["progressfunc"]:
            options["progressfunc"](utilities.calcPercent(c, l))
        c += 1
        # Make sure we don't remove files that are listed to exclude from removal
        if file__ in excludes:
            #logger.logVV(tn, logger.I, utilities.utf8all(file_, " ",
            #                                             _("is to be excluded. Skipping a CPU cycle")))
            continue
        fullpath = file__
        dfile = delink(fullpath)
        if dfile is None:
            if os.path.isfile(fullpath):
                rm(fullpath)
            elif os.path.isdir(fullpath) and options["remdirs"]:
                rm(fullpath)
        else:
            if options["remsymlink"]:
                if (os.path.isdir(fullpath)
                        and options["remdirs"]) or os.path.isfile(fullpath):
                    #logger.logVV(tn, logger.I, utilities.utf8all(_("Removing symlink"), " ", fullpath))
                    rm(fullpath)
            if options["remfullpath"]:
                #logger.logVV(tn, logger.I, utilities.utf8all(_("Removing"), " ", dfile, " (",
                #                                   _("directed by symlink"), fullpath, ")"))
                rm(dfile)
    if options["remdirs"] and options["remoriginal"]:
        #logger.logVV(tn, logger.I, utilities.utf8all(_("Removing source directory"), " ", dirs))
        rm(dirs)
Esempio n. 4
0
def adrm(dirs, **options):
    utilities.setDefault(
        options,
        excludes=[],
        remdirs=True,
        remsymlink=True,
        remfullpath=False,
        remoriginal=True,
        tn="",
        progressfunc=None,
    )
    # Get a list of all files inside the directory
    files = list(listdir(dirs, recurse=True, dirs=True, symlinks=False, tn=options["tn"]))
    excludes = []
    # Exclude the files listed to exclude
    if len(options["excludes"]) > 0:
        excludes = exclude(files, options["excludes"])
    c = 0
    l = len(files)
    # Remove the wanted files
    for file_ in files:
        file__ = utilities.utf8(file_)
        file_ = utilities.utf8(os.path.basename(file__))
        if options["progressfunc"]:
            options["progressfunc"](utilities.calcPercent(c, l))
        c += 1
        # Make sure we don't remove files that are listed to exclude from removal
        if file__ in excludes:
            # logger.logVV(tn, logger.I, utilities.utf8all(file_, " ",
            #                                             _("is to be excluded. Skipping a CPU cycle")))
            continue
        fullpath = file__
        dfile = delink(fullpath)
        if dfile is None:
            if os.path.isfile(fullpath):
                rm(fullpath)
            elif os.path.isdir(fullpath) and options["remdirs"]:
                rm(fullpath)
        else:
            if options["remsymlink"]:
                if (os.path.isdir(fullpath) and options["remdirs"]) or os.path.isfile(fullpath):
                    # logger.logVV(tn, logger.I, utilities.utf8all(_("Removing symlink"), " ", fullpath))
                    rm(fullpath)
            if options["remfullpath"]:
                # logger.logVV(tn, logger.I, utilities.utf8all(_("Removing"), " ", dfile, " (",
                #                                   _("directed by symlink"), fullpath, ")"))
                rm(dfile)
    if options["remdirs"] and options["remoriginal"]:
        # logger.logVV(tn, logger.I, utilities.utf8all(_("Removing source directory"), " ", dirs))
        rm(dirs)
Esempio n. 5
0
def listdir(x, **options):
    utilities.setDefault(options, recurse=True, dirs=True, symlinks=False, tn="")
    if os.path.isdir(x):
        if options["dirs"]:
            yield utilities.utf8(x)
        for i in os.listdir(x):
            f = utilities.utf8(os.path.join(x, i))
            if os.path.isdir(f):
                if (os.path.islink(f) and not options["symlinks"]) or (not options["recurse"] and options["dirs"]):
                    yield f
                    continue
                for y in listdir(f, **options):
                    yield y
            else:
                yield f
Esempio n. 6
0
def delink(files, absolutify=True, recursive=False):
    if os.path.exists(files) and os.path.islink(files):
        link = ""
        if recursive:
            notfound = True
            while notfound:
                link = delink(files, True, False)
                notfound = os.path.islink(link)
        else:
            link_ = utilities.utf8(os.readlink(files))
            if absolutify:
                link = utilities.utf8(abspath(link_, os.path.dirname(files)))
            else:
                link = link_
        return link
    return None
Esempio n. 7
0
def delink(files, absolutify=True, recursive=False):
    if os.path.exists(files) and os.path.islink(files):
        link = ""
        if recursive:
            notfound = True
            while notfound:
                link = delink(files, True, False)
                notfound = os.path.islink(link)
        else:
            link_ = utilities.utf8(os.readlink(files))
            if absolutify:
                link = utilities.utf8(abspath(link_, os.path.dirname(files)))
            else:
                link = link_
        return link
    return None
Esempio n. 8
0
def adrm(dirs, options, excludes1=[], tn=""):
    # Get a list of all files inside the directory
    files = listdir(dirs, {
        "recurse": True,
        "dirs": True,
        "symlinks": False
    }, tn)
    excludes = []
    # Exclude the files listed to exclude
    if options["excludes"] and len(excludes1) > 0:
        excludes = exclude(files, excludes1)
    # Remove the wanted files
    for file_ in files:
        file__ = utilities.utf8(file_)
        file_ = utilities.utf8(os.path.basename(file__))
        # Make sure we don't remove files that are listed to exclude from removal
        if file__ in excludes:
            logger.logVV(
                tn, logger.I,
                utilities.utf8all(
                    file_, " ", _("is to be excluded. Skipping a CPU cycle")))
            continue
        fullpath = file__
        dfile = delink(fullpath)
        if dfile == None:
            if os.path.isfile(fullpath):
                rm(fullpath)
            elif os.path.isdir(fullpath) and options["remdirs"]:
                rm(fullpath)
        else:
            if options["remsymlink"]:
                logger.logVV(
                    tn, logger.I,
                    utilities.utf8all(_("Removing symlink"), " ", fullpath))
                rm(fullpath)
            if options["remfullpath"]:
                logger.logVV(
                    tn, logger.I,
                    utilities.utf8all(_("Removing"), " ", dfile, " (",
                                      _("directed by symlink"), fullpath, ")"))
                rm(dfile)
    if options["remdirs"] is True:
        logger.logVV(
            tn, logger.I,
            utilities.utf8all(_("Removing source directory"), " ", dirs))
        rm(dirs)
Esempio n. 9
0
def fscopy(src, dst, excludes1, tn = ""):
    src1 = re.sub(r"/+$", "", src)
    src = src1
    # Get a list of all files
    files = listdir(src, {"recurse": True, "dirs": True, "symlinks": False}, tn)
    # Exclude the files that are not wanted
    excludes = []
    if len(excludes1) > 0:
        excludes = exclude(files, excludes1)
    makedir(dst)
    # Copy the files
    for file__ in files:
        file_ = utilities.utf8(os.path.basename(utilities.utf8(file__)))
        # Make sure we don't copy files that are supposed to be excluded
        if file_ in excludes:
            #logger.logVV(tn, logger.W, utilities.utf8all(file_, " ", _("is to be excluded. Skipping a CPU cycle")))
            continue
        fullpath = utilities.utf8(file__)
        #print(dst + " " + file__[len(src):])
        temp = re.sub(r"^/+", "", file__[len(src):])
        newpath = utilities.utf8(os.path.join(dst, temp))
        # Save some valuable time
        if os.path.exists(newpath):
            fpmd5 = genMD5(fullpath)
            npmd5 = genMD5(newpath)
            if fpmd5 == npmd5:
                continue
        dfile = delink(fullpath)
        if dfile is not None:
            logger.logVV(tn, logger.I, utilities.utf8all(file_, " ",
                                            _("is a symlink. Creating an identical symlink at"), " ",
                                            newpath))
            symlink(dfile, newpath)
        elif os.path.isdir(fullpath):
            logger.logVV(tn, logger.I, utilities.utf8all(_("Creating directory"), " ", file_))
            makedir(newpath)
            logger.logVV(tn, logger.I, _("Setting permissions"))
            copystat(fullpath, newpath)
        else:
            logger.logVV(tn, logger.I, utilities.utf8all(_("Copying"), " ", fullpath, " ", _("to"), " ", newpath))
            shutil.copy2(fullpath, newpath)
    logger.logVV(tn, logger.I, _("Setting permissions"))
    copystat(src, dst)
Esempio n. 10
0
def listdir(x, **options):
    utilities.setDefault(options,
                         recurse=True,
                         dirs=True,
                         symlinks=False,
                         tn="")
    if os.path.isdir(x):
        if options["dirs"]:
            yield utilities.utf8(x)
        for i in os.listdir(x):
            f = utilities.utf8(os.path.join(x, i))
            if os.path.isdir(f):
                if ((os.path.islink(f) and not options["symlinks"])
                        or (not options["recurse"] and options["dirs"])):
                    yield f
                    continue
                for y in listdir(f, **options):
                    yield y
            else:
                yield f
Esempio n. 11
0
def writeAll(status, lists, tn, importance, text, **options):
    if tn == "" or tn is None or not status:
        return
    text_ = "[" + tn + "] "
    if importance == E:
        text_ += MError
    elif importance == W:
        text_ += MWarning
    elif importance == D:
        text_ += MDebug
        from relinux import configutils
        if not configutils.getValue(config.Configuration["Relinux"]["DEBUG"]):
            return
    else:
        text_ += ""
    text__ = text_ + text
    text = text__
    for i in lists:
        if i in config.TermStreams and "noterm" in options and options[
                "noterm"]:
            continue
        if i == config.GUIStream and "nogui" in options and options["nogui"]:
            continue
        text_ = copy.copy(text)
        if i in config.TermStreams:
            text__ = "\033["
            if importance == E:
                text__ += str(config.TermRed)
            elif importance == W:
                text__ += str(config.TermYellow)
            elif importance == D:
                text__ += str(config.TermGreen)
            '''elif importance == I:
                text__ += config.TermBlue'''
            text__ += "m" + text_ + "\033[" + str(config.TermReset) + "m"
            text_ = text__
        i.write(utilities.utf8(text_ + MNewline))
Esempio n. 12
0
def writeAll(status, lists, tn, importance, text, **options):
    if tn == "" or tn is None or not status:
        return
    text_ = "[" + tn + "] "
    if importance == E:
        text_ += MError
    elif importance == W:
        text_ += MWarning
    elif importance == D:
        text_ += MDebug
        from relinux import configutils
        if not configutils.getValue(config.Configuration["Relinux"]["DEBUG"]):
            return
    else:
        text_ += ""
    text__ = text_ + text
    text = text__
    for i in lists:
        if i in config.TermStreams and "noterm" in options and options["noterm"]:
            continue
        if i == config.GUIStream and "nogui" in options and options["nogui"]:
            continue
        text_ = copy.copy(text)
        if i in config.TermStreams:
            text__ = "\033["
            if importance == E:
                text__ += str(config.TermRed)
            elif importance == W:
                text__ += str(config.TermYellow)
            elif importance == D:
                text__ += str(config.TermGreen)
            '''elif importance == I:
                text__ += config.TermBlue'''
            text__ += "m" + text_ + "\033[" + str(config.TermReset) + "m"
            text_ = text__
        i.write(utilities.utf8(text_ + MNewline))
Esempio n. 13
0
def listdir(dirs, options = {}, tn = ""):
    utilities.setDefault(options, recurse = True, dirs = True, symlinks = False)
    logger.logV(tn, logger.I, utilities.utf8all(_("Gathering a list of files in"), " ", dirs))
    listed = []
    if options["recurse"]:
        listed = os.walk(utilities.utf8(dirs), True, None, options["symlinks"])
    else:
        listed = os.listdir(utilities.utf8(dirs))
    returnme = []
    for i in listed:
        if options["dirs"]:
            if options["recurse"]:
                returnme.append(utilities.utf8(i[0]))
            elif os.path.isdir(i):
                returnme.append(utilities.utf8(i))
        if options["recurse"]:
            for x in i[2]:
                returnme.append(utilities.utf8(os.path.join(i[0], x)))
        elif os.path.isfile(i) or os.path.islink(i):
            returnme.append(utilities.utf8(i))
    return returnme
Esempio n. 14
0
def listdir(dirs, options={}, tn=""):
    utilities.setDefault(options, recurse=True, dirs=True, symlinks=False)
    logger.logV(
        tn, logger.I,
        utilities.utf8all(_("Gathering a list of files in"), " ", dirs))
    listed = []
    if options["recurse"]:
        listed = os.walk(utilities.utf8(dirs), True, None, options["symlinks"])
    else:
        listed = os.listdir(utilities.utf8(dirs))
    returnme = []
    for i in listed:
        if options["dirs"]:
            if options["recurse"]:
                returnme.append(utilities.utf8(i[0]))
            elif os.path.isdir(i):
                returnme.append(utilities.utf8(i))
        if options["recurse"]:
            for x in i[2]:
                returnme.append(utilities.utf8(os.path.join(i[0], x)))
        elif os.path.isfile(i) or os.path.islink(i):
            returnme.append(utilities.utf8(i))
    return returnme
Esempio n. 15
0
def delink(files):
    if os.path.islink(files):
        return utilities.utf8(os.readlink(files))
    return None
Esempio n. 16
0
def fscopy(src, dst, excludes1, tn="", **options):
    utilities.setDefault(options, progressfunc=None)
    src1 = re.sub(r"/+$", "", src)
    src = src1
    dst1 = re.sub(r"/+$", "", dst)
    dst = dst1
    dstp = re.sub(r"/+$", "", os.path.dirname(dst))
    # Get a list of all files
    files = list(listdir(src, tn=tn))
    # Get the length of the file list
    lfiles = len(files)
    # Exclude the files that are not wanted
    excludes = []
    if len(excludes1) > 0:
        excludes = exclude(files, excludes1)
    makedir(dst)
    c = 0
    # Copy the files
    for file___ in files:
        if options["progressfunc"]:
            options["progressfunc"](utilities.calcPercent(c, lfiles))
        c += 1
        file__ = utilities.utf8(os.path.abspath(file___))
        file_ = utilities.utf8(os.path.basename(utilities.utf8(file__)))
        # Make sure we don't copy files that are supposed to be excluded
        if file_ in excludes:
            # logger.logVV(tn, logger.W, utilities.utf8all(file_, " ", _("is to be excluded. Skipping a CPU cycle")))
            continue
        fullpath = utilities.utf8(file__)
        # print(dst + " " + file__[len(src):])
        temp = re.sub(r"^/+", "", file__[len(src) :])
        newpath = utilities.utf8(os.path.join(dst, temp))
        if not os.path.exists(fullpath):
            # Either an error on fsutil's part, or the file got deleted
            continue
        # Save some valuable time
        if os.path.exists(newpath):
            fpmd5 = genMD5(fullpath)
            npmd5 = genMD5(newpath)
            if fpmd5 == npmd5:
                continue
        dfile = delink(fullpath, False)
        if dfile is not None:
            """logger.logVV(tn, logger.D, utilities.utf8all(file_, " ",
                                            _("is a symlink. Creating an identical symlink at"), " ",
                                            newpath))
            logger.logI(
                tn, logger.D, utilities.utf8all("ORIGINAL ", dfile, "NEW ",
                                                        os.path.relpath(dfile, fullpath)))
            symlink(os.path.normpath("/" +
                                    abspath(
                                        os.path.relpath(
                                            dfile, fullpath), newpath)[len(
                                                dstp):]),
                                newpath)"""
            symlink(dfile, newpath)
        elif os.path.isdir(fullpath):
            # logger.logVV(tn, logger.I, utilities.utf8all(_("Creating directory"), " ", file_))
            makedir(newpath)
            # logger.logVV(tn, logger.I, _("Setting permissions"))
            copystat(fullpath, newpath)
        else:
            # logger.logVV(tn, logger.I, utilities.utf8all(_("Copying"), " ", fullpath, " ", _("to"), " ", newpath))
            try:
                shutil.copy2(fullpath, newpath)
            except Exception as e:
                print e
    # logger.logVV(tn, logger.I, _("Setting permissions"))
    copystat(src, dst)
Esempio n. 17
0
def delink(files):
    if os.path.islink(files):
        return utilities.utf8(os.readlink(files))
    return None
Esempio n. 18
0
def fscopy(src, dst, excludes1, tn="", **options):
    utilities.setDefault(options, progressfunc=None)
    src1 = re.sub(r"/+$", "", src)
    src = src1
    dst1 = re.sub(r"/+$", "", dst)
    dst = dst1
    dstp = re.sub(r"/+$", "", os.path.dirname(dst))
    # Get a list of all files
    files = list(listdir(src, tn=tn))
    # Get the length of the file list
    lfiles = len(files)
    # Exclude the files that are not wanted
    excludes = []
    if len(excludes1) > 0:
        excludes = exclude(files, excludes1)
    makedir(dst)
    c = 0
    # Copy the files
    for file___ in files:
        if options["progressfunc"]:
            options["progressfunc"](utilities.calcPercent(c, lfiles))
        c += 1
        file__ = utilities.utf8(os.path.abspath(file___))
        file_ = utilities.utf8(os.path.basename(utilities.utf8(file__)))
        # Make sure we don't copy files that are supposed to be excluded
        if file_ in excludes:
            #logger.logVV(tn, logger.W, utilities.utf8all(file_, " ", _("is to be excluded. Skipping a CPU cycle")))
            continue
        fullpath = utilities.utf8(file__)
        #print(dst + " " + file__[len(src):])
        temp = re.sub(r"^/+", "", file__[len(src):])
        newpath = utilities.utf8(os.path.join(dst, temp))
        if not os.path.exists(fullpath):
            # Either an error on fsutil's part, or the file got deleted
            continue
        # Save some valuable time
        if os.path.exists(newpath):
            fpmd5 = genMD5(fullpath)
            npmd5 = genMD5(newpath)
            if fpmd5 == npmd5:
                continue
        dfile = delink(fullpath, False)
        if dfile is not None:
            '''logger.logVV(tn, logger.D, utilities.utf8all(file_, " ",
                                            _("is a symlink. Creating an identical symlink at"), " ",
                                            newpath))
            logger.logI(
                tn, logger.D, utilities.utf8all("ORIGINAL ", dfile, "NEW ",
                                                        os.path.relpath(dfile, fullpath)))
            symlink(os.path.normpath("/" +
                                    abspath(
                                        os.path.relpath(
                                            dfile, fullpath), newpath)[len(
                                                dstp):]),
                                newpath)'''
            symlink(dfile, newpath)
        elif os.path.isdir(fullpath):
            #logger.logVV(tn, logger.I, utilities.utf8all(_("Creating directory"), " ", file_))
            makedir(newpath)
            #logger.logVV(tn, logger.I, _("Setting permissions"))
            copystat(fullpath, newpath)
        else:
            #logger.logVV(tn, logger.I, utilities.utf8all(_("Copying"), " ", fullpath, " ", _("to"), " ", newpath))
            try:
                shutil.copy2(fullpath, newpath)
            except Exception as e:
                print e
    #logger.logVV(tn, logger.I, _("Setting permissions"))
    copystat(src, dst)