问题 运行 bitbucket rest api 命令使用 python

Problem running bitbucket rest api command using python

我正在构建一个脚本来使用其余部分更新 Bitbucket 上的文件 api。

我的问题是:

运行 使用 subprocess lib 的命令和直接在命令行上 运行ning 命令给出了两个不同的结果。 如果我 运行 使用命令行命令,当我在 Bit bucket 应用程序上检查我的提交时,我可以看到一条提交消息和一个问题。 如果我 运行 命令使用子进程库的帮助,我最后没有提交消息和问题。默认情况下,提交消息将自身设置为“由 bitbucket 编辑”,问题为空。

这是命令:

curl -X PUT -u user:pass -F content=@conanfile_3_f62hu.py -F 'message= test 4' -F branch=develop -F sourceCommitId={} bitbucket_URL".format(latest_commit)

另一个问题是我需要将文件传递给内容以便更新它。 如果我像上面那样通过它,它就会起作用。问题是我将文件内容生成为原始字符串并使用该内容创建一个临时文件。 当我将文件作为变量传递时,它没有获取文件的内容。

我的代码:

content = b'some content'
current_dir = os.getcwd()
temp_file=tempfile.NamedTemporaryFile(suffix=".py",prefix="conanfile", dir=current_dir)
temp_file.name = temp_file.name.split("\")
temp_file.name = [x for x in temp_file.name if x.startswith("conanfile")][0]
temp_file.name = "@" + temp_file.name
temp_file.write(content)
temp_file.seek(0)
update_file_url = "curl -X PUT -u user:pass -F content={} -F 'message=test 4' -F branch=develop -F sourceCommitId={} bitbucket_url".format(temp_file.name, latest_commit)
subprocess.run(update_file_url)

基本上我像以前一样传递文件,只是将名称传递给内容,但它不起作用。 如果我打印命令,一切看起来都很好,所以我不知道为什么没有设置提交消息和文件内容。

更新: 我能够传递文件,我的错误是我没有像 temp_file.name 那样传递它。 但是我无法解决消息的问题。 我发现该消息只会使用第一个词。如果后面有一个space和一个单词,它会忽略它。 space 引起了一些问题。

我找到了解决方案,如果有人发现自己有这个问题,我们需要在 message= 之前使用 \。

示例:'-F message=\" Updated with latest dependencies"'