~/bin
包含我从各种 Github 存储库下载的许多脚本,而 Ubuntu 默认存储库没有提供这些脚本的最新版本和/或不存在 PPA。
有什么好方法可以保留所有这些脚本自动地是否保持最新并与各自 Github 存储库中的最新版本同步?
答案1
简单的解决方案
您实际上要做的是git fetch && git pull
从每个存储库文件夹内部运行~/bin
。最简单的方法是将此命令添加到启动应用程序:
find /home/user/bin -type d -exec sh -c 'cd {} && git fetch && git pull' \; 2> /dev/null
请注意,这/home/user/bin
必须是您的~/bin
文件夹的完整路径。
Python 脚本
但是,如果您想要一个专用实用程序来完成此任务,这里有一个 Python 脚本,它可以递归更新您的所有内容~/bin
。使用方法很简单 - 从终端运行它,python git_updater.py
它会处理其余的事情。甚至可以将同一命令作为启动应用程序的条目添加到每次登录时运行脚本
脚本源代码如下,也可以在我的GitHub 存储库。
#!/usr/bin/env python3
# Author: Serg Kolo
# Date: 10/21/2016
# Written for: https://askubuntu.com/q/759058/295286
from __future__ import print_function
import subprocess
import os
def run_cmd(cmdlist,workdir):
""" utility: reusable function for
running external commands """
new_env = dict(os.environ)
new_env['LC_ALL'] = 'C'
try:
stdout = subprocess.check_output(
cmdlist,
env=new_env,
cwd=workdir
)
except subprocess.CalledProcessError:
pass
else:
if stdout:
return stdout
def is_behind(cwd):
""" simple wrapper for checking
git status output
"""
fetch = run_cmd(['git','fetch','origin','master'],cwd)
status = run_cmd(['git','status'],cwd)
if 'Your branch is behind' in status.decode():
return True
def update(cwd):
print('> previous commit:')
print(run_cmd(['git','--no-pager','log','-1'],cwd).decode())
print(run_cmd(['git','pull'],cwd).decode())
print('> latest commit:')
print(run_cmd(['git','--no-pager','log','-1'],cwd).decode())
def main():
root_dir = os.path.join(os.path.expanduser('~'),'bin/')
base_cmd = ['git','--no-pager']
first_args = ['status']
second_args = ['log','-1']
third_args = ['pull']
for root,dirs,files in os.walk(root_dir):
for dir in dirs:
top_dir = os.path.join(root,dir)
git_dir = os.path.join(top_dir,'.git')
if os.path.exists(git_dir):
print('WORKING REPOSITORY:' + top_dir)
if is_behind(top_dir):
print('repository is behind, will update')
update(top_dir)
else:
print('not behind, skipped')
print('-'*80)
if __name__ == '__main__': main()
脚本演示
$ python git_updater.py
WORKING REPOSITORY:/home/xieerqi/bin/sergrep
From https://github.com/SergKolo/sergrep
* branch master -> FETCH_HEAD
repository is behind, will update
> previous commit:
commit ba629ef71c4d2bfe329b8ebe5f48d1ce21e76ebc
Author: SergKolo <[email protected]>
Date: Wed Aug 24 21:22:06 2016 -0600
fixed sending sigterm to process
Updating ba629ef..dd0c962
Fast-forward
disable_super_key.py | 2 +-
git_updater.py | 61 ++++++++++++
launcherctl.py | 162 +++++++++++++++++++++++++++++++
single_window_expo.py | 138 ++++++++++++++++++++++++++
windowctrl.py | 258 +++++++++++++++++++++++++++++++++++++++++++++++++
xml_wallpaper_maker.py | 152 +++++++++++++++++++++++++++++
6 files changed, 772 insertions(+), 1 deletion(-)
create mode 100644 git_updater.py
create mode 100755 launcherctl.py
create mode 100755 single_window_expo.py
create mode 100755 windowctrl.py
create mode 100755 xml_wallpaper_maker.py
> latest commit:
commit dd0c9627392540df7c4230e7898cb9e55adc7083
Author: SergKolo <[email protected]>
Date: Fri Oct 21 02:37:01 2016 -0600
git updater script
--------------------------------------------------------------------------------
WORKING REPOSITORY:/home/xieerqi/bin/PythonStuff
From https://github.com/mnovits1/PythonStuff
* branch master -> FETCH_HEAD
not behind, skipped
--------------------------------------------------------------------------------