kamal
kamal

Reputation: 9785

Python CLI Menu

I am trying to develop a menu which will:

  1. Check to see if /tmp has write permissions from the user who is running the script.
  2. Prompt the question : Are ALL your LOCAL repositories in the same location? e.g. /home/joe/hg/(y/n)
    If YES, then, prompt for the complete PATH's, of the repos:

    1. store the repo names (along with theor complete PATH) in a list
    2. copy some config files, from the directory /tmp/hg-jira-hook-config/ to ALL "home/joe/hg//.hg/"
    3. chmod 755 for all the config files for each repo.
  3. If the answer to the question in Step 2 is "n" or "NO" or "no" then:

    1. ask for individual PATHs of the repos
    2. copy the config files
    3. chmod 755

I have a partially working script:

#!/usr/bin/env python
import sys,os
import stat
import tempfile
import shutil

__title_prefix = 'Python %i.%i.%i %s %s' % (sys.version_info[0:4] +
                                           (sys.version_info[4] or "",))

if os.path.exists('/tmp'):
    pass
else:
    print '/tmp does not exist'
    sys.exit()

def isgroupreadable(filepath):
    st = os.stat(filepath)
    return bool(st.st_mode & stat.S_IRGRP)

src = '/tmp/hg-jira-hook-config/' # This directory will be created as a result of apt-get install <package>
                                  # and all config files will be copied 

if isgroupreadable('/tmp'):
    prompt_1 = ("Are ALL your LOCAL repositories in the same location? e.g. /home/joe/hg/<repo>[y/n], Press [q] when done\n")
    answer_1 = raw_input(prompt_1)
    if answer_1 == 'y' or None or 'YES' or 'yes':
        while True:
            answer_2 = raw_input("\n\nEnter the complete PATH of repos.Press [q] when done\n")
    elif answer_1 == 'n' or 'NO' or 'No' or 'no':
        result = raw_input("\n\nEnter the complete PATH of mercurial repositories you want this hook to be applied.Press [q] when done\n")
        print result

else:
    print 'cannot copy, wrong permissions on /tmp'


def main():

    if __name__ == '__main__':
        main()

test run:

python preinst 
Are ALL your LOCAL repositories in the same location? e.g. /home/joe/hg/<repo>[y/n], Press [q] when done
y


Enter the complete PATH of repos.Press [q] when done
/home/joe/hg/app1


Enter the complete PATH of repos.Press [q] when done
/home/joe/hg/app2


Enter the complete PATH of repos.Press [q] when done
q


Enter the complete PATH of repos.Press [q] when done

so the question is:

How can i store PATH's in a LIST (or any other data structure) and How do Exit the user from this loop

Upvotes: 1

Views: 5254

Answers (1)

unutbu
unutbu

Reputation: 879591

In all cases, the user must supply at least one path to the program. It is much much easier to supply that path or paths on the command line (using, say, argparse), than it is to make a nice interactive interface. Moreover, if you use an argument parser like argparse then your program will be automatically scriptable. In contrast, an interactive program is always as slow as its user. Therefore, I almost always prefer scriptable programs over interactive programs.

Here is how you could do this using argparse:

import argparse

if __name__ == '__main__':
    parser=argparse.ArgumentParser()
    parser.add_argument('locations', nargs = '+')
    args=parser.parse_args()
    print(args.locations)

Running

% test.py /path/to/repo
['/path/to/repo']

% test.py /home/joe/hg /home/joe/test
['/home/joe/hg', '/home/joe/test']

argparse can also be used in conjunction with an interactive program. For example,

import argparse
import shlex
response = raw_input('Enter repo PATHs (e.g. /path/to/repo1 /path/to/repo2)')
parser=argparse.ArgumentParser()
parser.add_argument('locations', nargs = '+')
args=parser.parse_args(shlex.split(response))
print(args.locations)

argparse is part of the standard library as of Python 2.7. For Python 2.3 or better, you can install argparse.

Upvotes: 3

Related Questions