问题描述
我正在尝试编写一个可以一次性提交自动生成的 .json 文件的脚本。但是我的脚本只能一个一个提交文件,这不是我想要的...请帮忙!
接下来是我的代码。我认为 repo.update_file/create_file 可能不会一次性提交。
from github import Github
def github_login(tokens):
g = Github(login_or_token=tokens)
return g
def github_all_files_name(repo):
contents = repo.get_contents("")
all_files = []
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
return all_files
def main(tokens,docker_path,github_path,branch):
g = github_login(tokens)
repo = g.get_repo(github_path)
github_files_name = github_all_files_name(repo)
generated_jsonnet_list = os.listdir(docker_path)
for jsonnet in generated_jsonnet_list:
try:
with open(docker_path + "/{jsonnet_name}".format(jsonnet_name=jsonnet),"r") as file:
content = file.read()
# whether jsonnet/wrapper jsonnet
if jsonnet == "xxx.jsonnet":
git_file = jsonnet
else:
git_file = "xxx/" + jsonnet
# check whether file is updated/created
if git_file in github_files_name:
contents = repo.get_contents(git_file)
code1 = contents.sha
repo.update_file(contents.path,"Update jsonnet",content,contents.sha,branch=branch)
code2 = repo.get_contents(git_file).sha
if code1 == code2:
print(git_file + " No Change!")
else:
print(git_file + " is updating!")
else:
repo.create_file(git_file,"Create jsonnet",branch=branch)
print(git_file + ' is pushing!')
except Exception as e:
print(e)
if __name__ == "__main__":
token = "ur token code from GitHub private token"
local_path1 = "/Users/tmp"
github_path1 = "Lei/test"
branch1 = "test"
main(token,docker_path1,github_path1,branch1)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)