-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwa.py
146 lines (134 loc) · 4.93 KB
/
pwa.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
import urllib.request
import os
import pathlib
import gzip
import setup as s
# ##################################################################
# pwaFileImport
# ##################################################################
def pwaFileImport(webRetrieve=False, unzipfiles=True, maxFiles = 5):
"""
Import files froms website https://www.cs.huji.ac.il
Logs (of real life) of set of job times.
Retrieve file catalog : address setted in constant s.URL_CATALOG_PWA
reads this file which contains the url addresses of the time files
and each file is retreived in the folder FOLDER_ZIPPEDLOG,
to be unzipped in the folder FOLDER_PWA
input
:param webRetrieve: True : retreive files from website.
:param unzipfiles : True : unzip files from zippedLog folder to PWA folder
:param maxFiles : number of files to retreive from website.
use 0 to retreive all files
"""
tabFiles = []
#-------------------------------
# log directory
# s.folder creates requested directories if not exists
# curDir = os.path.abspath(os.curdir)
#-------------------------------
zipDir = s.folder(s.FOLDER_ZIPPEDLOG)
logDir = s.folder(s.FOLDER_PWA)
#-------------------------------
# Web resource web --> zipDir
#-------------------------------
if webRetrieve:
# read distant file
fichierNom = s.URL_CATALOG_PWA # url of files log catalog
req = urllib.request.Request(url=fichierNom)
fichierId = urllib.request.urlopen(req)
# put the list on a list tabFiles
contentsLine = fichierId.readline().decode('utf-8')
while contentsLine:
tabFiles.append(contentsLine.rstrip("\n")) # erase \n caracter from the string
contentsLine = fichierId.readline().decode('utf-8')
# close the file
fichierId.close()
# now i have my list of pwa gz logs
n=0
for file in tabFiles:
n+=1
if (n > maxFiles or maxFiles==0):
break
fileInfo = pathlib.Path(file)
# destFile = os.path.join(zipDir, fileInfo.name)
destFile = zipDir+"/"+fileInfo.name
urllib.request.urlretrieve(file, destFile)
print("file ========> "+destFile+" retrieved.")
if unzipfiles == True:
unzipGZ(fileInfo.name, zipDir, logDir)
# ##################################################################
# unzipGZ
# ##################################################################
def unzipGZ(fileNameGZ, fromDir, destDir):
"""
Unzip the file named fromDir+fileNameGZ
in the folder destDir.
"""
#
fromFile = fromDir+s.sepDir()+fileNameGZ
destFile = destDir+s.sepDir()+fileNameGZ.rstrip(".gz")
#
print("Unzipping file %s in %s" % (fromFile, destDir))
#
src = gzip.GzipFile(fromFile, 'rb')
sRead = src.read()
src.close()
d = open(destFile, 'wb')
d.write(sRead)
d.close()
print("Unzipped.")
# ##################################################################
# pwaFileRead
# ##################################################################
def pwaFileRead(fileName):
"""
Reads the log file according to the predefined format,
to create an instance (set of times)
called from matrix.py
"""
with open(fileName, 'r') as f:
text = f.read()
# END WITH
times = []
for line in text.split('\n'):
line = line.strip()
if not(line) or line[0] == ";":
continue
# END IF
jobId, submitTime, waitTime, runTime, nbProc, avgCPUtime, mem, reqProc, reqTime, reqMem, status, uId, gId, appId, queueId, partitionId, precedingJob, timefromPrecedingJob = [float(x) for x in line.split()]
if runTime != 0:
# times += [runTime]
times.append(runTime)
# END IF
# END FOR
return times
# ##################################################################
# pwaFileChoice():
# ##################################################################
def pwaFileChoice(chooseMode = None):
"""
finds the files contained in the "FOLDER_PWA" directory,
and proposes to choose them, or not (for test instance creation).
:Param chooseMode : None Asc for use the current files / 1 Always answer YES, 0 Always answer NO,
Returns the list of selected files as a list files[]
"""
files = []
logDir = s.folder(s.FOLDER_PWA)
content = os.listdir(logDir)
for item in content:
if chooseMode == None:
r = int(input("Use this file %s ? (1 yes 0 no) : " % (item)))
else:
r=chooseMode
# END IF
if r == 1:
files.append(logDir+s.sepDir()+item)
# END IF
# END FOR
print(files)
return files
##TO TEST THIS SCRIPT
##pwaFileImport(True, True)
##logTimes = pwaFileRead(logFolder()+"/NASA-iPSC-1993-3.1-cln.swf")
##print(logTimes)
##pwaFileChoice()