Example #1
0
def unify(source, dest, pace=cfg.PACE[0]):

    global bytes, lins

    # pace counter
    p = 0

    # this will also strip trailing slashes
    source, dest = os.path.abspath(source), os.path.abspath(dest)

    print 'Unifying %s -> %s ... (this will take a while)' % (source, dest)

    # this will prevent some warnings
    os.chdir(cfg.VSERVERS_ROOT)

    #print source, dest

    for root, dirs, files in os.walk(source):

        #        print root, dirs, files

        for file in files + dirs:

            if pace and p >= pace:
                sys.stdout.write('.')
                sys.stdout.flush()
                time.sleep(cfg.PACE[1])
                p = 0
            else:
                p += 1

            src = os.path.join(root, file)

            # reldst is they way it would look inside vserver
            reldst = os.path.join(max(root[len(source):], '/'), file)
            dst = os.path.join(dest, reldst[1:])

            if not os.path.exists(dst) or not os.path.isfile(
                    src) or os.path.islink(src):
                # nothing to do here
                continue


#            print reldst, src, dst

            c, t, s = match_path(reldst)

            # copy/touch/skip?
            if not (c or t or s):

                # config?
                if not is_config(source, reldst):

                    # do they look the same?

                    src_stat = os.lstat(src)
                    dst_stat = os.lstat(dst)

                    if src_stat.st_dev == dst_stat.st_dev and \
                       src_stat.st_ino != dst_stat.st_ino and \
                       src_stat.st_uid == dst_stat.st_uid and \
                       src_stat.st_gid == dst_stat.st_gid and \
                       src_stat.st_size == dst_stat.st_size and \
                       src_stat.st_mtime == dst_stat.st_mtime:

                        # XXX add MD5 (of at least beginning) check here?

                        # flags ok?
                        if vsutil.is_file_immutable_unlink(src):

                            # go for it
                            vsutil.unify(src, dst)
                            bytes += src_stat.st_size
                            lins += 1
                        else:
                            print 'Warning: not unifying %s because it is not iunlink' % src

    print 'Done.'

    print 'Files unified:'.ljust(20), lins
    print 'Bytes saved:'.ljust(20), bytes
Example #2
0
def unify(source, dest, pace=cfg.PACE[0]):

    global bytes, lins

    # pace counter
    p = 0

    # this will also strip trailing slashes
    source, dest = os.path.abspath(source), os.path.abspath(dest)

    print 'Unifying %s -> %s ... (this will take a while)' % (source, dest)

    # this will prevent some warnings
    os.chdir(cfg.VSERVERS_ROOT)
    
    #print source, dest

    for root, dirs, files in os.walk(source):

#        print root, dirs, files

        for file in files + dirs:

            if pace and p >= pace:
                sys.stdout.write('.'); sys.stdout.flush()
                time.sleep(cfg.PACE[1])
                p = 0
            else:
                p += 1

            src = os.path.join(root, file)

            # reldst is they way it would look inside vserver
            reldst = os.path.join(max(root[len(source):], '/'), file)
            dst = os.path.join(dest, reldst[1:])

            if not os.path.exists(dst) or not os.path.isfile(src) or os.path.islink(src):
                # nothing to do here
                continue

#            print reldst, src, dst

            c, t, s = match_path(reldst)

            # copy/touch/skip?
            if not (c or t or s):

                # config?
                if not is_config(source, reldst):

                    # do they look the same?

                    src_stat = os.lstat(src)
                    dst_stat = os.lstat(dst)

                    if src_stat.st_dev == dst_stat.st_dev and \
                       src_stat.st_ino != dst_stat.st_ino and \
                       src_stat.st_uid == dst_stat.st_uid and \
                       src_stat.st_gid == dst_stat.st_gid and \
                       src_stat.st_size == dst_stat.st_size and \
                       src_stat.st_mtime == dst_stat.st_mtime:

                        # XXX add MD5 (of at least beginning) check here?
                    
                        # flags ok?
                        if vsutil.is_file_immutable_unlink(src):

                            # go for it
                            vsutil.unify(src, dst)
                            bytes += src_stat.st_size
                            lins += 1
                        else:
                            print 'Warning: not unifying %s because it is not iunlink' % src

    print 'Done.'

    print 'Files unified:'.ljust(20), lins
    print 'Bytes saved:'.ljust(20), bytes
