Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if os.path.lexists(destname):
# remove incorrect contents
try:
if os.path.isdir(destname):
shutil.rmtree(destname)
else:
os.remove(destname)
except OSError:
self.logger.info('Failed to remove %s' %
destname)
return False
dulwich.file.ensure_dir_exists(destname)
destr = dulwich.repo.Repo.init(destname)
determine_wants = destr.object_store.determine_wants_all
cl, host_path = dulwich.client.get_transport_and_path(
entry.get('sourceurl'))
remote_refs = cl.fetch(host_path,
destr,
determine_wants=determine_wants,
progress=sys.stdout.write)
if entry.get('revision') in remote_refs:
destr.refs['HEAD'] = remote_refs[entry.get('revision')]
else:
destr.refs['HEAD'] = entry.get('revision')
dtree = destr['HEAD'].tree
index = dulwich.index.Index(destr.index_path())
for fname, mode, sha in destr.object_store.iter_tree_contents(dtree):
full_path = os.path.join(destname, fname)
dulwich.file.ensure_dir_exists(os.path.dirname(full_path))
origin_uri = origin_uri or self.origin_uri
# Fail if inexistant
if not origin_uri:
raise InvalidRemoteUrl()
client_kwargs = {}
auth_kwargs = self.authenticator.kwargs()
client_kwargs.update(auth_kwargs)
client_kwargs.update(kwargs)
client_kwargs.update({
'report_activity': self.report_activity
})
client, remote_path = get_transport_and_path(origin_uri, **client_kwargs)
return client, remote_path
def list_remote_refs(git_url):
"""Get the refs from a remote git repo as a dictionary of name->hash."""
client, path = dulwich.client.get_transport_and_path(git_url)
try:
return client.fetch_pack(path, lambda refs: [], None, None)
except dulwich.errors.HangupException as e:
raise LSRemoteException("Unable to fetch remote refs: %s" % e)
def _fetch_pack(self):
"""Fetch changes and store them in a pack."""
def prepare_refs(refs):
return [ref.hash.encode('utf-8') for ref in refs
if not ref.refname.endswith('^{}')]
def determine_wants(refs):
remote_refs = prepare_refs(self._discover_refs(remote=True))
local_refs = prepare_refs(self._discover_refs())
wants = [ref for ref in remote_refs if ref not in local_refs]
return wants
client, repo_path = dulwich.client.get_transport_and_path(self.uri)
repo = dulwich.repo.Repo(self.dirpath)
fd = io.BytesIO()
local_refs = self._discover_refs()
graph_walker = _GraphWalker(local_refs)
result = client.fetch_pack(repo_path,
determine_wants,
graph_walker,
fd.write)
refs = [GitRef(ref_hash.decode('utf-8'), ref_name.decode('utf-8'))
for ref_name, ref_hash in result.refs.items()]
if len(fd.getvalue()) > 0:
fd.seek(0)
pack = repo.object_store.add_thin_pack(fd.read, None)
outstream=default_bytes_out_stream,
errstream=default_bytes_err_stream):
"""Remote push with dulwich via dulwich.client
:param repo: Path to repository
:param remote_location: Location of the remote
:param refspecs: Refs to push to remote
:param outstream: A stream file to write output
:param errstream: A stream file to write errors
"""
# Open the repo
with open_repo_closing(repo) as r:
# Get the client and path
client, path = get_transport_and_path(remote_location)
selected_refs = []
def update_refs(refs):
selected_refs.extend(parse_reftuples(r.refs, refs, refspecs))
new_refs = {}
# TODO: Handle selected_refs == {None: None}
for (lh, rh, force) in selected_refs:
if lh is None:
new_refs[rh] = ZERO_SHA
else:
new_refs[rh] = r.refs[lh]
return new_refs
err_encoding = getattr(errstream, 'encoding', None) or DEFAULT_ENCODING
remote_location_bytes = client.get_url(path).encode(err_encoding)
def WrapClient(location):
"""Returns a ClientWrapper."""
transport, path = client.get_transport_and_path(location)
if isinstance(transport, client.TraditionalGitClient):
return TraditionalClient(transport, path)
elif isinstance(transport, client.HttpGitClient):
return HTTPClient(transport, path)
elif isinstance(transport, client.LocalGitClient):
return LocalClient(transport, path)
else:
# We can't currently trigger this, everything falls through to a local
# repository, but just in case...
raise UnsupportedClientType(transport.__class__.__name__)
ssh_kwargs = {}
if course.ssh_private_key:
from io import StringIO
key_file = StringIO(course.ssh_private_key)
ssh_kwargs["pkey"] = paramiko.RSAKey.from_private_key(key_file)
def get_dulwich_ssh_vendor():
from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor
vendor = ParamikoSSHVendor(**ssh_kwargs)
return vendor
# writing to another module's global variable: gross!
dulwich.client.get_ssh_vendor = get_dulwich_ssh_vendor
from dulwich.client import get_transport_and_path
client, remote_path = get_transport_and_path(
course.git_source)
return client, remote_path