Gentoo Archives: gentoo-commits

From: Brian Dolbec <brian.dolbec@×××××.com>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/mirrorselect:ssl commit in: mirrorselect/
Date: Fri, 31 Jan 2014 15:44:55
Message-Id: 1390517588.a3b025b7f3d5e67b1735bb773b72b5766537225f.dol-sen@gentoo
1 commit: a3b025b7f3d5e67b1735bb773b72b5766537225f
2 Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org>
3 AuthorDate: Thu Jan 23 22:53:08 2014 +0000
4 Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com>
5 CommitDate: Thu Jan 23 22:53:08 2014 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/mirrorselect.git;a=commit;h=a3b025b7
7
8 Remove connections.py. Instead moved it to it's own ssl-fetch pkg.
9
10 ---
11 mirrorselect/connections.py | 182 --------------------------------------------
12 1 file changed, 182 deletions(-)
13
14 diff --git a/mirrorselect/connections.py b/mirrorselect/connections.py
15 deleted file mode 100644
16 index ca4aa88..0000000
17 --- a/mirrorselect/connections.py
18 +++ /dev/null
19 @@ -1,182 +0,0 @@
20 -#-*- coding:utf-8 -*-
21 -
22 -"""Mirrorselect 2.x
23 - Tool for selecting Gentoo source and rsync mirrors.
24 -
25 -Copyright 2005-2012 Gentoo Foundation
26 -
27 - Copyright (C) 2005 Colin Kingsley <tercel@g.o>
28 - Copyright (C) 2008 Zac Medico <zmedico@g.o>
29 - Copyright (C) 2009 Sebastian Pipping <sebastian@×××××××.org>
30 - Copyright (C) 2009 Christian Ruppert <idl0r@g.o>
31 - Copyright (C) 2012 Brian Dolbec <dolsen@g.o>
32 -
33 -Distributed under the terms of the GNU General Public License v2
34 - This program is free software; you can redistribute it and/or modify
35 - it under the terms of the GNU General Public License as published by
36 - the Free Software Foundation, version 2 of the License.
37 -
38 - This program is distributed in the hope that it will be useful,
39 - but WITHOUT ANY WARRANTY; without even the implied warranty of
40 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 - GNU General Public License for more details.
42 -
43 - You should have received a copy of the GNU General Public License
44 - along with this program; if not, write to the Free Software
45 - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
46 -
47 -"""
48 -
49 -import sys
50 -import os
51 -
52 -VERIFY_SSL = False
53 -VERIFY_MSGS = []
54 -
55 -import requests
56 -from requests.exceptions import SSLError
57 -
58 -# py3.2
59 -if sys.hexversion >= 0x30200f0:
60 - VERIFY_SSL = True
61 -else:
62 - try: # import and enable SNI support for py2
63 - from requests.packages.urllib3.contrib import pyopenssl
64 - pyopenssl.inject_into_urllib3()
65 - VERIFY_SSL = True
66 - VERIFY_MSGS = ["Successfully enabled ssl certificate verification."]
67 - except ImportError as e:
68 - VERIFY_MSGS = [
69 - "Failed to import and inject pyopenssl/SNI support into urllib3",
70 - "Disabling certificate verification",
71 - "Error was:" + e
72 - ]
73 - VERIFY_SSL = False
74 -
75 -
76 -from mirrorselect.version import version
77 -
78 -
79 -class Connector(object):
80 - """Primary connection interface using the dev-python/requests package
81 - """
82 -
83 - def __init__(self, output, proxies):
84 - self.output = output
85 - self.proxies = proxies
86 - self.headers = {'Accept-Charset': 'utf-8',
87 - 'User-Agent': 'Mirrorselect-' + version}
88 -
89 - if VERIFY_MSGS:
90 - for msg in VERIFY_MSGS:
91 - self.output.write(msg + '\n', 2)
92 -
93 -
94 - def add_timestamp(self, headers, tpath=None, timestamp=None):
95 - """for possilble future caching of the list"""
96 - if tpath and os.path.exists(tpath):
97 - # fileopen is a layman comaptibility function not yet implemented here
98 - with fileopen(tpath,'r') as previous:
99 - timestamp = previous.read()
100 - if timestamp:
101 - headers['If-Modified-Since'] = timestamp
102 - self.output.write('Current-modified: %s\n' % timestamp, 2)
103 - return headers
104 -
105 -
106 - def fetch_url(self, url, headers=None, timestamp=None):
107 - """Fetches the url
108 -
109 - @param url: string
110 - @param headers: dictionary, optional headers to use
111 - @param tpath: string, optional filepath to a timestamp file
112 - to use in the headers
113 - @param timestamp: string, optional timestamp to use in the headers
114 -
115 - """
116 -
117 - if not headers:
118 - headers = self.headers
119 -
120 - if timestamp:
121 - self.add_timestamp(headers, timestamp=timestamp)
122 -
123 - verify = 'https' in url and VERIFY_SSL
124 - self.output.write("Enabled ssl certificate verification: %s, for: %s\n"
125 - %(str(verify), url), 3)
126 -
127 - self.output.write('Connector.fetch_url(); headers = %s\n' %str(headers), 4)
128 - self.output.write('Connector.fetch_url(); connecting to opener\n', 2)
129 -
130 - try:
131 - connection = requests.get(
132 - url,
133 - headers=headers,
134 - verify=verify,
135 - proxies=self.proxies,
136 - )
137 - except SSLError as error:
138 - self.output.print_err('Connector.fetch_url(); Failed to update the '
139 - 'mirror list from: %s\nSSLError was:%s\n'
140 - % (url, str(error)))
141 - except Exception as error:
142 - self.output.print_err('Connector.fetch_url(); Failed to retrieve '
143 - 'the content from: %s\nError was: %s\n'
144 - % (url, str(error)))
145 -
146 - self.output.write('Connector.fetch_url() HEADERS = %s\n' %str(connection.headers), 4)
147 - self.output.write('Connector.fetch_url() Status_code = %i\n' % connection.status_code, 2)
148 - return connection
149 -
150 -
151 - @staticmethod
152 - def normalize_headers(headers, to_lower=True):
153 - """ py2, py3 compatibility function, since only py2 returns keys as lower()
154 - """
155 - if to_lower:
156 - return dict((x.lower(), x) for x in list(headers))
157 - return dict((x.upper(), x) for x in list(headers))
158 -
159 -
160 - def fetch_content(self, url, tpath=None):
161 - """Fetch the mirror list
162 -
163 - @param url: string of the content to fetch
164 - @param headers: dictionary, optional headers to use
165 - @param tpath: string, optional filepath to a timestamp file
166 - to use in the headers
167 - @returns (success bool, content fetched , timestamp of fetched content,
168 - content headers returned)
169 - """
170 -
171 - fheaders = self.headers
172 -
173 - if tpath:
174 - fheaders = self.add_timestamp(fheaders, tpath)
175 -
176 - connection = self.fetch_url(url, fheaders)
177 -
178 - headers = self.normalize_headers(connection.headers)
179 -
180 - if 'last-modified' in headers:
181 - timestamp = headers['last-modified']
182 - elif 'date' in headers:
183 - timestamp = headers['date']
184 - else:
185 - timestamp = None
186 -
187 - if connection.status_code in [304]:
188 - self.output.write('Content already up to date: %s\n'
189 - % url, 4)
190 - self.output.write('Last-modified: %s\n' % timestamp, 4)
191 - elif connection.status_code not in [200]:
192 - self.output.print_err('Connector.fetch_content(); HTTP Status-Code was:\n'
193 - 'url: %s\n%s'
194 - % (url, str(connection.status_code)))
195 -
196 - if connection.status_code in [200]:
197 - self.output.write('New content downloaded for: %s\n'
198 - % url, 4)
199 - return (True, connection.content, timestamp)
200 - return (False, '', '')
201 -