📄 cvsdb.py
字号:
while tableList.count(table) > 1: tableList.remove(table) tables = string.join(tableList, ",") conditions = string.join(condList, " AND ") ## limit the number of rows requested or we could really slam ## a server with a large database limit = "" if cfg.cvsdb.row_limit: limit = "LIMIT %s" % (str(cfg.cvsdb.row_limit)) sql = "SELECT checkins.* FROM %s WHERE %s %s %s" % ( tables, conditions, order_by, limit) return sql def RunQuery(self, query): sql = self.CreateSQLQueryString(query) cursor = self.db.cursor() cursor.execute(sql) while 1: row = cursor.fetchone() if not row: break (dbType, dbCI_When, dbAuthorID, dbRepositoryID, dbDirID, dbFileID, dbRevision, dbStickyTag, dbBranchID, dbAddedLines, dbRemovedLines, dbDescID) = row commit = CreateCommit() ## TIME, TIME, TIME is all fucked up; dateobject.gmticks() ## is broken, dateobject.ticks() returns somthing like ## GMT ticks, except it forgets about daylight savings ## time -- we handle it ourself in the following painful way gmt_time = time.mktime( (dbCI_When.year, dbCI_When.month, dbCI_When.day, dbCI_When.hour, dbCI_When.minute, dbCI_When.second, 0, 0, dbCI_When.dst)) commit.SetTime(gmt_time) commit.SetFile(self.GetFile(dbFileID)) commit.SetDirectory(self.GetDirectory(dbDirID)) commit.SetRevision(dbRevision) commit.SetRepository(self.GetRepository(dbRepositoryID)) commit.SetAuthor(self.GetAuthor(dbAuthorID)) commit.SetBranch(self.GetBranch(dbBranchID)) commit.SetPlusCount(dbAddedLines) commit.SetMinusCount(dbRemovedLines) commit.SetDescription(self.GetDescription(dbDescID)) query.AddCommit(commit) def CheckCommit(self, commit): repository_id = self.GetRepositoryID(commit.GetRepository(), 0) if repository_id == None: return None dir_id = self.GetDirectoryID(commit.GetDirectory(), 0) if dir_id == None: return None file_id = self.GetFileID(commit.GetFile(), 0) if file_id == None: return None sql = "SELECT * FROM checkins WHERE "\ " repositoryid=%s AND dirid=%s AND fileid=%s AND revision=%s" sql_args = (repository_id, dir_id, file_id, commit.GetRevision()) cursor = self.db.cursor() cursor.execute(sql, sql_args) try: (ci_type, ci_when, who_id, repository_id, dir_id, file_id, revision, sticky_tag, branch_id, plus_count, minus_count, description_id) = cursor.fetchone() except TypeError: return None return commit## the Commit class holds data on one commit, the representation is as## close as possible to how it should be committed and retrieved to the## database engineclass Commit: ## static constants for type of commit CHANGE = 0 ADD = 1 REMOVE = 2 def __init__(self): self.__directory = '' self.__file = '' self.__repository = '' self.__revision = '' self.__author = '' self.__branch = '' self.__pluscount = '' self.__minuscount = '' self.__description = '' self.__gmt_time = 0.0 self.__type = Commit.CHANGE def SetRepository(self, repository): ## clean up repository path; make sure it doesn't end with a ## path seperator while len(repository) and repository[-1] == os.sep: repository = repository[:-1] self.__repository = repository def GetRepository(self): return self.__repository def SetDirectory(self, dir): ## clean up directory path; make sure it doesn't begin ## or end with a path seperator while len(dir) and dir[0] == os.sep: dir = dir[1:] while len(dir) and dir[-1] == os.sep: dir = dir[:-1] self.__directory = dir def GetDirectory(self): return self.__directory def SetFile(self, file): ## clean up filename; make sure it doesn't begin ## or end with a path seperator while len(file) and file[0] == os.sep: file = file[1:] while len(file) and file[-1] == os.sep: file = file[:-1] self.__file = file def GetFile(self): return self.__file def SetRevision(self, revision): self.__revision = revision def GetRevision(self): return self.__revision def SetTime(self, gmt_time): self.__gmt_time = float(gmt_time) def GetTime(self): return self.__gmt_time def SetAuthor(self, author): self.__author = author def GetAuthor(self): return self.__author def SetBranch(self, branch): if not branch: self.__branch = '' else: self.__branch = branch def GetBranch(self): return self.__branch def SetPlusCount(self, pluscount): self.__pluscount = pluscount def GetPlusCount(self): return self.__pluscount def SetMinusCount(self, minuscount): self.__minuscount = minuscount def GetMinusCount(self): return self.__minuscount def SetDescription(self, description): self.__description = description def GetDescription(self): return self.__description def SetTypeChange(self): self.__type = Commit.CHANGE def SetTypeAdd(self): self.__type = Commit.ADD def SetTypeRemove(self): self.__type = Commit.REMOVE def GetType(self): return self.__type def GetTypeString(self): if self.__type == Commit.CHANGE: return 'Change' elif self.__type == Commit.ADD: return 'Add' elif self.__type == Commit.REMOVE: return 'Remove'## QueryEntry holds data on one match-type in the SQL database## match is: "exact", "like", or "regex"class QueryEntry: def __init__(self, data, match): self.data = data self.match = match## CheckinDatabaseQueryData is a object which contains the search parameters## for a query to the CheckinDatabaseclass CheckinDatabaseQuery: def __init__(self): ## sorting self.sort = "date" ## repository to query self.repository_list = [] self.branch_list = [] self.directory_list = [] self.file_list = [] self.author_list = [] ## date range in DBI 2.0 timedate objects self.from_date = None self.to_date = None ## list of commits -- filled in by CVS query self.commit_list = [] ## commit_cb provides a callback for commits as they ## are added self.commit_cb = None def SetRepository(self, repository, match = "exact"): self.repository_list.append(QueryEntry(repository, match)) def SetBranch(self, branch, match = "exact"): self.branch_list.append(QueryEntry(branch, match)) def SetDirectory(self, directory, match = "exact"): self.directory_list.append(QueryEntry(directory, match)) def SetFile(self, file, match = "exact"): self.file_list.append(QueryEntry(file, match)) def SetAuthor(self, author, match = "exact"): self.author_list.append(QueryEntry(author, match)) def SetSortMethod(self, sort): self.sort = sort def SetFromDateObject(self, ticks): self.from_date = dbi.TimestampFromTicks(ticks) def SetToDateObject(self, ticks): self.to_date = dbi.TimestampFromTicks(ticks) def SetFromDateHoursAgo(self, hours_ago): ticks = time.time() - (3600 * hours_ago) self.from_date = dbi.TimestampFromTicks(ticks) def SetFromDateDaysAgo(self, days_ago): ticks = time.time() - (86400 * days_ago) self.from_date = dbi.TimestampFromTicks(ticks) def SetToDateDaysAgo(self, days_ago): ticks = time.time() - (86400 * days_ago) self.to_date = dbi.TimestampFromTicks(ticks) def AddCommit(self, commit): self.commit_list.append(commit) if self.commit_cb: self.commit_cb(commit) def SetCommitCB(self, callback): self.commit_cb = callback#### entrypoints##def CreateCheckinDatabase(host, user, passwd, database): return CheckinDatabase(host, user, passwd, database) def CreateCommit(): return Commit() def CreateCheckinQuery(): return CheckinDatabaseQuery()def ConnectDatabaseReadOnly(): global gCheckinDatabaseReadOnly if gCheckinDatabaseReadOnly: return gCheckinDatabaseReadOnly gCheckinDatabaseReadOnly = CreateCheckinDatabase( cfg.cvsdb.host, cfg.cvsdb.readonly_user, cfg.cvsdb.readonly_passwd, cfg.cvsdb.database_name) gCheckinDatabaseReadOnly.Connect() return gCheckinDatabaseReadOnlydef ConnectDatabase(): global gCheckinDatabase gCheckinDatabase = CreateCheckinDatabase( cfg.cvsdb.host, cfg.cvsdb.user, cfg.cvsdb.passwd, cfg.cvsdb.database_name) gCheckinDatabase.Connect() return gCheckinDatabasedef RLogDataToCommitList(repository, rlog_data): commit_list = [] ## the filename in rlog_data contains the entire path of the ## repository; we strip that out here temp = rlog_data.filename[len(repository):] directory, file = os.path.split(temp) for rlog_entry in rlog_data.rlog_entry_list: commit = CreateCommit() commit.SetRepository(repository) commit.SetDirectory(directory) commit.SetFile(file) commit.SetRevision(rlog_entry.revision) commit.SetAuthor(rlog_entry.author) commit.SetDescription(rlog_entry.description) commit.SetTime(rlog_entry.time) commit.SetPlusCount(rlog_entry.pluscount) commit.SetMinusCount(rlog_entry.minuscount) commit.SetBranch(rlog_data.LookupBranch(rlog_entry)) if rlog_entry.type == rlog_entry.CHANGE: commit.SetTypeChange() elif rlog_entry.type == rlog_entry.ADD: commit.SetTypeAdd() elif rlog_entry.type == rlog_entry.REMOVE: commit.SetTypeRemove() commit_list.append(commit) return commit_listdef GetCommitListFromRCSFile(repository, filename): try: rlog_data = rlog.GetRLogData(cfg, filename) except rlog.error, e: raise error, e commit_list = RLogDataToCommitList(repository, rlog_data) return commit_listdef GetUnrecordedCommitList(repository, filename): commit_list = GetCommitListFromRCSFile(repository, filename) db = ConnectDatabase() unrecorded_commit_list = [] for commit in commit_list: result = db.CheckCommit(commit) if not result: unrecorded_commit_list.append(commit) return unrecorded_commit_list
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -