Personal tools
You are here: Home Current Classes Operating Systems Assignments Assignment 2 : A Python Shell Assignment 2 : Do Pythons Speak With Forked Tongues?

Assignment 2 : Do Pythons Speak With Forked Tongues?

Do Pythons Speak With Forked Tongues?

 Due :

7 November
Points : 
3
Teams :
You must do this one on your own. 

Description

 

This file contains a minimal (very minimal) implementation of a command line interpreter (CLI) in Python.  This program (run it with "python interp.py") reads lines from  input files specified on the command line, or from the terminal if no such files are specified and writes results to the terminal (but see below).   The line is broken up into a list at the spaces and the interpreter tries to use the first word on the line as the name of a command.   This command name is looked up in a table and the entry in the table should be a function to be run on the list of arguments.   A very simple way to substitute variables is provided.    Any line beginning with a # is ignored.

The "say" command provides for a single flag "+format".   Flags will always begin with "+". 

This interpreter provides a tiny (and not overly useful) set of commands :

say
Prints all of the arguments after the first to the output.   If the first argument to say is "+format", the second argument should be a format string.   In this format string "_" is replaced by space and "\\n" by newlines.   Otherwise it works like a standard Python format string with any further arguments provided to the format string in the usual Python way.    Thus "say +format foo_%s_bar hello" would print "foo hello bar".
do
Reads input from the file named in the first argument instead of the terminal.   This is done recursively and when either the end of file or a quit command is encountered it returns. 
set-output 
Sends the output from the interpreter to the file named as its first argument.   If no such argument is provided, the output is sent to standard output.   If the first argument is "+append" the filename should be in the second argument and the file is opened for appending.  
quit
Exits the interpreter.
foo 
Prints the string "foo!"
verbose 
Turns on verbose mode in which information gets printed about what is happening. 
set
There is a simple variable substitution mechanism which occurs when the command line is parsed.   After being broken up into words, any word beginning with a dollar sign ($) is looked up in a dictionary and if it has a value there, that value is substituted for the word.    "set" defines this translation.    Try :
      set foo bar   
      say foo $foo
 
times 
times provides a simple looping mechanism.    The first argument should be a number and the second a variable name.   The rest of the arguments will be interpreted as a command and its arguments.   For example:
     times 7 i say i= %i
should print out "i= 0", "i= 1" and so on up to "i= 6".  

What to do

 You are to extend this interpreter by providing some simple file operations and some simple process based operations.  Use brief error messages as appropriate.

File Operations

Add each of the following commands to the interpreter.   Use the python file operations in the Python os module or in the shutil module as appropriate.

md 
 For each following argument make a directory with that name in the current directory.    So "md foo bar" will make directories "foo"and "bar" in the current directory.   "md foo/bar" should create both foo and bar if necessary.
cd
Set the current directory to the first argument in the argument list.   If there are no arguments, don't change the directory.
up
Set the current directory to the directory one up from the current directory.   (Essentially the same as "cd .." in unix.) 
rename 
Rename the file named in the first argument  to the name in the second argument.  
copy
Copy the file named in the first argument to the name given by the second argument. 
zap
Delete any files or directories named in the argument list completely.    (Note, you may prefer to do this as a process operation.)

Process Operations

There is one process operation "run" with several possible flags.   All "run" operations must be done using the os.process module using "fork", "exec", "wait", "dup", "dup2" as appropriate.

run
Run should run the program named as its first argument with the rest of the arguments (but see below) provided as the command line arguments.    Executables should always be named in full.    Thus, "run /bin/ls -l" should run "ls" with the "-l" flag.   "run ls -l" should not run anything as ls is not given.  "run" also may take several flags :
+input
the input for the command should come from the next filename in the arguments
+output
the output from the command should go to the next file named in the arguments.    Thus "run /bin/ls +input foo +output bar -l" should run "ls -l" but with input coming from "foo" and output going to "bar".   
+script
should run this interpreter with the command line arguments coming from the arguments after the "+script" flag.
+outscript
sends the output for the command to a new process running the interpreter.

As an example :   "run /bin/ls  +outscript  -l"  should run "/bin/ls -l" with the output being send to a new process running the interpreter (which isn't likely to result in anything interesting happening unless there are files named things like "say hello" or "foo"). 

 

What to Submit

 Your submission should include your python code with your additions (at least) properly commented and a set of test files that check to see if the basic operations work.      Each of these test files is likely to be a Python or shell script that sets up a test, runs the test, checks the output and cleans up afterwards.    An example of such a file may be found here.

 

Document Actions