def clone_watcher(repo, base=None, name=None): """clone a watcher from Github (or other version control with git) meaning that we clone to a temporary folder, and then move to a new folder. By default, the user gets all tasks associated with the watcher, along with the git folder so that removing is also done with version control. Parameters ========== repo: the repository to clone base: the watchme base, defaults to $HOME/.watchme name: a new name for the watcher, if a rename is desired. """ if base is None: base = WATCHME_BASE_DIR # clone_watcher(repo=repo, base=args.base, name=extra) # STOPPED HERE - need to test this. # Validate the repository address if not re.search("^git@|http", repo): bot.exit("Please provide a valid url to git repository") # if the name is None, use the repo name if name is None: name = os.path.basename(repo) # Ensure we aren't overwriting dest = os.path.join(base, name) if os.path.exists(dest): bot.exit("%s already exists, choose a different watcher name." % name) clone_dest = get_tmpdir(prefix="watchme-clone", create=False) run_command("git clone %s %s" % (repo, clone_dest)) # Valid by default - will copy over if valid valid = True # Iterate over watchers watchers = os.listdir(clone_dest) for watcher in watchers: watcher = os.path.join(clone_dest, watcher) tasks = os.listdir(watcher) # Check that tasks include watchme.cfg for task in tasks: if not task.startswith("task"): continue task_folder = os.path.join(watcher, task) content = os.listdir(task_folder) if "watcher.cfg" not in content: bot.error("%s is missing a watcher.cfg" % task) valid = False break if valid: shutil.move(clone_dest, dest) if os.path.exists(clone_dest): shutil.rmtree(clone_dest)
def from_env_task(**kwargs): '''Get some set of variables from the environment. We look for those defined in kwargs, but also any in the environment that start with the prefix WATCHMENEV_. They are saved to files that are equivalently named, and the idea is that we can track a changing (single) result, meaning that the timestamp is meaningful, or we can track values for many different results, so the timestamps just serve to record when each was recorded. Parameters ========== *: any number of parameters from the task configuration that will be looked for in the environment. ''' results = [] # Create a temporary directory for results tmpdir = get_tmpdir() # First extract variables from the environment environ = get_watchme_env() for key, value in environ.items(): # Write the result to file (don't include extension) filename = os.path.join(tmpdir, key) write_file(filename, value) results.append(filename) # If no results, return None if len(results) == 0: shutil.rmtree(tmpdir) return results
def git_clone(repo, name=None, base=None, force=False): """clone a git repo to a destination. The user can provide the following groupings of arguments: base without name: destination is ignored, the repo is cloned (named as it is) to the base. If the folder exists, --force must be used to remove it first. base with name: destination is ignored, repo is cloned (and named based on name variable) to the base. The same applies for force. dest provided: the repo is cloned to the destination, if it doesn't exist and/or force is True. Parameters ========== name: the name of the watcher to add base: the base of the watcher (defaults to $HOME/.watchme force: remove first if already exists """ if base is None: base = WATCHME_BASE_DIR # Derive the repository name if name is None: name = os.path.basename(repo).replace(".git", "") # First clone to temporary directory tmpdir = get_tmpdir() command = "git clone %s %s" % (repo, tmpdir) bot.debug(command) run_command(command) # ensure there is a watchme.cfg if not os.path.exists(os.path.join(tmpdir, "watchme.cfg")): shutil.rmtree(tmpdir) bot.exit("No watchme.cfg found in %s, aborting." % repo) # If it's good, move the repository dest = os.path.join(base, name) # Don't allow for overwrite if os.path.exists(dest): if force is False: shutil.rmtree(tmpdir) bot.exit("%s exists. Use --force to overwrite" % dest) else: shutil.rmtree(dest) # Move the repository there shutil.move(tmpdir, dest) # Ensure we don't sign gpg key run_command("git --git-dir=%s/.git config commit.gpgsign false" % dest) bot.info("Added watcher %s" % name)
def test_mkdir(self): print('Testing utils.mkdir_p') from watchme.utils import mkdir_p tmpdir = os.path.join(self.tmpdir, "if", "I", "were", "an", "avocado") mkdir_p(tmpdir) self.assertTrue(os.path.exists(tmpdir)) print('Testing utils.get_tmpdir') from watchme.utils import get_tmpdir tmpdir = get_tmpdir() self.assertTrue(os.path.exists(tmpdir)) self.assertTrue('watchme' in tmpdir) shutil.rmtree(tmpdir)