Python实现自动推本地github博客到远程仓库
生活随笔
收集整理的這篇文章主要介紹了
Python实现自动推本地github博客到远程仓库
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Python實現(xiàn)自動推本地github博客到遠程倉庫
以前的簡單版本
通過python中的os模塊操作系統(tǒng)命令
詳情可參考:Python實現(xiàn)一行代碼推本地git到遠程倉庫
升級版本
本次加入了監(jiān)聽文件修改功能
這樣腳本只需在后臺運行,即可檢測到對應(yīng)的文件夾中的內(nèi)容是否變化
如果變化,則調(diào)用自動push函數(shù),即可實現(xiàn)推本地倉庫到遠程中
依賴庫的配置
這里我們需要用到一個庫watchdog
關(guān)于它的安裝和使用可以參考:python中文件變化監(jiān)控-watchdog
代碼如下:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Created by victor# 本模塊的功能:<檢測文件夾變化># 導(dǎo)入watchdog對應(yīng)模塊 from watchdog.observers import Observer from watchdog.events import * # 導(dǎo)入時間模塊 import time # 導(dǎo)入系統(tǒng)模塊 import osdef push(change):print('-'*76)os.system('git add .')os.system('git commit -m\"auto'+change+'\"')os.system('git push -u origin master')print('-'*76)class FileEventHandler(FileSystemEventHandler):# 初始化魔術(shù)方法def __init__(self):FileSystemEventHandler.__init__(self)# 文件或文件夾移動def on_moved(self, event):if event.is_directory:print("directory moved from {0} to {1}".format(event.src_path, event.dest_path))else:print("file moved from {0} to {1}".format(event.src_path, event.dest_path))# 這里我們只判斷文件修改,如需加入文件夾修改,只需在上面的if條件中調(diào)用push函數(shù)即可push("文件移動: {0} to {1}".format(event.src_path, event.dest_path))# 創(chuàng)建文件或文件夾def on_created(self, event):if event.is_directory:print("directory created:{0}".format(event.src_path))else:print("file created:{0}".format(event.src_path))push("創(chuàng)建文件:{0}".format(event.src_path))# 刪除文件或文件夾def on_deleted(self, event):if event.is_directory:print("directory deleted:{0}".format(event.src_path))else:print("file deleted:{0}".format(event.src_path))push("刪除文件:{0}".format(event.src_path))# 移動文件或文件夾def on_modified(self, event):if event.is_directory:print("directory modified:{0}".format(event.src_path))else:print("file modified:{0}".format(event.src_path))push("文件修改:{0}".format(event.src_path))if __name__ == "__main__":# 實例化Observer對象observer = Observer()event_handler = FileEventHandler()# 設(shè)置監(jiān)聽目錄dis_dir = "./_posts/"observer.schedule(event_handler, dis_dir, True)observer.start()try:while True:# 設(shè)置監(jiān)聽頻率(間隔周期時間)time.sleep(1)except KeyboardInterrupt:observer.stop()observer.join()總結(jié)
以上是生活随笔為你收集整理的Python实现自动推本地github博客到远程仓库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为云服务器搭建PaddlePaddle
- 下一篇: python安装环境傻瓜式安装_前后端分