/
pget.py
307 lines (220 loc) · 7.85 KB
/
pget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import os
import sys
import time
import base64
from urldownloader import UrlDownloader
from urldownloader import UrlDownloadTask
from urldownloader import UrlDownloaderMonitor
from pbsloader import PbsUrlLoader
from pbsloader import PbsProductsAnalyser
from urlfileinfo import UrlFileInfo
from user import User
from jconfig import JConfig
class PbsProductGetter:
PBS_URL = 'http://suwon.qb.sec.samsung.net'
def __init__(self, pbsNumber,
user,
downloadHeaderlist,
outputDir,
monitorInterval = UrlDownloaderMonitor.DEF_TIME_INTERVAL,
pbsUrl = PBS_URL):
self._pbsUrl = pbsUrl;
self._pbsNumber = pbsNumber
self._user = user
self._downloadHeaderlist = downloadHeaderlist
self._outputDir = outputDir
self._downloader = UrlDownloader()
self._downloaderMonitor = PbsDownloadMonitor(self._downloader,
monitorInterval)
def __isFileNeedToDownload(self, name):
for header in self._downloadHeaderlist:
if name.startswith(header):
return True
return False
def getPbsUrl(self):
return self._pbsUrl
def _waitForBuilding(self, analyser, timeInterval):
if analyser == None:
return
while analyser.getPercentage() >= 0:
sys.stdout.write("\n-------------------------------\n")
sys.stdout.write('building in progress: '
+ str(analyser.getPercentage())
+ '%\n')
sys.stdout.write('\n-------------------------------\n')
time.sleep(timeInterval)
def _prepare(self):
urlLoader = PbsUrlLoader(url = self.getPbsUrl(),
user = self._user,
pageId = self._pbsNumber)
analyser = PbsProductsAnalyser(urlLoader)
if analyser.analyse() == False:
self._waitForBuilding(analyser,
self._downloaderMonitor.getMonitoringInterval())
if self._outputDir == None:
self._outputDir = ''
else:
self._outputDir += '/'
products = analyser.getProducts()
for p in products:
if self.__isFileNeedToDownload(p.getFileName()):
task = UrlDownloadTask(p,
self._outputDir + p.getFileName())
self._downloader.append(task)
def startDownloading(self):
self._prepare()
self._downloader.startDownload()
if self._downloaderMonitor != None:
self._downloaderMonitor.startMonitoring()
class PbsDownloadMonitor(UrlDownloaderMonitor):
def __init__(self, downloader,
monitoringInterval = UrlDownloaderMonitor.DEF_TIME_INTERVAL):
UrlDownloaderMonitor.__init__(self, downloader, monitoringInterval)
self._downloadedSize = 0
def _onBeforeStartMonitoring(self):
sys.stdout.write('download started\n')
def _onMonitoringRoundStart(self, aliveTaskList):
sys.stdout.write('\n-------------------------------\n')
self._downloadedSize = 0
def _onMonitoringRoundEnd(self, aliveTaskList):
totalSize = self.getDownloader().getSizeToDownload()
percent = self._downloadedSize / totalSize * 100
sys.stdout.write('[total %.2f' % percent + '%] '
+ str(len(aliveTaskList))
+ ' of ' + str(len(self.getDownloader().getTaskList()))
+ ' left')
sys.stdout.write('\n-------------------------------\n')
sys.stdout.flush()
def _onMonitoringTask(self, task):
info = task.getUrlFileInfo()
downloaded = task.getDownloadedSize()
total = info.getFileSize()
percent = downloaded / total * 100
self._downloadedSize += downloaded
sys.stdout.write(''
+ '[%.2f' % percent + '%] '
+ info.getFileName() + '\n')
def _onFinishMoniting(self):
sys.stdout.write('download finished\n')
def print_help():
helpInfo = 'usage:\n\tpget [config=value ..] {cmd / pbs number}\n\
\n\
configs probably are:\n\
pbsurl=url\n\
configfile=filepath\n\
outputdir=dir\n\
\n\
cmds are:\n\
-h show this message\n\
-v show version\n\
'
sys.stdout.write(helpInfo)
sys.stdout.flush()
def print_version():
version = 'pget v0.1\n'
sys.stdout.write(version)
sys.stdout.flush()
def arg_get_config_key(arg):
index = arg.replace(' ', '').find('=')
if index < 0:
return None
return arg[0:index]
def arg_get_config_value(arg):
index = arg.replace(' ', '').find('=')
if index < 0:
return None
return arg[index + 1:]
def report_error_arg_value(value):
sys.stdout.write('error: used error config value: ' + value + '\n')
sys.stdout.flush()
def report_error_arg_cmd(cmd):
sys.stdout.write('error: unknown cmd: ' + cmd + '\n')
sys.stdout.flush()
def is_pbs_number(number):
try:
num = int(number)
return True
except:
return False
def encrypt_user_password(config):
password = config.get('password')
if password == None:
return
password = base64.encodestring(password)
password = password.replace('\n', '')
password = password.replace('\r', '')
config.set('password', password)
if __name__ == '__main__':
configFile = 'pget.config'
outputDir = '.'
pbsNumber = ''
pbsUrl = PbsProductGetter.PBS_URL
downloadHeaderList = []
argc = len(sys.argv)
if argc <= 1:
print_help()
exit(-1)
for i in range(1, argc):
arg = sys.argv[i]
configKey = arg_get_config_key(arg)
if configKey != None:
configVal = arg_get_config_value(arg)
if configVal == None:
report_error_arg_value(configVal)
exit(1)
if configKey == 'pbsurl':
pbsUrl = configVal
elif configKey == 'configfile':
configFile = configVal
elif configKey == 'outputdir':
outputDir = configVal
else:
report_error_arg_value(configVal)
exit(-1)
else:
if arg == '-h':
print_help()
exit(0)
elif arg == '-v':
print_version()
exit(0)
else:
if is_pbs_number(arg):
pbsNumber = arg
else:
report_error_arg_cmd(arg)
exit(-1)
config = JConfig()
config.loadFromFile(configFile)
print(os.getcwd())
print('loading config from ' + configFile)
print(config.toString())
userName = config.get('user')
password = config.get('password')
if config.get('download-header') != None:
downloadHeaders = str(config.get('download-header'))
downloadHeaderList = downloadHeaders.split('|')
if config.get('output-dir') != None:
outputDir = config.get('output-dir')
try:
decodePassword = base64.decodestring(password)
except:
encrypt_user_password(config)
decodePassword = password
user = User()
user.set('userName', userName)
user.set('password', decodePassword)
if len(downloadHeaderList) == 0:
report_error_arg_value('download-header')
exit(0)
config.saveToFile(configFile)
outputDir += '/' + str(pbsNumber)
if os.path.isdir(outputDir) == False:
os.mkdir(outputDir)
print(downloadHeaderList)
getter = PbsProductGetter(pbsNumber,
user,
downloadHeaderList,
outputDir,
1.0)
getter.startDownloading()