annotate python/pypi.py @ 701:de7bf9523e21

-
author Jeff Hammel <k0scist@gmail.com>
date Wed, 13 Aug 2014 13:16:56 -0700
parents 11cf7018ebff
children 016d27ca39e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
369
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 pypi strawman that should really be in PaInt
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 ref http://wiki.python.org/moin/PyPIJSON
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 """
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import json
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import optparse
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import os
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import sys
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 import textwrap
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 import urllib2
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 def package_data(package, index='http://pypi.python.org/pypi'):
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 """
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 returns dict of {url:, description:, home_page: }
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 """
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 # if package name given, -> url
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 url = package
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 if '://' not in url:
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 url = '%s/%s' % (index, package)
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 url = url.rstrip('/')
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 if not url.endswith('/json'):
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 url = '%s/json' % (url)
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 # read the URL
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 try:
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 page = urllib2.urlopen(url).read()
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 except:
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 raise # TODO: better error handling
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 data = json.loads(page)
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 info = data['info']
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 # massage the data
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 return {'summary': info.get('summary', ''),
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 'package_url': info['package_url'],
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 'name': info['name'],
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 'home_page': info.get('home_page')
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 # etc
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 }
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 def main(args=sys.argv[1:]):
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 # command line parser
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 usage = '%prog [options] package <another_package> <...>'
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 parser = optparse.OptionParser(usage=usage, description=__doc__)
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 parser.add_option('-t', '--tag', dest='tags',
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 action='append', help="tags for packages")
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 options, args = parser.parse_args(args)
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 if not args:
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 parser.print_usage()
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 parser.exit()
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 # one crappy ass template
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 template = """ * %(package_url)s
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 %(summary)s
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 %(tags)s
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 """
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 # get package data
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 packages = {}
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 for package in args:
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 data = package_data(package)
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 packages[data['name']] = data
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72 # render the damn thing
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 rendered = []
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 for name in sorted(packages.keys(), key=lambda x: x.lower()):
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 values = packages[name].copy()
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 if options.tags:
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 values['tags'] = '{%s}' % ', '.join(options.tags)
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78 else:
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 values['tags'] = ''
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80 if values['home_page'] and values['home_page'] != values['package_url']:
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81 values['summary'] = '%s %s' % (values['summary'],
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 values['home_page'])
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 values['summary'] = textwrap.fill(values['summary'], 69,
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 initial_indent=' ',
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 subsequent_indent=' ')
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 string = template % values
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 rendered.append(string.strip())
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 print '\n\n'.join([i.strip() for i in rendered])
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 if __name__ == '__main__':
11cf7018ebff new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 main()