1

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

1
  • "How can i store PATH's in a LIST"? Are you asking about list and list.append? If so, please read a tutorial because list and append are usually covered. Exit from a loop? Are you asking about break? If so, please read a tutorial, because break is usually covered. Commented Feb 1, 2012 at 20:14

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

in my case THIS is not possible, since this menu is run as a by product , so a human cannot run this by hand and supply arguments.
I'm not sure I understand you situation, but perhaps you could write a wrapper script around the top-level command the human is running, which accepts argparse style arguments. Then you could pass that information down to the script you are running "as a by product".
Sorry, i should explain better... This script is run as a pre-install script with "apt-get install <package name>" and is packaged with the debian package and hosted locally, so the only way to interact with the human is to prompt them , and on thta prompt, maybe i can have them write ALL the "PATHs" in one line and then store them in a list, right?
In that case, you could pass the response string from raw_input into parser.parse_args. I've added code above to show what I mean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.