def test_run_command_password(self): vendor = ParamikoSSHVendor( allow_agent=False, look_for_keys=False, ) vendor.run_command('127.0.0.1', 'test_run_command_password', username=USER, port=self.port, password=PASSWORD) self.assertIn(b'test_run_command_password', self.commands)
def test_run_command_with_privkey(self): key = paramiko.RSAKey.from_private_key(StringIO(CLIENT_KEY)) vendor = ParamikoSSHVendor( allow_agent=False, look_for_keys=False, ) vendor.run_command('127.0.0.1', 'test_run_command_with_privkey', username=USER, port=self.port, pkey=key) self.assertIn(b'test_run_command_with_privkey', self.commands)
def ParamikoSSHVendor(**kwargs): import warnings warnings.warn( "ParamikoSSHVendor has been moved to dulwich.contrib.paramiko_vendor.", DeprecationWarning) from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor return ParamikoSSHVendor(**kwargs)
def test_run_command_data_transfer(self): vendor = ParamikoSSHVendor( allow_agent=False, look_for_keys=False, ) con = vendor.run_command('127.0.0.1', 'test_run_command_data_transfer', username=USER, port=self.port, password=PASSWORD) self.assertIn(b'test_run_command_data_transfer', self.commands) channel = self.transport.accept(5) channel.send(b'stdout\n') channel.send_stderr(b'stderr\n') channel.close() # Fixme: it's return false # self.assertTrue(con.can_read()) self.assertEqual(b'stdout\n', con.read(4096))
def get_dulwich_ssh_vendor(): from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor vendor = ParamikoSSHVendor(**ssh_kwargs) return vendor
def __init__(self): ParamikoSSHVendor.__init__(self) self.ssh_kwargs['key_filename']=ParamikoSSHwithKeyFile.key_filename
def push_with_key(repo_path, remote="ssh://[email protected]:22/QGB/QPSU.git", private_key='', refspecs='master', errstream=getattr(sys.stderr, 'buffer', None), private_key_password=None, retry=3): r''' #important# 三引号string中不能出现 \util 这种字符(常见于路径) # 会导致 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1-2: truncated \uXXXX escape 错误 # 最好 引号前加r 强制用 raw-string push ... 2021-04-15__06.26.44__.288 File "C:/QGB/babun/cygwin/bin\qgb\git.py", line 106, in up push_with_key(repo.path,url,**ka) File "C:/QGB/babun/cygwin/bin\qgb\git.py", line 205, in push_with_key progress=errstream.write) File "C:\QGB\Anaconda3\lib\site-packages\dulwich\client.py", line 789, in send_pack proto, unused_can_read, stderr = self._connect(b'receive-pack', path) File "C:\QGB\Anaconda3\lib\site-packages\dulwich\client.py", line 1410, in _connect **kwargs) File "C:\QGB\Anaconda3\lib\site-packages\dulwich\contrib\paramiko_vendor.py", line 103, in run_command client.connect(**connection_kwargs) File "C:\QGB\Anaconda3\lib\site-packages\paramiko\client.py", line 349, in connect retry_on_signal(lambda: sock.connect(addr)) File "C:\QGB\Anaconda3\lib\site-packages\paramiko\util.py", line 283, in retry_on_signal return function() File "C:\QGB\Anaconda3\lib\site-packages\paramiko\client.py", line 349, in <lambda> retry_on_signal(lambda: sock.connect(addr)) Out[26]: 'C:/QGB/babun/cygwin/bin/qgb/' ''' from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor import dulwich.porcelain from dulwich.protocol import ( Protocol, ZERO_SHA, ) from dulwich.objectspec import ( parse_commit, parse_object, parse_ref, parse_reftuples, parse_tree, ) from dulwich.errors import ( GitProtocolError, NotGitRepository, SendPackError, UpdateRefsError, ) DEFAULT_ENCODING = 'utf-8' selected_refs = [] if not private_key: private_key = U.get_or_set('id_rsa', F.get_home() + '.ssh/id_rsa') private_key = U.set('id_rsa', private_key) with open_private_key( private_key) as fpkey, dulwich.porcelain.open_repo_closing( repo_path) as r: 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 import paramiko pkey = paramiko.RSAKey.from_private_key(fpkey, password=private_key_password) if 'git@' not in repo_path: # raise py.NotImplementedError("'git@' not in repo_path") repo_path client, path = dulwich.client.get_transport_and_path( remote, vendor=ParamikoSSHVendor(pkey=pkey)) err_encoding = getattr(errstream, 'encoding', None) or DEFAULT_ENCODING remote_location_bytes = client.get_url(path).encode(err_encoding) #b'ssh://[email protected]:22/QGB/QPSU.git' try: client.send_pack( path, update_refs, generate_pack_data=r.object_store.generate_pack_data, progress=errstream.write) # print(remote_location_bytes) errstream.write(b"Push to " + remote_location_bytes + b" successful.\n") errstream.flush() # 与 stdout 混合打印时 ,顺序可能不同 except (UpdateRefsError, SendPackError) as e: errstream.write(b"Push to " + remote_location_bytes + b" failed -> " + e.message.encode(err_encoding) + b"\n") return py.No(e) finally: errstream.flush() return client