Made dry-run work.
parent
a682acdbc6
commit
71ba2de1e2
|
@ -50,13 +50,15 @@ def writeOutput(scriptString, className):
|
|||
if( chunksneeded == 1):
|
||||
# If it can be done in one chunk, do it in one chunk.
|
||||
outfile = tempfile.NamedTemporaryFile( prefix=className, delete=False )
|
||||
if args.debug:
|
||||
print( f'DEBUG: created temporary {outfile.name} ')
|
||||
outfile.write(scriptString.encode("utf8"))
|
||||
outfile.close()
|
||||
os.replace(outfile.name, f'{prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
if args.debug:
|
||||
print( f'DEBUG: Created {prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
if( args.dry_run != True ):
|
||||
os.replace(outfile.name, f'{prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
if args.debug:
|
||||
print( f'DEBUG: Created {prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
else:
|
||||
if args.debug:
|
||||
print( f'DEBUG: {outfile.name} would be {prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
else:
|
||||
# Gotta do it in multiple chunks
|
||||
classLines = scriptString.splitlines()
|
||||
|
@ -64,11 +66,11 @@ def writeOutput(scriptString, className):
|
|||
# extra 4 bytes is just a little buffer so we don't get exactly chunksize bytes
|
||||
reservedSpace = len(execString) + 4
|
||||
n = 0
|
||||
pieces = {}
|
||||
while( chunk <= chunksneeded ):
|
||||
outfile = tempfile.NamedTemporaryFile( prefix=className, delete=False )
|
||||
pieces[outfile.name] = f'{prefix}/{className}_script_{chunk:02d}.cfg'
|
||||
byteswritten = 0
|
||||
if args.debug:
|
||||
print( f'DEBUG: created temporary {outfile.name} ')
|
||||
while( n < len(classLines) and (byteswritten + len(classLines[n]) + reservedSpace) < chunksize ):
|
||||
line = classLines[n].encode("utf8") + os.linesep.encode("utf8")
|
||||
outfile.write(line)
|
||||
|
@ -79,10 +81,17 @@ def writeOutput(scriptString, className):
|
|||
outfile.write(line)
|
||||
byteswritten += len(line)
|
||||
outfile.close()
|
||||
os.replace(outfile.name, f'{prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
|
||||
if args.debug:
|
||||
print( f'DEBUG: Wrote {byteswritten} bytes to {prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
print( f'DEBUG: Wrote {byteswritten} bytes to {className} ({chunk}/{chunksneeded})' )
|
||||
chunk += 1
|
||||
for tmpname, realname in pieces.items():
|
||||
if( args.dry_run ):
|
||||
if( args.debug ):
|
||||
print( f'DEBUG: {outfile.name} would be {prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
else:
|
||||
os.replace(tmpname, realname)
|
||||
print( f'DEBUG: Created {prefix}/{className}_script_{chunk:02d}.cfg')
|
||||
|
||||
def parseConfig(config):
|
||||
"""With validated data structure, write out all the files."""
|
||||
|
|
Loading…
Reference in New Issue