str 参数仅在通过单引号而不是分配的变量提交时起作用

str argument only functioning when submitted via single quotes as opposed to assigned variable

我正在使用 Libsaas to pull a specific commit via sha

此 python 函数将 sha 拉到 HEAD

sha_in_memory = StringIO()

def get_sha():
    p1 = Popen(["git", "rev-parse", "HEAD"], stdout = PIPE)
    sha = p1.communicate()[0]
    print ("the sha is: %s" % sha)
    p1.stdout.close()
    sha_in_memory.write(sha)
    # sha = local("git rev-parse HEAD")
    sha_in_file = open('shafile.txt', 'w')
    sha_in_file.write(sha)
    sha_in_file.close()

这个冗余文件和写入内存是我尝试各种解决方案的结果。

接下来我将 sha 读入一个名为 'value'

的变量中
with open('shafile.txt', 'r') as f:
        value = f.read()

值作为 libsaas commit function

的 sha arg 提交
print ("it still is :%s" % value) #sanity check it is returning the expected sha hash
    print  repo.commit(value).get() 

当在命令行上 运行 时返回以下响应

it still is :137993b2f8408cbb66b82dd67c85e610c4f83874

Traceback (most recent call last):
  File "remote_git_tasks.py", line 48, in <module>
    print  repo.commit(value).get()
  File "/Library/Python/2.7/site-packages/libsaas/services/base.py", line 95, in wrapped
    return current.process(request, parser)
  File "/Library/Python/2.7/site-packages/libsaas/executors/urllib2_executor.py", line 80, in __call__
    return parser(body, resp.code, headers)
  File "/Library/Python/2.7/site-packages/libsaas/parsers.py", line 16, in parse_json
    raise http.HTTPError(body, code, headers)
libsaas.http.HTTPError: <HTTPError code 404>

我已经确定它是一个 str 我正在返回使用

print type(value) is str

这个returns'True'

当我通过简单地将 sha 放在引号中提交参数时它起作用,从 api 返回预期的 JSON。

print  repo.commit('137993b2f8408cbb66b82dd67c85e610c4f83874').get()

我尝试了各种转换,甚至重写了函数以利用 Fabric 的 local() 函数和 Subprocess 的 Popen。在每种情况下,当我使用定义的变量时它都不起作用,当我在 '' 中提交 sha 时有效。

任何清晰度将不胜感激。

当您从文件中读取时,您通常会得到一个尾随换行符。尝试先将其剥离:

with open('shafile.txt', 'r') as f:
    value = f.read().strip()