~ubuntu-branches/ubuntu/vivid/software-properties/vivid-proposed

« back to all changes in this revision

Viewing changes to softwareproperties/ppa.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt, Matthew Paul Thomas, Sami Jaktholm, Robert Roth, Martin Pitt
  • Date: 2013-06-24 12:52:34 UTC
  • Revision ID: package-import@ubuntu.com-20130624125234-52uk01xkj8h3efhw
Tags: 0.92.20
[ Matthew Paul Thomas ]
* Fix spelling of "occurred".

[ Sami Jaktholm ]
* softwareproperties/ppa.py
  - Use PycURL only with python2, not if urllib raises an unexpected
    exception in python3. (LP: #1111291)
  - Catch http.client.HTTPExceptions from urlopen and raise a PPAException
    to show a formatted error message instead of traceback.

[ Robert Roth ]
* softwareproperties/SoftwarePropterties.py: Remove white space from
  comments for sources (LP: #1185144)
* Use two-line items in other sources (LP: #930827)
* Deprecate --enable-component option, superseded by add-apt-repository.
  (LP: #1170114)

[ Martin Pitt ]
* Replace obsolete "scrollkeeper" build dependency with rarian-compat.
* debian/control: Bump Standards-Version to 3.9.4.
* debian/tests/run-tests: Make failures in python 2 tests fatal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    import urllib.request
36
36
    from urllib.error import URLError
37
37
    import urllib.parse
 
38
    from http.client import HTTPException
 
39
    NEED_PYCURL = False
38
40
except ImportError:
 
41
    NEED_PYCURL = True
39
42
    import pycurl
40
43
 
41
44
DEFAULT_KEYSERVER = "hkp://keyserver.ubuntu.com:80/"
92
95
 
93
96
def get_ppa_info_from_lp(owner_name, ppa_name):
94
97
    lp_url = LAUNCHPAD_PPA_API % (owner_name, ppa_name)
95
 
    try:
96
 
        try:
97
 
            request = urllib.request.Request(str(lp_url), headers={"Accept":" application/json"})
98
 
            lp_page = urllib.request.urlopen(request, cafile=LAUNCHPAD_PPA_CERT)
99
 
            json_data = lp_page.read().decode("utf-8", "strict")
100
 
        except URLError as e:
101
 
            raise PPAException("Error reading %s: %s" % (lp_url, e.reason), e)
102
 
    except PPAException:
103
 
        raise
104
 
    except:
105
 
        import pycurl
106
 
        try:
107
 
            callback = CurlCallback()
108
 
            curl = pycurl.Curl()
109
 
            curl.setopt(pycurl.SSL_VERIFYPEER, 1)
110
 
            curl.setopt(pycurl.SSL_VERIFYHOST, 2)
111
 
            curl.setopt(pycurl.WRITEFUNCTION, callback.body_callback)
112
 
            if LAUNCHPAD_PPA_CERT:
113
 
                curl.setopt(pycurl.CAINFO, LAUNCHPAD_PPA_CERT)
114
 
            curl.setopt(pycurl.URL, str(lp_url))
115
 
            curl.setopt(pycurl.HTTPHEADER, ["Accept: application/json"])
116
 
            curl.perform()
117
 
            curl.close()
118
 
            json_data = callback.contents
119
 
        except pycurl.error as e:
120
 
            raise PPAException("Error reading %s: %s" % (lp_url, e), e)
121
 
    return json.loads(json_data)
122
 
 
 
98
    if NEED_PYCURL:
 
99
        # python2 has no cert verification so we need pycurl
 
100
        return _get_https_content_pycurl(lp_url)
 
101
    else:
 
102
        # python3 has cert verification so we can use the buildin urllib
 
103
        return _get_https_content_py3(lp_url)
 
104
 
 
105
def _get_https_content_py3(lp_url):
 
106
    try:
 
107
        request = urllib.request.Request(str(lp_url), headers={"Accept":" application/json"})
 
108
        lp_page = urllib.request.urlopen(request, cafile=LAUNCHPAD_PPA_CERT)
 
109
        json_data = lp_page.read().decode("utf-8", "strict")
 
110
    except (URLError, HTTPException) as e:
 
111
        # HTTPException doesn't have a reason but might have a string
 
112
        # representation
 
113
        reason = hasattr(e, "reason") and e.reason or e
 
114
        raise PPAException("Error reading %s: %s" % (lp_url, reason), e)
 
115
    return json.loads(json_data)
 
116
 
 
117
def _get_https_content_pycurl(lp_url):
 
118
    # this is the fallback code for python2
 
119
    try:
 
120
        callback = CurlCallback()
 
121
        curl = pycurl.Curl()
 
122
        curl.setopt(pycurl.SSL_VERIFYPEER, 1)
 
123
        curl.setopt(pycurl.SSL_VERIFYHOST, 2)
 
124
        curl.setopt(pycurl.WRITEFUNCTION, callback.body_callback)
 
125
        if LAUNCHPAD_PPA_CERT:
 
126
            curl.setopt(pycurl.CAINFO, LAUNCHPAD_PPA_CERT)
 
127
        curl.setopt(pycurl.URL, str(lp_url))
 
128
        curl.setopt(pycurl.HTTPHEADER, ["Accept: application/json"])
 
129
        curl.perform()
 
130
        curl.close()
 
131
        json_data = callback.contents
 
132
    except pycurl.error as e:
 
133
        raise PPAException("Error reading %s: %s" % (lp_url, e), e)
 
134
    return json.loads(json_data)
123
135
 
124
136
def verify_keyid_is_v4(signing_key_fingerprint):
125
137
    """Verify that the keyid is a v4 fingerprint with at least 160bit"""