Example #3
0
def copy(src, dst, link=1, touch=0):
    """Copy a file, a directory or a link.
    When link is 1 (default), regular files will be hardlinked,
    as opposed to being copied. When touch is 1, only the file, but
    not the contents are copied (useful for logfiles).
    """

    global bytes, lins, drs, syms, touchs, copys, devs

    if os.path.islink(src):

        # if it is a symlink, always copy it
        # (no sense in trying to hardlink a symlink)

        if DRYRUN:
            print 'ln -s %s %s' % (os.readlink(src), dst)
        else:
            os.symlink(os.readlink(src), dst)
            copyown(src, dst)
        syms += 1

    elif os.path.isdir(src):

        # directories are also copied always

        if DRYRUN:
            s = os.stat(src)
            print 'mkdir %s; chmod 4%s %s' % (dst, oct(stat.S_IMODE(
                s.st_mode)), dst)
            copyown(src, dst)
            copytime(src, dst)
        else:
            os.mkdir(dst)
            copyown(src, dst)
            shutil.copystat(src, dst)
        drs += 1

    elif os.path.isfile(src):

        # this a file, not a dir or symlink

        if touch:

            # means create a new file and copy perms

            if DRYRUN:
                print 'touch %s' % dst
            else:
                open(dst, 'w')
                copyown(src, dst)
                shutil.copystat(src, dst)

            touchs += 1

        elif link:

            # means we should hardlink

            if DRYRUN:
                print 'ln %s %s' % (src, dst)
            else:
                if vsutil.is_file_immutable_unlink(src):
                    os.link(src, dst)
                    lins += 1
                else:
                    # since it is not iunlink, copy it anyway
                    print 'Warning: not hardlinking %s because it is not iunlink' % src
                    shutil.copy(src, dst)
                    copyown(src, dst)
                    shutil.copystat(src, dst)
                    bytes += os.path.getsize(dst)
                    copys += 1

        else:

            # else copy it

            if DRYRUN:
                print 'cp -a %s %s' % (src, dst)
            else:
                shutil.copy(src, dst)
                copyown(src, dst)
                shutil.copystat(src, dst)
                bytes += os.path.getsize(dst)

            copys += 1

    else:

        # this is a special device?

        s = os.stat(src)
        if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode) \
           or stat.S_ISFIFO(s.st_mode):
            if DRYRUN:
                print "mknod %s %o %02x:%02x" % (
                    dst, s.st_mode, os.major(s.st_rdev), os.minor(s.st_rdev))
            else:
                os.mknod(dst, s.st_mode,
                         os.makedev(os.major(s.st_rdev), os.minor(s.st_rdev)))
                copyown(src, dst)
                shutil.copystat(src, dst)

            devs += 1
Example #4
0
def copy(src, dst, link=1, touch=0):
    """Copy a file, a directory or a link.
    When link is 1 (default), regular files will be hardlinked,
    as opposed to being copied. When touch is 1, only the file, but
    not the contents are copied (useful for logfiles).
    """

    global bytes, lins, drs, syms, touchs, copys, devs
    
    if os.path.islink(src):

        # if it is a symlink, always copy it
        # (no sense in trying to hardlink a symlink)

        if DRYRUN:
            print 'ln -s %s %s' % (os.readlink(src), dst)
        else:
            os.symlink(os.readlink(src), dst)
            copyown(src, dst)
        syms += 1

    elif os.path.isdir(src):

        # directories are also copied always

        if DRYRUN:
            s = os.stat(src)
            print 'mkdir %s; chmod 4%s %s' % (dst, oct(stat.S_IMODE(s.st_mode)), dst)
            copyown(src, dst)
            copytime(src, dst)
        else:
            os.mkdir(dst)
            copyown(src, dst)
            shutil.copystat(src, dst)
        drs += 1

    elif os.path.isfile(src):

        # this a file, not a dir or symlink

        if touch:

            # means create a new file and copy perms
            
            if DRYRUN:
                print 'touch %s' % dst
            else:
                open(dst, 'w')
                copyown(src, dst)
                shutil.copystat(src, dst)

            touchs += 1
            
        elif link:

            # means we should hardlink
            
            if DRYRUN:
                print 'ln %s %s' % (src, dst)
            else:
                if vsutil.is_file_immutable_unlink(src):
                    os.link(src, dst)
                    lins += 1
                else:
                    # since it is not iunlink, copy it anyway
                    print 'Warning: not hardlinking %s because it is not iunlink' % src
                    shutil.copy(src, dst)
                    copyown(src, dst)
                    shutil.copystat(src, dst)
                    bytes += os.path.getsize(dst)
                    copys += 1
            
        else:

            # else copy it

            if DRYRUN:
                print 'cp -a %s %s' % (src, dst)
            else:
                shutil.copy(src, dst)
                copyown(src, dst)
                shutil.copystat(src, dst)
                bytes += os.path.getsize(dst)
                
            copys += 1

    else:

        # this is a special device?

        s = os.stat(src)
        if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode) \
           or stat.S_ISFIFO(s.st_mode):
            if DRYRUN:
                print "mknod %s %o %02x:%02x" % (dst, s.st_mode, os.major(s.st_rdev),
                                                 os.minor(s.st_rdev))
            else:
                os.mknod(dst, s.st_mode, os.makedev(os.major(s.st_rdev),
                                                    os.minor(s.st_rdev)))
                copyown(src, dst)
                shutil.copystat(src, dst)

            devs += 1