remove_files.py 1.79 KB
#!/usr/bin/env python

import sys
import os
import re
import shutil
from collections import defaultdict
from optparse import OptionParser

# Solution with no hard coded path would be welcome
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), ".."))

from dfs.database import Database
from dfs.repo import Repo
from dfs.config import Config

def remove_file(wc, db, file_id, extensions, anno_per_file):
    try:
        db.remove(file_id)
    except Exception as ex:
        print str(ex)
        return False
    
    print "Removing "+file_id+" :"
    for ext in extensions:
        file = file_id+ext     
        for r in wc.remove(file, anno_per_file):
            print "\t - "+r
    
    return True

if __name__ == "__main__":
    optparser = OptionParser(usage="""usage: %prog [options] CONFIG FILES""")
    optparser.add_option("--extensions", dest="exts", default=".mmax,_mentions.xml,_words.xml",
            help="List of comma-separated file extensions")
    (options, args) = optparser.parse_args()
    if len(args) < 2:
        optparser.print_help()
        sys.exit(0)

    conf_path = args[0]
    cfg = Config(conf_path)
    ids = args[1:]
    extensions = options.exts.split(",")
    anno_per_file = int(cfg["anno_per_file"])
    wc = Repo(cfg["svn.repository"], cfg["svn.login"], cfg["svn.passwd"])
    db = Database(wc.db_path(), anno_per_file)
    
    success = []
    fail = []
    for file_id in ids:
        if remove_file(wc, db, file_id, extensions, anno_per_file):
            success.append(file_id)
        else:
            fail.append(file_id)       

    db.save()
    wc.commit("Removed files: "+str(ids))
            
    print ""
    if len(success) > 0:
        print "Removed files: "+str(success)
    if len(fail) > 0:
        print "Failed to remove files: "+str(fail)