Skip to content

Commit

Permalink
f-sa
Browse files Browse the repository at this point in the history
  • Loading branch information
unkcpz committed Oct 17, 2023
1 parent 1184f99 commit ca86c2f
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions aiidalab_widgets_base/computational_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import copy
import enum
import os
import re
import subprocess
import threading
from collections import namedtuple
from pathlib import Path
from uuid import UUID

import ipywidgets as ipw
import jinja2
import pexpect
import shortuuid
import traitlets as tl
Expand All @@ -18,7 +20,6 @@
from aiida.transports.plugins import ssh as aiida_ssh_plugin
from humanfriendly import InvalidSize, parse_size
from IPython.display import clear_output, display
from jinja2 import Environment, meta

from .databases import ComputationalResourcesDatabaseWidget
from .utils import MessageLevel, StatusHTML, wrap_message
Expand Down Expand Up @@ -241,11 +242,12 @@ def __init__(self, ssh_folder=None, **kwargs):
# ssh folder init
if ssh_folder is None:
ssh_folder = Path.home() / ".ssh"
if not ssh_folder.exists():
ssh_folder.mkdir()
ssh_folder.chmod(0o700)

self.ssh_folder = ssh_folder
if not ssh_folder.exists():
ssh_folder.mkdir()
ssh_folder.chmod(0o700)

self._ssh_folder = ssh_folder

self._ssh_connection_message = None
self._password_message = ipw.HTML()
Expand Down Expand Up @@ -277,11 +279,11 @@ def __init__(self, ssh_folder=None, **kwargs):
)

# Username.
self.username = ipw.Text(description="username:", layout=LAYOUT, style=STYLE)
self.username = ipw.Text(description="Username:", layout=LAYOUT, style=STYLE)

# Port.
self.port = ipw.IntText(
description="port:",
description="Port:",
value=22,
layout=LAYOUT,
style=STYLE,
Expand Down Expand Up @@ -357,7 +359,7 @@ def _ssh_keygen(self):
"Generating SSH key pair.",
MessageLevel.SUCCESS,
)
fpath = Path.home() / ".ssh" / "id_rsa"
fpath = self._ssh_folder / "id_rsa"
keygen_cmd = [
"ssh-keygen",
"-f",
Expand Down Expand Up @@ -393,7 +395,7 @@ def _can_login(self):

def _is_in_config(self):
"""Check if the SSH config file contains host information."""
config_path = self.ssh_folder / "config"
config_path = self._ssh_folder / "config"
if not config_path.exists():
return False
sshcfg = aiida_ssh_plugin.parse_sshconfig(self.hostname.value)
Expand All @@ -406,7 +408,7 @@ def _is_in_config(self):

def _write_ssh_config(self, private_key_abs_fname=None):
"""Put host information into the config file."""
config_path = self.ssh_folder / "config"
config_path = self._ssh_folder / "config"

self.message = wrap_message(
f"Adding {self.hostname.value} section to {config_path}",
Expand Down Expand Up @@ -483,9 +485,9 @@ def _on_setup_ssh_button_pressed(self, _=None):

# if the private key filename is exist, generate random string and append to filename subfix
# then override current name.
if filename in [str(p.name) for p in Path(self.ssh_folder).iterdir()]:
if filename in [str(p.name) for p in Path(self._ssh_folder).iterdir()]:
private_key_fname = str(
Path(self.ssh_folder) / f"{filename}-{shortuuid.uuid()}"
Path(self._ssh_folder) / f"{filename}-{shortuuid.uuid()}"
)

self._add_private_key(private_key_fname, private_key_content)
Expand Down Expand Up @@ -600,7 +602,7 @@ def _on_verification_mode_change(self, change):
if self._verification_mode.value == "private_key":
display(self._inp_private_key)
elif self._verification_mode.value == "public_key":
public_key = Path.home() / ".ssh" / "id_rsa.pub"
public_key = self._ssh_folder / "id_rsa.pub"
if public_key.exists():
display(
ipw.HTML(
Expand Down Expand Up @@ -1248,8 +1250,6 @@ def _observe_code_setup(self, _=None):
try:
self.default_calc_job_plugin.value = value
except tl.TraitError:
import re

# If is a template then don't raise the error message.
if not re.match(r".*{{.+}}.*", value):
self.message = wrap_message(
Expand Down Expand Up @@ -1431,11 +1431,11 @@ def _render(self):
self._help_text.value = f"""<div>{tooltip}</div>"""

for line_key, line_str in self.templates.items():
env = Environment()
env = jinja2.Environment()
parsed_content = env.parse(line_str)

# vars is a set of variables in the template
line_vars = meta.find_undeclared_variables(parsed_content)
line_vars = jinja2.meta.find_undeclared_variables(parsed_content)

# Create a widget for each variable.
# The var is the name in a template string
Expand Down Expand Up @@ -1518,7 +1518,7 @@ def _on_template_variable_filled(self, change):
}

# re-render the template
env = Environment()
env = jinja2.Environment()
filled_str = env.from_string(line.str).render(**inp_dict)

# Update the filled template.
Expand Down Expand Up @@ -1890,9 +1890,9 @@ def _fill_template(self):
filled_templates = copy.deepcopy(w_tmp.filled_templates)

for k, v in w_tmp.filled_templates.items():
env = Environment()
env = jinja2.Environment()
parsed_content = env.parse(v)
vs = meta.find_undeclared_variables(parsed_content)
vs = jinja2.meta.find_undeclared_variables(parsed_content)

# No variables in the template, all filled.
if len(vs) == 0:
Expand Down

0 comments on commit ca86c2f

Please sign in to comment.