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
将组织名称更改为您的组织名称,并通过传递reponame来运行脚本。它将克隆并打印repo中的所有文件
相关问题 更多 >
编程相关推荐