-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite and support non-git directory
- Loading branch information
Showing
6 changed files
with
203 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import time | ||
import subprocess | ||
import logging | ||
from threading import RLock, Thread | ||
|
||
|
||
class SyncRunner(Thread): | ||
|
||
def __init__(self, sync, **kwargs): | ||
super(SyncRunner, self).__init__(**kwargs) | ||
self._sync = sync | ||
self._stopped = False | ||
|
||
def run(self): | ||
while not self._stopped: | ||
time.sleep(self._sync.interval) | ||
self._sync.sync() | ||
|
||
def stop(self): | ||
self._stopped = True | ||
|
||
|
||
class Sync(object): | ||
|
||
def __init__(self, path, remote_path, interval=3): | ||
self._path = path | ||
self._remote_path = remote_path | ||
self.interval = interval | ||
|
||
self._abspath = os.path.abspath(self._path) | ||
self._paths_to_sync = set([]) | ||
self._lock = RLock() | ||
self._runner = SyncRunner(self) | ||
|
||
def sync(self, archive_mode=False): | ||
paths_to_sync = None | ||
with self._lock: | ||
paths_to_sync = self._paths_to_sync | ||
self._paths_to_sync = set([]) | ||
if paths_to_sync: | ||
logging.info('Syncing:%s%s', os.linesep, os.linesep.join( | ||
[os.path.relpath(p) for p in paths_to_sync] | ||
)) | ||
self._rsync_paths(paths_to_sync, archive_mode) | ||
|
||
def _rsync_paths(self, paths, archive_mode=False): | ||
if archive_mode: | ||
params = ['rsync', '-aqzR', '--delete'] | ||
else: | ||
params = ['rsync', '-dqR', '--delete'] | ||
params.extend([self._cal_path_with_implied_dirs(p) for p in paths]) | ||
params.append(self._remote_path) | ||
subprocess.Popen(params).wait() | ||
|
||
def _cal_path_with_implied_dirs(self, file_path): | ||
relpath = os.path.relpath(file_path, self._abspath) | ||
return os.path.join(self._path, '.', relpath) | ||
|
||
def start(self): | ||
self._runner.start() | ||
|
||
def stop(self): | ||
self._runner.stop() | ||
self._runner.join() | ||
|
||
def add_path(self, path): | ||
with self._lock: | ||
self._paths_to_sync.add(os.path.abspath(path)) | ||
|
||
def remove_path(self, path): | ||
with self._lock: | ||
self._paths_to_sync.remove(os.path.abspath(path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters