83 lines
3.0 KiB
Python
83 lines
3.0 KiB
Python
import os
|
|
import sys
|
|
from os.path import exists
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
def writeOutput(data, className, args) -> dict:
|
|
"""
|
|
Write `data' to various files as needed, returning a dict of
|
|
the temporary file names and their target destination names,
|
|
not including the target directory
|
|
"""
|
|
namesDict = {} # return dict
|
|
|
|
# Variables
|
|
lineList = [ l.encode('utf8') for l in data.splitlines() ]
|
|
fileNum = 1
|
|
bytesWritten = 0
|
|
|
|
# Constants
|
|
maxFileSize = 2 ** 20 # 1MiB maximum cfg file size
|
|
filesNeeded = 1 + int( len(data)/maxFileSize )
|
|
if args.debug:
|
|
print( f'DEBUG: need {filesNeeded} files for {className}', file=sys.stderr)
|
|
|
|
FilNedLen = len(str(filesNeeded))
|
|
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
|
|
namesDict.update({ outfile.name: '%s_script_%0*d.cfg' % (className, FilNedLen, fileNum) })
|
|
|
|
while (fileNum <= filesNeeded and len(lineList) > 0):
|
|
line = lineList.pop(0) + '\n'.encode('utf8')
|
|
lineLen = len(line) # nice
|
|
|
|
if bytesWritten + reservedSpace + lineLen > maxFileSize:
|
|
outfile.write( ('exec %s_script_%0*d' % (className, FilNedLen, fileNum+1)).encode('utf8') )
|
|
bytesWritten += reservedSpace
|
|
if args.debug:
|
|
print( f'DEBUG: Wrote {bytesWritten} bytes to {className} ({fileNum}/{filesNeeded})', file=sys.stderr)
|
|
|
|
outfile.close()
|
|
outfile = NamedTemporaryFile(prefix=className, delete=False)
|
|
|
|
fileNum += 1
|
|
namesDict.update({ outfile.name: '%s_script_%0*d.cfg' % (className, FilNedLen, fileNum) })
|
|
bytesWritten = 0
|
|
|
|
outfile.write(line)
|
|
bytesWritten += lineLen
|
|
|
|
outfile.close() # the most-recent tempfile will not have been closed
|
|
if args.debug:
|
|
print( f'DEBUG: Wrote {bytesWritten} bytes to {className} ({fileNum}/{filesNeeded})', file=sys.stderr)
|
|
|
|
return namesDict
|
|
|
|
def replaceFiles(targetDir, fileNames, args):
|
|
for tmpName, realName in fileNames.items():
|
|
if args.dry_run:
|
|
if args.debug:
|
|
print( f'DEBUG: {tmpName} would be {targetDir}/{realName}.cfg', file=sys.stderr)
|
|
else:
|
|
os.replace( 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}'
|
|
|
|
def appendToActuals(targetDir, fileList):
|
|
for currFile in fileList:
|
|
cfgName = targetDir + '/' + open(currFile.split('_')[0] + '.cfg', 'r')
|
|
if exists(cfgName):
|
|
if execStrInFile(currFile, cfgFile):
|
|
pass
|
|
else:
|
|
pass |