Copy rtvinfo files into local "old" dir before moving them to the server.
[recordtv.git] / src / rtv_delete.py
blob990b3f736aadde64234d919e22484d43080f4744
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 mp4path = fullpath + "_high.mp4"
18 if os.path.isfile( mp4path ):
19 print mp4path
20 os.remove( mp4path )
22 def collect_walk_no_slashes( top_dir ):
23 len_top_dir = len( top_dir ) + 1
24 ret = {}
25 for ( dirpath, dirnames, filenames ) in os.walk( top_dir ):
26 if os.path.basename( dirpath ) == "deleted":
27 continue
28 for filename in filenames:
29 filepath = os.path.join( dirpath[len_top_dir:], filename )
30 filepath_noslashes = filepath.replace( "/", "_" )
31 ret[filepath_noslashes] = filepath
32 return ret
34 def delete( config ):
36 print
37 print "Deleting:"
39 deleted_dir = os.path.join( config.converted_progs_dir, "deleted" )
41 rtv_utils.ensure_dir_exists( deleted_dir )
43 noslash_to_real = collect_walk_no_slashes( config.converted_progs_dir )
44 noslash_filenames = noslash_to_real.keys()
46 old_dir = os.path.join( config.recorded_progs_dir, "old" )
48 for fn in os.listdir( deleted_dir ):
49 if fn in noslash_filenames:
50 _delete_file( config.converted_progs_dir, noslash_to_real[fn] )
51 else:
52 print "Filename to delete '%s' not found!" % fn
54 rtvinfo_fn = fn[ : fn.rfind( "." ) ] + ".rtvinfo"
55 if rtvinfo_fn in noslash_filenames:
57 # Move the old .rtvinfo file into the old directory
58 # so we can avoid re-recording things that we've
59 # already seen and deleted.
60 rtv_utils.ensure_dir_exists( old_dir )
62 rtvinfo_fullpath = os.path.join( config.converted_progs_dir,
63 noslash_to_real[rtvinfo_fn] )
65 #print "mv %s -> %s" % ( rtvinfo_fullpath, old_dir )
66 shutil.move( rtvinfo_fullpath, old_dir )
68 del_path = os.path.join( deleted_dir, fn )
69 #print "rm %s" % del_path
70 os.remove( del_path )