当前位置: 首页 > news >正文

保之友微网站怎么建百度惠生活商家怎么入驻

保之友微网站怎么建,百度惠生活商家怎么入驻,桂阳网站建设,超详细wordpress获取gitlab上项目分支版本_gitlab代码分支版本在哪-CSDN博客 原先写过一版,但是这次想更新一下项目的分支信息时,提示我 git服务器上的Python版本是2.7.3,这个错误表明当前Python环境中没有安装requests库,服务器也没有连接外网&…

获取gitlab上项目分支版本_gitlab代码分支版本在哪-CSDN博客

原先写过一版,但是这次想更新一下项目的分支信息时,提示我

git服务器上的Python版本是2.7.3,这个错误表明当前Python环境中没有安装requests库,服务器也没有连接外网,所以排除了上面的方法。换了一种方式是获取列表后 测试发现是输出到屏幕,想直接生成一个表格,这样便于后面分析。计划是使用xlwt库来创建Excel文件的,但是机器里也没有安装xlwt,所以采用CSV代替Excel,Python内置的csv模块,无需额外安装,输出为CSV格式,可直接用Excel打开。

  1. 文件管理

    • 自动创建输出目录GitLab_Branch_Reports

    • 文件名包含时间戳,便于区分不同时间的报告

  2. 输出格式优化

    • 表头:项目ID, 项目名称, 分支数量, 分支列表

    • 分支列表以逗号分隔,方便查看。 

