Skip to content

Instantly share code, notes, and snippets.

@benergize
Last active December 16, 2020 17:06
Show Gist options
  • Save benergize/0d0f94e221ed7efa048ee662ea585243 to your computer and use it in GitHub Desktop.
Save benergize/0d0f94e221ed7efa048ee662ea585243 to your computer and use it in GitHub Desktop.
Python script to combine scripts into a single file.
import json
import sys
def main():
print("\npyleon 0.1 - Yet more dumb software by Ben Ehrlich (benergize.com)")
print("Let's Rock and Roll")
print("----------------------------\n")
if len(sys.argv) < 2:
print("Please provide the pyleon config file as an argument, or -h for help. Aborting.")
exit()
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
print("Use pyleon to combine scripts into a single file. Just create a JSON file in this format (and supply its name as an argument when you run pyleon):\n")
print('{ "output": "combinedScripts.js", "files": [ "script1.js", "script2.js", "script3.js" ] }')
print("\nThe scripts will be combined into a single file specified by the output property in the JSON file. The files property specifies what scripts to put together.")
exit()
shouldImport = sys.argv[1:]
for f in shouldImport:
outputToFile = "pyleon-output"
master = "";
fo = open(f, "r")
print("Reading " + f + ".")
goon = False
try:
obj = json.loads(fo.read())
goon = True
except (ValueError):
print("Failed to read JSON. Skipping file.")
if(goon):
if(obj['output']):
outputToFile = obj['output']
if(not isinstance(outputToFile,list)):
outputList = [outputToFile]
else:
outputList = outputToFile
print("\nWill output to " + ", ".join(outputList))
print("")
for j in obj['files']:
try:
print("Reading " + j + ".")
r2 = open(j,"r")
master += r2.read()
r2.close()
except (e):
print("Could not read " + j + ".");
fo.close()
print("")
for j in outputList:
try:
print(" - Writing to " + j)
wr = open(j,"w+")
wr.write(master)
wr.close()
except (e):
print(" X Could not write to file " + j +".")
print("\nThanks for playing!")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment