|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# coding=utf-8 |
| 3 | +import sys |
| 4 | +import time |
| 5 | +import urllib.request |
| 6 | +import json |
| 7 | +import logging |
| 8 | +import traceback |
| 9 | +import codecs |
| 10 | +import os |
| 11 | +from sdk_index_gen import generate_all_index, get_all_repositories |
| 12 | +# import mail_send |
| 13 | + |
| 14 | +sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) |
| 15 | + |
| 16 | +class PackagesSync: |
| 17 | + |
| 18 | + |
| 19 | + def gogs_create_organization(self, org): |
| 20 | + GOGS_URL = os.environ["GOGS_URL"] |
| 21 | + GOGS_TOKEN = os.environ["GOGS_TOKEN"] |
| 22 | + |
| 23 | + url = '%s/api/v1/admin/users/root/orgs?token=%s' |
| 24 | + url = url % (GOGS_URL, GOGS_TOKEN) |
| 25 | + #print('url: '+url) |
| 26 | + data = '{"username": "%s"}' |
| 27 | + data = data % (org) |
| 28 | + data = data.encode('utf-8') |
| 29 | + headers = {"Content-type": "application/json"} |
| 30 | + request = urllib.request.Request(url, headers=headers, data=data) |
| 31 | + # request = urllib.request.Request(url, data, {'content-type': 'application/json'}) |
| 32 | + try: |
| 33 | + response = urllib.request.urlopen(request) |
| 34 | + resp = response.read().decode('utf-8') |
| 35 | + # except urllib.URLError,e: |
| 36 | + except Exception as e: |
| 37 | + logging.error('create organization: '+org+' failed') |
| 38 | + logging.error("Error message: {0}.".format(e)) |
| 39 | + # print('e.code : '+str(e.code) ) |
| 40 | + # print('e.reason: '+str(e.reason) ) |
| 41 | + resp = '{"id":0}' |
| 42 | + logging.error('create organization: '+org) |
| 43 | + #print(resp) |
| 44 | + return json.loads(resp) |
| 45 | + |
| 46 | + def gogs_get_or_create_organization(self, org): |
| 47 | + GOGS_URL = os.environ["GOGS_URL"] |
| 48 | + GOGS_TOKEN = os.environ["GOGS_TOKEN"] |
| 49 | + |
| 50 | + url = '%s/api/v1/orgs/%s?token=%s' |
| 51 | + url = url % (GOGS_URL, org, GOGS_TOKEN) |
| 52 | + # logging.error('gogs_get_or_create_organization url: '+url) |
| 53 | + request = urllib.request.Request(url) |
| 54 | + |
| 55 | + resp = '{"id":0}' # 默认返回空组织JSON |
| 56 | + try: |
| 57 | + response = urllib.request.urlopen(request) |
| 58 | + resp = response.read().decode('utf-8') |
| 59 | + except Exception as e: |
| 60 | + if not hasattr(e, 'code'): |
| 61 | + logging.error('Get organization: '+org+' failed, and no http code!') |
| 62 | + logging.error("Error message: {0}.".format(e)) |
| 63 | + return |
| 64 | + elif e.code == 404: |
| 65 | + return self.gogs_create_organization(org) |
| 66 | + elif e.code == 410: |
| 67 | + return self.gogs_create_organization(org) |
| 68 | + #logging.error(resp) |
| 69 | + return json.loads(resp) |
| 70 | + |
| 71 | + |
| 72 | + # POST /repos/migrate |
| 73 | + def gogs_migrate_repositories(self, org, repo, org_info): |
| 74 | + GOGS_URL = os.environ["GOGS_URL"] |
| 75 | + GOGS_TOKEN = os.environ["GOGS_TOKEN"] |
| 76 | + GITHUB_PROXY_URL = os.environ["GITHUB_PROXY_URL"] |
| 77 | + |
| 78 | + url = '%s/api/v1/repos/migrate?token=%s' |
| 79 | + url = url % (GOGS_URL, GOGS_TOKEN) |
| 80 | + #print('Migrate Repositories url: '+url) |
| 81 | + clone_addr = 'https://github.com/%s/%s.git' |
| 82 | + data = {} |
| 83 | + data['clone_addr'] = '%s/%s/%s.git' % (GITHUB_PROXY_URL, org, repo) # 这里指定镜像地址 |
| 84 | + data['mirror'] = True # 说明这是一个镜像仓库 |
| 85 | + data['uid'] = org_info['id'] |
| 86 | + data['repo_name'] = repo |
| 87 | + data = json.dumps(data) |
| 88 | + data = data.encode('utf-8') |
| 89 | + #print('data: '+data) |
| 90 | + request = urllib.request.Request(url, data, {'content-type': 'application/json'}) |
| 91 | + try: |
| 92 | + response = urllib.request.urlopen(request) |
| 93 | + resp = response.read().decode('utf-8') |
| 94 | + except Exception as e: |
| 95 | + logging.error('Migrate Repositories: '+org+'/'+repo+' failed') |
| 96 | + logging.error("Error message: {0}.".format(e)) |
| 97 | + # print('e.code : '+str(e.code) ) |
| 98 | + # print('e.reason: '+str(e.reason) ) |
| 99 | + logging.error('Migrate ' + org + ' ' + repo) |
| 100 | + #print(resp) |
| 101 | + |
| 102 | + #http://abc.com/api/v1/orgs/group_root_test/repos?token=token |
| 103 | + def gogs_get_or_create_Repositories(self, org, repo, org_info): |
| 104 | + GOGS_URL = os.environ["GOGS_URL"] |
| 105 | + GOGS_TOKEN = os.environ["GOGS_TOKEN"] |
| 106 | + |
| 107 | + url = '%s/api/v1/orgs/%s/repos?token=%s' |
| 108 | + url = url % (GOGS_URL, org, GOGS_TOKEN) |
| 109 | + request = urllib.request.Request(url) |
| 110 | + # 初始化resp变量 |
| 111 | + resp = '[]' # 默认空数组JSON字符串 |
| 112 | + |
| 113 | + try: |
| 114 | + response = urllib.request.urlopen(request) |
| 115 | + resp = response.read().decode('utf-8') |
| 116 | + except Exception as e: |
| 117 | + logging.error('Get: '+org+ ' Repositories: '+repo+' failed') |
| 118 | + logging.error("Error message: {0}.".format(e)) |
| 119 | + # print('e.code : '+str(e.code) ) |
| 120 | + # print('e.reason: '+str(e.reason) ) |
| 121 | + print(resp) |
| 122 | + org_repos_json = json.loads(resp) |
| 123 | + print(org_repos_json) |
| 124 | + for item in org_repos_json: |
| 125 | + if item['name'] == repo: |
| 126 | + print(item['name'] + ' Already existed, sync...') |
| 127 | + url = '%s/api/v1/repos/%s/%s/mirror-sync?token=%s' # /repos/:owner/:repo/mirror-sync |
| 128 | + url = url % (GOGS_URL, org, repo, GOGS_TOKEN) |
| 129 | + request = urllib.request.Request(url, data=''.encode('utf-8'), headers={'content-type': 'application/json'}) |
| 130 | + try: |
| 131 | + response = urllib.request.urlopen(request) |
| 132 | + resp = response.read().decode('utf-8') |
| 133 | + except Exception as e: |
| 134 | + logging.error('Get: '+org+ ' Repositories: '+repo+' failed') |
| 135 | + logging.error("Error message: {0}.".format(e)) |
| 136 | + # print('e.code : '+str(e.code) ) |
| 137 | + # print('e.reason: '+str(e.reason) ) |
| 138 | + return |
| 139 | + self.gogs_migrate_repositories(org, repo, org_info) |
| 140 | + |
| 141 | + def create_repo_in_gogs(self, org, repo_name): |
| 142 | + #logging.INFO('create_repo_in_gogs, org=[%s], repo_name=[%s]'%(org, repo_name)) |
| 143 | + org_info = self.gogs_get_or_create_organization(org) |
| 144 | + self.gogs_get_or_create_Repositories(org, repo_name, org_info) |
| 145 | + |
| 146 | + def fetch_packages_info_from_git(self, bsp_git_path): |
| 147 | + logging.info('======>Fetch package info from git repo: ' + bsp_git_path) |
| 148 | + tmp = bsp_git_path.split('/') |
| 149 | + |
| 150 | + org = tmp[3] # 获取 'RT-Thread-Studio' |
| 151 | + repo = tmp[4] # 获取 'sdk-bsp-rk3506-realthread-ruichingpi.git' |
| 152 | + repo_name = repo.replace('.git', '') # 去除.git后缀得到 'sdk-bsp-rk3506-realthread-ruichingpi' |
| 153 | + |
| 154 | + self.create_repo_in_gogs(org, repo_name) |
| 155 | + |
| 156 | + |
| 157 | +def get_gogs_access_token(): |
| 158 | + try: |
| 159 | + return os.environ["GOGS_TOKEN"] |
| 160 | + except Exception as e: |
| 161 | + logging.error("Error message: {0}.".format(e)) |
| 162 | + traceback.print_exc() |
| 163 | + print('get access token fail') |
| 164 | + |
| 165 | + |
| 166 | +def init_logger(): |
| 167 | + log_format = "%(module)s %(lineno)d %(levelname)s %(message)s \n" |
| 168 | + date_format = '%Y-%m-%d %H:%M:%S %a ' |
| 169 | + logging.basicConfig(level=logging.INFO, |
| 170 | + format=log_format, |
| 171 | + datefmt=date_format, |
| 172 | + ) |
| 173 | + |
| 174 | +index_entry_file = "../index.json" |
| 175 | +index_entry = generate_all_index(index_entry_file) |
| 176 | +repositorys_url = get_all_repositories(index_entry) |
| 177 | +def main(): |
| 178 | + # 初始化日志 |
| 179 | + init_logger() |
| 180 | + |
| 181 | + try: |
| 182 | + # 记录开始同步ruiching bsp packages的信息 |
| 183 | + logging.info("Begin to sync ruiching bsp packages") |
| 184 | + |
| 185 | + # 记录程序开始运行的时间 |
| 186 | + time_start = time.time() |
| 187 | + logging.info('Synchronization script start time : %s' % |
| 188 | + time.asctime(time.localtime(time.time()))) |
| 189 | + |
| 190 | + # 创建PackagesSync对象 |
| 191 | + packages_updata = PackagesSync() |
| 192 | + |
| 193 | + # 从git仓库中获取packages信息 |
| 194 | + if len(repositorys_url) == 0: |
| 195 | + logging.error("Error message: no repo url found.") |
| 196 | + sys.exit(1) |
| 197 | + for item in repositorys_url: |
| 198 | + logging.info('repository url-> %s', item) |
| 199 | + packages_updata.fetch_packages_info_from_git(item) |
| 200 | + |
| 201 | + # 显示程序运行时间 |
| 202 | + time_end = time.time() |
| 203 | + |
| 204 | + logging.info('Synchronization script end time : %s' % |
| 205 | + time.asctime(time.localtime(time.time()))) |
| 206 | + logging.info('Time cost : %s' % str(time_end - time_start)) |
| 207 | + |
| 208 | + sys.exit(0) |
| 209 | + |
| 210 | + except Exception as e: |
| 211 | + logging.error("Error message: {0}.".format(e)) |
| 212 | + traceback.print_exc() |
| 213 | + sys.exit(1) |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == '__main__': |
| 217 | + main() |
0 commit comments