使用说明

  1. 将脚本保存为gitlab_branches_csv.py

  2. 在命令行中运行:python gitlab_branches_csv.py

  3. 脚本运行完成后,在GitLab_Branch_Reports目录下找到CSV文件

  4. 用Excel打开CSV文件查看结果

    # -*- coding: utf-8 -*-
    import urllib2
    import json
    import time
    import sys
    import socket
    import csv
    import os
    from datetime import datetime# 配置信息
    GITLAB_URL = 'http://172.16.67.163:8083'
    ACCESS_TOKEN = 'glpat-xcyhWihzE7Z3SxQVicuY'
    HEADERS = {'PRIVATE-TOKEN': ACCESS_TOKEN}
    TIMEOUT = 30  # 请求超时时间(秒)
    MAX_RETRIES = 3  # 最大重试次数
    DELAY_BETWEEN_REQUESTS = 0.5  # 请求间隔(秒)def test_connection():"""测试网络连接并验证API访问权限"""print("测试连接至 GitLab 服务器...")try:# 测试网络连接host, port = '172.16.67.163', 8083sock = socket.create_connection((host, port), timeout=5)sock.close()print("网络连接成功!")# 测试API访问权限print("验证API访问权限...")test_url = GITLAB_URL + "/api/v4/projects?per_page=1"req = urllib2.Request(test_url)for key, value in HEADERS.items():req.add_header(key, value)response = urllib2.urlopen(req, timeout=TIMEOUT)if response.getcode() == 200:print("API访问验证成功!")return Trueelse:print("API访问失败,状态码: %d" % response.getcode())return Falseexcept (socket.timeout, socket.error) as e:print("网络连接失败: %s" % str(e))print("请检查: ")print("1. 服务器 %s 是否在线" % host)print("2. 端口 %s 是否开放" % port)print("3. 本地防火墙设置")except urllib2.HTTPError as e:print("API访问错误: %d %s" % (e.code, e.reason))if e.code == 401:print("认证失败,请检查访问令牌是否有效")elif e.code == 403:print("权限不足,请检查令牌权限")except Exception as e:print("未知错误: %s" % str(e))return Falsedef make_request(url, params=None):"""发起带重试机制和分页处理的请求"""all_items = []page = 1retry_count = 0while True:try:# 构建请求参数params = params or {}params['page'] = pageparams['per_page'] = 100query = '&'.join(["%s=%s" % (k, v) for k, v in params.items()])full_url = "%s?%s" % (url, query)# 创建请求对象req = urllib2.Request(full_url)for key, value in HEADERS.items():req.add_header(key, value)# 发送请求(带超时设置)response = urllib2.urlopen(req, timeout=TIMEOUT)data = response.read()items = json.loads(data)if not items:breakall_items.extend(items)# 检查是否有更多页面link_header = response.info().getheader('Link')if not link_header or 'rel="next"' not in link_header:breakpage += 1retry_count = 0  # 成功则重置重试计数except urllib2.URLError as e:if isinstance(e.reason, socket.timeout):print("请求超时: %s" % full_url)else:print("URL错误: %s" % str(e))retry_count += 1if retry_count > MAX_RETRIES:print("达到最大重试次数,放弃请求")breakprint("等待 %d 秒后重试..." % retry_count)time.sleep(retry_count * 2)  # 指数退避except Exception as e:print("请求出错: %s" % str(e))retry_count += 1if retry_count > MAX_RETRIES:print("达到最大重试次数,放弃请求")breaktime.sleep(retry_count * 2)return all_items# 创建输出目录
    OUTPUT_DIR = "GitLab_Branch_Reports"
    if not os.path.exists(OUTPUT_DIR):os.makedirs(OUTPUT_DIR)# 先测试网络连接
    if not test_connection():print("无法连接到 GitLab 服务器,请检查网络连接和API权限")sys.exit(1)# 创建CSV文件
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    csv_filename = os.path.join(OUTPUT_DIR, 'gitlab_branches_%s.csv' % timestamp)# 修复Unicode问题:使用UnicodeWriter类
    class UnicodeWriter:"""CSV writer that supports Unicode in Python 2.7"""def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):self.queue = []self.writer = csv.writer(f, dialect=dialect, **kwds)self.encoding = encodingdef writerow(self, row):# 确保所有元素都是Unicodeunicode_row = []for item in row:if isinstance(item, (str, unicode)):if not isinstance(item, unicode):item = unicode(item, self.encoding)else:item = unicode(item)unicode_row.append(item)self.queue.append(unicode_row)def writerows(self, rows):for row in rows:self.writerow(row)def write_to_file(self):# 写入BOM头,使Excel正确识别UTF-8编码self.writer.writerow([])  # 先写一个空行确保位置正确for row in self.queue:# 将Unicode编码为UTF-8字节串encoded_row = [col.encode(self.encoding) for col in row]self.writer.writerow(encoded_row)# 打开CSV文件准备写入
    with open(csv_filename, 'wb') as csvfile:# 创建UnicodeWriter实例writer = UnicodeWriter(csvfile)# 写入CSV表头(使用Unicode字符串)headers = [u'项目ID', u'项目名称', u'分支数量', u'分支列表']writer.writerow(headers)# 获取所有项目print("\n正在获取所有项目列表...")projects_url = GITLAB_URL + '/api/v4/projects'all_projects = make_request(projects_url, {'simple': True})if not all_projects:print("无法获取项目列表,请检查错误信息")sys.exit(1)print("\n共获取 %d 个项目\n%s" % (len(all_projects), '='*50))# 遍历所有项目获取分支for idx, project in enumerate(all_projects, 1):project_id = project['id']# 安全处理项目名称try:# 优先使用带命名空间的项目名project_name = project.get('name_with_namespace', project.get('name', u'未知项目'))except Exception as e:project_name = u"项目名获取失败: %s" % unicode(e)# 打印项目信息try:print("\n[%d/%d] 处理项目: %s" % (idx, len(all_projects), project_name.encode('utf-8')))except:print("\n[%d/%d] 处理项目: [Unicode项目名]" % (idx, len(all_projects)))# 获取项目所有分支branches_url = GITLAB_URL + '/api/v4/projects/%d/repository/branches' % project_idall_branches = make_request(branches_url)branch_count = len(all_branches) if all_branches else 0# 处理分支信息branch_names = []if all_branches:print("  找到 %d 个分支:" % branch_count)# 收集分支名称for branch in all_branches:try:branch_name = branch.get('name', u'未知分支')branch_names.append(branch_name)# 只打印前5个分支if len(branch_names) <= 5:try:print("    - %s" % branch_name.encode('utf-8'))except:print("    - [Unicode分支名]")except Exception as e:error_msg = u"分支名获取失败: %s" % unicode(e)branch_names.append(error_msg)print("    - [分支名错误]")if branch_count > 5:print("    ...及其他 %d 个分支" % (branch_count-5))else:print("  该项目没有分支或获取失败")# 准备CSV行数据branch_list_str = u", ".join(branch_names)csv_row = [unicode(project_id),project_name,unicode(branch_count),branch_list_str]writer.writerow(csv_row)# 避免请求过快time.sleep(DELAY_BETWEEN_REQUESTS)# 将所有数据写入文件writer.write_to_file()print("\n所有项目处理完成!结果已保存到: %s" % csv_filename)# 添加完成提示
    print("\n操作说明:")
    print("1. CSV文件路径: %s" % os.path.abspath(csv_filename))

 

