我需要编写一个脚本,定期从我的Github账户下载特定文件。我看到了this Github page, 这似乎是我要找的东西,但我一直无法让它正常工作。也许这是因为我不太理解"user"字段(而不是"username")的含义。如果有人使用该方法成功,请提供一个例子吗?另外,如果您使用其他方法,请告诉我一下?谢谢!
curl
或wget
进行下载。#!/bin/bash
# file: ./getFromGit
# cd into git-synced directory and update the file
cd /directory/path/
git checkout origin/branch /path/to/file/in/dirStructure
然后
$ chmod u+x ./getFromGit
$ cp getFromGit /usr/local/bin # or wherever your executables like git are
现在你可以从任何你喜欢的目录中调用getFromGit函数,它会获取你想要的文件。
请参考Jason Rudolph的这篇教程,了解如何使用git从给定分支中检出单个文件的更多信息。
如果没有同步,我同意@jh314的观点:只需使用wget即可。
这应该可以工作(未经测试,但应该能给你一个想法):
import time
import subprocess
import sys
import shlex
if __name__ == "__main__":
cmd = ("curl -L" if sys.platform == "darwin" else "wget")
url = ......
cmd += " {0} -o {1}".format(url, url.split("/")[-1])
p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(stdout, stderr) = p.communicate()
if stderr:
raise Exception(stderr)
time.sleep(...)
# Call another instance of this script
subprocess.Popen(['python', sys.argv[0]])
时间到后,它将再次调用脚本并退出。只要命令没有错误完成,它就会不断调用自己。