def main(): des = u'import Repository info' prog= u'AddRepository' ver = u'%prog v0.0.1' usage = u'%prog ip_address platform_path' parse = OptionParser(description=des, prog=prog, version=ver, usage=usage, add_help_option=True) parse.add_option('-t', '--server-ip', action='store', type='string', dest='ip_address', help='repository server ip address') parse.add_option('-p', '--platform-path', action='store', type='string', dest='platform_path', help='repository platform name') option, args = parse.parse_args() ip_address = option.ip_address platform_path = option.platform_path server_sql = "SELECT id FROM repo_server WHERE ip_address='%s'" % ip_address # mysql db_url = {'host': '192.168.33.7', 'db_name': 'tms', 'username': '******', 'password': '******'} db_option = DBOption(db_url, 'mysql') db_option.db_connect() server = db_option.fetch_one(server_sql) server_id = server[0] platform_sql = "SELECT id FROM repo_platform WHERE server_id=%s and path='%s'" % (server_id, platform_path) print "platform_sql:%s" % platform_sql platform = db_option.fetch_one(platform_sql) if platform: platform_id = platform[0] else: platform_insert_sql = "INSERT INTO repo_platform (name, path, server_id) VALUES ('%s', '%s', %s)" % \ (os.path.basename(platform_path), platform_path, server_id) print "platform_insert_sql:%s" % platform_insert_sql db_option.insert_one(platform_insert_sql) platform_id = db_option.fetch_one(platform_sql) for repo_path in platform_repo(platform_path): # 获取仓库数据库的id或添加仓库入数据库 repo_sql = "SELECT id FROM repo_repo WHERE path='%s' and platform_id=%s" % (repo_path, platform_id) print "repo_sql:%s" % repo_sql repo_obj = db_option.fetch_one(repo_sql) if repo_obj: repo_id = repo_obj[0] else: insert_repo = "INSERT INTO repo_repo (name, path, platform_id) VALUE ('%s', '%s', %s)" % \ (os.path.basename(repo_path).split('.git')[0], repo_path, platform_id) print "insert_repo:%s" % insert_repo repo_id = db_option.insert_one(insert_repo) # 初始化Git仓库操作对象 repo = GitRepository(repo_path) # 添加分支或tag入数据库 add_refs(db_option, repo, repo_id) # 添加commit记录入数据库 add_commits(db_option, repo, repo_id)
def main(): """ 1. 导入服务器上的所有平台 2. 导入平台下的所有仓库 """ des = u'import platform and Repository' prog= u'AddRepository' ver = u'%prog v0.0.1' usage = u'%prog ip_address platform_path dir_path' parse = OptionParser(description=des, prog=prog, version=ver, usage=usage, add_help_option=True) parse.add_option('-t', '--server-ip', action='store', type='string', dest='ip_address', help='repository server ip address') parse.add_option('-p', '--platform-name', action='store', type='string', dest='platform_name', help='repository platform name') # 解析命令行参数 options, args = parse.parse_args() ip_address = options.ip_address platform_name = options.platform_name.strip('/') # mysql db_url = {'host': '192.168.31.91', 'db_name': 'tms', 'username': '******', 'password': '******'} # sqlite #db_url = '/home/roy/workspace/tms/tms-dev.sqlite' db_option = DBOption(db_url, 'mysql') db_option.db_connect() server_sql = "SELECT id, root_path FROM repo_server WHERE ip_address='%s' " % ip_address server = db_option.fetch_one(server_sql) server_id = server[0] server_root_path = server[1] platform_path = os.path.join(server_root_path, platform_name) platform_sql = "SELECT id FROM repo_platform WHERE path='%s' AND server_id=%s" % (platform_path, server_id) platform = db_option.fetch_one(platform_sql) if not platform: platform_insert_sql = "INSERT INTO repo_platform (name, path, server_id) VALUES ('%s', '%s', %s)" % \ (platform_name, platform_path, server_id) db_option.insert_one(platform_insert_sql) platform = db_option.fetch_one(platform_sql) platform_id = platform[0] repo_paths = get_all_repo(platform_path) repo_sql = "SELECT path FROM repo_repo WHERE platform_id=%s" % platform_id exists_repo_paths = db_option.fetch_all(repo_sql) if exists_repo_paths: exists_repo_paths = [tmp[0] for tmp in exists_repo_paths] repo_values = [] for repo_path in repo_paths: if repo_path in exists_repo_paths: continue repo_name = repo_path.split(platform_path)[1].split('.git')[0].strip('/') repo_values.append((repo_name, repo_path, platform_id)) if repo_values: repo_insert_sql = "INSERT INTO repo_repo (name, path, platform_id) values (%s, %s, %s)" for r in range(len(repo_values)/50+1): db_option.insert_all(repo_insert_sql, repo_values[r*50:(r+1)*50])
def add_commit_reference(logger): # mysql操作对象初始化 db_url = {'host': '192.168.33.7', 'db_name': 'tms', 'username': '******', 'password': '******'} db_option = DBOption(db_url, 'mysql') db_option.db_connect() record_sql = "SELECT history.id, history.repository_path, history.push_type, history.commits, " \ "history.reference_name, repo_server.root_path, repo_server.backup_server_path, repo.id " \ "FROM repo_push_history AS history JOIN repo_server ON history.server_ip=repo_server.ip_address " \ "JOIN repo_repo AS repo ON repo.path=history.repository_path WHERE history.status=1 and " \ "history.server_ip IN('192.168.33.9', '192.168.33.13') LIMIT 100 " new_references = [] new_commits = [] for record in db_option.fetch_all(record_sql): logger.info(record[7]) row_id, repo_path, push_type, commits, reference_name, root_path, backup_server_path, repo_id = \ record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7] if push_type == 'create_reference': if reference_name.strip().startswith('refs/heads/'): new_references.append((reference_name, 1, repo_id)) else: new_references.append((reference_name, 0, repo_id)) elif push_type == 'update_reference': repo_path = repo_path.replace(root_path, backup_server_path) repo_obj = GitRepository(repo_path) if commits.find('/') > 0: for commit in repo_obj.diff_commit(commits.split('/')[0], commits.split('/')[1]): if len(commit) == 9: new_commits.append((commit[0], '', commit[1], commit[2], commit[3], commit[4], commit[5].split('+')[0], commit[6], commit[7], commit[8].split('+')[0], repo_id)) else: new_commits.append((commit[0], commit[1], commit[2], commit[3], commit[4], commit[5].split('+')[0], commit[6], commit[7], commit[8].split('+')[0], commit[9], repo_id)) else: commit = repo_obj.log_commit(commits) if len(commit) == 9: new_commits.append((commit[0], '', commit[1], commit[2], commit[3], commit[4], commit[5].split('+')[0], commit[6], commit[7], commit[8].split('+')[0], repo_id)) else: new_commits.append((commit[0], commit[1], commit[2], commit[3], commit[4], commit[5].split('+')[0], commit[6], commit[7], commit[8].split('+')[0], commit[9], repo_id)) elif push_type == 'delete_reference': update_reference_sql = "UPDATE repo_refs set status=1, push_id={0} WHERE repository_id={1} " \ "and name='{2}'".format(row_id, repo_id, reference_name) db_option.insert_one(update_reference_sql) update_sql = "UPDATE repo_push_history set status=3, import_time='{0}' " \ "WHERE id={1}".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), row_id) db_option.insert_one(update_sql) # 新增的分支和tag添加入数据库 if new_references: insert_refs_sql = "INSERT INTO repo_refs (name, category, repository_id) VALUES (%s, %s, %s)" db_option.insert_all(insert_refs_sql, new_references) # 新增的commit添加入数据库 if new_commits: insert_commit_sql = "INSERT INTO repo_commit (commit_hash, parent_hash, tree_hash, author_name, author_email, " \ "author_time, committer_name, committer_email, commit_time, message, repository_id) " \ "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" print new_commits db_option.insert_all(insert_commit_sql, new_commits) db_option.close()