Protect main program with __main__ test

Originally cppo was written as a shell script and was never intended to
be a library of functions to be used by anyone else.  Single-file Python
modules are often written to be run as standalone programs either to do
what they do from the command line or for testing purposes.

Theoretically you do this if your code provides useful stuff for other
programs to use, and it's hard to argue that cppo does that yet, but it
is intended to do so in the future.  Let's start working toward that.
This commit is contained in:
T. Joseph Carter 2017-06-22 01:59:07 -07:00
parent 1d1eed33d2
commit 3a3531514b

67
cppo
View File

@ -1058,9 +1058,10 @@ def isnumber(number):
# --- start
args = sys.argv
if __name__ == '__main__':
args = sys.argv
while True: # breaks when there are no more arguments starting with dash
while True: # breaks when there are no more arguments starting with dash
if len(args) == 1:
usage()
@ -1105,25 +1106,25 @@ while True: # breaks when there are no more arguments starting with dash
else:
usage()
if g.AD and g.EX:
if g.AD and g.EX:
usage()
if g.CAT:
if g.CAT:
if len(args) != 2:
usage()
else:
else:
if len(args) not in (3, 4):
usage()
g.image_file = args[1]
if not os.path.isfile(g.image_file):
g.image_file = args[1]
if not os.path.isfile(g.image_file):
print("Image/archive file \"" + g.image_file + "\" was not found.")
quitNow(2)
g.image_name = os.path.splitext(os.path.basename(g.image_file))
g.image_ext = g.image_name[1].lower()
g.image_name = os.path.splitext(os.path.basename(g.image_file))
g.image_ext = g.image_name[1].lower()
# automatically set ShrinkIt mode if extension suggests it
if (g.SHK or g.image_ext in ('.shk', '.sdk', '.bxy'):
# automatically set ShrinkIt mode if extension suggests it
if (g.SHK or g.image_ext in ('.shk', '.sdk', '.bxy'):
if (os.name == "nt"):
print("ShrinkIt archives cannot be extracted on Windows.")
quitNow(2)
@ -1136,10 +1137,10 @@ if (g.SHK or g.image_ext in ('.shk', '.sdk', '.bxy'):
print("Nulib2 is not available; not expanding ShrinkIt archive.")
quitNow(2)
if (len(args) == 4):
if (len(args) == 4):
g.extract_file = args[2]
if g.extract_file:
if g.extract_file:
targetPath = args[3]
if os.path.isdir(targetPath):
g.targetDir = targetPath
@ -1149,13 +1150,13 @@ if g.extract_file:
if not os.path.isdir(g.targetDir):
print("Target directory not found.")
quitNow(2)
else:
else:
if not g.CAT:
if not os.path.isdir(args[2]):
print("Target directory not found.")
quitNow(2)
if g.SHK:
if g.SHK:
g.PNAME = 0
if not g.CAT:
targetDir = (args[3] if g.extract_file else args[2])
@ -1245,15 +1246,15 @@ if g.SHK:
shutil.rmtree(unshkdir, True)
quitNow(0)
# end script if SHK
# end script if SHK
g.image_data = loadFile(g.image_file)
g.image_data = loadFile(g.image_file)
if g.image_ext in ('.2mg', '.2img'):
if g.image_ext in ('.2mg', '.2img'):
g.image_data = g.image_data[64:]
# handle 140K disk image
if (len(g.image_data) == 143360):
# handle 140K disk image
if (len(g.image_data) == 143360):
#print("140K disk")
prodosDisk = 0
fixOrder = 0
@ -1306,12 +1307,12 @@ if (len(g.image_data) == 143360):
if not prodosDisk and not g.D33:
print("Warning: Unable to determine disk format, assuming ProDOS.")
# enforce leading slash if ProDOS
if (not g.SHK and not g.D33 and g.extract_file and
# enforce leading slash if ProDOS
if (not g.SHK and not g.D33 and g.extract_file and
(args[2][0] not in ('/', ':'))):
usage()
if g.D33:
if g.D33:
diskName = (g.image_name[0] if g.image_ext in ('.dsk', '.do', '.po')
else "".join(g.image_name))
if g.PNAME:
@ -1331,17 +1332,17 @@ if g.D33:
print("ProDOS file not found within image file.")
quitNow(0)
# below: ProDOS
# below: ProDOS
g.activeDirBlock = 0
g.activeFileName = ""
g.activeFileSize = 0
g.activeFileBytesCopied = 0
g.resourceFork = 0
g.PDOSPATH_INDEX = 0
g.PNAME = 0
g.activeDirBlock = 0
g.activeFileName = ""
g.activeFileSize = 0
g.activeFileBytesCopied = 0
g.resourceFork = 0
g.PDOSPATH_INDEX = 0
g.PNAME = 0
if g.extract_file:
if g.extract_file:
g.PDOSPATH = g.extract_file.replace(':', '/').split('/')
g.extract_file = None
if not g.PDOSPATH[0]:
@ -1353,7 +1354,7 @@ if g.extract_file:
processDir(2)
print("ProDOS file not found within image file.")
quitNow(2)
else:
else:
if not g.CAT:
# print(args[0], args[1], args[2])
g.targetDir = (args[2] + "/" + getVolumeName().decode("L1"))