converted format strings to f strings

This commit is contained in:
Blake Harnden 2019-10-18 12:55:35 -07:00
parent 07b4408076
commit 5633d4d18b
4 changed files with 28 additions and 64 deletions

View file

@ -247,9 +247,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
""" """
session = self.coreemu.sessions.get(session_id) session = self.coreemu.sessions.get(session_id)
if not session: if not session:
context.abort( context.abort(grpc.StatusCode.NOT_FOUND, f"session {session_id} not found")
grpc.StatusCode.NOT_FOUND, "session {} not found".format(session_id)
)
return session return session
def get_node(self, session, node_id, context): def get_node(self, session, node_id, context):
@ -265,9 +263,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
try: try:
return session.get_node(node_id) return session.get_node(node_id)
except CoreError: except CoreError:
context.abort( context.abort(grpc.StatusCode.NOT_FOUND, f"node {node_id} not found")
grpc.StatusCode.NOT_FOUND, "node {} not found".format(node_id)
)
def CreateSession(self, request, context): def CreateSession(self, request, context):
""" """
@ -1577,17 +1573,13 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
nem_one = request.nem_one nem_one = request.nem_one
emane_one, netif = session.emane.nemlookup(nem_one) emane_one, netif = session.emane.nemlookup(nem_one)
if not emane_one or not netif: if not emane_one or not netif:
context.abort( context.abort(grpc.StatusCode.NOT_FOUND, f"nem one {nem_one} not found")
grpc.StatusCode.NOT_FOUND, "nem one {} not found".format(nem_one)
)
node_one = netif.node node_one = netif.node
nem_two = request.nem_two nem_two = request.nem_two
emane_two, netif = session.emane.nemlookup(nem_two) emane_two, netif = session.emane.nemlookup(nem_two)
if not emane_two or not netif: if not emane_two or not netif:
context.abort( context.abort(grpc.StatusCode.NOT_FOUND, f"nem two {nem_two} not found")
grpc.StatusCode.NOT_FOUND, "nem two {} not found".format(nem_two)
)
node_two = netif.node node_two = netif.node
if emane_one.id == emane_two.id: if emane_one.id == emane_two.id:

View file

@ -492,9 +492,7 @@ class CoreNode(CoreNodeBase):
raise ValueError("starting a node that is already up") raise ValueError("starting a node that is already up")
# create a new namespace for this node using vnoded # create a new namespace for this node using vnoded
vnoded = "{cmd} -v -c {name} -l {name}.log -p {name}.pid".format( vnoded = f"{VNODED_BIN} -v -c {self.ctrlchnlname} -l {self.ctrlchnlname}.log -p {self.ctrlchnlname}.pid"
cmd=VNODED_BIN, name=self.ctrlchnlname
)
if self.nodedir: if self.nodedir:
vnoded += f" -C {self.nodedir}" vnoded += f" -C {self.nodedir}"
env = self.session.get_environment(state=False) env = self.session.get_environment(state=False)
@ -593,9 +591,7 @@ class CoreNode(CoreNodeBase):
if self.server is None: if self.server is None:
return terminal return terminal
else: else:
return "ssh -X -f {host} xterm -e {terminal}".format( return f"ssh -X -f {self.server.host} xterm -e {terminal}"
host=self.server.host, terminal=terminal
)
def privatedir(self, path): def privatedir(self, path):
""" """

View file

