Repository: iWoz/file_sync Branch: master Commit: c8d1bd4a552b Files: 5 Total size: 8.9 KB Directory structure: gitextract_348knp3e/ ├── .gitignore ├── LICENSE ├── README.md ├── com.wuzhiwei.filesync.plist └── file_sync.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Logs file_sync_error.log file_sync_output.log # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *,cover .hypothesis/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # IPython Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # dotenv .env # virtualenv venv/ ENV/ # Spyder project settings .spyderproject # Rope project settings .ropeproject ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2016 Tim Wu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # file_sync Synchronize your files to a git repository automatically. When you have modified your files, files will be pushed to a remote git repository. With git, you can know every change with your files by commits. ## Setup * Install [git](https://git-scm.com/downloads) * Install [Python](https://www.python.org/downloads/) * Install [watchdog](http://pythonhosted.org/watchdog/) in OS X or *nix: ``` pip install watchdog ``` in Windows: ``` python -m pip install watchdog ``` ## How to use ### Make a new git repository for synchronize your files ![new git repository](imgs/new_git.png) I made a new git repository named `configs` in [Github](https://github.com/new). ### Setup your git repository locally ```bash cd ~/Documents ## REPLACE ~/Documents TO WHATEVER DIR YOU LIKE mkdir configs cd configs git init ## REPLACE git@github.com:iWoz/configs.git TO YOUR GIT REPOSITORY CREATED BEFORE git remote add origin git@github.com:iWoz/configs.git ## add submodule git submodule add git@github.com:iWoz/file_sync.git ## stage all, commit and push to remote git add -A git commit -m "First commit." git push -u origin master ## make sure you pushed successfully before you start the next step ``` ### Setup your **file_list.txt** for files to synchronize Make a new file name `file_list.txt` in root of your git repository. For example, contents in my file_list.txt are : ```planitext /Users/Tim/.zshrc /Users/Tim/Library/Application Support/Sublime Text 2/Packages/User/Default (OSX).sublime-keymap /Users/Tim/Library/Application Support/Sublime Text 2/Packages/User/Preferences.sublime-settings ``` **Make sure one line one file in its absolute path.** ### Test Run file_sync.py in submodule file_sync ``` cd file_sync python file_sync.py ``` Now modify one of your files mentioned in file_list.txt. For example, I modified .zshrc, and then logs are: ![logs](imgs/logs.png) And when I check the remote git repository, all worked ok: ![check](imgs/check.png) ### Auto run when system startup * OS X * make a file named `com.wuzhiwei.filesync.plist` in `~/Library/LaunchAgents/` * fill the plist with content below ``` Label com.wuzhiwei.filesync Program /Users/Tim/Documents/configs/file_sync/file_sync.py RunAtLoad KeepAlive UserName Tim StandardErrorPath /Users/Tim/Documents/configs/file_sync/file_sync_error.log StandardOutPath /Users/Tim/Documents/configs/file_sync/file_sync_output.log ``` * replace to your real path or username in lines below `REPLACE BELOW TO ...` * load the plist to system service: `launchctl load ~/Library/LaunchAgents/com.wuzhiwei.filesync.plist` * *if you want to unload the autorun service:* `launchctl unload ~/Library/LaunchAgents/com.wuzhiwei.filesync.plist` * check if the service is run in back: `launchctl list | grep com.wuzhiwei.filesync` * Windows I rarely use Windows, there is an ugly way to do this: * make a bat file named `file_sync.bat` * write `pythonw __YOUR_DIR__\file_sync.py` in this bat * put this bat to your startup folder `C:\Documents and Settings\All Users\Start Menu\Programs\Startup` * close the console because we use `pythonw` * more infomation can be found in [here](http://stackoverflow.com/questions/4438020/how-to-start-a-python-file-while-window-starts) * *nix I'm pretty sure you can make it happen by yourself when you use *nix :) ![image](https://user-images.githubusercontent.com/1621110/215088294-fc24b001-23d3-40e4-be2e-1f50a0d6d936.png) ================================================ FILE: com.wuzhiwei.filesync.plist ================================================ Label com.wuzhiwei.filesync Program /Users/Tim/Documents/configs/file_sync/file_sync.py RunAtLoad KeepAlive UserName Tim StandardErrorPath /Users/Tim/Documents/configs/file_sync/file_sync_error.log StandardOutPath /Users/Tim/Documents/configs/file_sync/file_sync_output.log ================================================ FILE: file_sync.py ================================================ #!/usr/bin/python # -*- coding: utf-8 -*- 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 # git root path for files to push to remote DIR_FOR_GIT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # files to synchronize SYNC_FILE_LIST = [] f = open(os.path.join(DIR_FOR_GIT, "file_list.txt"), "r") try: SYNC_FILE_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): def on_modified(self, event): src_path = event.src_path.replace('\\','/') if src_path in SYNC_FILE_LIST: copy(src_path, DIR_FOR_GIT) os.chdir(DIR_FOR_GIT) 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 ) if __name__ == "__main__": observer = Observer() event_handler = FileChangeHandler() for file_path in SYNC_FILE_LIST: copy(file_path, DIR_FOR_GIT) observer.schedule(event_handler, path=os.path.dirname(os.path.realpath(file_path)), recursive=False) observer.start() try: while True: time.sleep(10) except KeyboardInterrupt: observer.stop() observer.join()