Help me debug this script with argparse and if statements

S

Santosh Kumar

I have this script (setup.py):

import os
import sys
import shutil
from argparse import ArgumentParser

if os.getuid() != 0:
sys.exit("can't proceed, sudo privileges needed")


installManto = '/usr/share/man/man1/'
installBinTo = '/usr/local/bin/'
capsLoc = '/usr/local/bin/myapp'
mansLoc = '/usr/share/man/man1/mymanpage.1.gz'


def install():
shutil.copy2('doc/mymanpage.1.gz', installManto)
shutil.copy2('myapp', installBinTo)


def uninstall():
os.remove(capsLoc)
os.remove(mansLoc)


def update():
uninstall()
install()


parser = ArgumentParser(
prog='setup.py',
description='installer for myapp',
)

parser.add_argument(
'install',
nargs='?',
help='install myapp'
)

parser.add_argument(
'uninstall',
nargs='?',
help='uninstall myapp'
)

args = parser.parse_args()

if args.install:
if os.path.isfile(capsLoc) or os.path.isfile(mansLoc):
print("The files exists, getting ready to update.")
update()
else:
print("Running the initial setup...")
install()
elif args.uninstall:
print("Uninstalling..")
uninstall()
else:
parser.print_help()


Here is what happens:

1. I can copy the *myapp* and *mymanpage.1.gz* to their appropriate
locations with *sudo python setup.py install*. This is what I expected,
their is no problem upto here.

2. Running* python setup.py uninstall* *copies* the files instead of
removing them.

3. Running * python setup.py uninstall *when installed *updates* the files
instead of removing them.

So what's the problem? What is the fix? Any further tips for me?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,520
Members
44,996
Latest member
rainocode

Latest Threads

Top