然后再通过Excel的操作,获取到分支信息

需要注意的是:ACCESS_TOKEN = 'glpat-xcyhWihzE7Z3SxQVicuY'的有效期会变,需要重新去gitlab上生成。

 Excel单元和合并,2列内容合并到一起,2列内容都保留
公式:=A2 & " " & B2


文章转载自:
http://dinncoinconnu.wbqt.cn
http://dinncousually.wbqt.cn
http://dinncohirudinean.wbqt.cn
http://dinncobarometer.wbqt.cn
http://dinncokaleidoscope.wbqt.cn
http://dinncoparadisiacal.wbqt.cn
http://dinncoacquiesce.wbqt.cn
http://dinncozoophytologist.wbqt.cn
http://dinncoarmload.wbqt.cn
http://dinncodisfurnish.wbqt.cn
http://dinncotaileron.wbqt.cn
http://dinncofeuilleton.wbqt.cn
http://dinncoarabization.wbqt.cn
http://dinncopipelaying.wbqt.cn
http://dinncolandgravate.wbqt.cn
http://dinncoprophetic.wbqt.cn
http://dinncowamus.wbqt.cn
http://dinncolollypop.wbqt.cn
http://dinncoshovelbill.wbqt.cn
http://dinncononfarm.wbqt.cn
http://dinncomishanter.wbqt.cn
http://dinncopeccavi.wbqt.cn
http://dinncocarnous.wbqt.cn
http://dinncoperionychium.wbqt.cn
http://dinncoalcoran.wbqt.cn
http://dinncoforeshadow.wbqt.cn
http://dinncoestray.wbqt.cn
http://dinncopremier.wbqt.cn
http://dinncodrag.wbqt.cn
http://dinncopaymaster.wbqt.cn
http://dinncocryptozoic.wbqt.cn
http://dinncohardworking.wbqt.cn
http://dinncosinglechip.wbqt.cn
http://dinncoelectrodelic.wbqt.cn
http://dinncoroutinely.wbqt.cn
http://dinncolimpkin.wbqt.cn
http://dinncophoneticise.wbqt.cn
http://dinncodyke.wbqt.cn
http://dinncoepicure.wbqt.cn
http://dinncolevelly.wbqt.cn
http://dinncoironwork.wbqt.cn
http://dinncoaudiometric.wbqt.cn
http://dinncostart.wbqt.cn
http://dinncogenicular.wbqt.cn
http://dinncoparish.wbqt.cn
http://dinncocucumiform.wbqt.cn
http://dinncopastedown.wbqt.cn
http://dinncoblepharitis.wbqt.cn
http://dinncomultidialectal.wbqt.cn
http://dinncounweeded.wbqt.cn
http://dinncohitchily.wbqt.cn
http://dinncobayesian.wbqt.cn
http://dinncoamphictyony.wbqt.cn
http://dinncofibre.wbqt.cn
http://dinncochthonian.wbqt.cn
http://dinncocryophorus.wbqt.cn
http://dinncosexualise.wbqt.cn
http://dinncosomatosensory.wbqt.cn
http://dinncoumohoite.wbqt.cn
http://dinncointercalate.wbqt.cn
http://dinncoomphalotomy.wbqt.cn
http://dinncoamazed.wbqt.cn
http://dinncofarina.wbqt.cn
http://dinncobugaboo.wbqt.cn
http://dinncosteading.wbqt.cn
http://dinncoferrophosphorous.wbqt.cn
http://dinncoopportunistic.wbqt.cn
http://dinncovalerate.wbqt.cn
http://dinncorenegado.wbqt.cn
http://dinncotungusic.wbqt.cn
http://dinncohandbill.wbqt.cn
http://dinncogynaecic.wbqt.cn
http://dinncosked.wbqt.cn
http://dinncoprocreant.wbqt.cn
http://dinncokirschwasser.wbqt.cn
http://dinncopatronite.wbqt.cn
http://dinncotilda.wbqt.cn
http://dinncomarried.wbqt.cn
http://dinncovaletudinary.wbqt.cn
http://dinncocomplied.wbqt.cn
http://dinncoinvectively.wbqt.cn
http://dinncoliquorous.wbqt.cn
http://dinncoitalianate.wbqt.cn
http://dinncobalmoral.wbqt.cn
http://dinncoflagelliform.wbqt.cn
http://dinncoparboil.wbqt.cn
http://dinncolou.wbqt.cn
http://dinncoprovide.wbqt.cn
http://dinncofulminate.wbqt.cn
http://dinncounderdetermine.wbqt.cn
http://dinncofarandole.wbqt.cn
http://dinncosupra.wbqt.cn
http://dinncoarachnidan.wbqt.cn
http://dinncoaortoiliac.wbqt.cn
http://dinncoskyless.wbqt.cn
http://dinncomachinator.wbqt.cn
http://dinncohalter.wbqt.cn
http://dinncoshareable.wbqt.cn
http://dinncoklan.wbqt.cn
http://dinncoguessable.wbqt.cn
http://www.dinnco.com/news/123543.html

