Skip to content

Commit 8153e7b

Browse files
committed
Async added!
1 parent 5fa8dc4 commit 8153e7b

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

ssri/ssri.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import fileinput
99
import readline # I like having inputs work well with navigation - sue me
1010
import sys
11+
import asyncio
1112
# from termcolor import colored
1213

1314
# Coloured Outputs Constants
@@ -240,9 +241,7 @@ def getTextForIncludeFile(templateFile): # Returns the contents of the template
240241
return textToCopyIn
241242

242243

243-
def writeTextToFiles(
244-
templateFile, fileList, verbose
245-
): # write the provided template file into the fileList (which is actually a set of named tuples)
244+
async def writeTextToFiles(templateFile, fileList, verbose): # write the provided template file into the fileList (which is actually a set of named tuples)
246245
# TODO: Change this to do multithreading - will want to swap out from using fileinput when you do this though
247246
verbosePrint(verbose, "\n")
248247
textToAdd = getTextForIncludeFile(templateFile)
@@ -338,7 +337,26 @@ def parse_args(args):
338337
def copyAllFiles(source, output):
339338
shutil.copytree(source, output, dirs_exist_ok=True)
340339

341-
def main():
340+
341+
async def copyAllFilesCaller(copyAll, inputFile, output):
342+
numWarnings = 0
343+
if copyAll:
344+
if not os.path.exists(inputFile[0]):
345+
print(
346+
f"{CRED}! Input directory: {inputFile[0]} does not exist - exiting{CEND}"
347+
)
348+
numWarnings += 1
349+
exit()
350+
if os.path.isfile(inputFile[0]):
351+
print(f"{CRED}! Input directory: {inputFile[0]} is a file, exiting{CEND}")
352+
exit()
353+
else:
354+
os.makedirs(os.path.dirname(output[0] + "/"), exist_ok=True)
355+
copyAllFiles(inputFile[0], output[0])
356+
357+
return(numWarnings)
358+
359+
async def main():
342360
args = parse_args(sys.argv[1:])
343361

344362
templatesDir = args.inputFile # This *should* be fine?
@@ -354,23 +372,15 @@ def main():
354372
# global numWarnings
355373
numWarnings = 0
356374

357-
os.makedirs(os.path.dirname(args.output[0] + "/"), exist_ok=True)
375+
358376
filesToSearch = None
359377

378+
copyAllFilesTask = asyncio.create_task(copyAllFilesCaller(args.copy_all, args.inputFile, args.output))
379+
360380
if args.copy_all:
361-
if not os.path.exists(args.inputFile[0]):
362-
print(
363-
f"{CRED}! Input directory: {args.inputFile[0]} does not exist - exiting{CEND}"
364-
)
365-
numWarnings += 1
366-
exit()
367-
if os.path.isfile(args.inputFile[0]):
368-
print(f"{CRED}! Input directory: {args.inputFile[0]} is a file, exiting{CEND}")
369-
exit()
370-
copyAllFiles(args.inputFile[0], args.output[0])
371381
noWarnings = True # Turns off warnings as we will be overwriting new copied files anyway
372382

373-
383+
os.makedirs(os.path.dirname(args.output[0] + "/"), exist_ok=True)
374384
if args.dir:
375385
for directory in args.inputFile:
376386
# Go through dir checking all files for an include
@@ -406,6 +416,8 @@ def main():
406416
# First copy files to new location
407417
# print(filesToSearch)
408418
dictOfTemplatesToFiles = None
419+
numWarningsFromCopyAll = await copyAllFilesTask
420+
numWarnings += numWarningsFromCopyAll
409421
for fileSearchIndex in range(len(filesToSearch[0])):
410422
copyFilesToNewLocation(
411423
filesToSearch[1][fileSearchIndex], filesToSearch[0][fileSearchIndex]
@@ -416,9 +428,9 @@ def main():
416428
else:
417429
dictOfTemplatesToFiles, numFilesChanged, numWarnings = checkFilesForIncludes(filesToSearch[1], args.templates_dir[0], numFilesChanged, verbose, numWarnings) # this is a dictionary where key is include file an values is a named tuple with fileName and includeText from the files that ask for the key
418430

431+
tasks = [writeTextToFiles(template[0], template[1], verbose) for template in dictOfTemplatesToFiles.items()] # This might break but heres hoping
419432
for template in dictOfTemplatesToFiles.items():
420-
writeTextToFiles(template[0], template[1], verbose)
421-
numIncludeStatements += len(template[1])
433+
numIncludeStatements += len(template[1])
422434

423435

424436
if numWarnings == 0:
@@ -428,6 +440,9 @@ def main():
428440
printColour = CRED
429441
includeText = "!"
430442

443+
444+
for task in asyncio.as_completed(tasks):
445+
result = await task # Urgh its late and I have no sleep - there is def a better solution than this but meh
431446
if args.dir:
432447
print(
433448
f"{printColour}{includeText} Looked at {fileCreatedCounter} files in {args.inputFile[0]}, found {numFilesChanged} file(s) with include statements (with a total of {numIncludeStatements} include statements found), and output files to {args.output[0]} {CEND}"
@@ -439,4 +454,4 @@ def main():
439454

440455

441456
if __name__ == "__main__":
442-
main()
457+
asyncio.run(main())

0 commit comments

Comments
 (0)