testfiles / utils /files.py
dunlp's picture
Upload 5 files
3b83fd0 verified
raw
history blame
1.92 kB
from os import listdir, makedirs
from os.path import isdir, isfile, join
from re import search, IGNORECASE
from shutil import copy
def getFilesWith(path: str, reg: str):
if not isdir(path):
print(path, "is not a valid path")
return None
content = listdir(path)
if len(content) == 0:
print(path, "has no content")
return None
files = [f for f in content if isfile(join(path, f)) and search(reg, f, IGNORECASE)]
if len(files) == 0:
print(path, "contains no", reg)
return None
return files
def createNewFolders(dirs: list):
for d in dirs:
if not isdir(d):
makedirs(d)
else:
print("directory already exists", d)
def createNewTemplates(objs, templatesDir, regTemplate, root):
templatefiles = getFilesWith(templatesDir, regTemplate)
for k in objs:
regPhase = r""
match k:
case "(i)SAT":
regPhase = r"sat"
case "iFAT":
regPhase = r"fat"
files = [f for f in templatefiles if search(regPhase, f, IGNORECASE)]
if len(files) == 0:
print("phase %s has no templates" % k)
continue
for o in objs[k]:
targetLocation = join(root, o)
tlFiles = getFilesWith(targetLocation, regPhase)
if tlFiles:
print(k, "files already exist in:", targetLocation)
print("--------------------")
[print("|-",f) for f in tlFiles]
print("--------------------")
continue
for f in files:
templatepath = join(templatesDir, f)
targetpath = targetLocation
if search(r"hut_\d{4}[a-zA-Z]{2}", f, IGNORECASE):
targetpath = join(targetLocation, f[:4] + o + f[10:])
copy(templatepath, targetpath)