Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Migrated from argparse to argh (a high-level wrapper)

  • Loading branch information...
commit a201de1d70536e7e4637a6079f03174b7b493ffa 1 parent 4c44bf8
@rubik authored
View
126 pyg/__init__.py
@@ -1,5 +1,6 @@
import os
import sys
+import urllib2
import _opts as opts
from .types import PygError, InstallationError, AlreadyInstalled
@@ -9,78 +10,83 @@
def _set_up():
- import argparse
+ from argh import ArghParser, arg, command
- parser = argparse.ArgumentParser(prog='pyg')
+ parser = ArghParser(prog='pyg')
parser.add_argument('-v', '--version', action='version', version=__version__)
- sub = parser.add_subparsers()
-
- sub_inst = sub.add_parser('install')
- sub_inst.add_argument('packname')
- sub_inst.add_argument('-r', '--req-file', action='store_true', help='Install packages from the specified requirement file')
- sub_inst.add_argument('-f', '--file', action='store_true', help='Do not download the package but use the local file')
- sub_inst.add_argument('-u', '--upgrade', action='store_true', help='If the package is already installed')
- sub_inst.add_argument('-n', '--no-deps', action='store_true', help='Do not install dependencies')
- sub_inst.add_argument('-i', '--index-url', default='http://pypi.python.org/pypi', metavar='<url>', help='Base URL of Python Package Index (default to %(default)s)')
- sub_inst.add_argument('-b', '--install-base', metavar='<path>', help='Base installation directory')
- sub_inst.add_argument('--user', action='store_true', help='Install to user site')
- sub_inst.set_defaults(func=opts.install_func)
-
- sub_un = sub.add_parser('uninstall')
- sub_un.add_argument('packname', nargs='+')
- sub_un.add_argument('-r', '--req-file', metavar='<path>', help='Uninstall all the packages listed in the given requirement file')
- sub_un.add_argument('-y', '--yes', action='store_true', help='Do not ask confirmation of uninstall deletions')
- sub_un.set_defaults(func=opts.uninst_func)
-
- sub_rm = sub.add_parser('rm')
- sub_rm.add_argument('packname', nargs='+')
- sub_rm.add_argument('-r', '--req-file', metavar='<path>', help='Uninstall all the packages listed in the given requirement file')
- sub_rm.add_argument('-y', '--yes', action='store_true', help='Do not ask confirmation of uninstall deletions')
- sub_rm.set_defaults(func=opts.uninst_func)
+
+
+ @ arg('packname')
+ @ arg('-r', '--req-file', action='store_true', help='Install packages from the specified requirement file')
+ @ arg('-f', '--file', action='store_true', help='Do not download the package but use the local file')
+ @ arg('-u', '--upgrade', action='store_true', help='If the package is already installed')
+ @ arg('-n', '--no-deps', action='store_true', help='Do not install dependencies')
+ @ arg('-i', '--index-url', default='http://pypi.python.org/pypi', metavar='<url>', help='Base URL of Python Package Index (default to %(default)s)')
+ @ arg('-b', '--install-base', metavar='<path>', help='Base installation directory')
+ @ arg('--user', action='store_true', help='Install to user site')
+ def install(args):
+ opts.install_func(args)
+
+ @ arg('packname', nargs='+')
+ @ arg('-r', '--req-file', metavar='<path>', help='Uninstall all the packages listed in the given requirement file')
+ @ arg('-y', '--yes', action='store_true', help='Do not ask confirmation of uninstall deletions')
+ def uninstall(args):
+ opts.uninst_func(args)
+
+ @ arg('packname', nargs='+')
+ @ arg('-r', '--req-file', metavar='<path>', help='Uninstall all the packages listed in the given requirement file')
+ @ arg('-y', '--yes', action='store_true', help='Do not ask confirmation of uninstall deletions')
+ def rm(args):
+ uninstall(args)
- sub_list = sub.add_parser('list')
- sub_list.add_argument('packname')
- sub_list.set_defaults(func=opts.list_func)
-
- sub_fr = sub.add_parser('freeze')
- sub_fr.add_argument('-c', '--count', action='store_true', help='Only returns requirements count')
- sub_fr.add_argument('-f', '--file', metavar='<path>', help='Writes requirements into the specified file')
- sub_fr.set_defaults(func=opts.freeze_func)
-
- sub_ln = sub.add_parser('link')
- sub_ln.add_argument('path')
- sub_ln.set_defaults(func=opts.link_func)
-
- sub_uln = sub.add_parser('unlink')
- sub_uln.add_argument('path', nargs='?')
- sub_uln.add_argument('-a', '--all', action='store_true', help='Remove all links')
- sub_uln.set_defaults(func=opts.unlink_func)
-
- sub_search = sub.add_parser('search')
- sub_search.add_argument('packname')
- sub_search.set_defaults(func=opts.search_func)
-
- sub_ck = sub.add_parser('check')
- sub_ck.add_argument('packname')
- sub_ck.set_defaults(func=opts.check_func)
-
- sub_down = sub.add_parser('download')
- sub_down.add_argument('packname')
- sub_down.add_argument('-u', '--unpack', action='store_true', help='Once downloaded, unpack the package')
- sub_down.add_argument('-d', '--download-dir', default='.', metavar='<path>', help='The destination directory')
- sub_down.add_argument('-p', '--prefer', metavar='<ext>', help='The preferred file type for the download')
- sub_down.set_defaults(func=opts.download_func)
+ @ command
+ def list(packname):
+ opts.list_func(packname)
+
+ @ arg('-c', '--count', action='store_true', help='Only returns requirements count')
+ @ arg('-f', '--file', metavar='<path>', help='Writes requirements into the specified file')
+ def freeze(args):
+ opts.freeze_func(args)
+
+ @ command
+ def link(path):
+ opts.link_func(path)
+
+ @ arg('path', nargs='?')
+ @ arg('-a', '--all', action='store_true', help='Remove all links')
+ def unlink(args):
+ opts.unlink_func(args)
+
+ @ command
+ def search(packname):
+ opts.search_func(packname)
+
+ @ command
+ def check(packname):
+ opts.check_func(packname)
+
+ @ arg('packname')
+ @ arg('-u', '--unpack', action='store_true', help='Once downloaded, unpack the package')
+ @ arg('-d', '--download-dir', default='.', metavar='<path>', help='The destination directory')
+ @ arg('-p', '--prefer', metavar='<ext>', help='The preferred file type for the download')
+ def download(args):
+ opts.download_func(args)
+
+ parser.add_commands([install, uninstall, rm, list, freeze, link, unlink, list, search, check, download])
return parser
def main():
try:
parser = _set_up()
- args = parser.parse_args()
- args.func(args)
+ args = parser.dispatch()
except (PygError, InstallationError):
sys.exit(1)
except AlreadyInstalled:
sys.exit(0)
+ except urllib2.HTTPError as e:
+ sys.exit(e.msg)
+ except urllib2.URLError as e:
+ sys.exit('urllib error: {0}'.format(e.reason))
sys.exit(0)
if __name__ == '__main__':
View
17 pyg/_opts.py
@@ -97,16 +97,16 @@ def uninst_func(args):
except PygError:
continue
-def link_func(args):
- return link(args.path)
+def link_func(path):
+ return link(path)
def unlink_func(args):
if args.all:
- return os.remove(pyg_links())
+ return os.remove(PYG_LINKS)
return unlink(args.path)
-def check_func(args):
- return sys.stdout.write(str(is_installed(args.packname)) + '\n')
+def check_func(name):
+ return sys.stdout.write(str(is_installed(name)) + '\n')
def freeze_func(args):
f = freeze()
@@ -119,9 +119,8 @@ def freeze_func(args):
req_file.write(f)
return sys.stdout.write(f)
-def list_func(args):
+def list_func(name):
res = []
- name = args.packname
versions = PyPI().package_releases(name, True)
if not versions:
versions = map(str, sorted(WebManager.versions_from_html(name), reverse=True))
@@ -132,8 +131,8 @@ def list_func(args):
res.append(v)
return sys.stdout.write('\n'.join(res) + '\n')
-def search_func(args):
- res = PyPI().search({'name': args.packname})
+def search_func(name):
+ res = PyPI().search({'name': name})
return sys.stdout.write('\n'.join('{name} {version} - {summary}'.format(**i) for i in \
sorted(res, key=lambda i: i['_pypi_ordering'], reverse=True)) + '\n')
View
1  pyg/req.py
@@ -24,6 +24,7 @@ class Requirement(object):
'>': operator.gt,
'<=': operator.le,
'<': operator.lt,
+ '!=': lambda a,b: a != b,
None: lambda a,b: True
}
View
18 pyg/types.py
@@ -3,13 +3,19 @@
import sys
import tarfile
import pkg_resources
+from StringIO import StringIO
+from pkgtools.pkg import Egg as EggTools
from .scripts import script_args
from .locations import EASY_INSTALL, INSTALL_DIR, BIN
from .utils import TempDir, ZipFile, call_setup, name_from_egg, glob, ext
from .log import logger
+class i(StringIO):
+ def fileno(self):
+ pass
+
## A generic error thrown by Pyg
class PygError(Exception):
pass
@@ -156,12 +162,6 @@ def install(self):
## When using this file for the first time
except IndexError:
pass
- try:
- with open(os.path.join(eggpath, 'EGG-INFO', 'requires.txt')) as f:
- for line in f:
- self.reqset.add(line.strip())
- except IOError:
- pass
dist = pkg_resources.get_distribution(self.packname)
for name, content, mode in script_args(dist):
logger.info('Installing {0} script to {1}', name, BIN)
@@ -169,6 +169,12 @@ def install(self):
with open(target, 'w' + mode) as f:
f.write(content)
os.chmod(target, 0755)
+ with TempDir() as tempdir:
+ try:
+ for req in EggTools(self.fobj, tempdir).file('requires.txt'):
+ self.reqset.add(req)
+ except KeyError:
+ logger.debug('requires.txt not found')
class Archive(object):
View
2  pyg/utils.py
@@ -41,6 +41,8 @@ def link(path):
if not os.path.exists(path):
logger.error('{0} does not exist', path)
if not os.path.exists(PYG_LINKS):
+ if not os.path.exists(os.path.dirname(PYG_LINKS)):
+ os.makedirs(os.path.dirname(PYG_LINKS))
open(PYG_LINKS, 'w').close()
path = os.path.abspath(path)
logger.info('Linking {0} in {1}...', path, PYG_LINKS)
View
10 pyg/web.py
@@ -1,30 +1,26 @@
import re
import os
import urllib2
-import xmlrpclib
+from pkgtools.pypi import PyPI
from .types import PygError, Version, args_manager
from .utils import FileMapper, ext, right_egg, version_egg
from .log import logger
-__all__ = ['WebManager', 'PackageManager', 'PyPI', 'PREFERENCES']
+__all__ = ['WebManager', 'PackageManager', 'PREFERENCES']
## This constants holds files priority
PREFERENCES = ('.egg', '.tar.gz', '.tar.bz2', '.zip')
-def PyPI(index_url='http://pypi.python.org/pypi'):
- return xmlrpclib.ServerProxy(index_url, xmlrpclib.Transport())
-
-
class WebManager(object):
_versions_re = r'{0}-(\d+\.?(?:\d\.?|\d\w)*)-?.*'
def __init__(self, req):
- self.pypi = PyPI(args_manager['index_url'])
+ self.pypi = PyPI(index_url=args_manager['index_url'])
self.req = req
self.name = self.req.name
self.versions = None
View
2  setup.py
@@ -3,7 +3,7 @@
from setuptools import setup, find_packages
-requires = ['setuptools']
+requires = ['setuptools', 'pkgtools']
try:
import argparse
except ImportError:
Please sign in to comment.
Something went wrong with that request. Please try again.