Merge branch 'master' of ssh://repo.or.cz/srv/git/recordtv
[recordtv.git] / src / rtv_delete.py
blobf33c309cf4987d0faa474def1c6f644bfe609a9e
1 #!/usr/bin/python
3 import os
4 import shutil
5 import rtv_utils
7 def _delete_file( base_dir, filename ):
8 fullpath = os.path.join( base_dir, filename )
9 print fullpath
10 os.remove( fullpath )
12 mp3path = fullpath + ".mp3"
13 if os.path.isfile( mp3path ):
14 print mp3path
15 os.remove( mp3path )
17 def collect_walk_no_slashes( top_dir ):
18 len_top_dir = len( top_dir ) + 1
19 ret = {}
20 for ( dirpath, dirnames, filenames ) in os.walk( top_dir ):
21 if os.path.basename( dirpath ) == "deleted":
22 continue
23 for filename in filenames:
24 filepath = os.path.join( dirpath[len_top_dir:], filename )
25 filepath_noslashes = filepath.replace( "/", "_" )
26 ret[filepath_noslashes] = filepath
27 return ret
29 def delete( config ):
31 print
32 print "Deleting:"
34 deleted_dir = os.path.join( config.converted_progs_dir, "deleted" )
36 rtv_utils.ensure_dir_exists( deleted_dir )
38 noslash_to_real = collect_walk_no_slashes( config.converted_progs_dir )
39 noslash_filenames = noslash_to_real.keys()
41 old_dir = os.path.join( config.recorded_progs_dir, "old" )
43 for fn in os.listdir( deleted_dir ):
44 if fn in noslash_filenames:
45 _delete_file( config.converted_progs_dir, noslash_to_real[fn] )
46 else:
47 print "Filename to delete '%s' not found!" % fn
49 rtvinfo_fn = fn[ : fn.rfind( "." ) ] + ".rtvinfo"
50 if rtvinfo_fn in noslash_filenames:
52 # Move the old .rtvinfo file into the old directory
53 # so we can avoid re-recording things that we've
54 # already seen and deleted.
55 rtv_utils.ensure_dir_exists( old_dir )
57 rtvinfo_fullpath = os.path.join( config.converted_progs_dir,
58 noslash_to_real[rtvinfo_fn] )
60 #print "mv %s -> %s" % ( rtvinfo_fullpath, old_dir )
61 shutil.move( rtvinfo_fullpath, old_dir )
63 del_path = os.path.join( deleted_dir, fn )
64 #print "rm %s" % del_path
65 os.remove( del_path )