I made a simplish script to create a symlink farm, I have a following
kind of structure

media/Music/Downloaded/various_semi_structured_paths
media/Music/Physical/album_name/flac/tracknum-song_title.flac
media/Music/Physical/album_name/mp3/tracknum-song_title.mp3

and then

media/symlink_farm

The following python script is in media/symlink_farm:


Code:
--------------------
    
  #!/usr/bin/python
  
  import os,sys
  # get this from hrnjad.net/src/6/scriptutil.py
  import scriptutil as SU
  
  # Absolute path to this script
  farm_path =  os.path.dirname( os.path.realpath( __file__ ) )
  # Paths to files by type
  flac_path = os.path.join(farm_path, 'flac')
  if not os.path.exists(flac_path):
  os.mkdir(flac_path)
  mp3_path = os.path.join(farm_path, 'mp3')
  if not os.path.exists(mp3_path):
  os.mkdir(mp3_path)
  mixed_path = os.path.join(farm_path, 'mixed')
  if not os.path.exists(mixed_path):
  os.mkdir(mixed_path)
  
  # dictionary for the mixed path (keyed by symlink target name without 
extension)
  mixed = {}
  
  # Helper to link files (to avoid code-duplication in the loops)
  def link_file(target_path, fpath, recurse_dnames):
  fpath_real = os.path.realpath(os.path.join(farm_path, fpath))
  fdir, fname = os.path.split(fpath_real)
  dname = os.path.split(fdir)[1]
  if dname.lower() in recurse_dnames:
  dname = os.path.split(os.path.split(fdir)[0])[1]
  targetname = os.path.join(target_path, dname, fname)
  mixed_key = os.path.splitext(os.path.join(dname, fname))[0]
  if not mixed.has_key(mixed_key):
  mixed[mixed_key] = fpath_real
  if not os.path.exists(os.path.dirname(targetname)):
  os.makedirs(os.path.dirname(targetname))
  if not os.path.exists(targetname):
  if os.path.lexists(targetname):
  print "%s exists but points to nonexisting file, removing and relinking" % 
targetname
  os.unlink(targetname)
  os.symlink(fpath_real, targetname)
  print "Linked %s -> %s" % (fpath_real, targetname)
  else:
  #print "Link %s -> %s already exists" % (fpath_real, targetname)
  pass
  
  # First find flacs, they thus get priority in the mixed dictionary
  flacs = SU.ffind('../Music/', shellglobs=('*.flac',))
  for fpath in flacs:
  link_file(flac_path, fpath, ('flac', 'flacs'))
  
  # Then find mp3s
  mp3s = SU.ffind('../Music/', shellglobs=('*.mp3',))
  for fpath in mp3s:
  link_file(mp3_path, fpath, ('mp3', 'mp3s'))
  
  # Finally create the mixed symlink farm
  for (key,fpath_real) in mixed.items():
  targetname =  os.path.join(mixed_path, key) + os.path.splitext(fpath_real)[1]
  #print "key=%s, fpath_real=%s, targetname=%s" % (key, fpath_real, targetname)
  if not os.path.exists(os.path.dirname(targetname)):
  os.makedirs(os.path.dirname(targetname))
  if not os.path.exists(targetname):
  if os.path.lexists(targetname):
  print "%s exists but points to nonexisting file, removing and relinking" % 
targetname
  os.unlink(targetname)
  os.symlink(fpath_real, targetname)
  print "Linked %s -> %s" % (fpath_real, targetname)
  else:
  #print "Link %s -> %s already exists" % (fpath_real, targetname)
  pass
  
--------------------


And it tries to build somewhat sane symlink farm, all flacs in tree,
all mp3s in other tree and the third tree will prefer flacs over mp3s
(though the heuristic for "same file" is very simple)

Hope that helps.

link to scriptutil.py is missing the usual prefix due to the
no-links-in-first-post -policy

Script tested on Nexenta (Opensolaris), it will almost certainly work
on linux/osx and probably work on Windows too (once you have installed
python, that is)


-- 
mashiara
------------------------------------------------------------------------
mashiara's Profile: http://forums.slimdevices.com/member.php?userid=44370
View this thread: http://forums.slimdevices.com/showthread.php?t=76431

_______________________________________________
unix mailing list
[email protected]
http://lists.slimdevices.com/mailman/listinfo/unix

Reply via email to