Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'args': 0.04; 'elif': 0.04; 'sys': 0.05; 'parser': 0.07; 'problem?': 0.07; 'setup.py': 0.07; 'shutil': 0.07; 'python': 0.09; 'exists,': 0.09; 'subject:script': 0.09; 'url:github': 0.09; 'subject:Help': 0.10; 'def': 0.10; "skip:' 30": 0.15; 'to:name:python-list': 0.15; 'argparse': 0.16; 'expected,': 0.16; 'install.': 0.16; 'subject:argparse': 0.16; 'sudo': 0.16; 'uninstall': 0.16; 'appropriate': 0.20; 'skip:p 30': 0.20; 'import': 0.21; 'installed': 0.23; 'script': 0.24; 'message-id:@mail.gmail.com': 0.27; 'initial': 0.28; 'privileges': 0.29; "skip:' 10": 0.30; "skip:' 20": 0.32; 'running': 0.32; 'getting': 0.33; 'skip:s 30': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'me?': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'locations': 0.35; 'subject:with': 0.36; 'skip:p 20': 0.36; 'files': 0.38; 'skip:o 20': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'further': 0.61; 'skip:\xc2 10': 0.62; 'skip:n 10': 0.63; 'here': 0.65; '8bit%:100': 0.70; 'subject:this': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=f+UlLng+Bc6hp1oJ222f2I9vFHFI0NjuMnlgjnBTSzI=; b=R/3np17myleMYLi7wqiooSMg9jNIn2MyMzxKoKJR0mvcxHm7JLzAhCMMPQyy/a8SNb nVrNNeIjslXjltX080NDqYH2eF+68NzozMCD66e9dK0SF06sMqUgtGVNZcL8GbJAM8Gk e2vxmJ3y1s5l/rR0zBbLpHrPozdWiXW49nA/pRhvFwuy8Du2mKzqbpWlFwWKMhoKZ79m 6Tdx0TIUxl4QJCQxfbatsjNxxxdyFAccDz5lh5IR9M2aj8ILHD6tcCVgucJd5FNZQyib 7I2N+jHHuyKKC8+7d/E6UO/cGbraueBnpNtzm7Fp0BDFianjcJ4ey8htgry9jelpQn22 T2uA== MIME-Version: 1.0 X-Received: by 10.152.112.231 with SMTP id it7mr19788993lab.10.1361441142546; Thu, 21 Feb 2013 02:05:42 -0800 (PST) Date: Thu, 21 Feb 2013 15:35:42 +0530 Subject: Help me debug this script with argparse and if statements From: Santosh Kumar To: python-list Content-Type: multipart/alternative; boundary=f46d04071533af1fc204d639374b X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 144 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361441149 news.xs4all.nl 6964 [2001:888:2000:d::a6]:48730 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39408 --f46d04071533af1fc204d639374b Content-Type: text/plain; charset=UTF-8 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? -- Twitter | Github --f46d04071533af1fc204d639374b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I have this script (setup.py):

import os
import = sys
import shutil
from argparse import ArgumentParser

if os.ge= tuid() !=3D 0:
=C2=A0=C2=A0=C2=A0 sys.exit("can't proceed, sudo= privileges needed")


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


def in= stall():
=C2=A0=C2=A0=C2=A0 shutil.copy2('doc/mymanpage.1.gz', installManto)=
=C2=A0=C2=A0=C2=A0 shutil.copy2('myapp', installBinTo)

<= br>def uninstall():
=C2=A0=C2=A0=C2=A0 os.remove(capsLoc)
=C2=A0=C2= =A0=C2=A0 os.remove(mansLoc)


def update():
=C2=A0=C2=A0=C2=A0 uninstall()
=C2=A0=C2=A0=C2=A0 install()


parser =3D ArgumentParser(
=C2= =A0=C2=A0=C2=A0 prog=3D'setup.py',
=C2=A0=C2=A0=C2=A0 descriptio= n=3D'installer for myapp',
=C2=A0=C2=A0=C2=A0 )

parser.ad= d_argument(
=C2=A0=C2=A0=C2=A0 'install',
=C2=A0=C2=A0=C2=A0 = nargs=3D'?',
=C2=A0=C2=A0=C2=A0 help=3D'install myapp'
=C2=A0=C2=A0=C2=A0 )
parser.add_argument(
=C2=A0=C2=A0=C2=A0 'uninstall',
= =C2=A0=C2=A0=C2=A0 nargs=3D'?',
=C2=A0=C2=A0=C2=A0 help=3D'u= ninstall myapp'
=C2=A0=C2=A0=C2=A0 )

args =3D parser.parse_ar= gs()

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


Here is what happens:

1. I can copy the myapp and mymanpage.1.gz to their appropri= ate locations with sudo python setup.py install. This is what I expe= cted, their is no problem upto here.

2. Running python setup.py uninstall copies th= e files instead of removing them.

3. Running python setup.py uni= nstall when installed updates the files instead of removing them= .

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