use either pycurl or wget to download from launchpad, also improve error message if neither is installed

This commit is contained in:
Michael Vogt 2013-08-13 19:25:31 +02:00 committed by James Cammarata
parent 5e56d42ed1
commit de404eac06

View file

@ -30,6 +30,7 @@ notes:
- This module works on Debian and Ubuntu and requires only C(python-apt) package. - This module works on Debian and Ubuntu and requires only C(python-apt) package.
- This module supports Debian Squeeze (version 6) as well as its successors. - This module supports Debian Squeeze (version 6) as well as its successors.
- This module treats Debian and Ubuntu distributions separately. So PPA could be installed only on Ubuntu machines. - This module treats Debian and Ubuntu distributions separately. So PPA could be installed only on Ubuntu machines.
- For PPA support you either need python-pycurl or wget on the client.
options: options:
repo: repo:
required: true required: true
@ -66,8 +67,8 @@ import glob
import json import json
import os import os
import re import re
import subprocess
import tempfile import tempfile
import pycurl
try: try:
import apt_pkg import apt_pkg
@ -80,6 +81,7 @@ except ImportError:
VALID_SOURCE_TYPES = ('deb', 'deb-src') VALID_SOURCE_TYPES = ('deb', 'deb-src')
class CurlCallback: class CurlCallback:
def __init__(self): def __init__(self):
self.contents = '' self.contents = ''
@ -87,10 +89,20 @@ class CurlCallback:
def body_callback(self, buf): def body_callback(self, buf):
self.contents = self.contents + buf self.contents = self.contents + buf
class InvalidSource(Exception): class InvalidSource(Exception):
pass pass
def get_executable_from_path(cmd):
full_path = [os.path.join(p, cmd)
for p in os.environ.get("PATH", "").split(":")
if os.path.isfile(os.path.join(p, cmd))]
if full_path:
return full_path[0]
return None
# Simple version of aptsources.sourceslist.SourcesList. # Simple version of aptsources.sourceslist.SourcesList.
# No advanced logic and no backups inside. # No advanced logic and no backups inside.
class SourcesList(object): class SourcesList(object):
@ -250,12 +262,30 @@ class SourcesList(object):
class UbuntuSourcesList(SourcesList): class UbuntuSourcesList(SourcesList):
LP_API = 'https://launchpad.net/api/1.0/~%s/+archive/%s'
def __init__(self, add_ppa_signing_keys_callback=None): def __init__(self, add_ppa_signing_keys_callback=None):
self.add_ppa_signing_keys_callback = add_ppa_signing_keys_callback self.add_ppa_signing_keys_callback = add_ppa_signing_keys_callback
super(UbuntuSourcesList, self).__init__() super(UbuntuSourcesList, self).__init__()
def _get_ppa_info(self, owner_name, ppa_name): def _get_ppa_info(self, owner_name, ppa_name):
lp_api = 'https://launchpad.net/api/1.0/~%s/+archive/%s' % (owner_name, ppa_name) # we can not use urllib2 here as it does not do cert verification
lp_api = self.LP_API % (owner_name, ppa_name)
try:
return self._get_ppa_info_curl(lp_api)
except ImportError:
return self._get_ppa_info_wget(lp_api)
def _get_ppa_info_wget(self, lp_api):
wget = get_executable_from_path("wget")
p = subprocess.Popen([wget, "-q", "-O", "-",
"--header=Accept: application/json",
lp_api], stdout=subprocess.PIPE)
return json.loads(p.communicate()[0])
def _get_ppa_info_curl(self, lp_api):
import pycurl
callback = CurlCallback() callback = CurlCallback()
curl = pycurl.Curl() curl = pycurl.Curl()
curl.setopt(pycurl.SSL_VERIFYPEER, 1) curl.setopt(pycurl.SSL_VERIFYPEER, 1)
@ -321,6 +351,15 @@ def main():
if not HAVE_PYTHON_APT: if not HAVE_PYTHON_APT:
module.fail_json(msg='Could not import python modules: apt_pkg. Please install python-apt package.') module.fail_json(msg='Could not import python modules: apt_pkg. Please install python-apt package.')
# see if we have something that can download from https with cert
# checking
try:
import pycurl
except ImportError:
wget = get_executable_from_path("wget")
if not wget:
module.fail_json(msg="Need the python-pycurl or wget package")
repo = module.params['repo'] repo = module.params['repo']
state = module.params['state'] state = module.params['state']
sourceslist = None sourceslist = None