| | |
| |
|
| | |
| | import os, sys, re |
| |
|
| | verbose = 0 |
| | dcount = fcount = 0 |
| | maxfileload = 100000 |
| | blksize = 1024 * 8 |
| |
|
| |
|
| | def cpfile(pathFrom, pathTo, maxfileload=maxfileload): |
| | """ |
| | copy file pathFrom to pathTo, byte for byte |
| | """ |
| | if os.path.getsize(pathFrom) <= maxfileload: |
| | bytesFrom = open(pathFrom, "rb").read() |
| | bytesTo = open(pathTo, "wb") |
| | bytesTo.write(bytesFrom) |
| | |
| | |
| | else: |
| | fileFrom = open(pathFrom, "rb") |
| | fileTo = open(pathTo, "wb") |
| | while 1: |
| | bytesFrom = fileFrom.read(blksize) |
| | if not bytesFrom: |
| | break |
| | fileTo.write(bytesFrom) |
| | |
| | |
| |
|
| |
|
| | def cpall(dirFrom, dirTo): |
| | """ |
| | copy contents of dirFrom and below to dirTo |
| | """ |
| | global dcount, fcount |
| | for file in os.listdir(dirFrom): |
| | print(file) |
| | pathFrom = os.path.join(dirFrom, file) |
| | pathTo = os.path.join(dirTo, file) |
| | if not os.path.isdir(pathFrom): |
| | try: |
| | if verbose > 1: |
| | print("copying ", pathFrom, " to ", pathTo) |
| | cpfile(pathFrom, pathTo) |
| | fcount = fcount + 1 |
| | except Exception: |
| | print("Error copying ", pathFrom, " to ", pathTo, " -- skipped") |
| | print(sys.exc_info()[0], sys.exc_info()[1]) |
| | else: |
| | if verbose: |
| | print("copying dir ", pathFrom, " to ", pathTo) |
| | try: |
| | os.mkdir(pathTo) |
| | cpall(pathFrom, pathTo) |
| | dcount = dcount + 1 |
| | except Exception: |
| | print("Error creating ", pathTo, " -- skipped") |
| | print(sys.exc_info()[0], sys.exc_info()[1]) |
| |
|
| |
|
| | def SetUpFilter(MatchList): |
| | RegList = [] |
| | for regexp in MatchList: |
| | a = re.compile(regexp) |
| | RegList.append(a) |
| | return RegList |
| |
|
| |
|
| | def cpallWithFilter(dirFrom, dirTo, MatchList): |
| | """ |
| | copy contents of dirFrom and below to dirTo without match |
| | """ |
| | global dcount, fcount |
| | for file in os.listdir(dirFrom): |
| | hitt = 0 |
| | for matchpat in MatchList: |
| | if re.match(matchpat, file): |
| | hitt = 1 |
| | print("Refuse: " + file) |
| | if hitt == 0: |
| | pathFrom = os.path.join(dirFrom, file) |
| | pathTo = os.path.join(dirTo, file) |
| | if not os.path.isdir(pathFrom): |
| | try: |
| | if verbose > 1: |
| | print("copying ", pathFrom, " to ", pathTo) |
| | cpfile(pathFrom, pathTo) |
| | fcount = fcount + 1 |
| | except Exception: |
| | print("Error copying ", pathFrom, " to ", pathTo, " -- skipped") |
| | print(sys.exc_info()[0], sys.exc_info()[1]) |
| | else: |
| | if verbose: |
| | print("copying dir ", pathFrom, " to ", pathTo) |
| | try: |
| | os.mkdir(pathTo) |
| | cpallWithFilter(pathFrom, pathTo, MatchList) |
| | dcount = dcount + 1 |
| | except Exception: |
| | print("Error creating ", pathTo, " -- skipped") |
| | print(sys.exc_info()[0], sys.exc_info()[1]) |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | fcount = dcount = 0 |
| |
|
| |
|
| | def rmall(dirPath): |
| | global fcount, dcount |
| | namesHere = os.listdir(dirPath) |
| | for name in namesHere: |
| | path = os.path.join(dirPath, name) |
| | if not os.path.isdir(path): |
| | os.remove(path) |
| | fcount = fcount + 1 |
| | else: |
| | rmall(path) |
| | os.rmdir(dirPath) |
| | dcount = dcount + 1 |
| |
|