home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / fresh / scripts / process_collection_dir.py next >
Text File  |  2002-04-02  |  2KB  |  73 lines

  1. #!/usr/bin/python
  2. # Nathan DeBardeleben
  3. # ndebard@parl.clemson.edu
  4. # April 3, 2002
  5. #
  6. #   A simple, very unclean script which takes a base directory 
  7. #   and walks through its dir and subdirs looking for qualified
  8. #   MAME roms and spits out of stdout lines that look like
  9. #     base_path_offset_path:romname:"Some String - like a name"
  10. #   This can then be used with this scripts compliment to copy the roms
  11. #   from some dir based on a textfile (like that generated from this
  12. #   script).  The point is that you can take a directory that you like
  13. #   and turn that into a 'collection' text file that you can hand to
  14. #   other people, they can then run the complementary script to make
  15. #   the same directory that you had
  16.  
  17. import sys, string, re, os, posixpath
  18.  
  19. mame_path = "/usr/local/bin/xmame.x11"
  20.  
  21. help = sys.argv[0] + " --basedir <dir w/ roms>"
  22.  
  23. if '-h' in sys.argv or '-help' in sys.argv or '--h' in sys.argv or '--help' \
  24. in sys.argv:
  25.     print help
  26.     sys.exit(0)
  27.  
  28. if len(sys.argv) < 3:
  29.     print help
  30.     sys.exit(0)
  31.  
  32. basedir_set = 0
  33.  
  34. for i in range(len(sys.argv)):
  35.     if sys.argv[i] == "--basedir":
  36.         basedir = sys.argv[i+1]
  37.         basedir_set = 1
  38.  
  39. if basedir_set == 0:
  40.     print help
  41.     sys.exit(0)
  42.  
  43. def checkdir(direntry,base):
  44.     dir = base
  45.     if posixpath.isdir(base+"/"+direntry):
  46.         for file in os.listdir(dir+"/"+direntry):
  47.             checkdir(file,dir+"/"+direntry)
  48.     else:
  49.         # get rid of the end, incase it's got a .zip or something
  50.         possible_rom_tmp = string.split(direntry,'.')[0]
  51.         if string.find(possible_rom_tmp,'/') != -1:
  52.             possible_rom = possible_rom_tmp[string.rindex(possible_rom_tmp,'/')+1:]
  53.         else:
  54.             possible_rom = possible_rom_tmp
  55.         os.system(mame_path+" -nolcf -out /tmp/process_collection_dir.tmp -err /dev/null -lf "+possible_rom);
  56.         tmp_file = open("/tmp/process_collection_dir.tmp")
  57.         for line in tmp_file.readlines():
  58.             # the syntax says, only at start of line
  59.             if string.find(line, possible_rom) == 0:
  60.                 tmpstr = string.lstrip(line[len(possible_rom):])
  61.                 p = re.compile(r'\012')
  62.                 str = p.split(tmpstr)[0]
  63.                 tmp_dir = posixpath.normpath(dir)
  64.                 if string.find(tmp_dir,posixpath.normpath(basedir)) == 0:
  65.                     temp_dir = tmp_dir[len(posixpath.normpath(basedir))+1:]
  66.                 else:
  67.                     temp_dir = tmp_dir
  68.                 print temp_dir+":"+possible_rom+":"+str
  69.         os.unlink("/tmp/process_collection_dir.tmp")
  70.  
  71. for file in os.listdir(basedir):
  72.     checkdir(file,basedir)
  73.