class TestLocalMonitor(unittest.TestCase): def setUp(self): repo = Repo(ROOT) repo.hg_init() self.lm = LocalMonitor(repo, LocalHandler()) def tearDown(self): #self.lm.stop() pass def testLocalMonitor(self): print("Starting local monitor at '{}'...".format(ROOT)) self.lm.start() print("Hello there!") create_file(ROOT, "foo") create_file(ROOT, "bar") create_file(ROOT, "fizz") create_file(ROOT, "fuzz") touch_file(ROOT, "bar", "This is a file called bar.\n") touch_file(ROOT, "bar", "This is a file called bar.\n") folder = join(ROOT, "one") makedirs(folder) another_folder = join(ROOT, "two") makedirs(another_folder) rm(folder) sleep(1) create_file(another_folder, "klm.txt", "Hello there!") create_file(another_folder, "xyz.txt", "I'm created automagically :)") touch_file(another_folder, "xyz.txt", "Some extra text") sleep(1) self.lm.stop()
class VonkDaemon(): def __init__(self, configuration_file=None): """ Start a server, read the configuration file, make sure the repositories are available and start checking for changes in these repositories. :param configuration_file: the configuration file to be used. """ if configuration_file is None: configuration_file = Defaults.CONFIGURATION_FILE try: self.configuration = Configuration(configuration_file) except ConfigurationException: print("The configuration file is nonexistent or not valid.") exit(Defaults.EXIT_INVALID_CONFIGURATION_FILE) else: print("Read " + configuration_file + " successfully.") local_exists = repository_exists(self.configuration.local) remote_exists = repository_exists(self.configuration.remote) # make sure the local and remote repositories exist if not local_exists and remote_exists: self.no_local_but_remote_exists() elif not local_exists and not remote_exists: self.not_local_and_no_remote() elif local_exists and not remote_exists: self.local_but_no_remote() else: self.local_and_remote() local_properties = _get_local_properties(self.local_repo.path) structure = _get_local_structure(self.local_repo.path) handler = LocalHandler(local_properties, structure) self.monitor = LocalMonitor(self.local_repo, handler) self.status = READY def no_local_but_remote_exists(self): """ If no local repository but remote repository is available, clone the remote repository to the local repository. """ print("No local, but remote exists.") self._recreate_path(self.configuration.local) # clone the remote to the recreated local repo self.local_repo = hgapi.Repo.hg_clone(self.configuration.remote, self.configuration.local) self.remote_repo = hgapi.Repo(self.configuration.remote) def not_local_and_no_remote(self): """ If no local repository nor a remote repository is available, create remote repository and clone this remote repository to a local repository. """ print("No local, and no remote.") # remove remote (if it would exist) and recreate remote folder self._recreate_path(self.configuration.remote) # initialize remote repo self.remote_repo = hgapi.Repo(self.configuration.remote) self.remote_repo.hg_init() self._update_ignorefile(self.remote_repo, self.configuration.remote) # recreate local self._recreate_path(self.configuration.local) # clone the newly create remote to the recreated local repo self.local_repo = hgapi.Repo.hg_clone(self.configuration.remote, self.configuration.local) def local_but_no_remote(self): """ If local repository and remote repository are available, create the remote repository, and push the local repository to it. """ print("Local exists, but no remote.") # remove remote (if it would exist) and recreate remote folder self._recreate_path(self.configuration.remote) # create remote repo self.remote_repo = hgapi.Repo(self.configuration.remote) self.remote_repo.hg_init() # set the default path of the local to the remote self._set_default_repo_paths(self.configuration.local, self.configuration.remote) # push local to remote self.local_repo = hgapi.Repo(self.configuration.local) try: self.local_repo.hg_push() except hgapi.HgException as e: # allow when the local repository is empty, else re-raise if "no changes found" not in str(e): raise e def local_and_remote(self): print("Good. Both local and remote repositories are available.") self.local_repo = hgapi.Repo(abspath(self.configuration.local)) self.remote_repo = hgapi.Repo(abspath(self.configuration.remote)) def _update_ignorefile(self, repo, path): """ Check if the `.hgignore` file is valid, if not delete the invalid one and recreate it. """ ignorefile = join(path, ".hgignore") if update_ignorefile(ignorefile): # ...and add and commit repo.hg_add(ignorefile) if repo.hg_status(empty=True) != {}: print("Adding the ignored fileset...") repo.hg_commit("[VONK] Added .hgignore.") def _recreate_path(self, path): if exists(path): rm(path, recursive=True) mkdirs(path, parents=True) def _set_default_repo_paths(self, path, default, push=None): """ Makes sure that the default path (and if given the default-push) of path are set. This is required to link local to remote. """ file = open(path + "/.hg/hgrc", 'w') file.write("[paths]\n") file.write("default = " + default + "\n") if push is None: push = default file.write("default-push = " + push + "\n") file.close() def start(self): self.monitor.start() self.monitor.join() def stop(self): """Shutdown.""" print("Shutting down...") self.monitor.stop() print("System halted.")