def _check_repo(self): self.notebook_dir = super(GitNotebookManager, self)._get_os_path("") if self._repo is not None: return try: self._repo = git.Repo(self.notebook_dir) except NotGitRepository: git.Repo.init(self.notebook_dir) self._repo = git.Repo(self.notebook_dir)
def git_reset_repo_to_origin(): try: repo = porcelain.Repo(database) porcelain.fetch(repo) tree_head_id = repo[repo[b'refs/heads/master'].tree].id tree_origin_master_id = repo[ repo[b'refs/remotes/origin/master'].tree].id store = repo.object_store list_all_files_head = list_all_files(store, tree_head_id) list_all_files_origin_master = list_all_files(store, tree_origin_master_id) deleted_files = list( set(list_all_files_head) - set(list_all_files_origin_master)) # print(deleted_files) if deleted_files != []: for all in deleted_files: file_path = os.path.join(database, all.decode('utf-8')) os.remove(file_path) status = porcelain.status(repo) repo.stage(status.unstaged) porcelain.commit(repo, message="delete files") ###working### porcelain.reset(repo, "hard", treeish=b"refs/remotes/origin/master") porcelain.clean(repo=repo, target_dir=database) resolve_divergence() ######## return True except MaxRetryError: print('MaxRetryError') return False except ProtocolError: print('ProtocolError') return False
def push(name, api, domain): repo = git.Repo(os.getcwd()) branch = "temp-{}".format(str(uuid.uuid4())[:8]) set_deploy_branch(name, branch, api, domain) remote = git_url(name, api, domain) if is_dirty(): print("Nuking changes.") git.reset(repo, "hard") with TempBranch(branch, repo, delete=True): for fname, file_info in openshift_files.items(): with open(fname, 'w') as f: f.write(file_info.get("contents", "")) repo.stage(fname) repo.do_commit("Commit openshift files") push_out = StringIO() push_err = StringIO() print("Pushing to openshift (may take a few minutes)") git.push(repo, remote, "refs/heads/{}".format(branch), outstream=push_out, errstream=push_err) push_out.seek(0) out = push_out.read() if not re.match(r'^Push to .* successful.', out): print("There was a failure while pushing") print("---BEGIN STDERR---") push_err.seek(0) print(push_err.read()) print("---BEGIN STDOUT---") print(out) print("There was a failure while pushing") git.rm(repo, openshift_files.keys()) map(os.remove, openshift_files.keys()) return get_app(name, api, domain)['app_url']
def get_default_name(cwd): """ Return a default name for a directory, based on repo name or directory name """ try: repo = git.Repo(cwd) except NotGitRepository: repo = None if repo is not None: # Repo found, try to get repo name try: origin = repo.get_config()[('remote', 'origin')]['url'] dotgit = origin.split('/')[-1] return dotgit.replace('.git', '') except (KeyError, IndexError): pass # Not a repo, return folder name try: return cwd.split('/')[-1] except: pass
def main(appliances=[], credentials=[], timeout=120, no_check_hostname=False, base_dir=default_base_dir, comment=default_comment, persisted=False, recursive=False, no_strip_timestamp=False, page=False): """ track_getconfig.py Description: Store running or persisted domain configuration in a local git repository for auditing purposes. Usage: :::bash $ mast contrib/track_getconfig.py --appliances <HOSTNAMES> --credentials <USER:PASS> --base-dir tmp/config Parameters: * `-a, --appliances` - The hostname(s), ip addresse(s), environment name(s) or alias(es) of the appliances you would like to affect. For details on configuring environments please see the comments in `environments.conf` located in `$MAST_HOME/etc/default`. For details on configuring aliases please see the comments in `hosts.conf` located in `$MAST_HOME/etc/default`. * `-c, --credentials`: The credentials to use for authenticating to the appliances. Should be either one set to use for all appliances or one set for each appliance. Credentials should be in the form `username:password` and should be provided in a space-seperated list if multiple are provided. If you would prefer to not use plain-text passwords, you can use the output of `$ mast-system xor <username:password>`. * `-t, --timeout`: The timeout in seconds to wait for a response from an appliance for any single request. __NOTE__ Program execution may halt if a timeout is reached. * `-n, --no-check-hostname`: If specified SSL verification will be turned off when sending commands to the appliances. * `-b, --base-dir`: The base directory where to store the downloaded files. Files will actually be stored in a subdirectory of `base_dir` named after the hostname in the form of `base_dir/<hostname>` * `-p, --persisted`: If specified, the persisted configuration will be retrieved as opposed to the running configuration (which is the default) * `-r, --recursive`: If specified, the configuration will be retrieved recursively to the extent of the facilities provided by DataPower * `-N, --no-strip-timestamp`: If specified, the timestamp will not be stripped from the XML document, This is done because we are tracking this information with git and stripping this out allows us to alert on any changes in the repository as opposed to special-casing the difference in timestamp. * `-P, --page`: If specified, page the output when too long to display at once """ base_dir = os.path.abspath(base_dir) if not os.path.exists(base_dir): os.makedirs(base_dir) try: repo = git.Repo(base_dir) repo.head() except NotGitRepository: print "Initializing git repository" git.init(base_dir) git.add(base_dir) git.commit(base_dir, message="Initial Commit") except KeyError: git.add(base_dir) git.commit(base_dir, message="Initial Commit") check_hostname = not no_check_hostname env = datapower.Environment(appliances, credentials, timeout=timeout, check_hostname=check_hostname) for appliance in env.appliances: appliance_directory = os.path.join(base_dir, appliance.hostname) if not os.path.exists(appliance_directory): os.mkdir(appliance_directory) for domain in appliance.domains: config = appliance.get_config(domain=domain, recursive=recursive, persisted=persisted) config = config.pretty if no_strip_timestamp: pass else: config = re.sub(r"^.*?<dp:timestamp>.*?</dp:timestamp>.*?$", r"", config, flags=re.MULTILINE) filename = os.path.join(appliance_directory, "{}.xml".format(domain)) with open(filename, "wb") as fout: fout.write(config) git.add(base_dir) print git.status(base_dir) git.commit(base_dir, message=comment) tmp = StringIO() git.show(base_dir, outstream=tmp) tmp.seek(0) out = highlight(tmp.read(), DiffLexer(), TerminalFormatter()) if page: pprint.page(out) else: print out
def is_dirty(): """Check for uncommitted changes. True if dirty.""" repo = git.Repo(os.getcwd()) s = git.status(repo) return any(s.staged.values() + [s.unstaged])
def get_head_commit_sha(repo_path): return porcelain.Repo(str(repo_path)).head().decode()
def main(appliances=[], credentials=[], timeout=120, no_check_hostname=False, base_dir=default_base_dir, comment=default_comment, page=False, no_highlight_diff=False): """ track_autoconfig.py Description: Store persisted domain configuration in a local git repository for auditing purposes. Usage: :::bash $ mast contrib/track_autoconfig.py --appliances <HOSTNAMES> --credentials <USER:PASS> --base-dir tmp/config Parameters: * `-a, --appliances` - The hostname(s), ip addresse(s), environment name(s) or alias(es) of the appliances you would like to affect. For details on configuring environments please see the comments in `environments.conf` located in `$MAST_HOME/etc/default`. For details on configuring aliases please see the comments in `hosts.conf` located in `$MAST_HOME/etc/default`. * `-c, --credentials`: The credentials to use for authenticating to the appliances. Should be either one set to use for all appliances or one set for each appliance. Credentials should be in the form `username:password` and should be provided in a space-seperated list if multiple are provided. If you would prefer to not use plain-text passwords, you can use the output of `$ mast-system xor <username:password>`. * `-t, --timeout`: The timeout in seconds to wait for a response from an appliance for any single request. __NOTE__ Program execution may halt if a timeout is reached. * `-n, --no-check-hostname`: If specified SSL verification will be turned off when sending commands to the appliances. * `-b, --base-dir`: The base directory where to store the downloaded files. Files will actually be stored in a subdirectory of `base_dir` named after the hostname in the form of `base_dir/<hostname>` * `-p, --page`: If specified, page the output when too long to display at once * `-N, --no-highlight-diff`: If specified, the output of the diff will be syntax-highlighted """ base_dir = os.path.abspath(base_dir) if not os.path.exists(base_dir): os.makedirs(base_dir) try: repo = git.Repo(base_dir) first_sha1 = repo.head() print first_sha1 except NotGitRepository: print "Initializing git repository" git.init(base_dir) git.add(base_dir) first_sha1 = git.commit(base_dir, message="Initial Commit") print first_sha1 except KeyError: git.add(base_dir) git.commit(base_dir, message="Initial Commit") print pull_autoconfig(appliances=appliances, credentials=credentials, timeout=timeout, no_check_hostname=no_check_hostname, base_dir=base_dir) git.add(base_dir) print git.status(base_dir) second_sha1 = git.commit(base_dir, message=comment) print second_sha1 print "\n\nDIFF\n\n" tmp = StringIO() git.show(base_dir, outstream=tmp) tmp.seek(0) if no_highlight_diff: out = tmp.read() else: out = highlight(tmp.read(), DiffLexer(), TerminalFormatter()) if page: pprint.page(out) else: print out