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])