Now work on windows

main
Nicholas Hope 2022-08-13 20:48:47 -04:00
parent 1c501fa90b
commit d817af07ee
2 changed files with 56 additions and 13 deletions

View File

@ -153,7 +153,7 @@ def main() -> int:
fileNames = parseConfig(config, defaults)
fileList = writing.replaceFiles(targetDir, fileNames, args)
# writing.appendToActuals(targetDir, fileList)
writing.appendToActuals(targetDir, fileList)
return 0

View File

@ -2,6 +2,7 @@ import os
import sys
from os.path import exists
from tempfile import NamedTemporaryFile
from shutil import move as moveRobust
def writeOutput(data, className, args) -> dict:
"""
@ -23,13 +24,14 @@ def writeOutput(data, className, args) -> dict:
print( f'DEBUG: need {filesNeeded} files for {className}', file=sys.stderr)
FilNedLen = len(str(filesNeeded))
# extra 4 bytes is leeway
reservedSpace = len(f'{className}_script_{filesNeeded}.cfg') + 4
# Initialize variables
outfile = NamedTemporaryFile(prefix=className, delete=False)
# I know % formatting is old-school and pylint hates it,
# but "%*d" is the easiest way to left-pad with zeros
# without hardcoding a number. The extra 4 bytes is just some leeway
# without hardcoding a number.
namesDict.update({ outfile.name: '%s_script_%0*d.cfg' % (className, FilNedLen, fileNum) })
while (fileNum <= filesNeeded and len(lineList) > 0):
@ -64,20 +66,61 @@ def replaceFiles(targetDir, fileNames, args):
if args.debug:
print( f'DEBUG: {tmpName} would be {targetDir}/{realName}.cfg', file=sys.stderr)
else:
os.replace( tmpName, f'{targetDir}/{realName}' )
# using shutil.move() because it can move files across disk drives on windows
moveRobust( tmpName, f'{targetDir}/{realName}' )
if args.debug:
print( f'DEBUG: Created {targetDir}/{realName}', file=sys.stderr)
return fileNames.values()
def execStrInFile(fileName, f):
execStr = f'exec {fileName}'
return list(fileNames.values())
def appendToActuals(targetDir, fileList):
fileList = onlyFirsts(fileList)
for currFile in fileList:
cfgName = targetDir + '/' + open(currFile.split('_')[0] + '.cfg', 'r')
if exists(cfgName):
if execStrInFile(currFile, cfgFile):
pass
else:
pass
execStr = f'exec {currFile.split(".")[0]}'
realFilePath = targetDir + '/' + getRealName(currFile)
realExists = exists(realFilePath)
# creates if it doesn't exist, so must come after the exists() call
cfgFile = open(realFilePath, 'a')
if (realExists == False or not strInFile(execStr, cfgFile)):
# since the file was created if it didn't exist,
# and it was opened in append mode, this will
# always place it at the end
cfgFile.write( execStr.encode('utf8') )
cfgFile.close()
def onlyFirsts(fileList):
for i, fileName in enumerate(fileList):
noExtension = fileName.split('.')[0]
number = int(noExtension.split('_')[2])
if number != 1:
fileList.pop(i)
return fileList
def getRealName(fileName):
className = fileName.split('_')[0]
targetNames = {
"demo": "demoman",
"heavy": "heavyweapons",
"engi": "engineer"
}
if className in targetNames:
className = targetNames[className]
return className + '.cfg'
def strInFile(execStr, f):
while True:
line = f.readline()
if line == "":
# eof
break
# Remove indent and outdent, including trailing newline
if execStr in line.strip():
return True
return False