import os import sys import time import glob # Make sure that Adobe PDF output is set to "C:/TEMP/PDF/*.pdf" # And that View Adobe PDF results is unchecked. # answer = input("Is ADOBE PDF output set to \"C:\TEMP\PDF\*.pdf\" in \"printing preferences\"\n\ # and is \"View Adobe PDF Results\" unchecked? (y/n)") # if (answer != "y"): # sys.exit(1) #Globals startTime = int(time.time()) FRONIMO = 'c:\\apps\\fronimo\\fronimo.exe' FRONMIDI = '-m' MIDITEMP = 'd:\\temp\\midis' #Functions # Checks if is newer than #If target doesn't exist it counts as older def newer(source, target): if not(os.path.isfile(target)): return(1) if (os.path.getmtime(source) > os.path.getmtime(target)): # get rid of older file try: os.remove(target) except: print("newer:cannot remove ", target,"\n", file = flErr) return(1) else: return(0) # END newer # removeStrays function def removeStrays(currDir): arMidiLocal = glob.glob(os.path.join(currDir, "*.mid")) arPdfLocal = glob.glob(os.path.join(currDir, "*.pdf")) arRemove = arMidiLocal + arPdfLocal for file in arRemove: try: os.remove(file) except: print("removeStrays:cannot remove ", file, "\n", file=flErr) continue #end removeStrays # doMidi function # Creates midi file, if needed, and puts it in the right place." def doMidi(currDir, fronFile): makeMidi = 0 midiDir = os.path.join(currDir, "midi") # Check if the current directory has a midi sub-directory if not os.path.isdir(midiDir): try: os.mkdir(midiDir) except: print("doMidi:cannot create directory ", midiDir, "\n", file=flErr) return(0) midiRoot = fronFile.replace(".ft3", "") fronPath = os.path.join(currDir, fronFile) absFron = os.path.abspath(fronPath) singleMidi = os.path.join(currDir, midiRoot) + ".mid" midiDest = os.path.join(midiDir, midiRoot) + ".mid" # Fronimo -m writes midi file[s] to current directory # Make midi file[s] and place it/them in currDir command = FRONIMO + " " + absFron + " " + FRONMIDI try: result = os.system(command) # Give time for fronimo operation to complete. # time.sleep(0.5) except: print("doMidi:Cannot create midi file[s] from ", fronPath, " in ",\ currDir, ". Error =", result, ".\n", file=flErr) return(0) # check if we need to concatenate the midi files # if we have one simply ending in .mid, we don't if (os.path.isfile(singleMidi)): os.remove(singleMidi) else: print("doMidi:multiple sections for ", fronPath, ".\n",file=flErr) globArg = os.path.join(currDir, midiRoot) globArg += "[0-9]*.mid" arMidis = glob.glob(globArg) if len(arMidis) <=1: print("midiCat: error creating midis; globArg= ", globArg, "\n",\ file=flErr) return(0) removeStrays(currDir) return(1) # end doMidi #MAIN PROGRAM SEGMENT argc = len(sys.argv) if argc == 1: # current directory is default starting place startDir = "." elif argc == 2: # or a starting directory specified on the command line. startDir = sys.argv[1] else: print("Usage: argv[0] [single directory name].\n") sys.exit(2) if not (os.path.isdir(startDir)): print(startDir, " is not a directory.\n") print("Usage: argv[0] [single directory name].\n") sys.exit(2) flErr = open(os.path.join(startDir, 'ft2err.txt'), 'w+') startTime = int (time.time()) oldDir = "" pdfCount = 0 midiCount = 0 # Walk the directory tree for root, dirs, files in os.walk(startDir): # Only when changing root directories do we check for pdf/midi sub-directories if root != oldDir: # Now we have a new "old directory" oldDir = root for fname in files: # Is there a fronimo file in this directory? if fname.endswith(".ft3"): # Get rid of old midifiles, etc. removeStrays(root) if (doMidi(root, fname)): midiCount += 1 # go back to look for another .ft3 file # move to another directory. # done walking the tree; print stats. endTime = int(time.time()) elapsedTime = endTime - startTime elapsedSeconds = elapsedTime % 3600 elapsedHours = (elapsedTime - elapsedSeconds) / 3600 elapsedMinutes = elapsedSeconds / 60 elapsedSeconds = elapsedSeconds % 60 elapsedMinutes = elapsedMinutes - elapsedSeconds / 60 print("Midi/PDF update completed.\n", pdfCount, " pdf files and ",\ midiCount, " midi files created in ",\ elapsedHours, " hrs, ", elapsedMinutes, " mins, ", elapsedSeconds, " secs.\n") # input("type something")