[
  {
    "path": ".gitignore",
    "content": "# Logs\nfile_sync_error.log\nfile_sync_output.log\n\n# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packaging\n.Python\nenv/\nbuild/\ndevelop-eggs/\ndist/\ndownloads/\neggs/\n.eggs/\nlib/\nlib64/\nparts/\nsdist/\nvar/\n*.egg-info/\n.installed.cfg\n*.egg\n\n# PyInstaller\n#  Usually these files are written by a python script from a template\n#  before PyInstaller builds the exe, so as to inject date/other infos into it.\n*.manifest\n*.spec\n\n# Installer logs\npip-log.txt\npip-delete-this-directory.txt\n\n# Unit test / coverage reports\nhtmlcov/\n.tox/\n.coverage\n.coverage.*\n.cache\nnosetests.xml\ncoverage.xml\n*,cover\n.hypothesis/\n\n# Translations\n*.mo\n*.pot\n\n# Django stuff:\n*.log\nlocal_settings.py\n\n# Flask stuff:\ninstance/\n.webassets-cache\n\n# Scrapy stuff:\n.scrapy\n\n# Sphinx documentation\ndocs/_build/\n\n# PyBuilder\ntarget/\n\n# IPython Notebook\n.ipynb_checkpoints\n\n# pyenv\n.python-version\n\n# celery beat schedule file\ncelerybeat-schedule\n\n# dotenv\n.env\n\n# virtualenv\nvenv/\nENV/\n\n# Spyder project settings\n.spyderproject\n\n# Rope project settings\n.ropeproject\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2016 Tim Wu\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# file_sync\n\nSynchronize your files to a git repository automatically.\n\nWhen you have modified your files, files will be pushed to a remote git repository.\n\nWith git, you can know every change with your files by commits.\n\n## Setup\n* Install [git](https://git-scm.com/downloads)\n* Install [Python](https://www.python.org/downloads/)\n\n* Install [watchdog](http://pythonhosted.org/watchdog/)\n\n\tin OS X or *nix:\n\t\n\t```\n\tpip install watchdog\n\t```\n\t\n\tin Windows:\n\t\n\t```\n\tpython -m pip install watchdog\n\t```\n\n## How to use\n### Make a new git repository for synchronize your files\n\n![new git repository](imgs/new_git.png)\n\nI made a new git repository named `configs` in [Github](https://github.com/new).\n\t\n### Setup your git repository locally\n\n```bash\ncd ~/Documents ## REPLACE ~/Documents TO WHATEVER DIR YOU LIKE\n\nmkdir configs\ncd configs\n\ngit init\n\n## REPLACE git@github.com:iWoz/configs.git TO YOUR GIT REPOSITORY CREATED BEFORE\ngit remote add origin git@github.com:iWoz/configs.git \n\n## add submodule\ngit submodule add git@github.com:iWoz/file_sync.git\n\n## stage all, commit and push to remote\ngit add -A\ngit commit -m \"First commit.\"\ngit push -u origin master\n## make sure you pushed successfully before you start the  next step\n\n```\n\n### Setup your **file_list.txt** for files to synchronize\n\nMake a new file name `file_list.txt` in root of your git repository.\n\nFor example, contents in my file_list.txt are :\n \t\n ```planitext\n/Users/Tim/.zshrc\n/Users/Tim/Library/Application Support/Sublime Text 2/Packages/User/Default (OSX).sublime-keymap\n/Users/Tim/Library/Application Support/Sublime Text 2/Packages/User/Preferences.sublime-settings\n ```\n**Make sure one line one file in its absolute path.**\n\n### Test\nRun file_sync.py in submodule file_sync\n\n```\ncd file_sync\npython file_sync.py\n```\n\nNow modify one of your files mentioned in file_list.txt.\n\nFor example, I modified .zshrc, and then logs are:\n\n![logs](imgs/logs.png)\n\nAnd when I check the remote git repository, all worked ok:\n\n![check](imgs/check.png)\n\n### Auto run when system startup\n* OS X\n\t* make a file named `com.wuzhiwei.filesync.plist` in `~/Library/LaunchAgents/`\n\t* fill the plist with content below\n\n\t\t```\n\t\t<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\t\t<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >\n\t\t<plist version=\"1.0\">\n\t\t  <dict>\n\t\t    <key>Label</key>\n\t\t    <string>com.wuzhiwei.filesync</string>\n\t\t    <key>Program</key>\n\t\t    <!-- REPLACE BELOW TO YOUR file_sync.py PATH -->\n\t\t    <string>/Users/Tim/Documents/configs/file_sync/file_sync.py</string>\n\t\t    <key>RunAtLoad</key>\n\t\t    <true/>\n\t\t    <key>KeepAlive</key>\n\t\t    <true/>\n\t\t    <key>UserName</key>\n\t\t    <!-- REPLACE BELOW TO YOUR USERNAME -->\n\t\t    <string>Tim</string>\n\t\t    <key>StandardErrorPath</key>\n\t\t    <!-- REPLACE BELOW TO WHAT EVER PATH YOU LIKE -->\n\t\t    <string>/Users/Tim/Documents/configs/file_sync/file_sync_error.log</string>\n\t\t    <key>StandardOutPath</key>\n\t\t    <!-- REPLACE BELOW TO WHAT EVER PATH YOU LIKE -->\n\t\t    <string>/Users/Tim/Documents/configs/file_sync/file_sync_output.log</string>\n\t\t  </dict>\n\t\t</plist>\n\t\t```\n\t* replace to your real path or username in lines below `REPLACE BELOW TO ...`\n\t* load the plist to system service: `launchctl load ~/Library/LaunchAgents/com.wuzhiwei.filesync.plist`\n\t* *if you want to unload the autorun service:* `launchctl unload ~/Library/LaunchAgents/com.wuzhiwei.filesync.plist`\n\t* check if the service is run in back: `launchctl list | grep com.wuzhiwei.filesync`\n\t\n* Windows\n\n\tI rarely use Windows, there is an ugly way to do this:\n\t\n\t* make a bat file named `file_sync.bat`\n\t* write `pythonw  __YOUR_DIR__\\file_sync.py` in this bat\n\t* put this bat to your startup folder `C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup`\n\t* close the console because we use `pythonw`\n\t* more infomation can be found in [here](http://stackoverflow.com/questions/4438020/how-to-start-a-python-file-while-window-starts)\n* *nix\n\n\tI'm pretty sure you can make it happen by yourself when you use *nix :)\n\t\n![image](https://user-images.githubusercontent.com/1621110/215088294-fc24b001-23d3-40e4-be2e-1f50a0d6d936.png)\n\n"
  },
  {
    "path": "com.wuzhiwei.filesync.plist",
    "content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >\n<plist version=\"1.0\">\n  <dict>\n    <key>Label</key>\n    <string>com.wuzhiwei.filesync</string>\n    <key>Program</key>\n    <!-- REPLACE BELOW TO YOUR file_sync.py PATH -->\n    <string>/Users/Tim/Documents/configs/file_sync/file_sync.py</string>\n    <key>RunAtLoad</key>\n    <true/>\n    <key>KeepAlive</key>\n    <true/>\n    <key>UserName</key>\n    <!-- REPLACE BELOW TO YOUR USERNAME -->\n    <string>Tim</string>\n    <key>StandardErrorPath</key>\n    <!-- REPLACE BELOW TO WHAT EVER PATH YOU LIKE -->\n    <string>/Users/Tim/Documents/configs/file_sync/file_sync_error.log</string>\n    <key>StandardOutPath</key>\n    <!-- REPLACE BELOW TO WHAT EVER PATH YOU LIKE -->\n    <string>/Users/Tim/Documents/configs/file_sync/file_sync_output.log</string>\n  </dict>\n</plist>"
  },
  {
    "path": "file_sync.py",
    "content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\nimport sys\nimport time\nimport ntpath\nimport os\nimport re\nimport platform\n\nfrom subprocess import call\nfrom shutil import copy\nfrom watchdog.observers import Observer\nfrom watchdog.events import FileSystemEventHandler\n\n# git root path for files to push to remote\nDIR_FOR_GIT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\n\n# files to synchronize\nSYNC_FILE_LIST = []\nf = open(os.path.join(DIR_FOR_GIT, \"file_list.txt\"), \"r\")\ntry:\n    SYNC_FILE_LIST = [line.strip().replace('\\\\','/') for line in f if os.path.isfile(line.strip().replace('\\\\','/'))]\nexcept Exception as e:\n    raise e\nfinally:\n    f.close()\n\nclass FileChangeHandler(FileSystemEventHandler):\n    def on_modified(self, event):\n        src_path = event.src_path.replace('\\\\','/')\n        if src_path in SYNC_FILE_LIST:\n            copy(src_path, DIR_FOR_GIT)\n            os.chdir(DIR_FOR_GIT)\n            git_add_cmd = \"git add -A\"\n            git_commit_cmd = \"git commit -m \" + re.escape(\"Update \"+os.path.basename(src_path))\n            if platform.system() == \"Windows\":\n                git_commit_cmd = \"git commit -m Update.\"\n            git_pull_cmd = \"git pull origin master\"\n            git_push_cmd = \"git push origin master\"\n            call(\n                git_add_cmd + \"&&\" +\n                git_commit_cmd + \"&&\" +\n                git_pull_cmd + \"&&\" +\n                git_push_cmd,\n                shell=True\n            )\n\nif __name__ == \"__main__\":\n    observer = Observer()\n    event_handler = FileChangeHandler()\n\n    for file_path in SYNC_FILE_LIST:\n        copy(file_path, DIR_FOR_GIT)\n        observer.schedule(event_handler, path=os.path.dirname(os.path.realpath(file_path)), recursive=False)\n\n    observer.start()\n\n    try:\n        while True:\n            time.sleep(10)\n    except KeyboardInterrupt:\n        observer.stop()\n    observer.join()\n"
  }
]