2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
# auto.py
|
|
|
|
|
|
|
|
"""
|
|
|
|
This module is an integeral part of the program
|
|
|
|
MMA - Musical Midi Accompaniment.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
Bob van der Poel <bob@mellowood.ca>
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
import MMA.midi
|
|
|
|
import gbl
|
|
|
|
import MMA.parse
|
|
|
|
from MMA.common import *
|
|
|
|
|
|
|
|
grooveDir = {}
|
2007-04-29 06:47:40 +00:00
|
|
|
mmadir = ".mmaDB" # constant, name of the lib database file
|
2006-11-10 08:07:56 +00:00
|
|
|
fileCount = 0
|
|
|
|
grooveCount = 0
|
|
|
|
gdDate = None
|
|
|
|
processedFiles = []
|
|
|
|
mkGrooveList = []
|
|
|
|
|
|
|
|
def updateGrooveList(n):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Called from parse when new grooves are defined in a -g. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
global mkGrooveList
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
mkGrooveList.append(n)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def libUpdate():
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Update the mma library database file(s) with -g or -G option.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
This is called from the main program after the initialization
|
|
|
|
and other option parsing. No RETURN.
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
global fileCount, gdDate, grooveDir, processedfiles
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
print "Creating MMA groove directory database(s). Standby..."
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
""" gbl.libPath points to one main directory tree which should include
|
|
|
|
gbl.autoLib (defaults to 'stdlib'). We create a separate .mmaDB
|
|
|
|
file for each directory found in the main tree. IE. if we have the
|
|
|
|
directories stdlib and bvstuff we end up with stdlib/.mmaDB and
|
|
|
|
bvstuff/.mmaDB.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
for d in os.listdir(gbl.libPath):
|
|
|
|
libpath = os.path.join(gbl.libPath, d)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if not os.path.isdir(libpath): # skip files, just process directories
|
|
|
|
continue
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Attempt to read existing database
|
|
|
|
There is a flag gbl.makeGrvDefs set to 0, 1, 2
|
|
|
|
0 - there was no -g or -G so we're not here
|
|
|
|
1 - -g - read existing database and update
|
|
|
|
2 - -G - don't read existing, create new
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
grooveDir = {}
|
|
|
|
gdDate = None
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if gbl.makeGrvDefs == 1:
|
|
|
|
try:
|
|
|
|
infile = os.path.join(libpath, mmadir)
|
|
|
|
f=file(infile, "rb")
|
|
|
|
f.readline() # Read/discard comment line
|
|
|
|
grooveDir = pickle.load(f)
|
|
|
|
f.close()
|
|
|
|
gdDate = os.path.getmtime(infile)
|
|
|
|
except:
|
|
|
|
pass
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
dolibupdate(libpath, '')
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
# Strip out defs of deleted (not found) files.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
for f in grooveDir.keys():
|
|
|
|
if f not in processedFiles:
|
|
|
|
print " Deleting: %s" % f
|
|
|
|
del grooveDir[f]
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
try:
|
|
|
|
outpath = file(os.path.join(libpath, mmadir), 'wb')
|
|
|
|
except:
|
|
|
|
error("Error creating lib-database file '%s'. CRITICAL!" % libpath)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
outpath.write("### mmaDB ... AUTOGENERATED BINARY DATA. "
|
|
|
|
"DO NOT EDIT!!!\n")
|
|
|
|
pickle.dump(grooveDir, outpath, pickle.HIGHEST_PROTOCOL )
|
|
|
|
outpath.close()
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
print
|
|
|
|
print "Database update complete."
|
|
|
|
print " Files processed: %s" % fileCount
|
|
|
|
print " Total number of grooves: %s" % grooveCount
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
sys.exit(0)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dolibupdate(root, subdir):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Recursive function to read groove files in a directory. """
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
global fileCount, grooveCount, gdDate, grooveDir, processedFiles, mkGrooveList
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if subdir == '.':
|
|
|
|
print "Skipping: '.'"
|
|
|
|
return
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if subdir:
|
|
|
|
print " Processing library directory '%s'." % subdir
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Get a list of the files in this directory. If the list
|
|
|
|
includes a file called 'MMAIGNORE' the entire directory
|
|
|
|
(and subdirs) is ignored. Otherwise, each file in the
|
|
|
|
directory ending in 'mma' is parsed for groove defs.
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
p = os.path.join(root,subdir)
|
|
|
|
dirfiles = os.listdir(p)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if "MMAIGNORE" in dirfiles:
|
|
|
|
print "Skipping: %s" % p
|
|
|
|
return
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
for fn in sorted(dirfiles):
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
# Ignore hidden files and emacs auto-save and dead.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if fn.startswith('.') or fn.startswith('#'):
|
|
|
|
continue
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
# Create full path name
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
f=os.path.join(root, subdir, fn)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if os.path.isdir(f):
|
|
|
|
dolibupdate(root, os.path.join(subdir,fn)) # recursive!
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
elif f.endswith(gbl.ext):
|
|
|
|
ename = os.path.join(subdir, fn)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
processedFiles.append(ename)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if gdDate and grooveDir.has_key(ename) and \
|
|
|
|
os.path.getmtime(f) < gdDate:
|
|
|
|
print " Existing: %s" % f
|
|
|
|
grooveCount += len(grooveDir[ename])
|
|
|
|
continue
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if grooveDir.has_key(ename):
|
|
|
|
print " Updating: %s" % f
|
|
|
|
else:
|
|
|
|
print " Creating: %s" % f
|
|
|
|
mkGrooveList = []
|
|
|
|
gbl.mtrks = {}
|
|
|
|
for c in gbl.midiAssigns.keys():
|
|
|
|
gbl.midiAssigns[c]=[]
|
|
|
|
for a,v in enumerate(gbl.midiAvail):
|
|
|
|
gbl.midiAvail[a]=0
|
|
|
|
gbl.mtrks[0]=MMA.midi.Mtrk(0)
|
|
|
|
gbl.tnames = {}
|
|
|
|
MMA.parse.parseFile(f) # read current file, grab grooves
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
fileCount += 1 # just so we can report to user
|
|
|
|
grooveCount += len(mkGrooveList)
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
grooveDir[ename]=mkGrooveList
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
else:
|
|
|
|
if not f.endswith(mmadir):
|
|
|
|
print " Ignoring: %s" % f
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#################################################################
|
|
|
|
|
|
|
|
|
|
|
|
def loadGrooveDir(g):
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Try to auto-load a groove from the library.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
The compliation of all the MMADIR files is stored in the dict
|
|
|
|
grooveDir{}.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
Check the main libpath directory for the MMADIR file. The
|
|
|
|
names of the files and corresponding grooves are extracted.
|
|
|
|
This is stored in a dictionary with the filename as the key
|
|
|
|
and a list of grooves as the data.
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
global grooveDir
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
""" If the global dict grooveDir is empty we first load the MMADIR info.
|
|
|
|
We're assuming that not much goes wrong here...if we don't find
|
|
|
|
the database we set grooveDir{} to a BS value to avoid future
|
|
|
|
load attempts. The entire load is in a try, which means it either
|
|
|
|
all works, or not ...
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
if not grooveDir:
|
|
|
|
try:
|
|
|
|
infile = os.path.join(gbl.libPath, gbl.autoLib, mmadir)
|
|
|
|
f=file(infile, "rb")
|
|
|
|
f.readline() # Read/discard comment line
|
|
|
|
grooveDir = pickle.load(f)
|
|
|
|
f.close()
|
|
|
|
except:
|
|
|
|
grooveDir[0]=''
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
""" Search the dict for a match. grooveDir{} is a dictionary
|
|
|
|
for lists. Each dictionary key is filename (eg: "lib/rhumba.mma")
|
|
|
|
and the list associated with it is a list of grooves defined
|
|
|
|
in that file. Just a matter of stepping though the dict. and
|
|
|
|
returning the proper filename.
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
RETURN: Lib-Filename if found
|
|
|
|
None if not found
|
|
|
|
"""
|
2006-11-10 08:07:56 +00:00
|
|
|
|
2007-04-29 06:47:40 +00:00
|
|
|
for filename, namelist in grooveDir.items():
|
|
|
|
if g in namelist:
|
|
|
|
return filename
|
|
|
|
|
|
|
|
return None
|
2006-11-10 08:07:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|