def test_exit(self):
     pipeline = [Exit("exit", [])]
     stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
     with open(stdin[0],
               "r") as sin, open(stdout[1],
                                 "w") as sout, open(stderr[1], "w") as serr:
         sh = Shell(sin, sout, serr, {}, None)
         with self.assertRaises(SystemExit):
             sh._execute(pipeline)
 def test_echo(self):
     hello = "Hello, world!"
     pipeline = [Echo("echo", [hello])]
     stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
     with open(stdin[0],
               "r") as sin, open(stdout[1],
                                 "w") as sout, open(stderr[1], "w") as serr:
         sh = Shell(sin, sout, serr, {}, None)
         sh._execute(pipeline)
     with open(stdout[0], "r") as res:
         self.assertEqual(res.read(), hello + "\n")
 def test_eq(self):
     pipeline = [Eq("=", ["a", "b"])]
     stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
     with open(stdin[0],
               "r") as sin, open(stdout[1],
                                 "w") as sout, open(stderr[1], "w") as serr:
         env = {}
         sh = Shell(sin, sout, serr, env, None)
         sh._execute(pipeline)
         self.assertIn("a", env)
         self.assertEqual("b", env["a"])
    def test_pwd(self):
        pwd = os.getcwd()
        pipeline = [Pwd("pwd", [])]
        stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()

        with open(stdin[0],
                  "r") as sin, open(stdout[1],
                                    "w") as sout, open(stderr[1], "w") as serr:
            sh = Shell(sin, sout, serr, {"PWD": pwd}, None)
            sh._execute(pipeline)

        with open(stdout[0], "r") as res:
            self.assertEqual(pwd, res.read())
    def test_wc(self):
        testfile = "./tests/test.txt"
        content = "1 5 21"
        pipeline = [Wc("wc", [testfile])]
        stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()

        with open(stdin[0],
                  "r") as sin, open(stdout[1],
                                    "w") as sout, open(stderr[1], "w") as serr:
            sh = Shell(sin, sout, serr, {}, None)
            sh._execute(pipeline)

        with open(stdout[0], "r") as res:
            self.assertEqual(content, res.read())
    def test_system(self):
        hello = "Hello, world!\n"
        pipeline = [Command("./tests/test.sh", [])]
        stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
        with open(stdin[1], "w") as sin:
            sin.write(hello)

        with open(stdin[0],
                  "r") as sin, open(stdout[1],
                                    "w") as sout, open(stderr[1], "w") as serr:
            sh = Shell(sin, sout, serr, {}, None)
            sh._execute(pipeline)
        with open(stdout[0], "r") as res:
            self.assertEqual(res.read(), hello)
    def test_pipes(self):
        hello = "hello world"

        pipe1, pipe2 = os.pipe(), os.pipe()
        err1, err2 = os.pipe(), os.pipe()
        pipeline = [
            Echo("echo", hello.split(" "), outfd=pipe1[1], errfd=err1[1]),
            Cat("cat", [], infd=pipe1[0], outfd=pipe2[1], errfd=err2[1]),
            Cat("cat", [], infd=pipe2[0]),
        ]
        stdin, stdout, stderr = os.pipe(), os.pipe(), os.pipe()
        with open(stdin[0],
                  "r") as sin, open(stdout[1],
                                    "w") as sout, open(stderr[1], "w") as serr:
            sh = Shell(sin, sout, serr, {}, None)
            sh._execute(pipeline)
        with open(stdout[0], "r") as res:
            self.assertEqual(hello + "\n", res.read())
Esempio n. 8
0
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from cli.shell import Shell

if __name__ == "__main__":

    s = Shell()
    s.prompt = "\033[93m$_$ \033[00m"
    s.init()

    try:
        s.cmdloop(
            "\n----\n" +
            "Welcome to the amazingly awesome shell for WG10 SmartCards :D\n" +
            "Remember to initialize the reader using the command 'init'\n" +
            "All commands that start with 'll_' are low level. " +
            "The rest of them are the ones meant to be used to automatically "
            + "interact with the SmartCard.\n\n" +
            "=> Execute 'help' to list all the available commands.\n" +
            "=> Execute 'get_status' to get info about the session key, NT...\n\n"
            +
            "=> To operate with a smartcard, you have to execute first 'connect'"
Esempio n. 9
0
            'class': 'logging.Formatter',
            'format':
            '%(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
        }
    },
    'handlers': {
        'file_debug': {
            'class': 'logging.FileHandler',
            'filename': 'shell_debug.log',
            'mode': 'w',
            'formatter': 'detailed',
        },
        'file_info': {
            'class': 'logging.FileHandler',
            'filename': 'shell_info.log',
            'level': 'INFO',
            'mode': 'w',
            'formatter': 'simple',
        },
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['file_debug', 'file_info']
    },
}

if __name__ == '__main__':
    logging.config.dictConfig(log_config)
    shell = Shell()
    shell.main_loop()