def Del_sql(fname, cursor=cursor, mconn=mconn): global FileDEL global HSdel ttDel = [] try: cursor.execute('''use fortest;''') cursor.execute('''set sql_safe_updates = 0''') cursor.execute('''''') for fi in fname: cursor.execute('''delete from fmate_code where namepath = %s''', [fi]) ttDel.append(fi) #pdb.set_trace() cursor.execute( '''select hashid from hash_code where hashid not in (select hashid from fmate_code)''' ) #predel = [ pi[0] for pi in cursor.fetchall() ] predel = cursor.fetchall() if predel: cursor.executemany( '''delete from fline_code where hashid = %s''', predel) cursor.executemany( '''delete from hash_code where hashid = %s''', predel) cursor.execute('''set sql_safe_updates = 1''') mconn.commit() HSdel = len(predel) FileDEL = FileDEL + ttDel logger.info('Execute Del_sql() Success!') except: logger.exception('Failure Of Del_sql:') logger.warn('FAILURE OF DEL_SQL()!')
def fidAdd_sql(fname, hs, cursor=cursor, mconn=mconn): global FailCount global AddOkCount global FaiList #get fmtime,fctime,fcontent logger.debug('get fmtime,fctime,fcontent') fmate = [] fmt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(fname))) fct = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(fname))) fmate.append(fname) fmate.append(fmt) fmate.append(fct) try: lhs = [hs] flineargs = GetContent(fname) cursor.execute('''use fortest;''') cursor.execute('''insert into hash_code( strhash ) values( %s );''', lhs) cursor.execute('''select last_insert_id() into @lshs;''') cursor.execute( '''insert into fmate_code( namepath, hashid, fmtime, fctime ) values( %s, @lshs, %s, %s );''', fmate) cursor.executemany( '''insert into fline_code( hashid, line, flid ) values( @lshs, %s, %s )''', flineargs) mconn.commit() AddOkCount = AddOkCount + 1 except: pdb.set_trace() logger.exception('Error msg') logger.info('fidAdd_sql--->Failed to write to Sql:\n%r', fname) MailCount = FailCount + 1 FailList.append(fname)
def fidMCR_sql(fname, hs, cursor=cursor, mconn=mconn): logger.debug('fidMCR_sql:本函数执行移动复制重命名文件的写入') global FailCount global McrOkCount global FaiList #get fmtime,fctime,fcontent logger.debug('get fmtime,fctime,fcontent') fmate = [] fmt = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(fname))) fct = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getmtime(fname))) fmate.append(fname) fmate.append(fmt) fmate.append(fct) lhs = [hs] try: #pdb.set_trace() cursor.execute('''use fortest;''') cursor.execute( '''select hashid from hash_code where strhash = %s into @filehs''', lhs) cursor.execute( '''insert into fmate_code( namepath, hashid, fmtime, fctime ) values( %s, @filehs, %s, %s );''', fmate) mconn.commit() logger.info('MCRing --->%r', fname) McrOkCount = McrOkCount + 1 except: pdb.set_trace() logger.exception('Error msg') logger.info('fidMCR_sql--->Failed to write to Sql:\n%r', fname) MailCount = FailCount + 1 FailList.append(fname)
def fRefresh_sql(nfmate, cursor=cursor, mconn=mconn): global FailCount global FileRefresh for nfi in nfmate: #print(nfi) cursor.execute('''use fortest;''') cursor.execute( '''select hashid from fmate_code where namepath = %s into @oldhashid''', nfi[0:1]) cursor.execute( '''select count(namepath) from fmate_code where hashid = @oldhashid''' ) c_nfi = cursor.fetchall()[0][0] logger.debug('c_nfi = %r', c_nfi) try: flineargs = GetContent(nfi[0]) #暂时:这里以文件时间判断为准,即使hash值一样,也会运行更新程序 if nfi[3] == nfi[4]: cursor.execute('''select @oldhashid into @lshs''') else: cursor.execute( '''insert into hash_code( strhash ) values( %s )''', nfi[3:4]) cursor.execute('''select last_insert_id() into @lshs''') #更新文件信息 cursor.execute( '''update fmate_code set hashid = @lshs, fctime = %s, fmtime = %s where namepath = %s''', nfi[-3:-6:-1]) mconn.commit() if c_nfi > 1 and 0: cursor.executemany( '''insert into fline_code( hashid, line, flid ) values( @lshs, %s, %s )''', flineargs[0]) elif c_nfi == 1: pdb.set_trace() cursor.execute('''update fline_code set hashid = @lshs where hashid = @oldhashid ''') if nfi[3] != nfi[4]: cursor.execute( '''delete from hash_code where hashid = @oldhashid''') cursor.execute('''update fline_code set fstatus = 7 where hashid = @lshs and fstatus = 1''') cursor.execute('''update fline_code set fstatus = -1 where hashid = @lshs and fstatus = 0''') mconn.commit() flNew = [] flidChange = [] for fline in flineargs: cursor.execute( '''select lid, line, flid from fline_code where hashid = @lshs and fstatus in ( 7,-1 ) and line = %s limit 1''', [fline[0]]) fli_sql = cursor.fetchall() if not fli_sql: pdb.set_trace() flNew.append(fline) elif fli_sql[0][2] == fline[1]: cursor.execute( '''update fline_code set fstatus = 1 where lid = %s''', fli_sql[0][:1]) else: flidChange.append(fline[1:] + (fli_sql[0][0], )) cursor.execute( '''update fline_code set fstatus = 1 where lid = %s''', fli_sql[0][:1]) pdb.set_trace() cursor.executemany( '''insert into fline_code( hashid, line, flid ) values( @lshs, %s, %s )''', flNew) cursor.executemany( '''update fline_code set flid = %s, fstatus = 1 where lid = %s''', flidChange) cursor.execute('''update fline_code set fstatus = 0 where hashid = @lshs and fstatus = 7''' ) cursor.execute('''delete from fline_code where hashid = @lshs and fstatus = -1''' ) mconn.commit() FileRefresh.append(nfi[0]) logger.info('Refresh:\t%r', nfi[0]) except: logger.exception('Refresh Failed:\t%s', nfi[0]) logger.warn('Refresh Failed:\t%s', nfi[0]) FailCount += 1
#这里传递的参数,除了包含路径的完整的文件名,还有相应的信息,我觉得每个文件可以看做一个对象 #是文件class的一个实例。而fidAdd_sql()接收的参数就是每一个文件实例。 """ if fsh[1] in HsIsql: fidMCR_sql(*fsh) else: fidAdd_sql(*fsh) logger.info('fnum_sql: %d', fnum_sql) logger.info('fnum_disk: %d', fnum_disk) logger.info('FailCount: %d', FailCount) logger.info('AddOkCount: %d', AddOkCount) logger.info('McrOkCount: %d', McrOkCount) for Fli in FailList: print(Fli) preDel = f_sql - f_disk Del_sql(preDel) logger.info('HSdel: %d', HSdel) logger.info('DelCount: %d', len(FileDEL)) except: logger.exception('Fail to run codetsql!') """ fh_insql:在数据库中记录的文件名-hash值组合的集合,简称库文件 文件名-hash值组合:是判断独立文件的标志具有唯一性,文件名指的是包含路径的完整文件名 f_infolder:磁盘上实际存在的包含路径的完整文件名的集合 class fh_insql: connect():建立与数据库的链接 getfname():获取数据库中文件名,返回一个集合 数据库文件记录的删除: 先删除fid对文件内容标志hash值的指向,此时如果没有其他fid指向这个hash,则删除这个hash值指向的文件内容 """