miketrethewey

MSU Renamer Utility

Dec 4th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. # Function
  2. #  Analyzes MSU Pack
  3. #  Creates MSU manifest if necessary (zero-length file, appropriately named, based on PCM files)
  4. #  Creates a .txt with the original filename of the game file
  5. #  Renames the game file to match the PCM files
  6. # Usage
  7. #  Place game file (*.sfc, *.smc) into MSU Pack folder
  8. #  py msu.py --dir="path\to\MSU Pack"
  9.  
  10. from argparse import ArgumentParser # get argument parser for commandline
  11. import os               # for file manipulation
  12.  
  13. # process arguments from commandline
  14. def process_command_line_args():
  15.   parser = ArgumentParser()
  16.   # give directory where MSU pack resides
  17.   parser.add_argument("--dir",
  18.     dest="dir",
  19.     help="Directory where MSU pack resides",
  20.     metavar="<dir>",
  21.     default="."
  22.   )
  23.   command_line_args = vars(parser.parse_args())
  24.   return command_line_args
  25.  
  26. # do the thing
  27. def process_msu(dir):
  28.   msu_root = ""     # keep track of MSU slug
  29.   msu_found = False # keep track if we found an MSU Manifest
  30.  
  31.   print("Processing MSU in dir: " + dir)
  32.  
  33.   # walk through the MSU & PCM files
  34.   for _,_,f in os.walk(os.path.join(dir)):
  35.     for file in f:
  36.       if os.path.exists(os.path.join(dir,file)):
  37.         filename,file_extension = os.path.splitext(file)
  38.         # if this is a .msu, this is our MSU Manifest
  39.         if file_extension.lower() == ".msu":
  40.           msu_root = filename
  41.           msu_found = True
  42.           print("Found an MSU Manifest! Using this as Manifest name instead of any PCMs.")
  43.         # if we don't have a slug yet, keep looking by searching the PCM files
  44.         if msu_root == "":
  45.           if file_extension.lower() == ".pcm":
  46.             filenameParts = filename.split('-')
  47.             filenameParts.pop()
  48.             msu_root = '-'.join(filenameParts)
  49.             print("Found a PCM! Using this as Manifest name.")
  50.  
  51.   # if we got a slug from the PCMs but we never found an MSU Manifest, make one
  52.   if not msu_root == "":
  53.     if not msu_found:
  54.       print("Didn't find an MSU Manifest! Making one!")
  55.       manifest = open(os.path.join(dir,msu_root + ".msu"),"w")
  56.       manifest.write("")
  57.       manifest.close()
  58.  
  59.     # walk through the game files
  60.     for _,_,f in os.walk(os.path.join(dir)):
  61.       for file in f:
  62.         if os.path.exists(os.path.join(dir,file)):
  63.           filename,file_extension = os.path.splitext(file)
  64.           # if this is a game file
  65.           if file_extension.lower() in [ ".sfc", ".smc" ]:
  66.             # if we need to rename it
  67.             if not filename == msu_root:
  68.               # make a note
  69.               note = open(os.path.join(dir,filename + ".txt"),"w")
  70.               note.write("")
  71.               note.close()
  72.               src = os.path.join(dir,file)
  73.               dest = os.path.join(dir,msu_root + file_extension)
  74.               print("Found game file:       " + src)
  75.               print("Renaming note to:      " + os.path.join(dir,filename) + ".txt")
  76.               print("Renaming game file to: " + dest)
  77.               # rename the game file
  78.               os.rename(src,dest)
  79.             else:
  80.               # else, it already matches, bail and do nothing
  81.               print("Game file found with MSU pack already! Aborting!")
  82.               exit(1)
  83.   else:
  84.     # else, we couldn't find anything useful
  85.     print("Failed to find an MSU pack!")
  86.  
  87. def main():
  88.   command_line_args = process_command_line_args()
  89.   if not command_line_args["dir"]:
  90.     command_line_args["dir"] = "."
  91.   process_msu(command_line_args["dir"])
  92.  
  93. #main
  94. if __name__ == "__main__":
  95.     main()
Advertisement
Add Comment
Please, Sign In to add comment