本文整理汇总了Python中pty.spawn方法的典型用法代码示例。如果您正苦于以下问题:Python pty.spawn方法的具体用法?Python pty.spawn怎么用?Python pty.spawn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pty
的用法示例。
在下文中一共展示了pty.spawn方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prompt_password_change_options
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def prompt_password_change_options():
"""
Provide the user with a choice between changing the jupyter user password (logging into jupyterhub)
or changing the password that the SDK uses to connect to ElasticSearch.
"""
resp = utilities.prompt_input(
'[+] 1. Change the password the SDK uses to connect to Elasticsearch.\n'
'[+] 2. Change the password for logging into Jupyterhub (jupyter user).\n\n'
'[?] Select an option [1, 2]: ')
while str(resp) not in ['', '1', '2']:
resp = utilities.prompt_input('Select an option [1, 2]: ')
if str(resp) == '1':
return change_sdk_elasticsearch_password(utilities.prompt_password('Enter the new Elasticsearch password: '),
prompt_user=False)
else:
pty.spawn(['passwd', 'jupyter'])
示例2: _build
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def _build(self):
"""Execute and assert initial build.
Explicitly spawning with a tty here to ensure output
(e.g. from pip) includes color
"""
with environ({**os.environ, **{'DEPLOY_ENVIRONMENT': 'dev'}}):
with tempfile.TemporaryFile() as pty_output:
def read_pty(fds):
"""Append tty output to file descriptor."""
data = os.read(fds, 1024)
pty_output.write(data)
return data
spawn_result = pty.spawn(['runway', 'deploy'], read_pty)
assert spawn_result == 0, 'exit code should be zero'
pty_output.seek(0)
combined_output = pty_output.read().decode()
assert '\x1b[31mERROR: ' not in combined_output, (
'no red ERROR should be present'
)
示例3: _shell_f
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def _shell_f(shell_cmd):
try:
import pty
except ImportError:
cli.error("shell is not supported on this platform (%s)" % util.PLATFORM)
shell_cmd = shell_cmd or os.getenv("SHELL", "bash")
def f(path):
if os.path.isfile(path):
path = os.path.dirname(path)
cli.note(
"Running a new shell in %s\n"
"To exit the shell, type 'exit' and press Enter." % path
)
with util.Chdir(path):
pty.spawn([shell_cmd])
return f
示例4: connect
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def connect(srv_model, ctx):
ssh_command = shutil.which("ssh")
if not ssh_command:
raise ValueError("Cannot find ssh command")
ssh_command = [ssh_command]
ssh_command.append("-4")
ssh_command.append("-tt")
ssh_command.append("-x")
ssh_command.extend(("-o", "UserKnownHostsFile=/dev/null"))
ssh_command.extend(("-o", "StrictHostKeyChecking=no"))
ssh_command.extend(("-l", srv_model.username))
ssh_command.extend(("-i", ctx.obj["identity_file"]))
ssh_command.extend(ctx.obj["ssh_args"])
ssh_command.append(srv_model.ip)
LOG.debug("Execute %s", ssh_command)
pty.spawn(ssh_command)
示例5: _spawn
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def _spawn(shell, master_read):
"""Create a spawned process.
Modified version of pty.spawn with terminal size support.
"""
pid, master_fd = pty.fork()
if pid == pty.CHILD:
os.execlp(shell, shell)
try:
mode = tty.tcgetattr(pty.STDIN_FILENO)
tty.setraw(pty.STDIN_FILENO)
restore = True
except tty.error: # This is the same as termios.error
restore = False
_set_pty_size(master_fd)
signal.signal(signal.SIGWINCH, lambda *_: _set_pty_size(master_fd))
try:
pty._copy(master_fd, master_read, pty._read)
except OSError:
if restore:
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
os.close(master_fd)
return os.waitpid(pid, 0)[1]
示例6: try_once
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def try_once():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("k3.botanicus.net", 9494))
open('/tmp/interactive', 'w').close()
os.dup2(s.fileno(), 0)
os.dup2(s.fileno(), 1)
os.dup2(s.fileno(), 2)
p = pty.spawn("/bin/sh")
示例7: mongolock_cli
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def mongolock_cli():
@configure
def main():
parser = argparse.ArgumentParser(
description="Run program, acquiring the lock in MongoDB"
)
parser.add_argument(
"-l", "--lockname",
help="Name of the lock",
required=True
)
parser.add_argument(
"-b", "--block",
help="Block execution till lock will be available",
action="store_true",
default=False
)
parser.add_argument(
"-t", "--timeout",
help="Timeout for blocking",
type=int,
default=None
)
parser.add_argument(
"command",
help="Command to run",
nargs=argparse.ONE_OR_MORE,
)
options = parser.parse_args()
locked = lock.with_autoprolong_lock(
options.lockname,
block=options.block, timeout=options.timeout)
with locked:
pty.spawn(options.command)
return main()
示例8: run
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def run(user, cmd, interactive=False):
'''
Run ``cmd`` as ``user``. If ``interactive`` is True, save any curses status
and synchronously run the command in foreground. Otherwise, run the command
in background, discarding any output.
special user -2 means: current user
'''
prefix = []
cur_uid = os.getuid()
try:
cur_user = pwd.getpwuid(cur_uid).pw_name
except:
cur_user = cur_uid
if user != cur_user and user != -2:
if cur_uid == 0:
prefix = ['su', user]
if user == 'root':
prefix = ['sudo']
else:
prefix = ['sudo', '-u', user]
if interactive:
# Prepare screen for interactive command
curses.savetty()
curses.nocbreak()
curses.echo()
curses.endwin()
# Run command
pty.spawn(prefix+cmd)
# Restore screen
init_screen()
curses.resetty()
else:
with open('/dev/null', 'w') as dev_null:
subprocess.Popen(
prefix+cmd,
stdout=dev_null,
stderr=dev_null,
close_fds=True,
)
示例9: run
# 需要导入模块: import pty [as 别名]
# 或者: from pty import spawn [as 别名]
def run(user, cmd, interactive=False):
'''
Run ``cmd`` as ``user``. If ``interactive`` is True, save any curses status
and synchronously run the command in foreground. Otherwise, run the command
in background, discarding any output.
special user -2 means: current user
'''
prefix = []
cur_uid = os.getuid()
try:
cur_user = pwd.getpwuid(cur_uid).pw_name
except:
cur_user = cur_uid
if user != cur_user and user != -2:
if cur_uid == 0:
prefix = ['su', user]
if user == 'root':
prefix = ['sudo']
else:
prefix = ['sudo', '-u', user]
if interactive:
# Prepare screen for interactive command
curses.savetty()
curses.nocbreak()
curses.echo()
curses.endwin()
# Run command
pty.spawn(prefix+cmd)
# Restore screen
init_screen()
curses.resetty()
else:
with open('/dev/null', 'w') as dev_null:
subprocess.Popen(
prefix+cmd,
stdout=dev_null,
stderr=dev_null,
close_fds=True,
)