Fix minor bugs in the logging for programme clashes.
[recordtv.git] / src / rtv_delete.py
blobfb96b132d9b05f94912b322b090d0b1a72b05dd6
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 def collect_walk_no_slashes( top_dir ):
13 len_top_dir = len( top_dir ) + 1
14 ret = {}
15 for ( dirpath, dirnames, filenames ) in os.walk( top_dir ):
16 if os.path.basename( dirpath ) == "deleted":
17 continue
18 for filename in filenames:
19 filepath = os.path.join( dirpath[len_top_dir:], filename )
20 filepath_noslashes = filepath.replace( "/", "_" )
21 ret[filepath_noslashes] = filepath
22 return ret
24 def delete( config ):
26 print
27 print "Deleting:"
29 deleted_dir = os.path.join( config.converted_progs_dir, "deleted" )
31 rtv_utils.ensure_dir_exists( deleted_dir )
33 noslash_to_real = collect_walk_no_slashes( config.converted_progs_dir )
34 noslash_filenames = noslash_to_real.keys()
36 old_dir = os.path.join( config.recorded_progs_dir, "old" )
38 for fn in os.listdir( deleted_dir ):
39 if fn in noslash_filenames:
40 _delete_file( config.converted_progs_dir, noslash_to_real[fn] )
41 else:
42 print "Filename to delete '%s' not found!" % fn
44 rtvinfo_fn = fn[ : fn.rfind( "." ) ] + ".rtvinfo"
45 if rtvinfo_fn in noslash_filenames:
47 # Move the old .rtvinfo file into the old directory
48 # so we can avoid re-recording things that we've
49 # already seen and deleted.
50 rtv_utils.ensure_dir_exists( old_dir )
52 rtvinfo_fullpath = os.path.join( config.converted_progs_dir,
53 noslash_to_real[rtvinfo_fn] )
55 #print "mv %s -> %s" % ( rtvinfo_fullpath, old_dir )
56 shutil.move( rtvinfo_fullpath, old_dir )
58 del_path = os.path.join( deleted_dir, fn )
59 #print "rm %s" % del_path
60 os.remove( del_path )