Setup wizard like script in Python
Question: I'm writing a simple _setup wizard_ like script in Python. Basically it
prompts the user to enter some values and answer some yes/no questions. Based
on the user input the script will then make directories, create and initialize
config files, create symlinks, set permissions and so on.

As the user makes choices different paths are taken and the structure of
directories and existence of symlinks may differ. Many problems may occur at
each step that might need the user to change their input or rollback the whole
thing.

1 -Is this the best approach to write this script? Is this text menu setup
wizard a good idea at all?

2- Is there a module that can help make this simpler so that I don't reinvent
the wheel?

3- Should I actually perform each step as user makes a choice or wait until
the end and do everything at once?

4- What is the best way to remember the already created structure so that I
can write a rollback function?

I don't want any code as an answer; any suggestions, opinions or external
links are appreciated.


Answer: I don't do GUI stuff. You can write one, but let's say you do this entirely on
command line.

  1. I would suggest take in all user inputs before making physical side-effects. In other words, don't start creating directories until the user has finished all the options. Python documentation tool Sphinx is a good example. It asks users many questions when a user launches `quickstart`. Sphinx doesn't generate the physical directory and configuration file until the end. This eliminates the need to "remember" is tiring. Too many branches. Don't do that. Do the whole setup at the very end.

  2. Depends. If you want to make a simple command line interface, Python has argpase to make command line options. The above is made possible using [docopt](http://docopt.org/) library which is built on top of argparse. But this is useful if you want to have command-lines. If your script only need to invoke "python script.py" and then start asking user questions, I don't know any useful library that handles setup stuff. 

Actually I was in the middle of developing one, called `dcoprompt` but it
isn't finished. <https://bitbucket.org/yeukhon/docprompt> basically it was
supposed to allow you write down your setup prompts and then remember them.
The code base is terrible, not very efficient. You can try but I won't finish
the feature until summer due to heavy homework load this semester.

So the answer is no. you have to write the code yourself. Just a lot of raw
input and a lot of variables.

  1. Again, wait until the end to make side-effect.

  2. Again, wait until the end to make side-effect.

* * *

**edit**

Say you wait until the end to create directories and symlinks and at one of
the step IOError occurs, you want to undo the whole setup. If all you are
creating are directories, files and symlinks, add them to a dictionary of
lists. See my edit.

    
    
    def physical_setup(...):
        memory = {
            'dirs': [],
            'symlinks': [],
            'files': []
        }
        try:
            # start doing physical setup
            memory['dirs'].append('/tmp/dir1')        
            os.path.mkdir('/tmp/dir1')
    
        # catching all exceptions is considered a bad practice but sometimes be a little badass
        except Exception as e:
            for key, valist in memory.iteritems():
                if key == 'dirs':
                    for dir in valist:
                        shutil.rmtree(dir)
    

**important** : the code above has one issue, you should unlink, delete files
and dirs before delting the folders. Because if the files are part of the
already-deleted directory you will have to catch the exception silently. A lot
of code. Just unlink, delete file and dir.

