Python从bitbucket读取配置文件

2024-03-29 12:36:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,它使用JSON文件作为配置。现在,决定将JSON文件放在bitbucket云帐户中。 在我的脚本中读取JSON文件的最佳解决方案是什么


Tags: 文件脚本jsonbitbucket帐户解决方案
1条回答
网友
1楼 · 发布于 2024-03-29 12:36:31

将组织名称更改为您的组织名称,并通过传递reponame来运行脚本。它将克隆并打印repo中的所有文件

import requests
from requests.auth import HTTPBasicAuth
import sys, os
import subprocess
import logging

# connect to bitbucket and get repo
repoName = sys.argv[1]
bitBucketUrl = 'https://api.bitbucket.org/2.0/repositories/<org_name>/{}/src'.format(repoName)
userName = '*******'
password = '*******'
response = requests.get(bitBucketUrl, auth=HTTPBasicAuth(userName,password))
json_response = response.json()
#print(response)
if response.status_code == 200:
    print("success")
else:
    print("Unable to retrive bitbucket repo details")

#remove directory if exists
check_for_repo = os.path.exists(repoName)
if (check_for_repo):
    remove_dir = 'rm -rf {}'.format(repoName)
    os.system(remove_dir)
    print("Removed the directory if already exists")
else:
    print("The directory is not present ! Skipping for now")

# clone repo from bitbucket 
clone_repo = ['git', 'clone', 'https://{}:{}@bitbucket.org/<org_name>/{}.git'.format(userName ,password ,repoName)]
res = subprocess.Popen(clone_repo, stdout=subprocess.PIPE)
output, _error = res.communicate()
currentPath = os.getcwd()
destinationPath = os.chdir(currentPath + '/' + repoName)

#List files in directory
files_in_repo = os.listdir()
print(files_in_repo)```

noe that you have all the files you can just read them like you would read normal files

相关问题 更多 >