Git Product home page Git Product logo

file_sync's People

Contributors

iwoz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

file_sync's Issues

SameFileError 异常

Hi,iWoz,我在使用python file_sync的时候发现把config目录下的file_list.txt和README.md文件都加入到file_list.txt里面,在执行代码里面的copy方法复制的时候会抛出SameFileError,这种情况怎么解决呀?

H:\config\file_sync>python file_sync.py
Traceback (most recent call last):
  File "file_sync.py", line 54, in <module>
    copy(file_path, DIR_FOR_GIT)
  File "C:\Program Files\Anaconda3\lib\shutil.py", line 241, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Program Files\Anaconda3\lib\shutil.py", line 104, in copyfile
    raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
shutil.SameFileError: 'H:/config/README.md' and 'H:\\config\\README.md' are the same file

H:\config\file_sync>python file_sync.py
Traceback (most recent call last):
  File "file_sync.py", line 54, in <module>
    copy(file_path, DIR_FOR_GIT)
  File "C:\Program Files\Anaconda3\lib\shutil.py", line 241, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Program Files\Anaconda3\lib\shutil.py", line 104, in copyfile
    raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
shutil.SameFileError: 'H:/config/README.md' and

项目功能不是很适用

1.研究了下项目,运行环境需要Python和一个Python的库,对于开发人员来说还好,不是很难。
2.支持的文件更新同步需要全部配置在file_list.txt里面,这个局限性就很大了,很多时候希望同步的都是一个文件夹,而且文件夹里面增删改都会会有的。
3.自动同步和拉取通过脚本也是可以的。
该项目其实蛮有借鉴意义的,思路非常好。

支持文件夹同步

感谢思路,改进为文件夹同步,设置为3s内最多同步一次

#!/usr/bin/python
# -*- coding: utf-8 -*-

from sqlite3 import Timestamp
import sys
import time
import ntpath
import os
import re
import platform

from subprocess import call
from shutil import copy
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import threading

# git root path for files to push to remote
DIR_FOR_GIT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# folders to synchronize
SYNC_FOLDER_LIST = []
f = open(os.path.join(DIR_FOR_GIT, "file_list.txt"), "r")
try:
    SYNC_FOLDER_LIST = [line.strip().replace('\\','/') for line in f] # if os.path.isfile(line.strip().replace('\\','/'))]
except Exception as e:
    raise e
finally:
    f.close()

class FileChangeHandler(FileSystemEventHandler):
    timer = None
    def on_any_event(self, event):
        if self.timer:
            self.timer.cancel()
        self.timer = threading.Timer(3, self.checkSnapshot) #延后
        self.last_event = event
        self.timer.start()
    def checkSnapshot(self):
        # print(self.last_event.event_type, self.last_event.src_path)
        src_path = self.last_event.src_path.replace('\\','/')
        for SYNC_PATH in SYNC_FOLDER_LIST:
            if SYNC_PATH in src_path and ".git" not in src_path: #排除.git
                os.chdir(SYNC_PATH)
                git_add_cmd = "git add -A"
                git_commit_cmd = "git commit -m " + re.escape("Update "+os.path.basename(src_path))
                if platform.system() == "Windows":
                    git_commit_cmd = "git commit -m Update."
                git_pull_cmd = "git pull origin master"
                git_push_cmd = "git push origin master"
                call(
                    git_add_cmd + "&&" +
                    git_commit_cmd + "&&" +
                    git_pull_cmd + "&&" +
                    git_push_cmd,
                    shell=True
                )
                break

if __name__ == "__main__":
    observer = Observer()
    event_handler = FileChangeHandler()

    for file_path in SYNC_FOLDER_LIST:
        observer.schedule(event_handler, path=os.path.dirname(os.path.realpath(file_path)), recursive=True)

    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.