forked from pykickstart/pykickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced URLGrabber with requests (rhbz#1141245)
Because urlgrabber is being dropped by many significant software projects in Fedora, Pykickstart can't keep depending on it once it is converted to Python 3 (rhbz#985310). This commit replaces the dep with requests with SSL verification turned on by default. This could pose trouble in Python 2 if proper underlying packages aren't installed [1]. In case this becomes a problem, the SSL_VERIFY constant in constants.py may be switched to False. [1] https://github.com/kennethreitz/requests/issues/749#issuecomment-19187417
- Loading branch information
Showing
5 changed files
with
108 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
import requests | ||
import shutil | ||
import tempfile | ||
|
||
from pykickstart.constants import SSL_VERIFY | ||
from pykickstart.errors import KickstartError | ||
from requests.exceptions import SSLError, RequestException | ||
|
||
_is_url = lambda location: '://' in location # RFC 3986 | ||
|
||
def load_to_str(location): | ||
'''Load a destination URL or file into a string. | ||
Type of input is inferred automatically. | ||
Arguments: | ||
location -- URL or file name to load | ||
Returns: string with contents | ||
Raises: KickstartError on error reading''' | ||
|
||
if _is_url(location): | ||
return _load_url(location) | ||
else: | ||
return _load_file(location) | ||
|
||
|
||
def load_to_file(location, destination): | ||
'''Load a destination URL or file into a file name. | ||
Type of input is inferred automatically. | ||
Arguments: | ||
location -- URL or file name to load | ||
destination -- destination file name to write to | ||
Returns: file name with contents | ||
Raises: KickstartError on error reading or writing''' | ||
|
||
if _is_url(location): | ||
contents = _load_url(location) | ||
|
||
# Write to file | ||
try: | ||
with open(destination, 'w') as fh: | ||
fh.write(contents) | ||
except IOError as e: | ||
raise KickstartError('Error writing file: {e}'.format(e=str(e))) | ||
This comment has been minimized.
Sorry, something went wrong.
clumens
|
||
|
||
return destination | ||
else: | ||
_copy_file(location, destination) | ||
return destination | ||
|
||
|
||
|
||
def _load_url(location): | ||
'''Load a location (URL or filename) and return contents as string''' | ||
|
||
try: | ||
request = requests.get(location, verify=SSL_VERIFY) | ||
except SSLError as e: | ||
raise KickstartError('Error securely accessing URL "{url}": {msg}'.format( | ||
url=location, msg=e)) | ||
except RequestException as e: | ||
raise KickstartError('Error accessing URL "{url}": {msg}'.format( | ||
url=location, msg=e)) | ||
|
||
return request.content | ||
|
||
|
||
def _load_file(filename): | ||
'''Load a file's contents and return them as a string''' | ||
|
||
try: | ||
with open(filename, 'r') as fh: | ||
contents = fh.read() | ||
except IOError as e: | ||
raise KickstartError('Error opening file: {e}'.format(e=str(e))) | ||
|
||
return contents | ||
|
||
def _copy_file(filename, destination): | ||
'''Copy file to destination''' | ||
|
||
try: | ||
shutil.copyfile(filename, destination) | ||
except OSError as e: | ||
raise KickstartError('Error copying file: {e}'.format(str(e))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constants.py is for things that pykickstart is exporting to users. If SSL_VERIFY is for internal-use only, I'd not put it into constants.py.