def clone(self, original_git_url, repo_name=None, mode="https"): clone_git_url = self._parse_git_url(original_git_url, mode=mode) if not repo_name: repo_name = clone_git_url.split("/")[-1][:-4] try: process = subprocess.Popen([ self.execpath, "clone", str(clone_git_url), os.path.join(self.filepath, repo_name) ], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.clone", (original_git_url, str(stderr)))) except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.clone", (original_git_url, str(e)))) return True
def stash_apply(self, regex=None): # TODO: Test this function try: if regex: process = subprocess.Popen([ self.execpath, "stash", "apply", "stash^{/" + regex + "}" ], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: process = subprocess.Popen( [self.execpath, "stash", "apply", "stash^{0}"], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.stash_apply", str(stderr))) git_stash_apply = stdout.decode().strip() except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.stash_apply", str(e))) return git_stash_apply
def init(self): try: process = subprocess.Popen( [self.execpath, "init", str(self.filepath)], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.init", str(stderr))) except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.init", str(e))) try: code_refs_success = self.ensure_code_refs_dir() except Exception as e: raise FileIOException( __("error", "controller.code.driver.git.init.file", str(e))) try: datmo_files_ignored_success = self.ensure_datmo_files_ignored() except Exception as e: raise FileIOException( __("error", "controller.code.driver.git.init.file", str(e))) return code_refs_success and datmo_files_ignored_success
def remote(self, mode, origin, git_url): # TODO: handle multiple remote urls try: if mode == "set-url": process = subprocess.Popen( [self.execpath, "remote", "set-url", origin, git_url], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.filepath) stdout, stderr = process.communicate() stdout, stderr = stdout.decode(), stderr.decode() elif mode == "add": process = subprocess.Popen( [self.execpath, "remote", "add", origin, git_url], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.filepath) stdout, stderr = process.communicate() stdout, stderr = stdout.decode(), stderr.decode() else: raise GitExecutionException( __("error", "controller.code.driver.git.remote", (mode, origin, git_url, "Incorrect mode specified"))) if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.remote", (mode, origin, git_url, stderr))) except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.remote", (mode, origin, git_url, str(e)))) return True
def fetch_ref(self, commit_id): try: datmo_ref = "refs/datmo/" + commit_id datmo_ref_map = "+" + datmo_ref + ":" + datmo_ref success, err = self.fetch("origin", datmo_ref_map, option="-fup") if not success: raise GitExecutionException( __("error", "controller.code.driver.git.fetch_ref", (commit_id, err))) except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.fetch_ref", (commit_id, str(e)))) return True
def __init__(self, filepath, execpath, remote_url=None): super(GitCodeDriver, self).__init__() self.filepath = filepath # Check if filepath exists if not os.path.exists(self.filepath): raise PathDoesNotExist( __("error", "controller.code.driver.git.__init__.dne", filepath)) self.execpath = execpath # Check the execpath and the version try: p = subprocess.Popen([self.execpath, "version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.filepath) out, err = p.communicate() out, err = out.decode(), err.decode() if err: raise GitExecutionException( __("error", "controller.code.driver.git.__init__.giterror", err)) version = str(out.split()[2].split(".windows")[0]) if not semver.match(version, ">=1.9.7"): raise GitExecutionException( __("error", "controller.code.driver.git.__init__.gitversion", out.split()[2])) except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.__init__.giterror", str(e))) # TODO: handle multiple remote urls self.git_host_manager = GitHostDriver() self._is_initialized = self.is_initialized if self._is_initialized: # If initialized ensure .datmo is not in working tree else error if self.exists_datmo_files_in_worktree(): raise DatmoFolderInWorkTree( __("error", "controller.code.driver.git.__init__.datmo")) # If initialized ensure .datmo is ignored (in .git/info/exclude) self.ensure_datmo_files_ignored() # If initialized update remote information if remote_url: self.remote("set-url", "origin", remote_url) self._remote_url = self.remote_url self._remote_access = False self.type = "git"
def reset(self, git_commit): try: process = subprocess.Popen([self.execpath, "reset", git_commit], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() _ = stdout.decode().strip() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.reset", str(stderr))) except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.reset", str(e))) return True
def reset(self, git_commit): try: subprocess.check_output([self.execpath, "reset", git_commit], cwd=self.filepath).strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.reset", str(e))) return True
def exists_datmo_files_in_worktree(self): try: process = subprocess.Popen( [self.execpath, "ls-files", "|", "grep", ".datmo"], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.init", str(stderr))) result = stdout.decode().strip() return True if result else False except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.init", str(e)))
def push_ref(self, commit_id="*"): datmo_ref = "refs/datmo/" + commit_id datmo_ref_map = "+" + datmo_ref + ":" + datmo_ref try: return self.push("origin", name=datmo_ref_map) except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.push_ref", str(e)))
def latest_commit(self): try: process = subprocess.Popen( [self.execpath, "log", "--format=%H", "-n", "1"], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.latest_commit", str(stderr))) git_commit = stdout.decode().strip() except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.latest_commit", str(e))) return git_commit
def exists_datmo_files_in_worktree(self): try: result = subprocess.check_output( [self.execpath, "ls-files", "|", "grep", ".datmo"], cwd=self.filepath).strip() return True if result else False except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.init", str(e)))
def check_git_work_tree(self): try: process = subprocess.Popen( [self.execpath, "rev-parse", "--is-inside-work-tree"], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() git_work_tree_exists = stdout.decode().strip() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.check_git_work_tree", str(stderr))) except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.check_git_work_tree", str(e))) return True if git_work_tree_exists == "true" else False
def stash_list(self, regex="datmo"): # TODO: Test this function try: process = subprocess.Popen([ self.execpath, "stash", "list", "|", "grep", """ + regex + """ ], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.stash_list", str(stderr))) git_stash_list = stdout.decode().strip() except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.stash_list", str(e))) return git_stash_list
def push(self, origin, option=None, name=None): try: if option: if name: p = subprocess.Popen( [self.execpath, "push", option, origin, name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.filepath) out, err = p.communicate() out, err = out.decode(), err.decode() else: p = subprocess.Popen( [self.execpath, "push", option, origin], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.filepath) out, err = p.communicate() out, err = out.decode(), err.decode() else: if name: p = subprocess.Popen([self.execpath, "push", origin, name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.filepath) out, err = p.communicate() out, err = out.decode(), err.decode() else: p = subprocess.Popen([self.execpath, "push", origin], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.filepath) out, err = p.communicate() out, err = out.decode(), err.decode() if err: raise GitExecutionException( __("error", "controller.code.driver.git.push", (origin, err))) except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.push", (origin, str(e)))) return True
def check_git_work_tree(self): try: git_work_tree_exists = subprocess.check_output( [self.execpath, "rev-parse", "--is-inside-work-tree"], cwd=self.filepath) git_work_tree_exists = git_work_tree_exists.decode().strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.check_git_work_tree", str(e))) return True if git_work_tree_exists == "true" else False
def latest_commit(self): try: git_commit = subprocess.check_output( [self.execpath, "log", "--format=%H", "-n", "1"], cwd=self.filepath) git_commit = git_commit.decode().strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.latest_commit", str(e))) return git_commit
def stash_list(self, regex="datmo"): # TODO: Test this function try: git_stash_list = subprocess.check_output([ self.execpath, "stash", "list", "|", "grep", """ + regex + """ ], cwd=self.filepath) git_stash_list = git_stash_list.decode().strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.stash_list", str(e))) return git_stash_list
def checkout_ref(self, commit_id, remote=False): try: # Run checkout for the specific ref as usual if remote: self.fetch_ref(commit_id) datmo_ref = "refs/datmo/" + commit_id checkout_result = self.checkout(datmo_ref) return checkout_result except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.checkout_ref", (commit_id, str(e))))
def add(self, filepath, option=None): try: if option: subprocess.check_output( [self.execpath, "add", option, filepath], cwd=self.filepath).strip() else: subprocess.check_output([self.execpath, "add", filepath], cwd=self.filepath).strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.add", (filepath, str(e)))) return True
def fetch(self, origin, name, option=None): try: if option: subprocess.check_output( [self.execpath, "fetch", option, origin, name], cwd=self.filepath).strip() else: subprocess.check_output([self.execpath, "fetch", origin, name], cwd=self.filepath).strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.fetch", (origin, name, str(e)))) return True
def stash_save(self, message=None): # TODO: Test this function try: if message: subprocess.check_output( [self.execpath, "stash", "save", message], cwd=self.filepath).strip() else: subprocess.check_output([self.execpath, "stash"], cwd=self.filepath).strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.stash_save", str(e))) return True
def stash_save(self, message=None): # TODO: Test this function try: if message: process = subprocess.Popen( [self.execpath, "stash", "save", message], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: process = subprocess.Popen([self.execpath, "stash"], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.stash_save", str(stderr))) except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.stash_save", str(e))) return True
def checkout(self, name, option=None): try: if option: process = subprocess.Popen( [self.execpath, "checkout", option, name], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: process = subprocess.Popen([self.execpath, "checkout", name], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.checkout", (name, str(stderr)))) except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.checkout", (name, str(e)))) return True
def commit(self, options): """Git commit wrapper Parameters ---------- options: list List of strings for the command (e.g. ["-m", "hello"]) Returns ------- bool True if new commit was created, False if no commit was created Raises ------ GitExecutionException If any errors occur in running git """ try: process = subprocess.Popen([self.execpath, "commit"] + options, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.filepath) stdout, stderr = process.communicate() stdout, stderr = stdout.decode(), stderr.decode() if "nothing" in stdout: return False if process.returncode > 0: raise GitExecutionException( __("error", "controller.code.driver.git.commit", (options, stderr))) except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.commit", (options, str(e)))) return True
def stash_pop(self, regex=None): # TODO: Test this function try: if regex: git_stash_pop = subprocess.check_output( [self.execpath, "stash", "pop", "stash^{/" + regex + "}"], cwd=self.filepath) git_stash_pop = git_stash_pop.decode().strip() else: git_stash_pop = subprocess.check_output( [self.execpath, "stash", "pop"], cwd=self.filepath) git_stash_pop = git_stash_pop.decode().strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.stash_pop", str(e))) return git_stash_pop
def clone(self, original_git_url, repo_name=None, mode="https"): clone_git_url = self._parse_git_url(original_git_url, mode=mode) if not repo_name: repo_name = clone_git_url.split("/")[-1][:-4] try: subprocess.check_output([ self.execpath, "clone", str(clone_git_url), os.path.join(self.filepath, repo_name) ], cwd=self.filepath).strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.clone", (original_git_url, str(e)))) return True
def init(self): try: subprocess.check_output( [self.execpath, "init", str(self.filepath)], cwd=self.filepath).strip() except Exception as e: raise GitExecutionException( __("error", "controller.code.driver.git.init", str(e))) try: code_refs_success = self.ensure_code_refs_dir() except Exception as e: raise FileIOException( __("error", "controller.code.driver.git.init.file", str(e))) try: datmo_files_ignored_success = self.ensure_datmo_files_ignored() except Exception as e: raise FileIOException( __("error", "controller.code.driver.git.init.file", str(e))) return code_refs_success and datmo_files_ignored_success
def get_remote_url(self): try: # TODO: handle multiple remote urls process = subprocess.Popen( [self.execpath, "config", "--get", "remote.origin.url"], cwd=self.filepath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() if process.returncode > 0: return None # raise GitExecutionException(__("error", # "controller.code.driver.git.get_remote_url", # str(stderr))) git_url = stdout.decode().strip() except subprocess.CalledProcessError as e: raise GitExecutionException( __("error", "controller.code.driver.git.get_remote_url", str(e))) return git_url