@ -19,23 +19,18 @@ class DockerClient(object):
def create_container(self): def create_container(self):
self.run( self.run(
"docker run -td --init --net=none --hostname {name} --name {name} " f"docker run -td --init --net=none --hostname {self.name} --name {self.name} "
"--sysctl net.ipv6.conf.all.disable_ipv6=0 " f"--sysctl net.ipv6.conf.all.disable_ipv6=0 {self.image} /bin/bash"
"{image} /bin/bash".format( )
name=self.name,
image=self.image
))
self.pid = self.get_pid() self.pid = self.get_pid()
return self.pid return self.pid
def get_info(self): def get_info(self):
args = "docker inspect {name}".format(name=self.name) args = f"docker inspect {self.name}"
output = self.run(args) output = self.run(args)
data = json.loads(output) data = json.loads(output)
if not data: if not data:
raise CoreCommandError( raise CoreCommandError(-1, args, f"docker({self.name}) not present")
-1, args, "docker({name}) not present".format(name=self.name)
)
return data[0] return data[0]
def is_alive(self): def is_alive(self):
@ -46,43 +41,28 @@ class DockerClient(object):
return False return False
def stop_container(self): def stop_container(self):
self.run("docker rm -f {name}".format( self.run(f"docker rm -f {self.name}")
name=self.name
))
def check_cmd(self, cmd): def check_cmd(self, cmd):
logging.info("docker cmd output: %s", cmd) logging.info("docker cmd output: %s", cmd)
return utils.check_cmd("docker exec {name} {cmd}".format( return utils.check_cmd(f"docker exec {self.name} {cmd}")
name=self.name,
cmd=cmd
))
def create_ns_cmd(self, cmd): def create_ns_cmd(self, cmd):
return "nsenter -t {pid} -u -i -p -n {cmd}".format( return f"nsenter -t {self.pid} -u -i -p -n {cmd}"
pid=self.pid,
cmd=cmd
)
def ns_cmd(self, cmd, wait): def ns_cmd(self, cmd, wait):
args = "nsenter -t {pid} -u -i -p -n {cmd}".format( args = f"nsenter -t {self.pid} -u -i -p -n {cmd}"
pid=self.pid,
cmd=cmd
)
return utils.check_cmd(args, wait=wait) return utils.check_cmd(args, wait=wait)
def get_pid(self): def get_pid(self):
args = "docker inspect -f '{{{{.State.Pid}}}}' {name}".format(name=self.name) args = f"docker inspect -f '{{{{.State.Pid}}}}' {self.name}"
output = self.run(args) output = self.run(args)
self.pid = output self.pid = output
logging.debug("node(%s) pid: %s", self.name, self.pid) logging.debug("node(%s) pid: %s", self.name, self.pid)
return output return output
def copy_file(self, source, destination): def copy_file(self, source, destination):
args = "docker cp {source} {name}:{destination}".format( args = f"docker cp {source} {self.name}:{destination}"
source=source,
name=self.name,
destination=destination
)
return self.run(args) return self.run(args)
@ -185,7 +165,7 @@ class DockerNode(CoreNode):
:param str sh: shell to execute command in :param str sh: shell to execute command in
:return: str :return: str
""" """
return "docker exec -it {name} bash".format(name=self.name) return f"docker exec -it {self.name} bash"
def privatedir(self, path): def privatedir(self, path):
""" """
@ -195,7 +175,7 @@ class DockerNode(CoreNode):
:return: nothing :return: nothing
""" """
logging.debug("creating node dir: %s", path) logging.debug("creating node dir: %s", path)
args = "mkdir -p {path}".format(path=path) args = f"mkdir -p {path}"
self.node_net_cmd(args) self.node_net_cmd(args)
def mount(self, source, target): def mount(self, source, target):

View file

@ -18,19 +18,17 @@ class LxdClient(object):
self.pid = None self.pid = None
def create_container(self): def create_container(self):
self.run("lxc launch {image} {name}".format(name=self.name, image=self.image)) self.run(f"lxc launch {self.image} {self.name}")
data = self.get_info() data = self.get_info()
self.pid = data["state"]["pid"] self.pid = data["state"]["pid"]
return self.pid return self.pid
def get_info(self): def get_info(self):
args = "lxc list {name} --format json".format(name=self.name) args = f"lxc list {self.name} --format json"
output = self.run(args) output = self.run(args)
data = json.loads(output) data = json.loads(output)
if not data: if not data:
raise CoreCommandError( raise CoreCommandError(-1, args, f"LXC({self.name}) not present")
-1, args, "LXC({name}) not present".format(name=self.name)
)
return data[0] return data[0]
def is_alive(self): def is_alive(self):
@ -41,13 +39,13 @@ class LxdClient(object):
return False return False
def stop_container(self): def stop_container(self):
self.run("lxc delete --force {name}".format(name=self.name)) self.run(f"lxc delete --force {self.name}")
def create_cmd(self, cmd): def create_cmd(self, cmd):
return "lxc exec -nT {name} -- {cmd}".format(name=self.name, cmd=cmd) return f"lxc exec -nT {self.name} -- {cmd}"
def create_ns_cmd(self, cmd): def create_ns_cmd(self, cmd):
return "nsenter -t {pid} -m -u -i -p -n {cmd}".format(pid=self.pid, cmd=cmd) return f"nsenter -t {self.pid} -m -u -i -p -n {cmd}"
def check_cmd(self, cmd, wait=True): def check_cmd(self, cmd, wait=True):
args = self.create_cmd(cmd) args = self.create_cmd(cmd)
@ -57,9 +55,7 @@ class LxdClient(object):
if destination[0] != "/": if destination[0] != "/":
destination = os.path.join("/root/", destination) destination = os.path.join("/root/", destination)
args = "lxc file push {source} {name}/{destination}".format( args = f"lxc file push {source} {self.name}/{destination}"
source=source, name=self.name, destination=destination
)
self.run(args) self.run(args)
@ -142,7 +138,7 @@ class LxcNode(CoreNode):
:param str sh: shell to execute command in :param str sh: shell to execute command in
:return: str :return: str
""" """
return "lxc exec {name} -- {sh}".format(name=self.name, sh=sh) return f"lxc exec {self.name} -- {sh}"
def privatedir(self, path): def privatedir(self, path):
""" """
@ -152,7 +148,7 @@ class LxcNode(CoreNode):
:return: nothing :return: nothing
""" """
logging.info("creating node dir: %s", path) logging.info("creating node dir: %s", path)
args = "mkdir -p {path}".format(path=path) args = f"mkdir -p {path}"
return self.node_net_cmd(args) return self.node_net_cmd(args)
def mount(self, source, target): def mount(self, source, target):