Fixed g-pypi to replace dots in package name.
[g-pypi.git] / tests / rss_feed.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5
6 Functional CLI testing using the PyPI RSS feed to try to create an ebuild
7 for each package.
8
9 """
10
11 __docformat__ = 'restructuredtext'
12
13 import urllib
14 import os
15 import sys
16
17 #TODO:
18 """
19 Add switch for --pretend make default write ebuilds
20 Make option to write ebuild to tempdir and then cleanup after test is done
21
22
23
24
25 """
26 if sys.version_info[0] == 2 and sys.version_info[1] == 5:
27     #Python >=2.5 has elementtree 
28     from xml.etree.cElementTree import iterparse
29 else:
30     try:
31         #Python <2.5 has elementtree as 3rd party module
32         from cElementTree import iterparse
33     except ImportError:
34         print "You need to install cElementTree"
35         sys.exit(2)
36
37 PYPI_URL = 'http://www.python.org/pypi?:action=rss'
38
39 #Packages we don't want to test. Mainly ones that require svn auth
40 SKIP = ['byCycleCore']
41
42 def get_pkg_ver(pv, add_quotes=True):
43     """Return package name and version"""
44     n = len(pv.split())
45     if n == 2:
46         #Normal package_name 1.0
47         pkg_name, ver = pv.split()
48     else:
49         parts = pv.split()
50         ver = parts[-1:]
51         if add_quotes:
52             pkg_name = "'%s'" % " ".join(parts[:-1])
53         else:
54             pkg_name = "%s" % " ".join(parts[:-1])
55     return pkg_name, ver
56
57 def cli_test(pypi_xml):
58     """Test the command-line tool"""
59     for event, elem in iterparse(pypi_xml):
60         if elem.tag == "title":
61             if not elem.text.startswith('Cheese Shop recent updates'):
62                     pkg_name, ver = get_pkg_ver(elem.text)
63                     if pkg_name not in SKIP:
64                         #If I don't use os.system for the echo's, all the msgs
65                         #appear at the end of a log when redirecting output
66                         os.system('echo Testing %s' % elem.text)
67                         os.system('g-pypi -V %s' % pkg_name)
68                         os.system('echo %s' % ('-' * 79))
69             elem.clear()
70
71 if __name__ == "__main__":
72     cli_test(urllib.urlopen(PYPI_URL))