class Command(NoArgsCommand):
    help = "import testcase to test case db."
    
    option_list = NoArgsCommand.option_list + (
        make_option('--path', default=None, dest='path',
            help='update path'),
        make_option('--status', default='A', dest='status', 
            help='A=Add (default), D=Deleted '),
        make_option('--branch', default='trunk', dest='branch', 
            help='default is "trunk" '),            
    )

    requires_model_validation = False
    
    def handle_noargs(self, path='', status='A', branch='trunk', **param):
        import logging
        self.logger = logging.getLogger("db.update")
        self.logger.info("status=%s, path=%s" % (status, path))
        self.svnclient = CliClient(work_path=CONFIG.TESTCASE_ROOT)
        
        if (not os.path.exists(path)):
            self.logger.warning("not exists file:%s" % path)
            return
        
        info = self.format_path(branch, path)
        if os.path.isdir(path):
            self.update_directory(status, info)
        elif status == 'D':
            self.remove_test_case_file(info)
        else:
            self.update_test_case_file(info)
            
        self.logger.info("updated path=%s" % path)
    
    def update_test_case_file(self, path_info):
        f, created = IndexFile.objects.get_or_create(uuid=path_info.uuid)
        if created or not f.path:
            f.path = path_info.path
            f.file_type = 'F'
            f.status = 'U'
            f.branch = path_info.branch
            
        info = self.svnclient.info(path_info.abspath)
        f.svn_author = info['author']
        f.svn_lastupdated = info['date']
        f.svn_version = info['revision']
        f.save()
        self.__update_robot_test_case(f, path_info)
        
    def __update_robot_test_case(self, index_file, path_info):
        for e in RobotIndexItem.objects.filter(file=index_file):
            e.delete()
        try:
            main_suite = RobotTestSuite(path_info.abspath)            
            self.__add_suite(index_file, main_suite, "%s,%s" % (index_file.branch, 
                                                                path_info.path))
        except Exception, e:
            if str(e).count("contains no test cases") == 0: raise
            self.logger.info("'%s' is not a robot scripts." % path_info.path)
 def handle_noargs(self, path='', status='A', branch='trunk', **param):
     import logging
     self.logger = logging.getLogger("db.update")
     self.logger.info("status=%s, path=%s" % (status, path))
     self.svnclient = CliClient(work_path=CONFIG.TESTCASE_ROOT)
     
     if (not os.path.exists(path)):
         self.logger.warning("not exists file:%s" % path)
         return
     
     info = self.format_path(branch, path)
     if os.path.isdir(path):
         self.update_directory(status, info)
     elif status == 'D':
         self.remove_test_case_file(info)
     else:
         self.update_test_case_file(info)
         
     self.logger.info("updated path=%s" % path)