Skip to content

Commit 7a14562

Browse files
committed
Support Paramiko SSH
1 parent b3f431a commit 7a14562

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

QuickProject/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,3 @@ def _choose_server_target():
476476
return server_targets[index]
477477
except:
478478
return None
479-
480-
481-
def apply_fig_complete(name: str, team: str = "", token: str = ""):
482-
external_exec(
483-
f'npx @fig/publish-spec --spec-path complete/fig/*.ts --name {name}{" --team " + team if team else ""}{" --token " + token if token else ""}'
484-
)

QuickProject/ssh.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from . import *
2+
import paramiko
3+
SShConfig = requirePackage("paramiko.config", "SSHConfig", real_name='paramiko')
4+
from paramiko import SSHClient
5+
6+
class ParamikoSSH:
7+
def __init__(self):
8+
self.config = SShConfig()
9+
with open(os.path.expanduser("~/.ssh/config")) as f:
10+
self.config.parse(f)
11+
if os.path.exists(os.path.expanduser("~/.ssh/id_rsa")):
12+
self.identity = os.path.expanduser("~/.ssh/id_rsa")
13+
else:
14+
self.identity = None
15+
self.activate_connections = {}
16+
17+
def _parse_host(self, host: str, user: str = None, port: int = 22, identity: str = None):
18+
host_config = self.config.lookup(host)
19+
if not host_config and not user:
20+
raise Exception("Host not found in config file.")
21+
if host_config:
22+
host = host_config.get("hostname", host)
23+
identity = host_config.get("identityfile")
24+
if not user:
25+
user = host_config.get("user", None)
26+
if not user:
27+
from QuickProject import QproErrorString
28+
QproDefaultConsole.print(QproErrorString, "User not found in config file.")
29+
if not identity:
30+
identity = self.identity
31+
port = host_config.get("port", port)
32+
return host, user, port, identity
33+
34+
def connect(self, host: str, user: str = None, port: int = 22, identity: str = None) -> SSHClient:
35+
conn_id = self._parse_host(host, user, port, identity)
36+
37+
if conn_id in self.activate_connections:
38+
return self.activate_connections[conn_id]
39+
40+
ssh = SSHClient()
41+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
42+
ssh.connect(*conn_id)
43+
self.activate_connections[conn_id] = ssh
44+
return ssh
45+
46+
def close(self, host: str, user: str = None, port: int = 22, identity: str = None):
47+
conn_id = self._parse_host(host, user, port, identity)
48+
49+
if conn_id in self.activate_connections:
50+
self.activate_connections[conn_id].close()
51+
del self.activate_connections[conn_id]
52+
53+
def __del__(self):
54+
for k in self.activate_connections:
55+
self.activate_connections[k].close()
56+
self.activate_connections.clear()

dist/qpro-0.12.18.tar.gz

-26.4 KB
Binary file not shown.

dist/qpro-0.12.19.tar.gz

26.9 KB
Binary file not shown.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "qpro"
3-
version = "0.12.18"
3+
version = "0.12.19"
44
description = "Small but powerful command line APP framework"
55
authors = ["Rhythmicc <rhythmlian.cn@gmail.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)