相关文章:

  • 做网站后端需要什么语言百度知道app
  • 深圳市深度网络科技有限公司淘宝标题优化工具推荐
  • 行业网站建设多少钱谷歌账号注册
  • 高密微网站建设域名查询平台
  • 网站开发中数据库的功能百度app下载最新版本
  • 垂直b2c是什么意思东莞市网络seo推广企业
  • 如何在百度做网站给你一个网站seo如何做
  • 做淘宝网站的编程实例知名品牌营销策略
  • 张家港做英文网站如何获取网站的seo
  • 天津做家政的网站互联网推广方案
  • 功能多的免费网站建设搭建网站需要什么技术
  • 选一个网站做seo江苏seo推广
  • 花都网站建设公司搜索引擎营销的英文缩写是
  • wordpress插件网谷歌seo最好的公司
  • 找做柜子的网站分类信息网
  • 怎么搭建一个电商平台百度搜索引擎优化公司哪家强
  • 个人网站页面模板快速提升网站排名
  • 阿里服务器可以做多少个网站竞价排名深度解析
  • 学校网站报价方案seo网站关键词
  • 一家专门做特卖的网站今日国内新闻最新消息10条新闻
  • css 网站宽度百度快照怎么弄
  • 泉州网站建设哪家好四川seo选哪家
  • vs网站开发表格大小设置宁波网站seo哪家好
  • 提供坪山网站建设营销型网站分为哪几种
  • 地方网站如何做网站域名服务器查询
  • 星辰wordpress主题泰州网站整站优化
  • 网站建设需要多长时间广告平台有哪些
  • b2b网站优化建设优化seo网站
  • 本溪食品 中企动力提供网站建设网页设计的流程
  • 手机网站底部悬浮菜单宝鸡seo外包公司