home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / launchpadbugs / utils.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.8 KB  |  219 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import subprocess
  7. import apt_pkg
  8. import libxml2
  9. import urlparse
  10. from exceptions import LaunchpadURLError, PythonLaunchpadBugsValueError
  11. from lpconstants import BASEURL
  12.  
  13. try:
  14.     from bzrlib.branch import Branch
  15.     from bzrlib import trace
  16.     HAVE_BZR = True
  17. except:
  18.     HAVE_BZR = False
  19.  
  20.  
  21. def get_version_from_changelog(path):
  22.     changelog = os.path.join(path, 'debian/changelog')
  23.     if os.path.exists(changelog):
  24.         head = open(changelog).readline()
  25.         match = re.compile('.*\\((.*)\\).*').match(head)
  26.         if match:
  27.             return match.group(1)
  28.     
  29.     return 'unknown'
  30.  
  31.  
  32. def noerr(ctx, str):
  33.     pass
  34.  
  35. libxml2.registerErrorHandler(noerr, None)
  36.  
  37. def find_version_number(show_nick = False):
  38.     path = os.path.join(os.path.dirname(__file__), '..')
  39.     if path.startswith('/usr'):
  40.         output = subprocess.Popen([
  41.             'dpkg-query',
  42.             '-W',
  43.             'python-launchpad-bugs'], stdout = subprocess.PIPE).communicate()[0]
  44.         
  45.         try:
  46.             return output.split()[1]
  47.         return 'unknown'
  48.  
  49.     
  50.     if HAVE_BZR:
  51.         
  52.         try:
  53.             trace.be_quiet()
  54.             trace.enable_default_logging()
  55.             version = get_version_from_changelog(path)
  56.             if os.path.islink(os.path.dirname(__file__)):
  57.                 path = os.path.realpath(os.path.dirname(__file__))
  58.             
  59.             branch = Branch.open_containing(path)[0]
  60.             bzr_revno = branch.revno()
  61.             if show_nick:
  62.                 nick = branch.nick
  63.                 return '%sr%s, branch nick: %s' % (version, bzr_revno, nick)
  64.             return '%sr%s' % (version, bzr_revno)
  65.  
  66.     
  67.     return 'unknown'
  68.  
  69.  
  70. def package_exists(package_name):
  71.     
  72.     try:
  73.         apt_pkg.init()
  74.         sources = apt_pkg.GetPkgSrcRecords()
  75.         sources.Restart()
  76.         if not sources.Lookup(str(package_name)):
  77.             return False
  78.         return True
  79.     except:
  80.         print "You must put some 'source' URIs in your sources.list"
  81.         return False
  82.  
  83.  
  84.  
  85. def lazy_makedir(directory):
  86.     
  87.     try:
  88.         os.makedirs(directory)
  89.     except:
  90.         pass
  91.  
  92.  
  93.  
  94. def remove_obsolete_attachments(path, open_bugs):
  95.     open_bug_nrs = set(map((lambda a: str(a.nr)), open_bugs))
  96.     if os.path.exists(path):
  97.         bugs = set((filter,)((lambda a: os.path.isdir(os.path.join(path, a))), os.listdir(path)))
  98.         for bug in bugs.difference(bugs.intersection(open_bug_nrs)):
  99.             bug_path = os.path.join(path, bug)
  100.             if os.path.isdir(bug_path):
  101.                 os.system('rm -r %s' % bug_path)
  102.                 continue
  103.         
  104.     
  105.  
  106.  
  107. def bugnumber_to_url(nr):
  108.     return '%s/bugs/%s' % (BASEURL.BUG, nr)
  109.  
  110.  
  111. def valid_lp_url(url, type):
  112.     """ validates given 'url' against 'type'
  113.     raises PythonLaunchpadBugsValueError for invalid url and modifies
  114.     if necessary. Currently only validation for 'scheme' and 'netloc'
  115.     part of the url is implemented, path needs to be done but is more
  116.     complicated.    
  117.     """
  118.     if not url:
  119.         raise PythonLaunchpadBugsValueError(msg = 'url is empty')
  120.     url
  121.     (scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
  122.     (_, type, x, _, _) = urlparse.urlsplit(type)
  123.     if not type:
  124.         type = x
  125.     
  126.     if not type:
  127.         raise AssertionError, 'unable to validate against given type'
  128.     if not scheme and not netloc:
  129.         if not path.startswith('/'):
  130.             scheme = 'http'
  131.             x = path.split('/', 1)
  132.             if len(x) == 1:
  133.                 netloc = x[0]
  134.                 path = ''
  135.             else:
  136.                 netloc = x[0]
  137.                 path = x[1]
  138.         
  139.     
  140.     if not scheme or scheme == 'http':
  141.         scheme = 'https'
  142.     
  143.     if scheme not in ('http', 'https'):
  144.         raise PythonLaunchpadBugsValueError(values = {
  145.             'url': url,
  146.             'type': type }, msg = 'Wrong schema')
  147.     scheme not in ('http', 'https')
  148.     if not netloc:
  149.         netloc = type
  150.     else:
  151.         n = netloc
  152.         prefix = None
  153.         if 'edge.' in netloc:
  154.             n = netloc.replace('edge.', '')
  155.             prefix = 'edge'
  156.         elif 'staging.' in netloc:
  157.             n = netloc.replace('staging.', '')
  158.             prefix = 'staging'
  159.         
  160.         if n != type:
  161.             if n == 'launchpad.net':
  162.                 netloc = type
  163.                 if prefix:
  164.                     netloc = netloc.replace('.launchpad', '.%s.launchpad' % prefix)
  165.                 
  166.             else:
  167.                 raise PythonLaunchpadBugsValueError(values = {
  168.                     'url': url,
  169.                     'type': type }, msg = 'Wrong type')
  170.         n == 'launchpad.net'
  171.     return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
  172.  
  173.  
  174. def debug(*arg, **args):
  175.     for i in arg:
  176.         if type(i) == type(dict()):
  177.             for k in i:
  178.                 print '%s : %s' % (k, i[k])
  179.             
  180.         print i
  181.     
  182.     for k in args:
  183.         print k, args[k]
  184.     
  185.  
  186.  
  187. def get_open_milestones(dist = None, package = None, project = None):
  188.     url = BASEURL.BUG
  189.     if dist:
  190.         url += '/%s' % dist
  191.         if package:
  192.             url += '/+source/%s' % package
  193.         
  194.     elif project:
  195.         url += '/%s' % project
  196.     else:
  197.         raise TypeError, 'Wrong number of arguments'
  198.     dist += '/+bugs?advanced=1'
  199.     
  200.     try:
  201.         HTTPConnection = HTTPConnection
  202.         import http_connection
  203.         text = HTTPConnection().get(url).text
  204.     except LaunchpadURLError:
  205.         raise PythonLaunchpadBugsValueError({
  206.             'get_open_milestones': "Can't find milestones for (dist=%s, package=%s, project=%s)" % (dist, package, project) }, url)
  207.  
  208.     ctx = libxml2.htmlParseDoc(text, 'UTF-8')
  209.     milestones = ctx.xpathEval('//input[@name="field.milestone:list"]')
  210.     for m in milestones:
  211.         identifier = m.prop('id').split('.', 1).pop()
  212.         yield (identifier, int(m.prop('value')))
  213.         x = identifier.split(' ')[1:]
  214.         if x:
  215.             yield (' '.join(x), int(m.prop('value')))
  216.             continue
  217.     
  218.  
  219.