3
3
import requests
4
4
from BeautifulSoup import BeautifulStoneSoup as Soup
5
5
import urllib
6
- import hashlib
6
+ import hashlib
7
7
import os
8
8
import re
9
9
import sys
10
10
11
11
12
12
class BinTray (object ):
13
+
13
14
def __init__ (self ):
14
15
self .base_url = 'https://api.bintray.com/{section}/tmeiczin/opendcp/opendcp/{command}/'
15
16
self .auth = None
16
17
17
18
def version_exists (self ):
18
19
url = self .base_url .format (section = 'packages' , command = 'versions' )
19
20
url += self .version
20
-
21
+
21
22
r = requests .get (url , auth = self .auth )
22
-
23
+
23
24
if r .ok :
24
25
print 'version found'
25
26
return True
26
27
27
28
def publish (self , version ):
28
29
url = self .base_url .format (section = 'content' , command = version )
29
30
url += '/publish'
30
-
31
+
31
32
r = requests .post (url , auth = self .auth )
32
33
33
34
if r .ok :
@@ -38,9 +39,9 @@ def publish(self, version):
38
39
def do_upload (self , filename , version ):
39
40
url = self .base_url .format (section = 'content' , command = version )
40
41
url += os .path .basename (filename )
41
- files = { 'file' : open ( filename , 'rb' )}
42
-
43
- r = requests .put (url , files = files , auth = self .auth )
42
+
43
+ with open ( filename , 'rb' ) as f :
44
+ r = requests .put (url , data = f , auth = self .auth )
44
45
45
46
if r .ok :
46
47
return True
@@ -50,7 +51,9 @@ def do_upload(self, filename, version):
50
51
def upload (self , files ):
51
52
for filename in files :
52
53
basename = os .path .basename (filename )
53
- version = re .search ('opendcp(?:-|_)(\d+.\d+.\d+)' , basename ).group (1 )
54
+ version = re .search (
55
+ 'opendcp(?:-|_)(\d+.\d+.\d+)' ,
56
+ basename ).group (1 )
54
57
55
58
if self .do_upload (filename , version ):
56
59
print 'uploading %s... ok' % (filename )
@@ -60,34 +63,34 @@ def upload(self, files):
60
63
61
64
62
65
class OpenBuild (object ):
66
+
63
67
def __init__ (self ):
64
68
self .base_url = 'https://api.opensuse.org/build/home:tmeiczin:opendcp'
65
69
self .auth = None
66
70
67
71
def _get_repositories (self ):
68
72
url = 'https://api.opensuse.org/build/home:tmeiczin:opendcp'
69
-
73
+
70
74
r = requests .get (url , auth = self .auth )
71
- print r .text
72
-
75
+
73
76
if not r .ok :
74
77
return []
75
-
78
+
76
79
soup = Soup (r .text )
77
80
entries = soup .findAll ('entry' )
78
81
return [x ['name' ] for x in entries ]
79
82
80
83
def _get_arch (self , repo ):
81
84
url = 'https://api.opensuse.org/build/home:tmeiczin:opendcp/' + repo
82
-
85
+
83
86
r = requests .get (url , auth = self .auth )
84
-
87
+
85
88
if not r .ok :
86
89
return []
87
90
88
91
soup = Soup (r .text )
89
92
entries = soup .findAll ('entry' )
90
-
93
+
91
94
return [x ['name' ] for x in entries ]
92
95
93
96
def _get_paths (self ):
@@ -102,11 +105,12 @@ def _get_paths(self):
102
105
return data
103
106
104
107
def _arch (self , arch , binary ):
105
- deb = {'i586' : 'i386' , 'x86_64' : 'amd64' }
108
+ deb = {'i586' : 'i386' , 'x86_64' : 'amd64' }
109
+ rpm = {'i586' : 'i686' , 'x86_64' : 'x86_64' }
106
110
if 'deb' in binary :
107
111
return deb [arch ]
108
112
else :
109
- return arch
113
+ return rpm [ arch ]
110
114
111
115
def _get_md5 (self , link ):
112
116
md5_url = '%s.md5' % (link )
@@ -125,7 +129,7 @@ def links(self):
125
129
base = 'https://api.opensuse.org/build/home:tmeiczin:opendcp/'
126
130
127
131
paths = self ._get_paths ()
128
-
132
+
129
133
for path in paths :
130
134
for repo , v in path .items ():
131
135
for arch in v :
@@ -134,51 +138,89 @@ def links(self):
134
138
soup = Soup (r .text )
135
139
binaries = [x ['filename' ] for x in soup .findAll ('binary' )]
136
140
for binary in binaries :
137
- if any (ext in binary for ext in ['i386.deb' , 'amd64.deb' , 'i586.rpm' , 'i686.rpm' , 'x86_64.rpm' ]):
141
+ if any (
142
+ ext in binary for ext in [
143
+ 'i386.deb' ,
144
+ 'amd64.deb' ,
145
+ 'i586.rpm' ,
146
+ 'i686.rpm' ,
147
+ 'x86_64.rpm' ]):
138
148
link = 'http://download.opensuse.org/repositories/home:/tmeiczin:/opendcp'
139
- link = '%s/%s/%s/%s' % (link , repo , self ._arch (arch , binary ), binary )
149
+ link = '%s/%s/%s/%s' % (link ,
150
+ repo ,
151
+ self ._arch (
152
+ arch ,
153
+ binary ),
154
+ binary )
140
155
md5 = self ._get_md5 (link )
141
- links .append ({'name' : repo , 'url' :link , 'md5' :md5 })
156
+ links .append (
157
+ {'name' : repo , 'url' : link , 'md5' : md5 })
142
158
143
159
return links
144
160
145
161
146
162
class Publish (object ):
163
+
147
164
def __init__ (self ):
148
165
self .tmp_path = '/tmp'
149
166
self .downloaded_files = []
150
-
167
+
151
168
def md5_checksum (self , filename , md5 ):
152
169
if not md5 :
170
+ print 'No MD5 found, skipping'
153
171
return True
154
172
155
- with open (filename , 'rb' ) as fh :
173
+ with open (filename , 'rb' ) as f :
156
174
m = hashlib .md5 ()
157
175
while True :
158
- data = fh .read (8192 )
176
+ data = f .read (8192 )
159
177
if not data :
160
178
break
161
179
m .update (data )
180
+ f .close ()
162
181
163
- if md5 == m .hexdigest ():
164
- return True
182
+ if md5 != m .hexdigest ():
183
+ print 'MD5 mismatch %s (%s -> %s)' % (filename , md5 , m .hexdigest ())
184
+ return False
165
185
166
- return False
186
+ return True
167
187
168
188
def replace_os (self , filename ):
169
189
filename = filename .replace ('centos_centos-6' , 'centos_6' )
170
190
filename = filename .replace ('redhat_rhel-6' , 'rhel_6' )
171
191
if 'opensuse' in filename :
172
- filename = re .sub (r'(opendcp-\d+.\d+.\d+-\w+_\d+.\d+)(-\d+.\d+)' , r'\1' , filename )
192
+ filename = re .sub (
193
+ r'(opendcp-\d+.\d+.\d+-\w+_\d+.\d+)(-\d+.\d+)' ,
194
+ r'\1' ,
195
+ filename )
173
196
else :
174
- filename = re .sub (r'(opendcp-\d+.\d+.\d+-\w+_\d+)(-\d+.\d+)' , r'\1' , filename )
197
+ filename = re .sub (
198
+ r'(opendcp-\d+.\d+.\d+-\w+_\d+)(-\d+.\d+)' ,
199
+ r'\1' ,
200
+ filename )
175
201
176
202
return filename
177
203
178
204
def get_links (self ):
179
205
print 'Getting OpenDCP URLs'
180
206
self .ob_files = OpenBuild ().links ()
181
207
208
+ def download_file (self , url , filename ):
209
+ with open (filename , 'wb' ) as f :
210
+ r = requests .get (url , stream = True )
211
+
212
+ if not r .ok :
213
+ print 'bad http request %s %s (%s)' % (url , filename , r .status_code )
214
+ return False
215
+
216
+ for block in r .iter_content (1024 ):
217
+ if not block :
218
+ break
219
+ f .write (block )
220
+ f .close ()
221
+
222
+ return True
223
+
182
224
def download (self ):
183
225
self .downloaded_files = []
184
226
self .get_links ()
@@ -196,16 +238,22 @@ def download(self):
196
238
replacement = '%s_%s' % (version , l ['name' ])
197
239
else :
198
240
replacement = '%s-%s' % (version , l ['name' ])
199
- filename = '%s/%s' % (self .tmp_path , re .sub (search , replacement , basename ).lower ())
241
+ filename = '%s/%s' % (self .tmp_path ,
242
+ re .sub (
243
+ search ,
244
+ replacement ,
245
+ basename ).lower ())
200
246
filename = self .replace_os (filename )
201
247
202
- urllib .urlretrieve (l ['url' ], filename )
248
+ if not self .download_file (l ['url' ], filename ):
249
+ continue
203
250
204
251
if self .md5_checksum (filename , l ['md5' ]):
205
252
print 'downloading %s... ok' % (filename )
206
253
self .downloaded_files .append (filename )
207
254
else :
208
255
print 'downloading %s... not ok' % (filename )
256
+ self .downloaded_files .append (filename )
209
257
210
258
def upload (self ):
211
259
bintray = BinTray ()
0 commit comments