blob: dd465572ca907ddec68ab872ad0ca1b73c404fcb [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2015-2019, The University of Memphis,
4# Arizona Board of Regents,
5# Regents of the University of California.
6#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
23
24import sys
25from os.path import isfile
26from subprocess import call
27from mininet.cli import CLI
28
29sshbase = ['ssh', '-q', '-t', '-i/home/mininet/.ssh/id_rsa']
30scpbase = ['scp', '-i', '/home/mininet/.ssh/id_rsa']
31devnull = open('/dev/null', 'w')
32
33def ssh(login, cmd):
34 rcmd = sshbase + [login, cmd]
35 call(rcmd, stdout=devnull, stderr=devnull)
36
37def scp(*args):
38 tmp = []
39 for arg in args:
40 tmp.append(arg)
41 rcmd = scpbase + tmp
42 call(rcmd, stdout=devnull, stderr=devnull)
43
44def copyExistentFile(node, fileList, destination):
45 for f in fileList:
46 if isfile(f):
47 node.cmd('cp {} {}'.format(f, destination))
48 break
49 if not isfile(destination):
50 fileName = destination.split('/')[-1]
51 raise IOError('{} not found in expected directory.'.format(fileName))
52
53def popenGetEnv(node, envDict=None):
54 env = {}
55 homeDir = node.params['params']['homeDir']
56 printenv = node.popen('printenv'.split(), cwd=homeDir).communicate()[0].decode('utf-8')
57 for var in printenv.split('\n'):
58 if var == '':
59 break
60 p = var.split('=')
61 env[p[0]] = p[1]
62 env['HOME'] = homeDir
63
64 if envDict is not None:
65 for key, value in envDict.items():
66 env[key] = str(value)
67
68 return env
69
70def getPopen(host, cmd, envDict=None, **params):
71 return host.popen(cmd, cwd=host.params['params']['homeDir'],
72 env=popenGetEnv(host, envDict), **params)
73
74class MiniNDNCLI(CLI):
75 prompt = 'mini-ndn> '
76 def __init__(self, mininet, stdin=sys.stdin, script=None):
77 CLI.__init__(self, mininet, stdin, script)