| 1 |
# ######################################################################## |
| 2 |
# Copyright 2013 Advanced Micro Devices, Inc. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
# ######################################################################## |
| 16 |
|
| 17 |
#This file contains a number of utilities function which could be independent of |
| 18 |
#any specific domain concept |
| 19 |
|
| 20 |
import signal |
| 21 |
from subprocess import check_output |
| 22 |
import errorHandler |
| 23 |
from datetime import datetime |
| 24 |
|
| 25 |
def currentUser(): |
| 26 |
try: |
| 27 |
return check_output("who", shell = True).split()[0]; |
| 28 |
except: |
| 29 |
print 'Unhandled Exception at performanceUtility::currentUser()' |
| 30 |
raise |
| 31 |
|
| 32 |
#Details: Generate sorted numbers in radices of 2,3 and 5 upto a given upper limit number |
| 33 |
def generate235Radices(maxSize): |
| 34 |
sizeList = list() |
| 35 |
i = 0 |
| 36 |
j = 0 |
| 37 |
k = 0 |
| 38 |
SUM = int() |
| 39 |
sumj = int() |
| 40 |
sumk = int() |
| 41 |
sumi = 1 |
| 42 |
while(True): |
| 43 |
sumj = 1 |
| 44 |
j = 0 |
| 45 |
while(True): |
| 46 |
sumk = 1 |
| 47 |
k = 0 |
| 48 |
while(True): |
| 49 |
SUM = sumi*sumj*sumk |
| 50 |
if ( SUM > maxSize ): break |
| 51 |
sizeList.append(SUM) |
| 52 |
k += 1 |
| 53 |
sumk *= 2 |
| 54 |
if (k == 0): break |
| 55 |
j += 1 |
| 56 |
sumj *= 3 |
| 57 |
if ( j == 0 and k == 0): break |
| 58 |
i += 1 |
| 59 |
sumi *= 5 |
| 60 |
sizeList.sort() |
| 61 |
return sizeList |
| 62 |
|
| 63 |
|
| 64 |
def timeout(timeout_time, default): |
| 65 |
def timeout_function(f): |
| 66 |
def f2(args): |
| 67 |
def timeout_handler(signum, frame): |
| 68 |
raise errorHandler.TimeoutException() |
| 69 |
|
| 70 |
old_handler = signal.signal(signal.SIGALRM, timeout_handler) |
| 71 |
signal.alarm(timeout_time) # triger alarm in timeout_time seconds |
| 72 |
retval = "" |
| 73 |
try: |
| 74 |
retval = f(args) |
| 75 |
except errorHandler.TimeoutException: |
| 76 |
raise errorHandler.ApplicationException(__file__, errorHandler.TIME_OUT) |
| 77 |
except: |
| 78 |
signal.alarm(0) |
| 79 |
raise |
| 80 |
finally: |
| 81 |
#print 'executing finally' |
| 82 |
signal.signal(signal.SIGALRM, old_handler) |
| 83 |
signal.alarm(0) |
| 84 |
return retval |
| 85 |
return f2 |
| 86 |
return timeout_function |
| 87 |
|
| 88 |
|
| 89 |
def logTxtOutput(fileName, mode, txt): |
| 90 |
todayFile = fileName+'-'+datetime.now().strftime('%Y-%b-%d')+'.txt' |
| 91 |
with open(todayFile, mode) as f: |
| 92 |
f.write('------\n'+txt+'\n') |
| 93 |
|
| 94 |
def log(filename, txt): |
| 95 |
with open(filename, 'a') as f: |
| 96 |
f.write(datetime.now().ctime()+'# '+txt+'\n') |
| 97 |
|