How to use getMsg method in pact-foundation-pact

Best JavaScript code snippet using pact-foundation-pact

pac_api.py

Source:pac_api.py Github

copy

Full Screen

1#!/usr/bin/env python2import sys3import os4import getopt5import httplib6import httplib27from xml.etree import ElementTree as ET8from xml.dom import minidom9from xml.parsers.expat import ExpatError10import getpass11import locale12import gettext13import urllib214import re15PACCONTEXT='platform'16PACPASSFILE='.pacpass'17ACCEPT_TYPE='text/plain,application/xml,text/xml,multipart/mixed'18ERROR_STR='errMsg'19NOTE_STR='note'20ERROR_TAG='<' + ERROR_STR + '>'21ACTION_STR='actionMsg'22ACTION_TAG='<' + ACTION_STR + '>'23CMD_STR='cmdMsg'24CMD_TAG='<' + CMD_STR + '>'25APP_NAME='APPLICATION_NAME'26def getSysLocale():27 lc, encoding = locale.getdefaultlocale()28 if not lc:29 lc = 'en_US' 30 return lc31def getTranslations():32 # All translation files are under mo/<LOCALE>/LC_MESSAGES/pacclient.mo33 mo_location = os.path.dirname(os.path.abspath(__file__)) + '/nls'34 APP_NAME = "pacclient"35 # Set default system locale36 # DEFAULT_LANGUAGES = os.environ.get('LANG', '').split(':')37 DEFAULT_LANGUAGES = ['en']38 # Determine current system locale settings39 languages = []40 lc, encoding = locale.getdefaultlocale()41 if lc:42 languages = [lc]43 # Concat all languages44 # gettext will use the first available translation in the list45 languages += DEFAULT_LANGUAGES46 47 # Get transtion using gettext API48 gettext.install (True)49 gettext.bindtextdomain (APP_NAME, mo_location)50 gettext.textdomain (APP_NAME)51 return ( gettext.translation (APP_NAME,52 mo_location,53 languages = languages,54 fallback = True) )55_getmsg = getTranslations().gettext56def parseUrl(param_url):57 if len(param_url) == 0:58 return param_url,PACCONTEXT59 find_index = -160 if param_url.startswith("http://"):61 find_index = 762 if param_url.startswith("https://"):63 find_index = 864 if find_index == -1:65 return param_url,PACCONTEXT66 slash_index = param_url.find("/",find_index)67 context = ""68 if slash_index > -1:69 context = param_url[slash_index+1:len(param_url)]70 param_url = param_url[0:slash_index+1]71 if param_url.endswith("/") == False:72 param_url = param_url + '/' 73 if (context == '/') or (len(context) == 0) :74 context = PACCONTEXT75 if context.endswith("/") == False:76 context = context + '/'77 return param_url,context78def downloadFile(srcName, dstPath, jobId, cmd):79 url,token = getToken();80 81 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)82 83 if ( (x509Flag == False) & (len(token) <= 0) ):84 print ( _getmsg("must_logon_pac") )85 return86 87 http = getHttp(url,x509Flag)88 if ( (x509Flag == True) & (len(token) <= 0) ):89 # X509Flag is True and token is empty, then add the key/cert files into http request.90 http.add_certificate(keypemfile, certpemfile, '')91 92 url_file = url + 'webservice/pacclient/file/' + jobId93 if cmd == '':94 body=getFileNameByFullPath(srcName)95 else:96 body=getFileNameByFullPath(srcName) + '|' + cmd97 headers = {'Content-Type': 'text/plain', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}98 try:99 response, content = http.request(url_file, 'GET', body=body, headers=headers)100 101 # Check response content102 if len(content) <= 0:103 fileName = getFileNameByFullPath(srcName)104 if response['status'] == '404':105 print ( _getmsg("downloadfailed_notfound") % fileName )106 elif response['status'] == '403':107 print ( _getmsg("downalodfile_errnopermission") % fileName )108 else:109 print ( _getmsg("downalodfile_nopermission_notfound") % fileName )110 return111 else:112 # Parse content and download files113 parseDownloadContent(dstPath, content)114 115 except (AttributeError, httplib.IncompleteRead):116 print ( _getmsg("connect_ws_err") % url )117 except IOError:118 print ( " --" + (_getmsg("permission_denied_writefile") % getDestFilePath(dstPath, srcName) ) )119def getFileNameByFullPath(filePath):120 nList = []121 if filePath[0] != '/':122 # For Windows path123 nList = filePath.split('\\')124 else:125 # For Linux path126 nList = filePath.split('/')127 fName = nList.pop()128 return fName129def getDestFilePath(dstPath, filePath):130 fName = getFileNameByFullPath(filePath)131 # Arrange the file path132 if dstPath[-1] != getFileSeparator():133 dstName= dstPath + getFileSeparator() + fName134 else:135 dstName= dstPath + fName136 return dstName137def parseDownloadContent(dstPath, content):138 """139 Parse the HTTP response when downloading multiple files140 Store them into dstPath141 """142 # Get the boundary and trim the \r\n\t143 boundary = content.split("\n")[1].strip()144 145 # Split content with boundary to parse the files146 fileSections = content.split(boundary)147 fileNum = len(fileSections) - 1148 149 # Loop the file sections and store the file to destination path150 for fileHeaders in fileSections:151 # If has Content-ID in this section, it contains one file152 if 'Content-ID:' in fileHeaders:153 154 # Get file name155 tempArray = fileHeaders.split("Content-ID: ")156 fName = tempArray[1][1:tempArray[1].index(">")]157 158 fName = getFileNameByFullPath(fName)159 160 # Arrange the file path161 if dstPath[-1] != getFileSeparator():162 dstName= dstPath + getFileSeparator() + fName163 else:164 dstName= dstPath + fName165 166 # Get the file content167 # Important logic168 strlength = len(fileHeaders)169 startIndex = fileHeaders.index(">") + 5170 # Remove extra characters171 endIndex = strlength172 if fileNum > 1:173 endIndex = strlength - 2174 fileContent = fileHeaders[startIndex : endIndex]175 176 # Tip message for downloading files177 print ( _getmsg("downloading_file").format(fName, dstName) )178 179 try:180 # Write file 181 f = open(dstName,'wb')182 f.write(fileContent)183 f.close()184 except IOError:185 print ( " --" + (_getmsg("permission_denied_writefile") % dstName) )186def downloadJobFiles(jobId, dstPath, dFile, cmd):187 # If you specify download files with -f option, use webservice/pacclient/file/{id} API to download188 if len(dFile) > 0:189 print ( _getmsg("start_download") )190 downloadFile(dFile, dstPath, jobId, cmd)191 return192 193 # Download all files of one job194 url, token = getToken();195 196 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)197 198 if ( (x509Flag == False) & (len(token) <= 0) ):199 print ( _getmsg("must_logon_pac") )200 return201 202 http = getHttp(url,x509Flag)203 if ( (x509Flag == True) & (len(token) <= 0) ):204 # X509Flag is True and token is empty, then add the key/cert files into http request.205 http.add_certificate(keypemfile, certpemfile, '')206 207 if os.access(dstPath, os.W_OK) == 0:208 print ( _getmsg("dirpermission_denied") % dstPath )209 return210 url_jobfiles= url + 'webservice/pacclient/jobfiles/' + jobId211 headers = {'Content-Type': 'text/plain', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}212 try:213 response, content = http.request(url_jobfiles, 'GET', headers=headers)214 if ('\\' not in content) and ('/' not in content):215 print content216 return217 if (response['status'] != '200'):218 print ( _getmsg("connect_ws_err") % url )219 return220 221 files = content.split(';')222 223 if len(files) <= 0:224 print ( _getmsg("app_nojob_data").format(jobId) )225 return226 227 print ( _getmsg("start_download") )228 # Download all files in job directory229 for ff in files:230 if len(ff) <= 0:231 break232 downloadUtil(dstPath, ff, jobId, cmd)233 except AttributeError:234 print ( _getmsg("connect_ws_err") % url )235 236def downloadUtil(dstPath, dFile, jobId, cmd):237 """238 download a file from server into local host path :dstPath.239 """240 # hostname*filepath*fileType*fileSize*controlFlag;241 fileArray = dFile.split("*")242 243 # Don't download job directory244 if ((len(fileArray) == 5) and (fileArray[2] == 'DIR')):245 return246 247 # If file size is 0, remember it and check it later248 fileSize = ''249 controlFlag = 'true'250 if ((len(fileArray) == 5) and (fileArray[2] != 'DIR')):251 fileSize = fileArray[3]252 controlFlag = fileArray[4]253 254 if ((len(fileArray) == 5) | (len(fileArray) == 2)):255 dFile = fileArray[1]256 257 # If has no control permission on job file, show error message258 if controlFlag == 'false':259 print ( _getmsg("downalodfile_errnopermission") % dFile )260 return261 262 if dFile[0] != '/':263 # For Windows path264 nList = dFile.split('\\')265 else:266 # For Linux path267 nList = dFile.split('/')268 269 fName= nList.pop()270 if fName[-1] == ' ':271 fName = fName[0:-1]272 if len(dstPath) > 0:273 if dstPath[-1] != getFileSeparator():274 dstName= dstPath + getFileSeparator() + fName275 else:276 dstName= dstPath + fName277 else:278 dstName= fName279 # print ( _getmsg("downloading_file").format(fName,dstName) )280 281 # For 0 kb file, just create it282 # if fileSize == '0':283 # f=open(dstName,'wb')284 # f.close()285 # return286 287 # For other files, download it from server288 downloadFile(dFile, dstPath, jobId, cmd)289def downloadMultipleFiles(dstPath, files, jobId, specifiedFileList, cmd):290 """291 download specified job files from server into local host path: dstPath.292 """293 if len(specifiedFileList) <= 0:294 return295 296 dFileList = specifiedFileList.split(',')297 for dFile in dFileList:298 # We support wild cards: ? * []299 if ('*' in dFile) | ('?' in dFile) | ('[' in dFile) | ('[!' in dFile):300 # File name includes wild card characters301 orignalFile = dFile302 dFile = dFile.replace('?', '.')303 dFile = dFile.replace('*', '.*')304 dFile = dFile.replace('[!', '[^')305 306 dFile = '^' + dFile + "$"307 308 pattern = re.compile(dFile)309 downloadFlag = False310 for file in files:311 # If file is empty string, skip it.312 if len(file) <= 0:313 continue314 315 # hostname*filepath*fileType*fileSize*controlFlag;316 fileArray = file.split("*")317 318 # Don't download job directory319 if ((len(fileArray) == 5) and (fileArray[2] == 'DIR')):320 continue321 322 fileAbsoPath = fileArray[1]323 324 if fileAbsoPath[0] != '/':325 # For Windows path326 fileStrList = fileAbsoPath.split('\\')327 else:328 # For Linux path329 fileStrList = fileAbsoPath.split('/')330 331 fileName = fileStrList.pop()332 if fileName[-1] == ' ':333 fileName = fileName[0:-1]334 335 # Search file name from job file list336 if pattern.match(fileName):337 # Set download flag338 downloadFlag = True339 # Download specified file340 downloadSpecifiedFile(dstPath, files, jobId, fileAbsoPath, cmd)341 342 # If don't download any files, print error message343 if downloadFlag == False:344 print ( _getmsg("downloadfailed_notfound") % orignalFile )345 return346 347 else:348 # Download specified file349 downloadSpecifiedFile(dstPath, files, jobId, dFile, cmd)350def downloadSpecifiedFile(dstPath, files, jobId, specifiedFile, cmd):351 """352 download a specified file from server into local host path: dstPath.353 """354 fileSize = ''355 controlFlag = 'true'356 specifiedFileList = ''357 downloadFlag = False358 for dFile in files:359 # hostname*filepath*fileType*fileSize*controlFlag;360 fileArray = dFile.split("*")361 362 # Don't download job directory363 if ((len(fileArray) == 5) and (fileArray[2] == 'DIR')):364 continue365 366 # If file size is 0, remember it and check it later367 if ((len(fileArray) == 5) and (fileArray[2] != 'DIR')):368 fileSize = fileArray[3]369 controlFlag = fileArray[4]370 371 if ((len(fileArray) == 5) | (len(fileArray) == 2)):372 dFile = fileArray[1]373 374 if dFile.endswith(specifiedFile):375 downloadFlag = True376 break377 378 if downloadFlag == False:379 print ( _getmsg("downloadfailed_notfound") % specifiedFile )380 return381 382 # If has no control permission on job file, show error message383 if controlFlag == 'false':384 print ( _getmsg("downalodfile_errnopermission") % dFile )385 return386 387 if dFile[0] != '/':388 # For Windows path389 nList = dFile.split('\\')390 else:391 # For Linux path392 nList = dFile.split('/')393 394 fName= nList.pop()395 if fName[-1] == ' ':396 fName = fName[0:-1]397 if len(dstPath) > 0:398 if dstPath[-1] != getFileSeparator():399 dstName= dstPath + getFileSeparator() + fName400 else:401 dstName= dstPath + fName402 else:403 dstName= fName404 405 print ( _getmsg("downloading_file").format(fName,dstName) )406 407 # For 0 kb file, just create it408 if fileSize == '0':409 f=open(dstName,'wb')410 f.close()411 return412 413 # For other files, download it from server414 downloadFile(dFile, dstPath, jobId, cmd)415def uploadUtil(dstPath, dFile, jobId):416 """417 upload a file to server :dstPath.418 """419 if dstPath.strip()=='':420 dstPath = 'current job directory'421 nList = dFile.split(',')422 fName= nList423 for ff in fName:424 if len(ff) <= 0:425 break426 if dstPath.strip()=='': 427 print ( _getmsg("uploading_file_currentjobdir").format(ff) )428 else:429 print ( _getmsg("uploading_file_jobdir").format(ff,dstPath) )430 431def uploadLargeFile(jobId, dstPath, dFile):432 url, token = getToken();433 434 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)435 436 if ( (x509Flag == False) & (len(token) <= 0) ):437 print ( _getmsg("must_logon_pac") )438 return439 440 http = getHttp(url,x509Flag)441 if ( (x509Flag == True) & (len(token) <= 0) ):442 # X509Flag is True and token is empty, then add the key/cert files into http request.443 http.add_certificate(keypemfile, certpemfile, '')444 445 boundary='4k89ogja023oh1-gkdfk903jf9wngmujfs95m'446 print ( _getmsg("start_uploading") )447 uploadUtil(dstPath, dFile, jobId)448 url_upfile = url + 'webservice/pacclient/uplargefile/' + jobId449 fileSize = os.path.getsize(dFile)450 # 500Mb451 chunksize = 536870912452 453 # Calculate the chunkamount454 if fileSize % chunksize != 0:455 chunkamount = fileSize / chunksize + 1456 else:457 chunkamount = fileSize / chunksize458 459 chunkid = 0460 fileobj = open(dFile, 'rb')461 while fileSize > 0:462 chunkid += 1463 if (fileSize - chunksize) <= 0:464 chunksize = fileSize465 fileContent = fileobj.read(chunksize)466 fileSize -= chunksize467 try:468 status, body = encode_body_uplargefile(boundary, dstPath, fileContent, dFile, chunkid, chunkamount)469 except MemoryError:470 print ( _getmsg("memory_not_enough") )471 return472 headers = {'Content-Type': 'multipart/mixed; boundary=' + boundary,473 'Accept': 'text/plain;', 'Cookie': token,474 'Content-Length': str(len(body))}475 try:476 response, content = http.request(url_upfile, 'POST', body=body, headers=headers)477 # Upload failed or there are some issues for uploading478 if "successfully" not in content:479 print content480 return481 if response['status'] != '200':482 print ( _getmsg("uploadfailed_connectws") )483 return484 except AttributeError:485 print ( _getmsg("connect_ws_err") % url )486 return487 # Print the returned message 488 print content489def uploadJobFiles(jobId, dstPath, dFile):490 url, token = getToken();491 492 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)493 494 if ( (x509Flag == False) & (len(token) <= 0) ):495 print ( _getmsg("must_logon_pac") )496 return497 498 http = getHttp(url,x509Flag)499 if ( (x509Flag == True) & (len(token) <= 0) ):500 # X509Flag is True and token is empty, then add the key/cert files into http request.501 http.add_certificate(keypemfile, certpemfile, '')502 503 boundary='4k89ogja023oh1-gkdfk903jf9wngmujfs95m'504 try:505 status,body = encode_body_upfile(boundary, dstPath, dFile)506 except MemoryError:507 print ( _getmsg("notenoughmem_upload") )508 return509 510 if status == 'error':511 print body512 headers = {'Content-Type': 'multipart/mixed; boundary=' + boundary,513 'Accept': 'text/plain;', 'Cookie': token,514 'Content-Length': str(len(body)),515 'Accept-Language': getSysLocale().replace("_", "-").lower()}516 url_upfile = url + 'webservice/pacclient/upfile/' + jobId517 try:518 print ( _getmsg("start_uploading") )519 uploadUtil(dstPath, dFile, jobId)520 response, content = http.request(url_upfile, 'POST', body=body, headers=headers)521 if response['status'] == '200':522 print content523 else:524 print ( _getmsg("uploadfailed_connectws") )525 except AttributeError:526 print ( _getmsg("connect_ws_err") % url )527 528def jobdataList(jobId):529 url, token = getToken()530 531 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)532 533 if ( (x509Flag == False) & (len(token) <= 0) ):534 print ( _getmsg("must_logon_pac") )535 return536 537 http = getHttp(url,x509Flag)538 if ( (x509Flag == True) & (len(token) <= 0) ):539 # X509Flag is True and token is empty, then add the key/cert files into http request.540 http.add_certificate(keypemfile, certpemfile, '')541 542 url_jobfiles= url + 'webservice/pacclient/jobfiles/' + jobId543 headers = {'Content-Type': 'text/plain', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}544 try:545 response, content = http.request(url_jobfiles, 'GET', headers=headers)546 if ('\\' not in content) and ('/' not in content):547 print content548 return ''549 files = content.split(';')550 return files551 except AttributeError:552 print ( _getmsg("connect_ws_err") % url )553 554def logon(url, username, password):555 if url[len(url) -1 ] != '/':556 url += '/'557 558 559 # Check whether or not to use the X509 client authentication560 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)561 562 #Get the http connection563 http = getHttp(url,x509Flag)564 565 # Initial the variables566 url_logon= url + 'webservice/pacclient/logon/'567 headers = {}568 body = ''569 570 # If not X509 client authentication, use the normal way.571 # When set the username value, that is to say you want to use the normal authentication to logon.572 if ( (x509Flag == False) | (len(username) > 0) ):573 url_check, token = getToken()574 if ( (url_check != url) | (False == token.startswith("platform_token=" + username + "#quote#")) ):575 token = "platform_token="576 headers = {'Content-Type': 'application/xml', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}577 body='<User><name>%s</name> <pass>%s</pass> </User>' % (username, password)578 else:579 headers = {'Content-Type': 'application/xml', 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}580 body='<User></User>'581 # http object add the certificate for http request582 http.add_certificate(keypemfile, certpemfile, '')583 584 try:585 response, content = http.request(url_logon, 'GET', body=body, headers=headers)586 if response['status'] == '200':587 xdoc=minidom.parseString(content)588 tk=xdoc.getElementsByTagName("token")589 jtk=xdoc.getElementsByTagName("jtoken")590 591 if x509Flag == True:592 if len(username) <= 0:593 # For X509 client authentication594 username = xdoc.getElementsByTagName("name")595 if len(username) > 0:596 print ( _getmsg("logon_pac_as").format(username[0].childNodes[0].nodeValue) )597 saveToken(url, '', jtk)598 else:599 try:600 err=xdoc.getElementsByTagName("errMsg")601 print err[0].childNodes[0].nodeValue602 except IndexError:603 print ( _getmsg("connect_ws_err") % url )604 else:605 if len(tk) > 0:606 print ( _getmsg("logon_pac_as").format(username) )607 #print tk[0].childNodes[0].nodeValue608 saveToken(url, tk[0].childNodes[0].nodeValue,jtk)609 else:610 err=xdoc.getElementsByTagName("errMsg")611 print err[0].childNodes[0].nodeValue612 else:613 if len(tk) > 0:614 print ( _getmsg("logon_pac_as").format(username) )615 #print tk[0].childNodes[0].nodeValue616 saveToken(url, tk[0].childNodes[0].nodeValue,jtk)617 else:618 err=xdoc.getElementsByTagName("errMsg")619 print err[0].childNodes[0].nodeValue620 else:621 print ( _getmsg("failed_connect_wsurl") % url_logon )622 except (AttributeError, httplib2.ServerNotFoundError, httplib.InvalidURL, ExpatError):623 print ( _getmsg("connect_ws_err") % url )624def logout():625 url, token = getToken()626 627 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)628 629 if ( (x509Flag == False) & (len(token) <= 0) ):630 print ( _getmsg("must_logon_pac") )631 return632 633 http = getHttp(url,x509Flag)634 if ( (x509Flag == True) & (len(token) <= 0) ):635 # For X.509 client authentication, don't need to send HTTP request to logout636 # We can only tell user that you have logout successfully637 http.add_certificate(keypemfile, certpemfile, '')638 #print "you have logout successfully."639 640 url_logout= url + 'webservice/pacclient/logout/'641 headers = {'Content-Type': 'text/plain', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}642 try:643 response, content = http.request(url_logout, 'GET', headers=headers)644 if response['status'] == '200':645 if content == 'ok':646 if os.name == 'nt': 647 fpath=os.environ['HOMEPATH']648 if len(fpath) > 0:649 fpath = os.environ['HOMEDRIVE'] + fpath + '\\' +PACPASSFILE650 else:651 fpath += '\\' + PACPASSFILE652 else:653 fpath = os.environ['HOME'] + '/' + PACPASSFILE654 os.remove(fpath)655 print ( _getmsg("logout_success") )656 else:657 print content658 else:659 print ( _getmsg("failed_connect_wsurl") % url_logout )660 except AttributeError:661 print ( _getmsg("connect_ws_err") % url )662 663def getJobListInfo(parameter):664 url, token = getToken()665 666 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)667 668 if ( (x509Flag == False) & (len(token) <= 0) ):669 return 'error', _getmsg("must_logon_pac")670 671 http = getHttp(url,x509Flag)672 if ( (x509Flag == True) & (len(token) <= 0) ):673 # X509Flag is True and token is empty, then add the key/cert files into http request.674 http.add_certificate(keypemfile, certpemfile, '')675 676 url_job = url + 'webservice/pacclient/jobs?' + parameter677 headers = {'Content-Type': 'application/xml', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}678 try:679 response, content = http.request(url_job, 'GET', headers=headers)680 if response['status'] == '200':681 xdoc=ET.fromstring(content)682 if ERROR_TAG in content:683 tree=xdoc.getiterator("Jobs")684 err=tree[0].find(ERROR_STR)685 return 'error', err.text686 elif NOTE_STR in content:687 tree=xdoc.getiterator("Jobs")688 err=tree[0].find(NOTE_STR)689 return 'error', err.text690 else:691 return 'ok', content692 else:693 return 'error', _getmsg("failed_connect_wsurl") % url_job694 except (AttributeError, ExpatError):695 return 'error', _getmsg("connect_ws_err") % url 696def getJobInfo(jobId):697 url, token = getToken()698 699 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)700 701 if ( (x509Flag == False) & (len(token) <= 0) ):702 return 'error', _getmsg("must_logon_pac")703 704 http = getHttp(url,x509Flag)705 if ( (x509Flag == True) & (len(token) <= 0) ):706 # X509Flag is True and token is empty, then add the key/cert files into http request.707 http.add_certificate(keypemfile, certpemfile, '')708 709 url_job = url + 'webservice/pacclient/jobs/' + jobId710 headers = {'Content-Type': 'application/xml', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}711 try:712 response, content = http.request(url_job, 'GET', headers=headers)713 if response['status'] == '200':714 xdoc=minidom.parseString(content)715 if ERROR_TAG in content:716 err=xdoc.getElementsByTagName(ERROR_STR)717 return 'error', err[0].childNodes[0].nodeValue718 else:719 return 'ok', content720 else:721 return 'error', _getmsg("failed_connect_wsurl") % url_job722 except (AttributeError, ExpatError):723 return 'error', _getmsg("connect_ws_err") % url724def getJobForStatus(jobStatus):725 url, token = getToken()726 727 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)728 729 if ( (x509Flag == False) & (len(token) <= 0) ):730 return 'error', _getmsg("must_logon_pac")731 732 http = getHttp(url,x509Flag)733 if ( (x509Flag == True) & (len(token) <= 0) ):734 # X509Flag is True and token is empty, then add the key/cert files into http request.735 http.add_certificate(keypemfile, certpemfile, '')736 737 url_job = url + 'webservice/pacclient/jobsforstatus/' + jobStatus738 headers = {'Content-Type': 'application/xml', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}739 try:740 response, content = http.request(url_job, 'GET', headers=headers)741 if response['status'] == '200':742 xdoc=minidom.parseString(content)743 if ERROR_TAG in content:744 err=xdoc.getElementsByTagName(ERROR_STR)745 return 'error', err[0].childNodes[0].nodeValue746 else:747 return 'ok', content748 else:749 return 'error', _getmsg("failed_connect_wsurl") % url_job750 except (AttributeError, ExpatError):751 return 'error', _getmsg("connect_ws_err") % url752 753def getJobForName(jobName):754 url, token = getToken()755 756 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)757 758 if ( (x509Flag == False) & (len(token) <= 0) ):759 return 'error', _getmsg("must_logon_pac")760 761 http = getHttp(url,x509Flag)762 if ( (x509Flag == True) & (len(token) <= 0) ):763 # X509Flag is True and token is empty, then add the key/cert files into http request.764 http.add_certificate(keypemfile, certpemfile, '')765 766 url_job = url + 'webservice/pacclient/jobsforname/' + jobName767 headers = {'Content-Type': 'application/xml', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}768 try:769 response, content = http.request(url_job, 'GET', headers=headers)770 if response['status'] == '200':771 xdoc=minidom.parseString(content)772 if ERROR_TAG in content:773 err=xdoc.getElementsByTagName(ERROR_STR)774 return 'error', err[0].childNodes[0].nodeValue775 else:776 return 'ok', content777 else:778 return 'error', _getmsg("failed_connect_wsurl") % url_job779 except (AttributeError, ExpatError):780 return 'error', _getmsg("connect_ws_err") % url781def saveToken(url, token,jtoken):782 #if len(token) <= 0:783 # return784 if len(jtoken) > 0:785 token = token + ",JSESSIONID=" + jtoken[0].childNodes[0].nodeValue786 if os.name == 'nt': 787 fpath=os.environ['HOMEPATH']788 if len(fpath) > 0:789 fpath = os.environ['HOMEDRIVE'] + fpath + '\\' +PACPASSFILE790 else:791 fpath += '\\' + PACPASSFILE792 else:793 fpath = os.environ['HOME'] + '/' + PACPASSFILE794 try:795 ff= open(fpath, "wb")796 except IOError as e:797 print ( _getmsg("cannot_openfile").format(fpath,e.strerror) )798 else:799 ff.write(url)800 ff.write('\n')801 ff.write(token)802 ff.close803def getToken():804 token=''805 url=''806 if os.name == 'nt': 807 fpath=os.environ['HOMEPATH']808 if len(fpath) > 0:809 fpath = os.environ['HOMEDRIVE'] + fpath + '\\' +PACPASSFILE810 else:811 fpath += '\\' + PACPASSFILE812 else:813 fpath = os.environ['HOME'] + '/' + PACPASSFILE814 try:815 ff= open(fpath, "rb") 816 except IOError:817 return url,token818 else:819 url_token=ff.read().split('\n')820 ff.close()821 url=url_token[0]822 token=url_token[1].replace('"', '#quote#')823 if len(token) <= 0:824 return url, token825 else:826 return url, 'platform_token='+token827def getFileSeparator():828 if os.name == 'nt':829 return '\\'830 else:831 return '/'832def submitJob(jobDict):833 url, token = getToken()834 835 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)836 837 if ( (x509Flag == False) & (len(token) <= 0) ):838 return 'error', _getmsg("must_logon_pac")839 840 http = getHttp(url,x509Flag)841 if ( (x509Flag == True) & (len(token) <= 0) ):842 # X509Flag is True and token is empty, then add the key/cert files into http request.843 http.add_certificate(keypemfile, certpemfile, '')844 845 if len(jobDict) == 0:846 return 'error', _getmsg("file_nojob_param")847 if APP_NAME not in jobDict.keys():848 return 'error', _getmsg("published_app_notfound") % APP_NAME849 850 boundary='bqJky99mlBWa-ZuqjC53mG6EzbmlxB'851 if 'PARAMS' in jobDict.keys():852 job_params=jobDict['PARAMS']853 else:854 job_params={}855 856 if 'INPUT_FILES' in jobDict.keys():857 job_files=jobDict['INPUT_FILES']858 else:859 job_files={}860 body = encode_body(boundary, jobDict[APP_NAME], job_params, job_files)861 if body == None:862 return 'error', _getmsg("wrong_inputfile_param")863 if "Submit job failed" in body:864 return 'error', body865 headers = {'Content-Type': 'multipart/mixed; boundary='+boundary,866 'Accept': 'text/xml,application/xml;', 'Cookie': token,867 'Content-Length': str(len(body)), 'Accept-Language': getSysLocale().replace("_", "-").lower()}868 url_submit = url + 'webservice/pacclient/submitapp'869 try:870 response, content = http.request(url_submit, 'POST', body=body, headers=headers)871 if response['status'] == '200':872 xdoc=minidom.parseString(content)873 if ERROR_TAG not in content:874 jobIdTag=xdoc.getElementsByTagName("id")875 return 'ok', jobIdTag[0].childNodes[0].nodeValue876 else:877 err=xdoc.getElementsByTagName(ERROR_STR)878 return 'error', err[0].childNodes[0].nodeValue879 else:880 return 'error', _getmsg("failed_connws_submit")881 except (AttributeError, ExpatError):882 return 'error', _getmsg("connect_ws_err") % url883 884def doJobAction(jobAction, jobId):885 url, token = getToken()886 887 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)888 889 if ( (x509Flag == False) & (len(token) <= 0) ):890 return 'error', _getmsg("must_logon_pac")891 892 http = getHttp(url,x509Flag)893 if ( (x509Flag == True) & (len(token) <= 0) ):894 # X509Flag is True and token is empty, then add the key/cert files into http request.895 http.add_certificate(keypemfile, certpemfile, '')896 897 url_jobaction = url + 'webservice/pacclient/jobOperation/' + jobAction +'/' + jobId898 headers = {'Content-Type': 'text/plain', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}899 try:900 response, content = http.request(url_jobaction, 'GET', headers=headers)901 if response['status'] == '200':902 xdoc = minidom.parseString(content)903 if ERROR_TAG in content:904 err = xdoc.getElementsByTagName(ERROR_STR)905 return 'error', err[0].childNodes[0].nodeValue906 elif ACTION_TAG in content:907 action = xdoc.getElementsByTagName(ACTION_STR)908 return 'ok', action[0].childNodes[0].nodeValue909 else:910 return 'error', _getmsg("failed_connws_logon")911 else:912 return 'error', _getmsg("failed_connect_wsurl") % url_jobaction913 except (AttributeError, ExpatError):914 return 'error', _getmsg("ws_notready_url") % url_jobaction915def doUserCmd(userCmd):916 url, token = getToken()917 918 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)919 920 if ( (x509Flag == False) & (len(token) <= 0) ):921 return 'error', _getmsg("must_logon_pac")922 923 http = getHttp(url,x509Flag)924 if ( (x509Flag == True) & (len(token) <= 0) ):925 # X509Flag is True and token is empty, then add the key/cert files into http request.926 http.add_certificate(keypemfile, certpemfile, '')927 928 body = '<UserCmd><cmd>%s</cmd></UserCmd>' % (userCmd)929 headers = {'Content-Type': 'application/xml',930 'Accept': 'text/xml;', 'Cookie': token,931 'Content-Length': str(len(body)),932 'Accept-Language': getSysLocale().replace("_", "-").lower()}933 934 url_usercmd = url + 'webservice/pacclient/userCmd'935 try:936 response, content = http.request(url_usercmd, 'POST', body=body, headers=headers)937 if response['status'] == '200':938 xdoc = minidom.parseString(content)939 msg = ''940 if ERROR_TAG in content:941 err = xdoc.getElementsByTagName(ERROR_STR)942 if ((err.length > 0) and (err[0].childNodes.length > 0)):943 msg = err[0].childNodes[0].nodeValue944 return 'error', msg945 elif CMD_STR in content:946 cmd = xdoc.getElementsByTagName(CMD_STR)947 if ((cmd.length > 0) and (cmd[0].childNodes.length > 0)):948 msg = cmd[0].childNodes[0].nodeValue949 return 'ok', msg950 else:951 return 'error', _getmsg("failed_connws_logon")952 else:953 return 'error', _getmsg("failed_connect_wsurl") % url_usercmd 954 except (AttributeError, ExpatError):955 return 'error', _getmsg("ws_notready_url") % url_usercmd956 957def ping(url):958 if url[len(url) -1 ] != '/':959 url += '/'960 961 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)962 963 http = getHttp(url,x509Flag)964 if x509Flag == True:965 # X509Flag is True and token is empty, then add the key/cert files into http request.966 http.add_certificate(keypemfile, certpemfile, '')967 968 url_ping = url + 'webservice/pacclient/ping/'969 body = url970 headers = {'Content-Type': 'text/plain', 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}971 try:972 response, content = http.request(url_ping, 'GET', body=body, headers=headers)973 if response['status'] == '200':974 print content975 else:976 print ( _getmsg("ws_notready_url") % url_ping )977 except (AttributeError, httplib2.ServerNotFoundError, httplib.InvalidURL):978 print ( _getmsg("connect_ws_err") % url )979 980def removeQuote(str):981 """982 Remove the single or double quote. for example: 'abc' --> abc983 """984 while len(str) > 2:985 if ((str[0] == '"') & (str[-1] == '"')):986 str = str[1:-1]987 elif ((str[0] == "'") & (str[-1] == "'")):988 str = str[1:-1]989 else:990 break991 return str992def getAllAppStatus():993 url, token = getToken()994 995 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)996 997 if ( (x509Flag == False) & (len(token) <= 0) ):998 return 'error', _getmsg("must_logon_pac")999 1000 http = getHttp(url,x509Flag)1001 if ( (x509Flag == True) & (len(token) <= 0) ):1002 # X509Flag is True and token is empty, then add the key/cert files into http request.1003 http.add_certificate(keypemfile, certpemfile, '')1004 1005 url_app = url + 'webservice/pacclient/appStatus'1006 headers = {'Content-Type': 'text/plain', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}1007 try:1008 response, content = http.request(url_app, 'GET', headers=headers)1009 if response['status'] == '200':1010 xdoc = minidom.parseString(content)1011 if ERROR_TAG in content:1012 err=xdoc.getElementsByTagName(ERROR_STR)1013 return 'error', err[0].childNodes[0].nodeValue1014 else:1015 return 'ok', content1016 else:1017 return 'error', _getmsg("failed_connect_wsurl") % url_app1018 except (AttributeError, ExpatError):1019 return 'error', _getmsg("connect_ws_err") % url1020 1021def getAppParameter(appName):1022 url, token = getToken()1023 1024 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)1025 1026 if ( (x509Flag == False) & (len(token) <= 0) ):1027 return 'error', _getmsg("must_logon_pac")1028 1029 http = getHttp(url,x509Flag)1030 if ( (x509Flag == True) & (len(token) <= 0) ):1031 # X509Flag is True and token is empty, then add the key/cert files into http request.1032 http.add_certificate(keypemfile, certpemfile, '')1033 1034 url_app = url + 'webservice/pacclient/appParams'1035 body = appName1036 headers = {'Content-Type': 'text/plain', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}1037 try:1038 response, content = http.request(url_app, 'GET', body = body, headers=headers)1039 if response['status'] == '200':1040 xdoc = minidom.parseString(content)1041 if ERROR_TAG in content:1042 err=xdoc.getElementsByTagName(ERROR_STR)1043 return 'error', err[0].childNodes[0].nodeValue1044 else:1045 return 'ok', content1046 else:1047 return 'error', _getmsg("failed_connect_wsurl") % url_app1048 except (AttributeError, ExpatError):1049 return 'error', _getmsg("connect_ws_err") % url1050# Get product information function1051def getProductInfo():1052 # Get url1053 url, token = getToken()1054 1055 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)1056 1057 if ( (x509Flag == False) & (len(token) <= 0) ):1058 return 'error', _getmsg("must_logon_pac")1059 1060 # Init the http object1061 http = getHttp(url,x509Flag)1062 if ( (x509Flag == True) & (len(token) <= 0) ):1063 # X509Flag is True and token is empty, then add the key/cert files into http request.1064 http.add_certificate(keypemfile, certpemfile, '')1065 1066 # Arrange the URI1067 url_app = url + 'ws/version'1068 # Arrange the http request headers1069 headers = {'Content-Type': 'text/plain', 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}1070 try:1071 # Send the request with GET method1072 response, content = http.request(url_app, 'GET', headers=headers)1073 if response['status'] == '200':1074 # Response status code is 200: OK1075 if 'simple_error.jsp' in content:1076 return 'error', _getmsg("internal_server_error")1077 return 'ok', content1078 elif response['status'] == '500':1079 # Response status code is 500: Interal Server Error1080 return 'error', _getmsg("internal_server_error")1081 elif response['status'] == '404':1082 # Response status code is 404: No service was found.1083 return 'error', _getmsg("no_service_found")1084 else:1085 # In general, give a common error message for other status codes.1086 return 'error', _getmsg("failed_connect_wsurl") % url_app1087 except (AttributeError):1088 # Parameter exception when send request.1089 return 'error', _getmsg("connect_ws_err") % url1090def encode_body(boundary, appName, params, inputFiles):1091 slash = getFileSeparator()1092 boundary2='_Part_1_701508.1145579811786'1093 def encode_appname():1094 return ('--' + boundary,1095 'Content-Disposition: form-data; name="AppName"',1096 'Content-ID: <AppName>',1097 '', appName)1098 def encode_paramshead():1099 return('--' + boundary,1100 'Content-Disposition: form-data; name="data"',1101 'Content-Type: multipart/mixed; boundary='+ boundary2,1102 'Accept-Language:' + getSysLocale().replace("_", "-").lower(),1103 'Content-ID: <data>', '')1104 def encode_param(param_name):1105 return('--' + boundary2,1106 'Content-Disposition: form-data; name="%s"' % param_name,1107 'Content-Type: application/xml; charset=UTF-8',1108 'Content-Transfer-Encoding: 8bit',1109 'Accept-Language:' + getSysLocale().replace("_", "-").lower(),1110 '', '<AppParam><id>%s</id><value>%s</value><type></type></AppParam>' %(param_name, params[param_name]))1111 def encode_fileparam(param_name, param_value):1112 return('--' + boundary2,1113 'Content-Disposition: form-data; name="%s"' % param_name,1114 'Content-Type: application/xml; charset=UTF-8',1115 'Content-Transfer-Encoding: 8bit',1116 'Accept-Language:' + getSysLocale().replace("_", "-").lower(),1117 '', '<AppParam><id>%s</id><value>%s</value><type>file</type></AppParam>' %(param_name, param_value))1118 def encode_file(filepath, filename):1119 return('--' + boundary,1120 'Content-Disposition: form-data; name="%s"; filename="%s"' %(filename, filename),1121 'Content-Type: application/octet-stream',1122 'Content-Transfer-Encoding: UTF-8',1123 'Accept-Language:' + getSysLocale().replace("_", "-").lower(),1124 'Content-ID: <%s>' % urllib2.quote(filename),1125 '', open(filepath, 'rb').read ())1126 lines = []1127 upType = ''1128 upFlag = False1129 lines.extend(encode_appname())1130 lines.extend(encode_paramshead())1131 for name in params:1132 lines.extend (encode_param(name))1133 for name in inputFiles:1134 value=inputFiles[name]1135 valueStr=inputFiles[name]1136 # Specify multiple files1137 valueArray = valueStr.split('#')1138 filepathList = ''1139 for value in valueArray:1140 if ',' in value:1141 try:1142 upType = value.split(',')[1]1143 if (upType == 'link') | (upType == 'copy'):1144 # lines.extend (encode_fileparam(name, value))1145 if upType == 'copy':1146 print ( _getmsg("copying_serverfile") % value.split(',')[0] )1147 else:1148 upFlag = True1149 value = value.replace('\\', '/').split('/').pop() 1150 #lines.extend (encode_fileparam(name, filename))1151 1152 if filepathList == '':1153 filepathList = value1154 else:1155 filepathList += ';' + value1156 1157 except IndexError:1158 return1159 else:1160 return1161 lines.extend (encode_fileparam(name, filepathList))1162 1163 lines.extend (('--%s--' % boundary2, ''))1164 if upFlag == True:1165 for name in inputFiles:1166 valueStr=inputFiles[name]1167 # Specify multiple files1168 valueArray = valueStr.split('#')1169 for value in valueArray:1170 if ',' in value:1171 upType = value.split(',')[1]1172 filepath = value.split(',')[0]1173 if upType == 'upload':1174 filename = filepath.replace('\\', '/').split('/').pop()1175 try:1176 lines.extend(encode_file(filepath, filename))1177 except IOError:1178 return _getmsg("submitjob_failed_filedirnotfound") % filepath1179 print ( _getmsg("uploading_inputfile") % filepath )1180 lines.extend (('--%s--' % boundary, ''))1181 return '\r\n'.join (lines)1182def encode_body_uplargefile(boundary, dir, fileContent, file, chunkid, chunkamount):1183 slash = getFileSeparator()1184 dir = urllib2.quote(dir)1185 def encode_dir():1186 return ('--' + boundary,1187 'Content-Disposition: form-data; name="DirName"',1188 'Content-ID: <DirName>',1189 '', dir)1190 def encode_chunkid():1191 return ('--' + boundary,1192 'Content-Disposition: form-data; name="chunkid"',1193 'Content-ID: <chunkid>',1194 '', str(chunkid))1195 def encode_chunkbase(filename):1196 return ('--' + boundary,1197 'Content-Disposition: form-data; name="chunkbase"',1198 'Content-ID: <chunkbase>',1199 '', filename)1200 def encode_chunkmount():1201 return ('--' + boundary,1202 'Content-Disposition: form-data; name="chunkamount"',1203 'Content-ID: <chunkamount>',1204 '', str(chunkamount))1205 def encode_file(filename):1206 return('--' + boundary,1207 'Content-Disposition: form-data; name="%s"; filename="%s"' %(filename, filename),1208 'Content-Type: application/octet-stream',1209 'Content-Transfer-Encoding: UTF-8',1210 'Content-ID: <%s>' % filename,1211 '', fileContent)1212 lines = []1213 lines.extend(encode_dir())1214 lines.extend(encode_chunkid())1215 lines.extend(encode_chunkmount())1216 filename = file.replace('\\', '/').split('/').pop()1217 filename = urllib2.quote(filename)1218 lines.extend(encode_chunkbase(filename))1219 chunkFilename = ".%s.%d" %(filename, chunkid)1220 lines.extend(encode_file(chunkFilename))1221 lines.extend (('--%s--' % boundary, ''))1222 return 'ok','\r\n'.join (lines) 1223def encode_body_upfile(boundary, dir, filelist):1224 slash = getFileSeparator()1225 def encode_dir():1226 return ('--' + boundary,1227 'Content-Disposition: form-data; name="DirName"',1228 'Content-ID: <DirName>',1229 '', urllib2.quote(dir))1230 def encode_file(filepath, filename):1231 return('--' + boundary,1232 'Content-Disposition: form-data; name="%s"; filename="%s"' %(filename, filename),1233 'Content-Type: application/octet-stream',1234 'Content-Transfer-Encoding: UTF-8',1235 'Accept-Language:' + getSysLocale().replace("_", "-").lower(),1236 'Content-ID: <%s>' % urllib2.quote(filename),1237 '', open(filepath, 'rb').read ())1238 lines = []1239 lines.extend(encode_dir())1240 files = filelist.split(',')1241 for f in files:1242 filename = f.replace('\\', '/').split('/').pop()1243 try:1244 lines.extend(encode_file(f, filename))1245 except IOError:1246 return 'error', _getmsg("failed_to_readfile") % f1247 lines.extend (('--%s--' % boundary, ''))1248 return 'ok','\r\n'.join (lines)1249# Check the X509 PEM key/cert files whether or not exist.1250# Return three value: X509Cert flag, keypemfile path, certpemfile path1251# If X509Cert flag is False, the other values are empty string1252def checkX509PEMCert(url):1253 # If url is invalid for https, won't check cert.1254 if ( (len(url) == 0) | ('https' not in url.lower()) ):1255 return False, '', ''1256 1257 # Get the current path for key/cert files1258 cwdPath = os.getcwd() + getFileSeparator();1259 1260 # Create the variables of key/cert files absolute path1261 keypemfile = cwdPath + '.key.pem'1262 certpemfile = cwdPath + '.cert.pem'1263 1264 # Check .key.pem and .cer.pem whether or not exist1265 if ( ( os.path.isfile(keypemfile) == True ) & ( os.path.isfile(certpemfile) == True ) ):1266 return True, keypemfile, certpemfile1267 else:1268 return False, '', ''1269 1270#Get HTTP1271def getHttp(url,x509Flag):1272 1273 if ( x509Flag == True ):1274 return httplib2.Http()1275 #Check whether or not https is enabled1276 sslHandshakeRequiredFlag, pemFile = checkSSLHandshakeRequired(url)1277 1278 #If the file exists and SSL Handshake is required(httplib2 version is 0.7+)1279 if ( (len(pemFile) > 0) & (sslHandshakeRequiredFlag == True) ):1280 http = httplib2.Http(ca_certs=pemFile)1281 else:1282 http = httplib2.Http()1283 return http1284# Return two values: HTTPS flag, pemFile path1285# If HTTPS flag is False, the other value is an empty string1286def checkSSLHandshakeRequired(url):1287 1288 httpsInURL = False;1289 if ( (len(url) != 0) & ('https' in url.lower()) ):1290 httpsInURL = True;1291 1292 #Check if httplib version is 7 or above1293 if httplib2.__version__ >= '0.7.0':1294 newVersion = True1295 else:1296 newVersion = False1297 1298 # Create if the cacert.pem file exists under the current directory1299 if httpsInURL == True:1300 pemFile = 'cacert.pem' 1301 if ( (newVersion == True) & ( os.path.isfile(pemFile) == True ) ):1302 return True, pemFile1303 else:1304 if ( newVersion == True ):1305 print ( _getmsg("https_certificate_missing") )1306 exit();1307 return False, ''1308def addUser(username, email, roles):1309 url, token = getToken()1310 1311 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)1312 1313 if ( (x509Flag == False) & (len(token) <= 0) ):1314 return 'error', _getmsg("must_logon_pac")1315 1316 http = getHttp(url,x509Flag)1317 if ( (x509Flag == True) & (len(token) <= 0) ):1318 # X509Flag is True and token is empty, then add the key/cert files into http request.1319 http.add_certificate(keypemfile, certpemfile, '')1320 1321 url_useradd = url + 'ws/users'1322 #constract xml as the request body1323 body = '<user name="%username" email="%email">'.replace('%username',username).replace('%email',email)1324 1325 if len(roles) > 0:1326 body += '<roles>'1327 role = '<role name="%role" />'1328 1329 if roles.find(',') < 0:1330 body += (role.replace('%role', roles.lstrip().rstrip()) + '</roles>')1331 else:1332 for x in roles.split(','):1333 if len(x) > 0:1334 body += role.replace('%role', x.lstrip().rstrip())1335 body += '</roles>'1336 1337 body += '</user>'1338 1339 headers = {'Content-Type': 'application/xml', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}1340 try:1341 response, content = http.request(url_useradd, 'POST', body = body, headers=headers)1342 1343 if response['status'] == '204':1344 return 'ok', ''1345 elif response['status'] == '200':1346 return 'ok', content1347 elif response['status'] == '403':1348 return 'ok', content1349 else:1350 return 'error', _getmsg("failed_connect_wsurl") % url_useradd1351 except (AttributeError, ExpatError):1352 return 'error', _getmsg("connect_ws_err") % url 1353 1354 1355def removeUser(username):1356 url, token = getToken()1357 1358 x509Flag, keypemfile, certpemfile = checkX509PEMCert(url)1359 1360 if ( (x509Flag == False) & (len(token) <= 0) ):1361 return 'error', _getmsg("must_logon_pac")1362 1363 http = getHttp(url,x509Flag)1364 if ( (x509Flag == True) & (len(token) <= 0) ):1365 # X509Flag is True and token is empty, then add the key/cert files into http request.1366 http.add_certificate(keypemfile, certpemfile, '')1367 1368 url_userdel = url + 'ws/users/' + username1369 1370 headers = {'Content-Type': 'application/xml', 'Cookie': token, 'Accept': ACCEPT_TYPE, 'Accept-Language': getSysLocale().replace("_", "-").lower()}1371 try:1372 response, content = http.request(url_userdel, 'DELETE', headers=headers)1373 1374 if response['status'] == '204':1375 return 'ok', ''1376 elif response['status'] == '200':1377 return 'ok', content1378 elif response['status'] == '403':1379 return 'ok', content1380 else:1381 return 'error', _getmsg("failed_connect_wsurl") % url_userdel1382 except (AttributeError, ExpatError):1383 return 'error', _getmsg("connect_ws_err") % url 1384 1385def main(argv):1386 """1387 url=raw_input("URL:")1388 if len(url) == 0:1389 print ( _getmsg("wrong_url_format") )1390 return1391 u=raw_input("Username:")1392 1393 if len(u) == 0:1394 print ( _getmsg("empty_username_err") )1395 return1396 p=getpass.getpass()1397 if len(p) == 0:1398 print ( _getmsg("password_cannot_empty") )1399 return1400 logon(url, u, p)1401 """1402 getJobInfo('356')1403 1404 downloadJobFiles('353', 'c:\\webservice\\350' )1405 """1406 JobDict={}1407 1408 JobDict[APP_NAME]='FLUENT:FLUENT_WEB' #format: "app_type:app_name"1409 JobDict['PARAMS']= {'JOB_NAME': 'FF_20100329',1410 'RELEASE': '6.3.26',1411 'CONSOLE_SUPPORT': 'No'}1412 JobDict['INPUT_FILES']={ 'FLUENT_JOURNAL':'C:\\portal_demo\\fluent\\fluent-test.jou,upload',1413 'CAS_INPUT_FILE':'C:\\portal_demo\\fluent\\fluent-test.cas.gz,upload'}1414 JobDict['RETURN_FILES']={ 'TARGET_DIR':'C:\jobResults', 'FILES':['*.zip', '*.txt']}1415 status, message = submitJob(JobDict)1416 print 'status =' + status1417 print 'message =' + message1418 """1419 1420if __name__ == "__main__":...

Full Screen

Full Screen

panelBackup.py

Source:panelBackup.py Github

copy

Full Screen

...48 self.cron_info = self.get_cron_info(cron_info["echo"])49 self._path = public.M('config').where("id=?",(1,)).getField('backup_path')50 def echo_start(self):51 print("="*90)52 print("★"+public.getMsg('START_BACKUP')+"[{}]".format(public.format_date()))53 print("="*90)54 def echo_end(self):55 print("="*90)56 print("☆"+public.getMsg('BACKUP_COMPLETED')+"[{}]".format(public.format_date()))57 print("="*90)58 print("\n")59 def echo_info(self,msg):60 print("|-{}".format(msg))61 def echo_error(self,msg):62 print("=" * 90)63 print("|-Error:{}".format(msg))64 if self._error_msg:65 self._error_msg += "\n"66 self._error_msg += msg67 #构造排除68 def get_exclude(self,exclude = []):69 if not exclude:70 tmp_exclude = os.getenv('BT_EXCLUDE')71 if tmp_exclude:72 exclude = tmp_exclude.split(',')73 if not exclude: return ""74 for ex in exclude:75 self._exclude += " --exclude=\"" + ex + "\""76 self._exclude += " "77 return self._exclude78 def GetDiskInfo2(self):79 #取磁盘分区信息80 temp = public.ExecShell("df -T -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev")[0]81 tempInodes = public.ExecShell("df -i -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev")[0]82 temp1 = temp.split('\n')83 tempInodes1 = tempInodes.split('\n')84 diskInfo = []85 n = 086 cuts = []87 for tmp in temp1:88 n += 189 try:90 inodes = tempInodes1[n-1].split()91 disk = re.findall(r"^(.+)\s+([\w\.]+)\s+([\w\.]+)\s+([\w\.]+)\s+([\w\.]+)\s+([\d%]{2,4})\s+(/.{0,50})$",tmp.strip())92 if disk: disk = disk[0]93 if len(disk) < 6: continue94 if disk[2].find('M') != -1: continue95 if disk[2].find('K') != -1: continue96 if len(disk[6].split('/')) > 10: continue97 if disk[6] in cuts: continue98 if disk[6].find('docker') != -1: continue99 if disk[1].strip() in ['tmpfs']: continue100 arr = {}101 arr['filesystem'] = disk[0].strip()102 arr['type'] = disk[1].strip()103 arr['path'] = disk[6]104 tmp1 = [disk[2],disk[3],disk[4],disk[5]]105 arr['size'] = tmp1106 if int(inodes[1]) == 0 and int(inodes[2]) == 0:107 arr['inodes'] = [inodes[1],10000,10000,0]108 else:109 arr['inodes'] = [inodes[1],inodes[2],inodes[3],inodes[4]]110 diskInfo.append(arr)111 except:112 continue113 return diskInfo114 #取磁盘可用空间115 def get_disk_free(self,dfile):116 diskInfo = self.GetDiskInfo2()117 if not diskInfo: return '',0,0118 _root = None119 for d in diskInfo:120 if d['path'] == '/': 121 _root = d122 continue123 if re.match("^{}/.+".format(d['path']),dfile):124 return d['path'],float(d['size'][2]) * 1024,int(d['inodes'][2])125 if _root:126 return _root['path'],float(_root['size'][2]) * 1024,int(_root['inodes'][2])127 return '',0,0128 #备份指定目录 129 def backup_path(self,spath,dfile = None,exclude=[],save=3):130 error_msg = ""131 self.echo_start()132 if not os.path.exists(spath):133 error_msg= public.getMsg('BACKUP_DIR_NOT_EXIST',(spath,))134 self.echo_error(error_msg)135 self.send_failture_notification(error_msg)136 return False137 if spath[-1] == '/':138 spath = spath[:-1]139 dirname = os.path.basename(spath)140 if not dfile:141 fname = 'path_{}_{}.tar.gz'.format(dirname,public.format_date("%Y%m%d_%H%M%S"))142 dfile = os.path.join(self._path,'path',fname)143 144 if not self.backup_path_to(spath,dfile,exclude):145 if self._error_msg:146 error_msg = self._error_msg147 self.send_failture_notification(error_msg)148 return False149 if self._cloud:150 self.echo_info(public.getMsg("BACKUP_UPLOADING",(self._cloud._title,)))151 if self._cloud.upload_file(dfile,'path'):152 self.echo_info(public.getMsg("BACKUP_UPLOAD_SUCCESS",(self._cloud._title,)))153 else:154 if hasattr(self._cloud, "error_msg"):155 if self._cloud.error_msg:156 error_msg = self._cloud.error_msg157 if not error_msg:158 error_msg = public.getMsg('BACKUP_UPLOAD_FAILED')159 self.echo_error(error_msg)160 if os.path.exists(dfile):161 os.remove(dfile)162 remark = "Backup to " + self._cloud._title163 self.send_failture_notification(error_msg, remark=remark)164 return False165 filename = dfile166 if self._cloud:167 filename = dfile + '|' + self._cloud._name + '|' + fname168 pdata = {169 'type': '2',170 'name': spath,171 'pid': 0,172 'filename': filename,173 'addtime': public.format_date(),174 'size': os.path.getsize(dfile)175 }176 public.M('backup').insert(pdata)177 if self._cloud:178 _not_save_local = True179 save_local = 0180 if self.cron_info:181 save_local = self.cron_info["save_local"]182 if save_local:183 _not_save_local = False184 else:185 if self._is_save_local:186 _not_save_local = False187 if _not_save_local:188 if os.path.exists(dfile):189 os.remove(dfile)190 self.echo_info(public.getMsg("BACKUP_DEL",(dfile,)))191 else:192 self.echo_info(public.getMsg('KEEP_LOCAL'))193 if not self._cloud:194 backups = public.M('backup').where("type=? and pid=? and name=? and filename NOT LIKE '%|%'",('2',0,spath)).field('id,name,filename').select()195 else:196 backups = public.M('backup').where("type=? and pid=? and name=? and filename LIKE '%{}%'".format(self._cloud._name),('2',0,spath)).field('id,name,filename').select()197 self.delete_old(backups,save,'path')198 self.echo_end()199 return dfile200 201 #清理过期备份文件202 def delete_old(self,backups,save,data_type = None):203 if type(backups) == str:204 self.echo_info(public.getMsg('BACKUP_CLEAN_ERR',(backups,)))205 return206 self.echo_info(public.getMsg('BACKUP_KEEP',(str(save),)))207 num = len(backups) - int(save)208 if num > 0:209 self._get_local_backdir()210 self.echo_info('-' * 88)211 for backup in backups:212 #处理目录备份到远程的情况213 if backup['filename'].find('|') != -1:214 tmp = backup['filename'].split('|')215 backup['filename'] = tmp[0]216 backup['name'] = tmp[-1]217 #尝试删除本地文件218 if os.path.exists(backup['filename']):219 try:220 os.remove(self._local_backdir + '/'+ data_type +'/' + backup['name'])221 except:222 pass223 self.echo_info(public.getMsg("BACKUP_CLEAN",(backup['filename'],)))224 #尝试删除远程文件225 if self._cloud:226 self._cloud.delete_file(backup['name'],data_type)227 self.echo_info(public.getMsg("BACKUP_CLEAN_REMOVE",(self._cloud._title,backup['name'])))228 #从数据库清理229 public.M('backup').where('id=?',(backup['id'],)).delete()230 num -= 1231 if num < 1: break232 # 获取本地备份目录233 def _get_local_backdir(self):234 self._local_backdir = public.M('config').field('backup_path').find()['backup_path']235 #压缩目录236 def backup_path_to(self,spath,dfile,exclude = [],siteName = None):237 if not os.path.exists(spath):238 self.echo_error(public.getMsg('BACKUP_DIR_NOT_EXIST',(spath,)))239 return False240 if spath[-1] == '/':241 spath = spath[:-1]242 dirname = os.path.basename(spath)243 dpath = os.path.dirname(dfile)244 if not os.path.exists(dpath):245 os.makedirs(dpath,384)246 247 p_size = public.get_path_size(spath)248 self.get_exclude(exclude)249 exclude_config = self._exclude250 if not self._exclude:251 exclude_config = "Not set"252 253 if siteName:254 self.echo_info(public.getMsg('BACKUP_SITE',(siteName,)))255 self.echo_info(public.getMsg('WEBSITE_DIR',(spath,)))256 else:257 self.echo_info(public.getMsg('BACKUP_DIR',(spath,)))258 259 self.echo_info(public.getMsg(260 "DIR_SIZE",261 (str(public.to_size(p_size),))262 ))263 self.echo_info(public.getMsg('BACKUP_EXCLUSION',(exclude_config,)))264 disk_path,disk_free,disk_inode = self.get_disk_free(dfile)265 self.echo_info(public.getMsg(266 "PARTITION_INFO",267 (disk_path,str(public.to_size(disk_free)),str(disk_inode))268 ))269 if disk_path:270 if disk_free < p_size:271 self.echo_error(public.getMsg(272 "PARTITION_LESS_THEN",273 (str(public.to_size(p_size)),)274 ))275 return False276 if disk_inode < self._inode_min:277 self.echo_error(public.getMsg(278 "INODE_LESS_THEN",279 (str(self._inode_min,))280 ))281 return False282 stime = time.time()283 self.echo_info(public.getMsg("START_COMPRESS",(public.format_date(times=stime),)))284 if os.path.exists(dfile):285 os.remove(dfile)286 public.ExecShell("cd " + os.path.dirname(spath) + " && tar zcvf '" + dfile + "' " + self._exclude + " '" + dirname + "' 2>{err_log} 1> /dev/null".format(err_log = self._err_log))287 tar_size = os.path.getsize(dfile)288 if tar_size < 1:289 self.echo_error(public.getMsg('ZIP_ERR'))290 self.echo_info(public.readFile(self._err_log))291 return False292 compression_time = str('{:.2f}'.format(time.time() - stime))293 self.echo_info(public.getMsg(294 'COMPRESS_TIME',295 (compression_time,str(public.to_size(tar_size)))296 ))297 if siteName:298 self.echo_info(public.getMsg("WEBSITE_BACKUP_TO",(dfile,)))299 else:300 self.echo_info(public.getMsg("DIR_BACKUP_TO",(dfile,)))301 if os.path.exists(self._err_log):302 os.remove(self._err_log)303 return dfile304 #备份指定站点305 def backup_site(self,siteName,save = 3 ,exclude = []):306 self.echo_start()307 find = public.M('sites').where('name=?',(siteName,)).field('id,path').find()308 spath = find['path']309 pid = find['id']310 fname = 'web_{}_{}.tar.gz'.format(siteName,public.format_date("%Y%m%d_%H%M%S"))311 dfile = os.path.join(self._path,'site',fname)312 error_msg = ""313 if not self.backup_path_to(spath,dfile,exclude,siteName=siteName):314 if self._error_msg:315 error_msg = self._error_msg316 self.send_failture_notification(error_msg)317 return False318 if self._cloud:319 self.echo_info(public.getMsg("BACKUP_UPLOADING",(self._cloud._title,)))320 if self._cloud.upload_file(dfile,'site'):321 self.echo_info(public.getMsg("BACKUP_UPLOAD_SUCCESS",(self._cloud._title,)))322 else:323 if hasattr(self._cloud, "error_msg"):324 if self._cloud.error_msg:325 error_msg = self._cloud.error_msg326 if not error_msg:327 error_msg = public.getMsg('BACKUP_UPLOAD_FAILED')328 self.echo_error(error_msg)329 if os.path.exists(dfile):330 os.remove(dfile)331 remark = "Backup to " + self._cloud._title332 self.send_failture_notification(error_msg, remark=remark)333 return False334 filename = dfile335 if self._cloud:336 filename = dfile + '|' + self._cloud._name + '|' + fname337 pdata = {338 'type': 0,339 'name': fname,340 'pid': pid,341 'filename': filename,342 'addtime': public.format_date(),343 'size': os.path.getsize(dfile)344 }345 public.M('backup').insert(pdata)346 if self._cloud:347 _not_save_local = True348 save_local = 0349 if self.cron_info:350 save_local = self.cron_info["save_local"]351 if save_local:352 _not_save_local = False353 else:354 if self._is_save_local:355 _not_save_local = False356 if _not_save_local:357 if os.path.exists(dfile):358 os.remove(dfile)359 self.echo_info(public.getMsg("BACKUP_DEL",(dfile,)))360 else:361 self.echo_info(public.getMsg('KEEP_LOCAL'))362 #清理多余备份363 if not self._cloud:364 backups = public.M('backup').where("type=? and pid=? and filename NOT LIKE '%|%'",('0',pid)).field('id,name,filename').select()365 else:366 backups = public.M('backup').where('type=? and pid=? and filename LIKE "%{}%"'.format(self._cloud._name),('0',pid)).field('id,name,filename').select()367 self.delete_old(backups,save,'site')368 self.echo_end()369 return dfile370 #备份所有数据库371 def backup_database_all(self,save = 3):372 databases = public.M('databases').field('name').select()373 self._backup_all = True374 failture_count = 0375 results = []376 for database in databases:377 self._error_msg = ""378 result = self.backup_database(database['name'],save=save)379 if not result:380 failture_count += 1381 results.append((database['name'], result, self._error_msg,))382 if failture_count > 0:383 self.send_all_failture_notification("database", results)384 self._backup_all = False385 #备份所有站点386 def backup_site_all(self,save = 3):387 sites = public.M('sites').field('name').select()388 self._backup_all = True389 failture_count = 0390 results = []391 for site in sites:392 self._error_msg = ""393 result = self.backup_site(site['name'],save)394 if not result:395 failture_count += 1396 results.append((site['name'], result, self._error_msg,))397 if failture_count > 0:398 self.send_all_failture_notification("site", results)399 self._backup_all = False400 #配置401 def mypass(self,act):402 conf_file = '/etc/my.cnf'403 conf_file_bak = '/etc/my.cnf.bak'404 if os.path.getsize(conf_file) > 2:405 public.writeFile(conf_file_bak,public.readFile(conf_file))406 public.set_mode(conf_file_bak,600)407 public.set_own(conf_file_bak,'mysql')408 elif os.path.getsize(conf_file_bak) > 2:409 public.writeFile(conf_file,public.readFile(conf_file_bak))410 public.set_mode(conf_file,600)411 public.set_own(conf_file,'mysql')412 public.ExecShell("sed -i '/user=root/d' {}".format(conf_file))413 public.ExecShell("sed -i '/password=/d' {}".format(conf_file))414 if act:415 password = public.M('config').where('id=?',(1,)).getField('mysql_root')416 mycnf = public.readFile(conf_file)417 if not mycnf: return False418 src_dump_re = r"\[mysqldump\][^.]"419 sub_dump = "[mysqldump]\nuser=root\npassword=\"{}\"\n".format(password)420 mycnf = re.sub(src_dump_re, sub_dump, mycnf)421 if len(mycnf) > 100: public.writeFile(conf_file,mycnf)422 return True423 return True424 #map to list425 def map_to_list(self,map_obj):426 try:427 if type(map_obj) != list and type(map_obj) != str: map_obj = list(map_obj)428 return map_obj429 except: return []430 #备份指定数据库431 def backup_database(self,db_name,dfile = None,save=3):432 self.echo_start()433 if not dfile:434 fname = 'db_{}_{}.sql.gz'.format(db_name,public.format_date("%Y%m%d_%H%M%S"))435 dfile = os.path.join(self._path,'database',fname)436 else:437 fname = os.path.basename(dfile)438 439 dpath = os.path.dirname(dfile)440 if not os.path.exists(dpath):441 os.makedirs(dpath,384)442 error_msg = ""443 import panelMysql444 if not self._db_mysql:self._db_mysql = panelMysql.panelMysql()445 d_tmp = self._db_mysql.query("select sum(DATA_LENGTH)+sum(INDEX_LENGTH) from information_schema.tables where table_schema='%s'" % db_name)446 try:447 p_size = self.map_to_list(d_tmp)[0][0]448 except:449 error_msg = public.getMsg('DB_CONN_ERR')450 self.echo_error(error_msg)451 self.send_failture_notification(error_msg)452 return False453 454 if p_size == None:455 error_msg = public.getMsg('DB_BACKUP_ERR',(db_name,))456 self.echo_error(error_msg)457 self.send_failture_notification(error_msg)458 return False459 character = public.get_database_character(db_name)460 self.echo_info(public.getMsg('DB_BACKUP',(db_name,)))461 self.echo_info(public.getMsg("DB_SIZE",(public.to_size(p_size),)))462 self.echo_info(public.getMsg("DB_CHARACTER",(character,)))463 disk_path,disk_free,disk_inode = self.get_disk_free(dfile)464 self.echo_info(public.getMsg(465 "PARTITION_INFO",(466 disk_path,str(public.to_size(disk_free)),str(disk_inode)467 )468 ))469 if disk_path:470 if disk_free < p_size:471 error_msg = public.getMsg("PARTITION_LESS_THEN",(472 str(public.to_size(p_size),)473 ))474 self.echo_error(error_msg)475 self.send_failture_notification(error_msg)476 return False477 if disk_inode < self._inode_min:478 error_msg = public.getMsg("INODE_LESS_THEN",(self._inode_min,))479 self.echo_error(error_msg)480 self.send_failture_notification(error_msg)481 return False482 483 stime = time.time()484 self.echo_info(public.getMsg("EXPORT_DB",(public.format_date(times=stime),)))485 if os.path.exists(dfile):486 os.remove(dfile)487 #self.mypass(True)488 try:489 password = public.M('config').where('id=?',(1,)).getField('mysql_root')490 os.environ["MYSQL_PWD"] = password491 backup_cmd = "/www/server/mysql/bin/mysqldump -E -R --default-character-set="+ character +" --force --hex-blob --opt " + db_name + " -u root" + " 2>"+self._err_log+"| gzip > " + dfile492 public.ExecShell(backup_cmd)493 except Exception as e:494 raise495 finally:496 os.environ["MYSQL_PWD"] = ""497 #public.ExecShell("/www/server/mysql/bin/mysqldump --default-character-set="+ character +" --force --hex-blob --opt " + db_name + " 2>"+self._err_log+"| gzip > " + dfile)498 #self.mypass(False)499 gz_size = os.path.getsize(dfile)500 if gz_size < 400:501 error_msg = public.getMsg("EXPORT_DB_ERR")502 self.echo_error(error_msg)503 self.send_failture_notification(error_msg)504 self.echo_info(public.readFile(self._err_log))505 return False506 compressed_time = str('{:.2f}'.format(time.time() - stime))507 self.echo_info(508 public.getMsg("COMPRESS_TIME",(str(compressed_time),509 str(public.to_size(gz_size))510 ))511 )512 if self._cloud:513 self.echo_info(public.getMsg("BACKUP_UPLOADING",(self._cloud._title,)))514 if self._cloud.upload_file(dfile, 'database'):515 self.echo_info(public.getMsg("BACKUP_UPLOAD_SUCCESS",(self._cloud._title,)))516 else:517 if hasattr(self._cloud, "error_msg"):518 if self._cloud.error_msg:519 error_msg = self._cloud.error_msg520 if not error_msg:521 error_msg = public.getMsg('BACKUP_UPLOAD_FAILED')522 self.echo_error(error_msg)523 if os.path.exists(dfile):524 os.remove(dfile)525 remark = "Backup to " + self._cloud._title526 self.send_failture_notification(error_msg, remark=remark)527 return False528 filename = dfile529 if self._cloud:530 filename = dfile + '|' + self._cloud._name + '|' + fname531 self.echo_info(public.getMsg("DB_BACKUP_TO",(dfile,)))532 if os.path.exists(self._err_log):533 os.remove(self._err_log)534 pid = public.M('databases').where('name=?',(db_name)).getField('id')535 pdata = {536 'type': '1',537 'name': fname,538 'pid': pid,539 'filename': filename,540 'addtime': public.format_date(),541 'size': os.path.getsize(dfile)542 }543 public.M('backup').insert(pdata)544 if self._cloud:545 _not_save_local = True546 save_local = 0547 if self.cron_info:548 save_local = self.cron_info["save_local"]549 if save_local:550 _not_save_local = False551 else:552 if self._is_save_local:553 _not_save_local = False554 if _not_save_local:555 if os.path.exists(dfile):556 os.remove(dfile)557 self.echo_info(public.getMsg("BACKUP_DEL",(dfile,)))558 else:559 self.echo_info(public.getMsg('KEEP_LOCAL'))560 #清理多余备份561 if not self._cloud:562 backups = public.M('backup').where("type=? and pid=? and filename NOT LIKE '%|%'",('1',pid)).field('id,name,filename').select()563 else:564 backups = public.M('backup').where('type=? and pid=? and filename LIKE "%{}%"'.format(self._cloud._name),('1',pid)).field('id,name,filename').select()565 self.delete_old(backups,save,'database')566 self.echo_end()567 return dfile568 def generate_success_title(self, task_name):569 from send_mail import send_mail570 sm = send_mail()571 now = public.format_date(format="%Y-%m-%d %H:%M")572 server_ip = sm.GetLocalIp()573 title = public.getMsg("BACKUP_TASK_TITLE",(server_ip, task_name))574 return title575 def generate_failture_title(self):576 title = "aaPanel backup task failed reminder"577 return title578 def generate_all_failture_notice(self, task_name, msg, backup_type, remark=""):579 # from send_mail import send_mail580 # sm = send_mail()581 now = public.format_date(format="%Y-%m-%d %H:%M:%S")582 server_ip = public.GetLocalIp()583 if remark:584 remark = "\n* Task notes: {}".format(remark)585 notice_content = """Hello,586 aaPanel reminds you that the cron you set failed to execute:587 * Server IP: {}588 * Time: {}589 * Task name: {}{}590 * The following is a list of {} that failed to backup:591 <table style="color:red;">592 {}593 </table>594 Please deal with it as soon as possible to avoid unnecessary trouble due to the failure of the backup task.595 - Notification by aaPanel""".format(596 server_ip, now, task_name, remark, backup_type, msg)597 return notice_content598 def generate_failture_notice(self, task_name, msg, remark):599 # from send_mail import send_mail600 # sm = send_mail()601 now = public.format_date(format="%Y-%m-%d %H:%M:%S")602 server_ip = public.GetLocalIp()603 if remark:604 remark = "\n* Task notes: {}".format(remark)605 notice_content = """Hello,606 aaPanel reminds you that the cron you set failed to execute:607 * Server IP: {}608 * Time: {}609 * Task name:{}{}610 * Error messages:611 <span style="color:red;">612 {}613 </span>614 Please deal with it as soon as possible to avoid unnecessary trouble due to the failure of the backup task.615 -- Notification by aaPanel""".format(616 server_ip, now, task_name, remark, msg)617 return notice_content618 def get_cron_info(self, cron_name):619 """ 通过计划任务名称查找计划任务配置参数 """620 try:621 cron_info = public.M('crontab').where('echo=?',(cron_name,))\622 .field('name,save_local,notice,notice_channel').find()623 return cron_info624 except Exception as e:625 pass626 return {}627 def send_failture_notification(self, error_msg, remark=""):628 """发送任务失败消息629 :error_msg 错误信息630 :remark 备注631 """632 if self._backup_all:633 return634 if not self.cron_info:635 return636 cron_info = self.cron_info637 cron_title = cron_info["name"]638 save_local = cron_info["save_local"]639 notice = cron_info["notice"]640 notice_channel = cron_info["notice_channel"]641 if notice == 0 or not notice_channel:642 return643 if notice == 1 or notice == 2:644 title = self.generate_failture_title()645 task_name = cron_title646 msg = self.generate_failture_notice(task_name, error_msg, remark)647 res = self.send_notification(notice_channel, title, msg)648 if res:649 self.echo_info(public.getMsg('NOTIFICATION_SENT'))650 def send_all_failture_notification(self, backup_type, results, remark=""):651 """统一发送任务失败消息652 :results [(备份对象, 备份结果,错误信息),...]653 :remark 备注654 """655 if not self.cron_info:656 return657 cron_info = self.cron_info658 cron_title = cron_info["name"]659 save_local = cron_info["save_local"]660 notice = cron_info["notice"]661 notice_channel = cron_info["notice_channel"]662 if notice == 0 or not notice_channel:663 return664 if notice == 1 or notice == 2:665 title = self.generate_failture_title()666 type_desc = {667 "site": "site",668 "database": "database"669 }670 backup_type_desc = type_desc[backup_type]671 task_name = cron_title672 failture_count = 0673 total = 0674 content = ""675 for obj in results:676 total += 1677 obj_name = obj[0]678 result = obj[1]679 if not result:680 failture_count += 1681 content += "<tr><td style='color:red'>{}</td><tr>".format(obj_name)682 if failture_count > 0:683 if self._cloud:684 remark = public.getMsg("BACKUP_MSG"),(685 self._cloud._title, total, backup_type_desc, failture_count)686 else:687 remark = public.getMsg("BACKUP_MSG1"),(688 failture_count, total, backup_type_desc)689 msg = self.generate_all_failture_notice(task_name, content, backup_type_desc, remark)690 res = self.send_notification(notice_channel, title, msg)691 if res:692 self.echo_info(public.getMsg('NOTIFICATION_SENT'))693 else:694 self.echo_error(public.getMsg('NOTIFICATION_ERR'))695 def send_notification(self, channel, title, msg = ""):696 try:697 from send_mail import send_mail698 tondao = []699 if channel.find(",") >= 0:700 tongdao = channel.split(",")701 else:702 tongdao = [channel]703 sm = send_mail()704 send_res = []705 error_count = 0706 channel_names = {707 "mail": "email",708 # "dingidng": "钉钉"...

Full Screen

Full Screen

基本参考代码.py

Source:基本参考代码.py Github

copy

Full Screen

...90#--------------------------------------加载用户------------------------------------------------------------91def load_User_date_Success(sdk):92 if sdk != 1: # 显示以下内容93 massage='感谢您进入内测,我们给您的账户添加了10元,仅在内测阶段使用\n\n内测阶段打印机可能出现各种故障,我们不建议您现在将他当作正式的服务使用。\n\n您发送的一切消息都会被记录下来作为参考使用。\n\n欢迎您提出各种建议\n\n您可以发送\n建议 (您的建议内容)\n给机器人\n或者联系qq:794358907\ntel:15767788124\n\n注:(打印机失去连接和充值不到位暂时属于正常现象)\n\n再次感谢您参与我们的测试。\n\n'94 recharge_money(sdk.getMsg().QQ,10,sdk.getMsg().QQ,time.time())95 sdk.sendPrivdteMsg(sdk.getMsg().QQ, massage)96 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '欢迎您第一次使用油印智能打印系统 输入 帮助 获得详细帮助')97 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '您的身份档案已建立\n用户创建时间' + str(dic_user_infomation[str(sdk.getMsg().QQ)].Numtime))98 return99# --------------------------------------------用户数据更新函数------------------------------------------------100def Save_User_date(): # 按照现在使用过的用户进行更新101 for user_id in user_list: # 进入循环(一个一个用户数据按编号更新)102 if os.path.exists('date\\User_infomation\\' + str(user_id)) == False: # 判断这个用户数据是否存在或一致103 os.mkdir('date\\User_infomation\\' + str(user_id)) # 修改用户数据104 dic_temp = {} # 创建一个新的空的字典数据结构105 dic_temp['user_id'] = dic_user_infomation[str(user_id)].user_id # 加入user_id信息106 dic_temp['user_state'] = dic_user_infomation[str(user_id)].user_state # 加入user_state信息107 dic_temp['user_level'] = dic_user_infomation[str(user_id)].user_level # 加入user_level信息108 dic_temp['money'] = dic_user_infomation[str(user_id)].money # 加入money信息109 dic_temp['Numtime'] = dic_user_infomation[str(user_id)].Numtime # 加入Numtime信息110 dic_temp['file_size'] = dic_user_infomation[str(user_id)].file_size # 加入file_size信息111 file = open('date\\User_infomation\\' + str(user_id) + '\\User_infomation.txt', 'w') # 打开对应user_id的date\\User_infomation\\和\\User_infomation.txt文件(以只写的方式)112 file.write(json.dumps(dic_temp)) # 向文件中写入字典数据结构113 file.close() # 关闭文件114 Save_User_Print_record(user_id) # 调用函数保护充值列表115 Save_User_recharge_list(user_id) # 调用函数保护文件列表116 Save_User_file_list(user_id) # 调用函数保护用户列表117 return118def Get_massage(sdk):119 # logging.info('收到消息 ' +sdk.getMsg().Msg+ ' Form ' + str(sdk.getMsg().QQ))120 print(sdk.getMsg().Msg) # 打印信息121 sdk.sendPrivdteMsg(sdk.getMsg().QQ, sdk.getMsg(),Msg) # 显示信息122 print(sdk.getMsg().Msg) # 打印信息123 return124 logging.exception("收到消息 " + sdk.getMsg().QQ + ' 的 '+ sdk.getMsg().Msg) # 显示信息125 if state.system_state == 3: # 若系统状态异常126 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '系统维护中。。。请稍后再试。。。') # 显示信息127 return128 if sdk.getMsg().QQ not in last_user_massage: # last_user_massage为字典数据结构,查找该数据结构中是否有sdk.getMsg().QQ这个键129 last_user_massage[sdk.getMsg().QQ]=time.time() # 创建并更新用户发送最后一条消息的时间戳130 else:131 if time.time() - last_user_massage[sdk.getMsg().QQ] >300: # 用当前时间戳减去用户最后一条消息的时间戳,看是否超过5分钟132 if sdk.getMsg().QQ in dic_user_infomation: # 若超过5分钟,就删除储存用户信息的几个临时数据结构133 del dic_user_infomation[sdk.getMsg().QQ] # 对应的用户信息134 if sdk.getMsg().QQ in tem_print_list: # 打印列表135 del tem_print_list[sdk.getMsg().QQ]136 if sdk.getMsg().QQ in user_state: # 用户状态137 del user_state[sdk.getMsg().QQ]138 last_user_massage[sdk.getMsg().QQ] = time.time() # 更新用户最后一条消息的时间戳139 if str(sdk.getMsg().QQ) not in dic_user_infomation: # 若不存在则创建并且更新140 load_User_date(sdk.getMsg().QQ,sdk) # 载入用户141 if sdk.getMsg().QQ not in user_state: # 若不存在则先将用户状态变为正常状态142 user_state[sdk.getMsg().QQ] = 0143 if sdk.getMsg().Type == 1: # 若用户发送的是普通消息144 if sdk.getMsg().Msg == '重启': # 若发送信息是重启145 if sdk.getMsg().QQ in dic_user_infomation: # 就删除储存用户信息的几个临时数据结构146 del dic_user_infomation[sdk.getMsg().QQ]147 if sdk.getMsg().QQ in tem_print_list:148 del tem_print_list[sdk.getMsg().QQ]149 if sdk.getMsg().QQ in user_state:150 del user_state[sdk.getMsg().QQ]151 last_user_massage[sdk.getMsg().QQ] = time.time() # 更新用户最后一条消息的时间戳152 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '重启成功!') # 显示信息153 return154155 if user_state[sdk.getMsg().QQ] == 1: # 若用户状态为1156 receive_add_tem_print(sdk) # 调用添加临时打印列表函数157 return158 if user_state[sdk.getMsg().QQ] == 2: # 若用户状态为2159 receive_del_tem_print(sdk) # 调用删除临时打印列表函数160 return161 if user_state[sdk.getMsg().QQ] == 3: # 若用户状态为3162 receive_del_file(sdk) # 调用删除文件表函数163 return164 if sdk.getMsg().Msg.find('帮助') != -1: # 判断用户发送消息中有无‘帮助’165 Help(sdk) # 调用帮助界面函数166 return167 if sdk.getMsg().Msg.find('save') != -1: # 判断用户发送消息中有无‘save’168 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '保存中。。。')169 Save_date() # 调用保存文件总函数170 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '保存完成!')171 return172 if sdk.getMsg().Msg.find('上传文件') != -1: # 判断用户发送消息中有无‘上传文件’173 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '请直接发文件给我!')174 if sdk.getMsg().Msg.find('关于') != -1: # 判断用户发送消息中有无‘关于’175 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '作者葡调皮~,欢迎与我联系哦! QQ:794358907')176 return177 if sdk.getMsg().Msg.find('建议') != -1: # 判断用户发送消息中有无‘建议’178 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '感谢您的建议!十分感谢!!')179 logging.exception(sdk.getMsg().Msg)180 return181182183 if sdk.getMsg().Msg.find('打印列表') != -1: # 判断用户发送消息中有无‘打印列表’184 if sdk.getMsg().Msg.find('添加') != -1: # 判断用户发送消息中有无‘添加’185 add_tem_print(sdk) # 调用添加临时打印列表函数186 return187 if sdk.getMsg().Msg.find('删除') != -1: # 判断用户发送消息中有无‘删除’188 del_tem_print(sdk) # 调用删除临时打印列表函数189 return190 if sdk.getMsg().Msg.find('查询') != -1: # 判断用户发送消息中有无‘查询’191 tem_print(sdk) # 调用打印列表文件函数192 return193 if sdk.getMsg().Msg.find('打印文件') != -1:194 if sdk.getMsg().Msg.find('添加') != -1: # 打印列表添加195 add_tem_print(sdk)196 return197 if sdk.getMsg().Msg.find('删除') != -1:198 del_tem_print(sdk)199 return200 if sdk.getMsg().Msg.find('查询') != -1:201 tem_print(sdk)202 return203 print_page(sdk,sdk.getMsg().QQ)204 return205 if sdk.getMsg().Msg.find('文件') != -1: # 判断用户发送消息中有无‘文件’206 if sdk.getMsg().Msg.find('添加') != -1:207 add_tem_print(sdk) # 调用添加临时打印列表 函数208 return209 if sdk.getMsg().Msg.find('删除') != -1:210 del_file(sdk) # 调用删除文件表函数211 return212 if sdk.getMsg().Msg.find('查询') != -1:213 User_file_list(sdk) # 显示储存文件信息214 return215 if sdk.getMsg().Msg.find('充值') != -1: # 判断用户发送消息中有无‘充值’216 way2recharge(sdk) # 调用充值方法函数217 return218 if sdk.getMsg().Msg.find('打印') != -1: # 判断用户发送消息中有无‘打印’219 print_page(sdk,sdk.getMsg().QQ) # 调用打印文件总函数220 return221 if sdk.getMsg().Msg.find('个人信息') != -1: # 判断用户发送消息中有无‘个人信息’222 tem='用户信息:' # 传递个人信息223 tem+='\n用户建立时间:'+str(dic_user_infomation[sdk.getMsg().QQ].Numtime)224 tem+='\n余额:'+str(dic_user_infomation[sdk.getMsg().QQ].money)225 try:226 if len(dic_user_infomation[sdk.getMsg().QQ].file.size) !=0:227 tem += '\n用户文件数:' + str(len(dic_user_infomation[sdk.getMsg().QQ].file.size))228 else:229 tem += '\n用户文件数:0'230 except:231 tem +='\n用户文件数:0'232 sdk.sendPrivdteMsg(sdk.getMsg().QQ, tem) # 显示个人信息233234 if sdk.getMsg().Msg.find('system') != -1: # 判断用户发送消息中有无‘system’235 sdk.sendPrivdteMsg(sdk.getMsg().QQ, 'file_number'+ str(state.file_number)+'\n') # 显示信息236 sdk.sendPrivdteMsg(sdk.getMsg().QQ, 'print_number' + str(state.print_number) + '\n') # 显示信息237 sdk.sendPrivdteMsg(sdk.getMsg().QQ, 'user_number' + str(state.user_number) + '\n') # 显示信息238 sdk.sendPrivdteMsg(sdk.getMsg().QQ, '您发送的消息为' + sdk.getMsg().Msg+'\n输入 \'帮助\' 获得帮助') # 显示信息239 print(sdk.getMsg().Msg)240 if sdk.getMsg().Type == 105: # 判断发送消息是否为文件消息241 file_get(sdk) # 调用文件判断函数242 return243244# --------------------------------------------------正式运行--------------------------------------------------------245# logging.info('正式运行')246#load_System_state()247app = Flask(__name__)248#logging.basicConfig(filename="test.log", filemode="a", format="%(asctime)s %(name)s:%(levelname)s:%(message)s", datefmt="%d-%M-%Y %H:%M:%S", level=logging.DEBUG)249# address=0250#logging.exception("开始")251# sending_list=[]252253@app.route('/', methods=['GET', 'POST'])254def index(): ...

Full Screen

Full Screen

pacclient.py

Source:pacclient.py Github

copy

Full Screen

1#!/usr/bin/env python2import sys3import os4import getopt5import httplib26from xml.etree import ElementTree as ET7from xml.dom import minidom8from ConfigParser import ConfigParser9from ConfigParser import NoSectionError10import getpass11import socket12import re13import urllib214from pac_api import *15def logon_usage():16 print ( (_getmsg("logon_usage") + "\n") )17 18def main_logon(argv):19 url=''20 user=''21 password=''22 try: 23 opts, args = getopt.getopt(argv, "hl:u:p:", ['help','url=','user=','pass=']) 24 except getopt.GetoptError: 25 logon_usage() 26 return27 for opt, arg in opts: 28 if ((opt == "-h") | (opt == "--help")) : 29 logon_usage() 30 return 31 elif ((opt == '-l') | (opt == "--url")) :32 url = arg 33 elif ((opt == '-u') | (opt == "--user")) : 34 user = arg 35 elif ((opt == '-p') | (opt == "--pass")) : 36 password = arg 37 if len(url) == 0: 38 url=raw_input( _getmsg("logon_url_prompt") )39 url,context = parseUrl(url);40 p = re.compile('^(http|https)://[\w\W]+:\d+[/]{0,1}$')41 if (len(url) == 0) | (p.match(url.lower()) == None):42 print ( _getmsg("logon_null_url") )43 return44 url = url + context45 url = removeQuote(url)46 x509Flag, key, cert = checkX509PEMCert(url)47 if (x509Flag == False) | ( len(user) > 0) | (len(password) > 0):48 if len(user) == 0:49 user=raw_input( _getmsg("logon_username") )50 if len(user) == 0:51 print ( _getmsg("logon_specify_username") )52 return53 if len(password) == 0:54 password=getpass.getpass()55 if len(password) == 0:56 print ( _getmsg("logon_specify_password") )57 return58 if ( (len(url) != 0) & ('https' in url.lower()) ):59 if ( ( os.path.isfile('cacert.pem') == False ) & (httplib2.__version__ >= '0.7.0') ):60 print ( _getmsg("https_certificate_missing") )61 return62 # In xml, & should be written as &amp; Or it will generate exception when CFX parses63 password = password.replace("&", "&amp;")64 # < === &lt;65 password = password.replace("<", "&lt;")66 # > === &gt;67 password = password.replace(">", "&gt;")68 69 # remove the quote70 password = removeQuote(password)71 user = removeQuote(user)72 73 # Log on action74 logon(url, user, password)75def submit_usage():76 print ( (_getmsg("submit_usage") + "\n") )77def main_submit(argv):78 if len(argv) == 0:79 submit_usage() 80 return81 appName=''82 profile=''83 params=''84 slash = getFileSeparator()85 try: 86 opts, args = getopt.getopt(argv, "ha:c:p:", ['help','app=','conf=', 'param=']) 87 except getopt.GetoptError: 88 submit_usage() 89 return90 for opt, arg in opts: 91 if ((opt == "-h") | (opt == "--help")) : 92 submit_usage() 93 return 94 elif ((opt == '-a') | (opt == "--app")) : 95 appName = arg 96 elif ((opt == '-c') | (opt == "--conf")) : 97 profile = arg 98 elif ((opt == '-p') | (opt == "--param")) : 99 params = arg 100 if len(appName) <= 0:101 print ( _getmsg("submit_arg_missing") )102 return103 inputParams={}104 inputFiles={}105 106 if len(profile) > 0:107 profile = removeQuote(profile)108 if (":" not in profile) & ( slash != profile[0]):109 dir = os.getcwd() + slash110 profile = dir + profile111 if os.path.isfile(profile) is False:112 print ( _getmsg("submit_file_notexist") % profile )113 return114 config = ConfigParser()115 config.optionxform = str #make option name case-sensitive116 try:117 config.read(profile)118 except IOError:119 print ( _getmsg("submit_cannot_openfile") % profile )120 return121 try:122 for option in config.options('Parameter'):123 inputParams[option]=config.get('Parameter', option)124 except NoSectionError:125 print ( _getmsg("submit_param_missing") % profile )126 return127 try:128 for option in config.options('Inputfile'):129 inputFiles[option]=config.get('Inputfile', option)130 except NoSectionError:131 print ( _getmsg("submit_inputfile_missing") % profile )132 return133 134 if len(params) > 0:135 for pp in params.split(';'):136 if len(pp) > 0:137 nv = pp.split('=',1)138 if len(nv) > 1:139 if ',' in nv[1]:140 inputFiles[nv[0]] = nv[1]141 else:142 inputParams[nv[0]] = nv[1]143 else:144 print ( _getmsg("submit_input_invalid") ) 145 return146 JobDict={}147 JobDict[APP_NAME]=appName148 try:149 span = inputParams['SPAN']150 inputParams['SPAN'] = "span[%s]" % span151 except KeyError:152 pass153 JobDict['PARAMS']=inputParams154 JobDict['INPUT_FILES']=inputFiles155 156 status, message = submitJob(JobDict)157 if status == 'ok':158 print ( _getmsg("submit_success").format(message) )159 else:160 print message161 162def job_usage():163 print ( (_getmsg("job_usage") + "\n\n") )164def main_job(argv):165 jobStatus=''166 jobName=''167 jobId=''168 long=''169 group=''170 user=''171 past=''172 try: 173 opts, args = getopt.getopt(argv, "hu:ls:n:g:p:", ['help','user=','long','status=','name=','group=','past=']) 174 except getopt.GetoptError: 175 job_usage() 176 return177 for opt, arg in opts: 178 if ((opt == "-h") | (opt == "--help")) : 179 job_usage() 180 return 181 elif ((opt=='-u') | (opt == '--user')) :182 user=arg183 elif ((opt=='-l') | (opt == '--long')) :184 long='yes' 185 elif ((opt == '-s') | (opt == "--status")) : 186 jobStatus = arg 187 elif ((opt == '-n') | (opt == "--name")) : 188 jobName = urllib2.quote(arg)189 elif ((opt == '-g') | (opt == "--group")) :190 group=urllib2.quote(arg)191 elif ((opt == '-p') | (opt == '--past')) :192 past=arg193 if len(args) > 0:194 jobId = args[0]195 p = re.compile('^[1-9]{1}[0-9]{0,}$')196 pl = re.compile('^[1-9]{1}[0-9]{0,}\[{1}[0-9]{0,}\]{1}$')197 if (len(jobId) == 0) | ((p.match(jobId.lower()) == None) and (pl.match(jobId.lower()) == None)) | (len(args)>1):198 job_usage()199 return 200 if (len(jobStatus) > 0 and len(jobName) > 0) | (len(jobStatus) > 0 and len(jobId) > 0) | (len(jobId) > 0 and len(jobName) > 0) | (len(jobId)>0 and len(group)>0) | (len(jobName)>0 and len(group)>0) | (len(jobStatus)>0 and len(group)>0):201 print ( _getmsg("job_usage_error") )202 return203 status = ''204 message = ''205 statusFlag = False206 nameFlag = False207 groupFlag=False208 if len(jobStatus) > 0:209 status, message = getJobListInfo('status='+jobStatus+'&user='+user+'&details='+long+'&past='+past)210 statusFlag = True211 elif len(jobName) > 0:212 status, message = getJobListInfo('name='+jobName+'&user='+user+'&details='+long+'&past='+past)213 nameFlag = True214 elif len(group) > 0:215 status, message = getJobListInfo('group='+group+'&user='+user+'&details='+long+'&past='+past)216 groupFlag=True217 if status != '':218 if status == 'ok':219 tree = ET.fromstring(message)220 jobs =tree.getiterator("Job")221 if len(jobs) == 0:222 if statusFlag == True:223 print ( _getmsg("job_nomatch_status").format(jobStatus) )224 elif nameFlag == True:225 print ( _getmsg("job_nomatch_name").format(jobName) )226 elif groupFlag== True:227 print ( _getmsg("job_nomatch_group").format(group) )228 return229 showJobinfo(jobs,long)230 else:231 print message232 return233 if len(jobId) > 0:234 status, message= getJobListInfo('id='+jobId+'&user='+user+'&details='+long+'&past='+past)235 if status == 'ok':236 tree = ET.fromstring(message)237 jobs =tree.getiterator("Job")238 showJobinfo(jobs,long)239 else:240 print message241 return242 else:243 status, message= getJobListInfo('user='+user+'&details='+long+'&past='+past)244 if status == 'ok':245 tree = ET.fromstring(message)246 jobs =tree.getiterator("Job")247 showJobinfo(jobs,long)248 else:249 print message250 return251 job_usage()252def showJobinfo(jobs,long):253 if long == '':254 print ( _getmsg("job_info_title") )255 for xdoc in jobs:256 jobId=xdoc.find('id').text257 status=xdoc.find('status')258 extStatus=xdoc.find('extStatus')259 name=xdoc.find('name').text260 cmd=xdoc.find('cmd')261 print '%-10s%-10s%-23s%-25s%s' % (jobId, checkFieldValidity(status), SubStr(checkFieldValidity(extStatus)), SubStr(name),checkFieldValidity(cmd))262 else:263 for xdoc in jobs:264 jobId=xdoc.find('id').text265 name=xdoc.find('name').text266 user=xdoc.find('user')267 jobType=xdoc.find('jobType')268 status=xdoc.find('status')269 appType=xdoc.find('appType')270 submitTime=xdoc.find('submitTime')271 endTime=xdoc.find('endTime')272 startTime=xdoc.find('startTime')273 queue=xdoc.find('queue')274 cmd=xdoc.find('cmd')275 projectName=xdoc.find('projectName')276 pendReason=xdoc.find('pendReason')277 description=xdoc.find('description')278 extStatus=xdoc.find('extStatus')279 priority=xdoc.find('priority')280 exitCode=xdoc.find('exitCode')281 swap=xdoc.find('swap')282 pgid=xdoc.find('pgid')283 pid=xdoc.find('pid')284 nthreads=xdoc.find('nthreads')285 numProcessors=xdoc.find('numProcessors')286 fromHost=xdoc.find('fromHost')287 exHosts=xdoc.find('exHosts')288 askedHosts=xdoc.find('askedHosts')289 runTime=xdoc.find('runTime')290 mem=xdoc.find('mem')291 timeRemaining=xdoc.find('timeRemaining')292 estimateRunTime=xdoc.find('estimateRunTime')293 infile=xdoc.find('infile')294 outfile=xdoc.find('outfile')295 execCwd=xdoc.find('execCwd')296 graphicJob=xdoc.find('graphicJob')297 cwd=xdoc.find('cwd')298 timeRemaining=xdoc.find('timeRemaining')299 app=xdoc.find('app')300 jobForwarding=xdoc.find('jobForwarding')301 localClusterName=xdoc.find('localClusterName')302 localJobId=xdoc.find('localJobId')303 remoteJobId=xdoc.find('remoteJobId')304 remoteClusterName=xdoc.find('remoteClusterName') 305 print ( _getmsg("job_info_id") % jobId )306 print ( _getmsg("job_info_name") % name )307 print ( _getmsg("job_info_type") % checkFieldValidity(jobType) )308 print ( _getmsg("job_info_status") % checkFieldValidity(status) )309 print ( _getmsg("job_info_apptype") % checkFieldValidity(appType) )310 print ( _getmsg("job_info_submittime") % checkFieldValidity(submitTime) )311 print ( _getmsg("job_info_user") % checkFieldValidity(user) )312 print ( _getmsg("job_info_endtime") % checkFieldValidity(endTime) )313 print ( _getmsg("job_info_starttime") % checkFieldValidity(startTime) )314 print ( _getmsg("job_info_queue") % checkFieldValidity(queue) )315 print ( _getmsg("job_info_cmd") % checkFieldValidity(cmd) )316 print ( _getmsg("job_info_projname") % checkFieldValidity(projectName) )317 print ( _getmsg("job_info_pending_reason") % checkFieldValidity(pendReason) )318 print ( _getmsg("job_info_desc") % checkFieldValidity(description) )319 print ( _getmsg("job_info_exstatus") % checkFieldValidity(extStatus) )320 print ( _getmsg("job_info_priority") % checkFieldValidity(priority) )321 print ( _getmsg("job_info_exitcode") % checkFieldValidity(exitCode) )322 print ( _getmsg("job_info_mem") % checkFieldValidity(mem) )323 print ( _getmsg("job_info_swap") % checkFieldValidity(swap) )324 print ( _getmsg("job_info_gid") % checkFieldValidity(pgid) )325 print ( _getmsg("job_info_pid") % checkFieldValidity(pid) )326 print ( _getmsg("job_info_numthread") % checkFieldValidity(nthreads) )327 print ( _getmsg("job_info_reqprocessors") % checkFieldValidity(numProcessors) )328 print ( _getmsg("job_info_submithost") % checkFieldValidity(fromHost) )329 print ( _getmsg("job_info_exeutionhost") % checkFieldValidity(exHosts) )330 print ( _getmsg("job_info_reqhost") % checkFieldValidity(askedHosts) )331 print ( _getmsg("job_info_runtime") % checkFieldValidity(runTime) )332 print ( _getmsg("job_info_timeremain") % checkFieldValidity(timeRemaining) )333 print ( _getmsg("job_info_est_runtime") % checkFieldValidity(estimateRunTime) )334 print ( _getmsg("job_info_inputfile") % checkFieldValidity(infile) )335 print ( _getmsg("job_info_outfile") % checkFieldValidity(outfile) )336 print ( _getmsg("job_info_exe_cwd") % checkFieldValidity(execCwd) )337 print ( _getmsg("job_info_gjob") % checkFieldValidity(graphicJob) )338 print ( _getmsg("job_info_curdir") % checkFieldValidity(cwd) )339 print ( _getmsg("job_info_app_profile") % checkFieldValidity(app) )340 print ( _getmsg("job_info_localid") % checkFieldValidity(localJobId) )341 print ( _getmsg("job_info_localcluster") % checkFieldValidity(localClusterName) )342 print ( _getmsg("job_info_fwd") % checkFieldValidity(jobForwarding) )343 if checkFieldValidity(jobForwarding) != 'None':344 print ( _getmsg("job_info_remoteid") % checkFieldValidity(remoteJobId) )345 print ( _getmsg("job_info_remotecluster") % checkFieldValidity(remoteClusterName) ) 346 if len(jobs)>1:347 print ' '348 print ' '349 350def SubStr(field):351 if len(field) > 10 :352 field = '*' + field[-9:]353 return field354 355def checkFieldValidity(field):356 if field != None:357 if field.text == None :358 field = ''359 else:360 field = field.text361 else:362 field='-'363 return field364def download_usage():365 print ( (_getmsg("download_usage") + "\n\n") )366def main_download(argv):367 if len(argv) == 0:368 download_usage() 369 return370 dir=''371 file = ''372 jobId=''373 cmd=''374 375 # Total size of uploaded files376 totalSize = 0377 378 try: 379 opts, args = getopt.getopt(argv, "hd:f:c:", ['help','dir=','file=','cmd=']) 380 except getopt.GetoptError: 381 download_usage() 382 return383 for opt, arg in opts: 384 if ((opt == "-h") | (opt == "--help")) : 385 download_usage() 386 return 387 elif ((opt == '-d') | (opt == "--dir")) : 388 dir = arg 389 elif ((opt == '-f') | (opt == '--file')) :390 file = arg391 elif ((opt == '-c') | (opt == '--cmd')) :392 cmd = arg393 if dir == '' and cmd == '' and file == '' and len(args) <=0:394 download_usage()395 return396 if len(args) <= 0:397 print ( _getmsg("download_specify_id") )398 return399 if len(dir) <=0:400 dir = os.getcwd()401 jobId = args[0]402 if os.path.exists(dir) == False :403 print ( _getmsg("download_dirnotexist") % dir )404 return405 dir = removeQuote(dir)406 file = removeQuote(file)407 downloadJobFiles(jobId, dir, file, cmd)408def upload_usage():409 print ( (_getmsg("upload_usage") + "\n") )410 411def main_upload(argv):412 if len(argv) == 0:413 upload_usage() 414 return415 dir=''416 file = ''417 jobId=''418 419 # Total size of uploaded files420 totalSize = 0421 422 try: 423 opts, args = getopt.getopt(argv, "hd:f:") 424 except getopt.GetoptError: 425 upload_usage() 426 return427 for opt, arg in opts: 428 if ((opt == "-h")) : 429 upload_usage() 430 return 431 elif ((opt == '-d')) : 432 dir = arg 433 elif ((opt == '-f')) :434 file = arg435 dir = removeQuote(dir)436 file = removeQuote(file)437 if file == '':438 print ( _getmsg("submit_file_notexist") )439 return440 p = re.compile('^[\w\W]+:/[\w\W]+')441 # Windows regular express compiler442 winp = re.compile('^[\w\W]+:[a-zA-Z]:[/\\\\]+')443 if (((dir == '') | ((len(dir) > 0) and (('/' != dir[0]) and (p.match(dir.lower()) == None) and ( winp.match(dir.lower()) == None) ))) and (len(args) <= 0)):444 print ( _getmsg("upload_specify_jobid") )445 return446 if ((':' in dir) and (p.match(dir.lower()) == None) and ( winp.match(dir.lower()) == None)):447 print ( _getmsg("upload_specify_absolutepath") )448 return449 cwd = os.getcwd()450 files = file.split(',')451 paths = ''452 p = re.compile('^[a-zA-Z]:[/\\][\w\W]+')453 slash = getFileSeparator()454 valid = True455 for f in files:456 f.strip()457 if len(f) > 0:458 if ((slash != f[0]) and (p.match(f.lower()) == None)):459 f = cwd + slash + f460 if not os.path.isfile(f):461 valid = False462 print ( _getmsg("upload_file_notfound") % f )463 elif os.access(f, os.R_OK) == 0:464 valid = False465 print ( _getmsg("upload_fileread_denied") % f )466 else:467 # Get all files total size468 totalSize += os.path.getsize(f)469 paths = paths + f + ','470 if not valid:471 return472 elif len(paths)<=0:473 print ( _getmsg("upload_file_notfound") % file )474 return475 else:476 paths = paths[:-1]477 p = re.compile('^[\w\W]+:/[\w\W]+')478 if ((len(dir) > 0) and (('/' == dir[0]) | (p.match(dir.lower()) != None) | (winp.match(dir.lower()) != None))):479 jobId = '0'480 else:481 jobId = args[0]482 p = re.compile('^[1-9]{1}[0-9]{0,}$')483 if p.match(jobId.lower()) == None:484 print ( _getmsg("upload_jobid_invalid") )485 return486 487 # If total file size is greater than 500Mb, upload them separately by WS API.488 if (totalSize > 536870912):489 # If one file size is greater than 500Mb, split it into chunks and every chunk max size is 500Mb490 files = paths.split(',')491 for f in files:492 if os.path.getsize(f) > 536870912:493 # Upload larger file494 uploadLargeFile(jobId, dir, f)495 else:496 # Upload normal file497 uploadJobFiles(jobId, dir, f)498 else: 499 uploadJobFiles(jobId, dir, paths)500def jobaction_usage():501 print ( (_getmsg("jobaction_usage") + "\n") )502def main_jobaction(argv):503 jobAction=''504 jobId=''505 try: 506 opts, args = getopt.getopt(argv, "ha:", ['help','action=']) 507 except getopt.GetoptError: 508 jobaction_usage() 509 return510 for opt, arg in opts: 511 if ((opt == "-h") | (opt == "--help")) : 512 jobaction_usage() 513 return 514 elif ((opt == '-a') | (opt == "--action")) : 515 jobAction = arg 516 if len(args) <= 0 and len(jobAction) <= 0:517 jobaction_usage()518 return519 if len(args) <= 0:520 print ( _getmsg("jobaction_specify_jobid") )521 return522 if len(args) >0 :523 jobId = args[0]524 p = re.compile('^[1-9]{1}[0-9]{0,}$')525 pl = re.compile('^[1-9]{1}[0-9]{0,}\[{1}[0-9]{0,}\]{1}$')526 if (p.match(jobId.lower()) == None) and (pl.match(jobId.lower()) == None):527 jobaction_usage()528 return529 jobId=args[0]530 status, message = doJobAction(jobAction, jobId)531 print message532def usercmd_usage():533 print ( (_getmsg("usercmd_usage") + "\n") )534def main_usercmd(argv):535 userCmd=''536 for i in range(0, len(argv)):537 if ((argv[i] == '-h')):538 usercmd_usage()539 return540 elif (((argv[i] == '-c')) & (i+1 < len(argv))):541 userCmd = removeQuote(argv[i+1]).strip()542 if len(userCmd) <= 0:543 usercmd_usage()544 return545 for arg in argv[i+2:]:546 temp = removeQuote(arg).strip()547 if len(temp) > 0:548 userCmd = userCmd + ' "' + temp + '"'549 break550 else:551 usercmd_usage()552 return553 if len(userCmd) <= 0:554 usercmd_usage()555 return556 status, message = doUserCmd(userCmd)557 print message558def ping_usage():559 print ( (_getmsg("ping_usage") + "\n") )560def main_ping(argv):561 url = ''562 try: 563 opts, args = getopt.getopt(argv, "hl:", ['help','url='])564 except getopt.GetoptError: 565 ping_usage() 566 return567 for opt, arg in opts: 568 if ((opt == "-h") | (opt == "--help")) : 569 ping_usage() 570 return 571 elif ((opt == '-l') | (opt == "--url")) :572 url = arg 573 if len(url) == 0: 574 url=raw_input( _getmsg("ping_url"))575 url,context = parseUrl(url);576 p = re.compile('^(http|https)://[\w\W]+:\d+[/]{0,1}$')577 if (len(url) == 0) | (p.match(url.lower()) == None):578 print ( _getmsg("ping_urlformat_example") )579 return580 url = url + context581 ping(url)582def logout_usage():583 print ( (_getmsg("logout_usage") + "\n") )584def main_logout(argv):585 try: 586 opts, args = getopt.getopt(argv, "h", ['help'])587 except getopt.GetoptError: 588 logout_usage() 589 return590 for opt, arg in opts: 591 if ((opt == "-h") | (opt == "--help")) : 592 logout_usage() 593 return 594 logout()595def main_usage():596 print ( (_getmsg("main_usage") + "\n") )597 598def app_usage():599 print ( (_getmsg("app_usage") + "\n") )600def main_app(argv):601 if len(argv) == 0:602 app_usage()603 return604 appName = ''605 list = False606 try:607 opts, args = getopt.getopt(argv, "hlp:", ['help','list','param='])608 except getopt.GetoptError:609 app_usage()610 return611 for opt, arg in opts:612 if ((opt == "-h") | (opt == "--help")):613 app_usage()614 return615 elif ((opt == "-l") | (opt == "--list")):616 list = True617 elif ((opt == "-p") | (opt == "--param")):618 appName = arg619 620 if list == True:621 status, message = getAllAppStatus()622 if status == 'ok':623 xdoc = minidom.parseString(message)624 apps = xdoc.getElementsByTagName('AppInfo')625 if len(apps) == 0:626 print ( _getmsg("app_no_published_app") )627 return628 print ( _getmsg("app_allappstatus_title") )629 for app in apps:630 appStatus=''631 appName=''632 for apparg in app.childNodes:633 if apparg.nodeName == 'appName':634 appName = apparg.childNodes[0].nodeValue635 elif apparg.nodeName == 'status':636 appStatus = apparg.childNodes[0].nodeValue637 638 print '%-24s%-15s' % (appName, appStatus)639 else:640 print message641 return642 643 if len(appName) > 0:644 status, message = getAppParameter(appName)645 if status == "ok":646 xdoc = minidom.parseString(message)647 params = xdoc.getElementsByTagName('AppParam')648 print ( _getmsg("app_app_param_title") )649 for param in params:650 id = ''651 label = ''652 mandatory = ''653 dValue = ''654 for paramValue in param.childNodes:655 if paramValue.nodeName == 'id':656 id = paramValue.childNodes[0].nodeValue657 elif paramValue.nodeName == 'label':658 label = paramValue.childNodes[0].nodeValue659 elif paramValue.nodeName == 'mandatory':660 mandatory = paramValue.childNodes[0].nodeValue661 elif paramValue.nodeName == 'defaultValue':662 dValue = paramValue.childNodes[0].nodeValue663 print '%-18s%-35s%-11s%-10s' % (id, label, mandatory, dValue)664 else:665 print message666 return667 app_usage()668 return669def userAdd_usage():670 print ( (_getmsg("useradd_usage") + "\n") )671 672def main_userAdd(argv):673 username = ''674 email = ''675 roles = ''676 try:677 opts, args = getopt.getopt(argv, "hu:e:r:", ['help','username=','email=','role='])678 except getopt.GetoptError:679 userAdd_usage()680 return681 682 for opt, arg in opts:683 if ((opt == "-h") | (opt == "--help")):684 userAdd_usage()685 return686 elif ((opt == "-u") | (opt == "--username")):687 username = arg688 elif ((opt == "-e") | (opt == "--email")):689 email = arg690 elif ((opt == "-r") | (opt == "--role")):691 roles = arg692 693 unameValid = 'false'694 695 if len(username) == 0:696 username=raw_input('username:')697 if (len(username) == 0):698 userAdd_usage()699 return700 else:701 unameValid = 'true'702 703 if (len(email) == 0) & (unameValid == "false"):704 email=raw_input('email:')705 706 if (len(roles) == 0) & (unameValid == "false"):707 roles=raw_input('role[Normal User]:')708 if (len(roles) == 0):709 roles = 'Normal User'710 711 status, message = addUser(username, email, roles)712 if status == "ok":713 if len(message) > 0:714 print message715 else:716 print 'User '+username+' added to IBM Spectrum LSF Application Center.'717 else:718 print message719 return720def userDel_usage():721 print ( (_getmsg("userdel_usage") + "\n") )722def main_userDel(argv):723 username = ''724 try:725 opts, args = getopt.getopt(argv, "hu:", ['help','username='])726 except getopt.GetoptError:727 userDel_usage()728 return729 730 for opt, arg in opts:731 if ((opt == "-h") | (opt == "--help")):732 userDel_usage()733 return734 elif ((opt == "-u") | (opt == "--username")):735 username = arg736 737 if len(username) == 0:738 username=raw_input('username:')739 if (len(username) == 0):740 userDel_usage()741 return742 743 status, message = removeUser(username)744 if status == "ok":745 if len(message) > 0:746 print message747 else:748 print 'User '+username+' removed from IBM Spectrum LSF Application Center.'749 else:750 print message751 return752 753def jobdata_usage():754 print ( (_getmsg("jobdata_usage") + "\n") )755def main_jobdata(argv):756 if len(argv) == 0:757 jobdata_usage()758 return759 jobId = ''760 list = False761 try:762 opts, args = getopt.getopt(argv, "hl",['help','list'])763 except getopt.GetoptError:764 jobdata_usage()765 return766 for opt, arg in opts:767 if ((opt == "-h") | (opt == "--help")):768 jobdata_usage()769 return770 elif ((opt == "-l") | (opt == "--list")):771 list = True772 if len(args) <= 0:773 print ( _getmsg("app_specify_jobid") )774 return775 jobId = args[0]776 if list == False:777 print ( _getmsg("app_specify_param") )778 jobdata_usage()779 return780 files = jobdataList(jobId)781 if len(files) > 0:782 print ( _getmsg("app_title") )783 else:784 return785 try:786 for f in files:787 fileArray = f.split("*")788 if len(fileArray) == 5:789 print "%-20s%-40s" % (fileArray[0], fileArray[1])790 except TypeError:791 print ( _getmsg("app_nojob_data").format(jobId) )792 return793def pacinfo_usage():794 print ( (_getmsg("pacinfo_usage") + "\n") )795def main_pacinfo(argv):796 797 try:798 opts, args = getopt.getopt(argv, "h",['help'])799 except getopt.GetoptError:800 pacinfo_usage()801 return802 for opt, arg in opts:803 if ((opt == "-h") | (opt == "--help")):804 pacinfo_usage()805 return806 if len(args) > 0:807 pacinfo_usage()808 return809 810 # Invoke the /ws/version web service811 status, content = getProductInfo()812 813 version = ''814 buildNo = ''815 buildDate = ''816 productName = ''817 # If status is error, do nothing.818 if status == "ok":819 # Parsing returned XML.820 tree = ET.fromstring(content)821 productInfo = tree.getiterator("ProductInformation")822 if len(productInfo) > 0:823 for xdoc in productInfo:824 version = xdoc.find('version')825 buildNo = xdoc.find('buildNo')826 buildDate = xdoc.find('buildDate')827 productName = xdoc.find('productName')828 # Or display all product information829 print "%s Version %s Build %s, %s" % (checkFieldValidity(productName), checkFieldValidity(version), checkFieldValidity(buildNo), checkFieldValidity(buildDate))830 else:831 # Display error message832 print content833 834 # Always return whatever it returns ok or error.835 return836def main(argv):837 try:838 if ((len(argv) <= 0) | (argv[0] == 'help')):839 main_usage()840 return841 if argv[0] == 'logon':842 main_logon(argv[1:])843 return844 if argv[0] == 'submit':845 main_submit(argv[1:])846 return 847 if argv[0] == 'job':848 main_job(argv[1:])849 return 850 if argv[0] == 'download':851 main_download(argv[1:])852 return853 if argv[0] == 'logout':854 main_logout(argv[1:])855 return856 if argv[0] == 'ping':857 main_ping(argv[1:])858 return859 if argv[0] == 'jobaction':860 main_jobaction(argv[1:])861 return862 if argv[0] == 'app':863 main_app(argv[1:])864 return865 if argv[0] == 'jobdata':866 main_jobdata(argv[1:])867 return868 if argv[0] == 'usercmd':869 main_usercmd(argv[1:])870 return871 if argv[0] == 'upload':872 main_upload(argv[1:])873 return874 if argv[0] == 'pacinfo':875 main_pacinfo(argv[1:])876 return877 if argv[0] == 'useradd':878 main_userAdd(argv[1:])879 return880 if argv[0] == 'userdel':881 main_userDel(argv[1:])882 return 883 884 print ( _getmsg("app_subcmd_notsupported") % argv[0] )885 main_usage()886 return887 except socket.error:888 errno, errstr = sys.exc_info()[:2]889 if errno == socket.timeout:890 print ( _getmsg("app_timeout") )891 else:892 print ( _getmsg("app_socket_err") )893 except (KeyboardInterrupt,EOFError):894 pass895 896if __name__ == "__main__":897 # define getmsg method to get translation message898 # based on current system locale setting899 _getmsg = getTranslations().gettext900 901 if len(sys.argv) <= 1:902 main_usage()903 else:...

Full Screen

Full Screen

test_assertrewrite.py

Source:test_assertrewrite.py Github

copy

Full Screen

1import os2import stat3import sys4import zipfile5import py6import pytest7ast = pytest.importorskip("ast")8if sys.platform.startswith("java"):9 # XXX should be xfail10 pytest.skip("assert rewrite does currently not work on jython")11import _pytest._code12from _pytest.assertion import util13from _pytest.assertion.rewrite import rewrite_asserts, PYTEST_TAG14from _pytest.main import EXIT_NOTESTSCOLLECTED15def setup_module(mod):16 mod._old_reprcompare = util._reprcompare17 _pytest._code._reprcompare = None18def teardown_module(mod):19 util._reprcompare = mod._old_reprcompare20 del mod._old_reprcompare21def rewrite(src):22 tree = ast.parse(src)23 rewrite_asserts(tree)24 return tree25def getmsg(f, extra_ns=None, must_pass=False):26 """Rewrite the assertions in f, run it, and get the failure message."""27 src = '\n'.join(_pytest._code.Code(f).source().lines)28 mod = rewrite(src)29 code = compile(mod, "<test>", "exec")30 ns = {}31 if extra_ns is not None:32 ns.update(extra_ns)33 py.builtin.exec_(code, ns)34 func = ns[f.__name__]35 try:36 func()37 except AssertionError:38 if must_pass:39 pytest.fail("shouldn't have raised")40 s = str(sys.exc_info()[1])41 if not s.startswith("assert"):42 return "AssertionError: " + s43 return s44 else:45 if not must_pass:46 pytest.fail("function didn't raise at all")47class TestAssertionRewrite:48 def test_place_initial_imports(self):49 s = """'Doc string'\nother = stuff"""50 m = rewrite(s)51 assert isinstance(m.body[0], ast.Expr)52 assert isinstance(m.body[0].value, ast.Str)53 for imp in m.body[1:3]:54 assert isinstance(imp, ast.Import)55 assert imp.lineno == 256 assert imp.col_offset == 057 assert isinstance(m.body[3], ast.Assign)58 s = """from __future__ import with_statement\nother_stuff"""59 m = rewrite(s)60 assert isinstance(m.body[0], ast.ImportFrom)61 for imp in m.body[1:3]:62 assert isinstance(imp, ast.Import)63 assert imp.lineno == 264 assert imp.col_offset == 065 assert isinstance(m.body[3], ast.Expr)66 s = """'doc string'\nfrom __future__ import with_statement\nother"""67 m = rewrite(s)68 assert isinstance(m.body[0], ast.Expr)69 assert isinstance(m.body[0].value, ast.Str)70 assert isinstance(m.body[1], ast.ImportFrom)71 for imp in m.body[2:4]:72 assert isinstance(imp, ast.Import)73 assert imp.lineno == 374 assert imp.col_offset == 075 assert isinstance(m.body[4], ast.Expr)76 s = """from . import relative\nother_stuff"""77 m = rewrite(s)78 for imp in m.body[0:2]:79 assert isinstance(imp, ast.Import)80 assert imp.lineno == 181 assert imp.col_offset == 082 assert isinstance(m.body[3], ast.Expr)83 def test_dont_rewrite(self):84 s = """'PYTEST_DONT_REWRITE'\nassert 14"""85 m = rewrite(s)86 assert len(m.body) == 287 assert isinstance(m.body[0].value, ast.Str)88 assert isinstance(m.body[1], ast.Assert)89 assert m.body[1].msg is None90 def test_name(self):91 def f():92 assert False93 assert getmsg(f) == "assert False"94 def f():95 f = False96 assert f97 assert getmsg(f) == "assert False"98 def f():99 assert a_global # noqa100 assert getmsg(f, {"a_global" : False}) == "assert False"101 def f():102 assert sys == 42103 assert getmsg(f, {"sys" : sys}) == "assert sys == 42"104 def f():105 assert cls == 42 # noqa106 class X(object):107 pass108 assert getmsg(f, {"cls" : X}) == "assert cls == 42"109 def test_assert_already_has_message(self):110 def f():111 assert False, "something bad!"112 assert getmsg(f) == "AssertionError: something bad!\nassert False"113 def test_assertion_message(self, testdir):114 testdir.makepyfile("""115 def test_foo():116 assert 1 == 2, "The failure message"117 """)118 result = testdir.runpytest()119 assert result.ret == 1120 result.stdout.fnmatch_lines([121 "*AssertionError*The failure message*",122 "*assert 1 == 2*",123 ])124 def test_assertion_message_multiline(self, testdir):125 testdir.makepyfile("""126 def test_foo():127 assert 1 == 2, "A multiline\\nfailure message"128 """)129 result = testdir.runpytest()130 assert result.ret == 1131 result.stdout.fnmatch_lines([132 "*AssertionError*A multiline*",133 "*failure message*",134 "*assert 1 == 2*",135 ])136 def test_assertion_message_tuple(self, testdir):137 testdir.makepyfile("""138 def test_foo():139 assert 1 == 2, (1, 2)140 """)141 result = testdir.runpytest()142 assert result.ret == 1143 result.stdout.fnmatch_lines([144 "*AssertionError*%s*" % repr((1, 2)),145 "*assert 1 == 2*",146 ])147 def test_assertion_message_expr(self, testdir):148 testdir.makepyfile("""149 def test_foo():150 assert 1 == 2, 1 + 2151 """)152 result = testdir.runpytest()153 assert result.ret == 1154 result.stdout.fnmatch_lines([155 "*AssertionError*3*",156 "*assert 1 == 2*",157 ])158 def test_assertion_message_escape(self, testdir):159 testdir.makepyfile("""160 def test_foo():161 assert 1 == 2, 'To be escaped: %'162 """)163 result = testdir.runpytest()164 assert result.ret == 1165 result.stdout.fnmatch_lines([166 "*AssertionError: To be escaped: %",167 "*assert 1 == 2",168 ])169 def test_boolop(self):170 def f():171 f = g = False172 assert f and g173 assert getmsg(f) == "assert (False)"174 def f():175 f = True176 g = False177 assert f and g178 assert getmsg(f) == "assert (True and False)"179 def f():180 f = False181 g = True182 assert f and g183 assert getmsg(f) == "assert (False)"184 def f():185 f = g = False186 assert f or g187 assert getmsg(f) == "assert (False or False)"188 def f():189 f = g = False190 assert not f and not g191 getmsg(f, must_pass=True)192 def x():193 return False194 def f():195 assert x() and x()196 assert getmsg(f, {"x" : x}) == "assert (x())"197 def f():198 assert False or x()199 assert getmsg(f, {"x" : x}) == "assert (False or x())"200 def f():201 assert 1 in {} and 2 in {}202 assert getmsg(f) == "assert (1 in {})"203 def f():204 x = 1205 y = 2206 assert x in {1 : None} and y in {}207 assert getmsg(f) == "assert (1 in {1: None} and 2 in {})"208 def f():209 f = True210 g = False211 assert f or g212 getmsg(f, must_pass=True)213 def f():214 f = g = h = lambda: True215 assert f() and g() and h()216 getmsg(f, must_pass=True)217 def test_short_circut_evaluation(self):218 def f():219 assert True or explode # noqa220 getmsg(f, must_pass=True)221 def f():222 x = 1223 assert x == 1 or x == 2224 getmsg(f, must_pass=True)225 def test_unary_op(self):226 def f():227 x = True228 assert not x229 assert getmsg(f) == "assert not True"230 def f():231 x = 0232 assert ~x + 1233 assert getmsg(f) == "assert (~0 + 1)"234 def f():235 x = 3236 assert -x + x237 assert getmsg(f) == "assert (-3 + 3)"238 def f():239 x = 0240 assert +x + x241 assert getmsg(f) == "assert (+0 + 0)"242 def test_binary_op(self):243 def f():244 x = 1245 y = -1246 assert x + y247 assert getmsg(f) == "assert (1 + -1)"248 def f():249 assert not 5 % 4250 assert getmsg(f) == "assert not (5 % 4)"251 def test_boolop_percent(self):252 def f():253 assert 3 % 2 and False254 assert getmsg(f) == "assert ((3 % 2) and False)"255 def f():256 assert False or 4 % 2257 assert getmsg(f) == "assert (False or (4 % 2))"258 @pytest.mark.skipif("sys.version_info < (3,5)")259 def test_at_operator_issue1290(self, testdir):260 testdir.makepyfile("""261 class Matrix:262 def __init__(self, num):263 self.num = num264 def __matmul__(self, other):265 return self.num * other.num266 def test_multmat_operator():267 assert Matrix(2) @ Matrix(3) == 6""")268 testdir.runpytest().assert_outcomes(passed=1)269 def test_call(self):270 def g(a=42, *args, **kwargs):271 return False272 ns = {"g" : g}273 def f():274 assert g()275 assert getmsg(f, ns) == """assert g()"""276 def f():277 assert g(1)278 assert getmsg(f, ns) == """assert g(1)"""279 def f():280 assert g(1, 2)281 assert getmsg(f, ns) == """assert g(1, 2)"""282 def f():283 assert g(1, g=42)284 assert getmsg(f, ns) == """assert g(1, g=42)"""285 def f():286 assert g(1, 3, g=23)287 assert getmsg(f, ns) == """assert g(1, 3, g=23)"""288 def f():289 seq = [1, 2, 3]290 assert g(*seq)291 assert getmsg(f, ns) == """assert g(*[1, 2, 3])"""292 def f():293 x = "a"294 assert g(**{x : 2})295 assert getmsg(f, ns) == """assert g(**{'a': 2})"""296 def test_attribute(self):297 class X(object):298 g = 3299 ns = {"x" : X}300 def f():301 assert not x.g # noqa302 assert getmsg(f, ns) == """assert not 3303 + where 3 = x.g"""304 def f():305 x.a = False # noqa306 assert x.a # noqa307 assert getmsg(f, ns) == """assert x.a"""308 def test_comparisons(self):309 def f():310 a, b = range(2)311 assert b < a312 assert getmsg(f) == """assert 1 < 0"""313 def f():314 a, b, c = range(3)315 assert a > b > c316 assert getmsg(f) == """assert 0 > 1"""317 def f():318 a, b, c = range(3)319 assert a < b > c320 assert getmsg(f) == """assert 1 > 2"""321 def f():322 a, b, c = range(3)323 assert a < b <= c324 getmsg(f, must_pass=True)325 def f():326 a, b, c = range(3)327 assert a < b328 assert b < c329 getmsg(f, must_pass=True)330 def test_len(self):331 def f():332 l = list(range(10))333 assert len(l) == 11334 assert getmsg(f).startswith("""assert 10 == 11335 + where 10 = len([""")336 def test_custom_reprcompare(self, monkeypatch):337 def my_reprcompare(op, left, right):338 return "42"339 monkeypatch.setattr(util, "_reprcompare", my_reprcompare)340 def f():341 assert 42 < 3342 assert getmsg(f) == "assert 42"343 def my_reprcompare(op, left, right):344 return "%s %s %s" % (left, op, right)345 monkeypatch.setattr(util, "_reprcompare", my_reprcompare)346 def f():347 assert 1 < 3 < 5 <= 4 < 7348 assert getmsg(f) == "assert 5 <= 4"349 def test_assert_raising_nonzero_in_comparison(self):350 def f():351 class A(object):352 def __nonzero__(self):353 raise ValueError(42)354 def __lt__(self, other):355 return A()356 def __repr__(self):357 return "<MY42 object>"358 def myany(x):359 return False360 assert myany(A() < 0)361 assert "<MY42 object> < 0" in getmsg(f)362 def test_formatchar(self):363 def f():364 assert "%test" == "test"365 assert getmsg(f).startswith("assert '%test' == 'test'")366 def test_custom_repr(self):367 def f():368 class Foo(object):369 a = 1370 def __repr__(self):371 return "\n{ \n~ \n}"372 f = Foo()373 assert 0 == f.a374 assert r"where 1 = \n{ \n~ \n}.a" in util._format_lines([getmsg(f)])[0]375class TestRewriteOnImport:376 def test_pycache_is_a_file(self, testdir):377 testdir.tmpdir.join("__pycache__").write("Hello")378 testdir.makepyfile("""379 def test_rewritten():380 assert "@py_builtins" in globals()""")381 assert testdir.runpytest().ret == 0382 def test_pycache_is_readonly(self, testdir):383 cache = testdir.tmpdir.mkdir("__pycache__")384 old_mode = cache.stat().mode385 cache.chmod(old_mode ^ stat.S_IWRITE)386 testdir.makepyfile("""387 def test_rewritten():388 assert "@py_builtins" in globals()""")389 try:390 assert testdir.runpytest().ret == 0391 finally:392 cache.chmod(old_mode)393 def test_zipfile(self, testdir):394 z = testdir.tmpdir.join("myzip.zip")395 z_fn = str(z)396 f = zipfile.ZipFile(z_fn, "w")397 try:398 f.writestr("test_gum/__init__.py", "")399 f.writestr("test_gum/test_lizard.py", "")400 finally:401 f.close()402 z.chmod(256)403 testdir.makepyfile("""404 import sys405 sys.path.append(%r)406 import test_gum.test_lizard""" % (z_fn,))407 assert testdir.runpytest().ret == EXIT_NOTESTSCOLLECTED408 def test_readonly(self, testdir):409 sub = testdir.mkdir("testing")410 sub.join("test_readonly.py").write(411 py.builtin._totext("""412def test_rewritten():413 assert "@py_builtins" in globals()414 """).encode("utf-8"), "wb")415 old_mode = sub.stat().mode416 sub.chmod(320)417 try:418 assert testdir.runpytest().ret == 0419 finally:420 sub.chmod(old_mode)421 def test_dont_write_bytecode(self, testdir, monkeypatch):422 testdir.makepyfile("""423 import os424 def test_no_bytecode():425 assert "__pycache__" in __cached__426 assert not os.path.exists(__cached__)427 assert not os.path.exists(os.path.dirname(__cached__))""")428 monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")429 assert testdir.runpytest_subprocess().ret == 0430 @pytest.mark.skipif('"__pypy__" in sys.modules')431 def test_pyc_vs_pyo(self, testdir, monkeypatch):432 testdir.makepyfile("""433 import pytest434 def test_optimized():435 "hello"436 assert test_optimized.__doc__ is None"""437 )438 p = py.path.local.make_numbered_dir(prefix="runpytest-", keep=None,439 rootdir=testdir.tmpdir)440 tmp = "--basetemp=%s" % p441 monkeypatch.setenv("PYTHONOPTIMIZE", "2")442 monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)443 assert testdir.runpytest_subprocess(tmp).ret == 0444 tagged = "test_pyc_vs_pyo." + PYTEST_TAG445 assert tagged + ".pyo" in os.listdir("__pycache__")446 monkeypatch.undo()447 monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)448 assert testdir.runpytest_subprocess(tmp).ret == 1449 assert tagged + ".pyc" in os.listdir("__pycache__")450 def test_package(self, testdir):451 pkg = testdir.tmpdir.join("pkg")452 pkg.mkdir()453 pkg.join("__init__.py").ensure()454 pkg.join("test_blah.py").write("""455def test_rewritten():456 assert "@py_builtins" in globals()""")457 assert testdir.runpytest().ret == 0458 def test_translate_newlines(self, testdir):459 content = "def test_rewritten():\r\n assert '@py_builtins' in globals()"460 b = content.encode("utf-8")461 testdir.tmpdir.join("test_newlines.py").write(b, "wb")462 assert testdir.runpytest().ret == 0463 @pytest.mark.skipif(sys.version_info < (3,3),464 reason='packages without __init__.py not supported on python 2')465 def test_package_without__init__py(self, testdir):466 pkg = testdir.mkdir('a_package_without_init_py')467 pkg.join('module.py').ensure()468 testdir.makepyfile("import a_package_without_init_py.module")469 assert testdir.runpytest().ret == EXIT_NOTESTSCOLLECTED470class TestAssertionRewriteHookDetails(object):471 def test_loader_is_package_false_for_module(self, testdir):472 testdir.makepyfile(test_fun="""473 def test_loader():474 assert not __loader__.is_package(__name__)475 """)476 result = testdir.runpytest()477 result.stdout.fnmatch_lines([478 "* 1 passed*",479 ])480 def test_loader_is_package_true_for_package(self, testdir):481 testdir.makepyfile(test_fun="""482 def test_loader():483 assert not __loader__.is_package(__name__)484 def test_fun():485 assert __loader__.is_package('fun')486 def test_missing():487 assert not __loader__.is_package('pytest_not_there')488 """)489 testdir.mkpydir('fun')490 result = testdir.runpytest()491 result.stdout.fnmatch_lines([492 '* 3 passed*',493 ])494 @pytest.mark.skipif("sys.version_info[0] >= 3")495 @pytest.mark.xfail("hasattr(sys, 'pypy_translation_info')")496 def test_assume_ascii(self, testdir):497 content = "u'\xe2\x99\xa5\x01\xfe'"498 testdir.tmpdir.join("test_encoding.py").write(content, "wb")499 res = testdir.runpytest()500 assert res.ret != 0501 assert "SyntaxError: Non-ASCII character" in res.stdout.str()502 @pytest.mark.skipif("sys.version_info[0] >= 3")503 def test_detect_coding_cookie(self, testdir):504 testdir.makepyfile(test_cookie="""505 # -*- coding: utf-8 -*-506 u"St\xc3\xa4d"507 def test_rewritten():508 assert "@py_builtins" in globals()""")509 assert testdir.runpytest().ret == 0510 @pytest.mark.skipif("sys.version_info[0] >= 3")511 def test_detect_coding_cookie_second_line(self, testdir):512 testdir.makepyfile(test_cookie="""513 # -*- coding: utf-8 -*-514 u"St\xc3\xa4d"515 def test_rewritten():516 assert "@py_builtins" in globals()""")517 assert testdir.runpytest().ret == 0518 @pytest.mark.skipif("sys.version_info[0] >= 3")519 def test_detect_coding_cookie_crlf(self, testdir):520 testdir.makepyfile(test_cookie="""521 # -*- coding: utf-8 -*-522 u"St\xc3\xa4d"523 def test_rewritten():524 assert "@py_builtins" in globals()""")525 assert testdir.runpytest().ret == 0526 def test_sys_meta_path_munged(self, testdir):527 testdir.makepyfile("""528 def test_meta_path():529 import sys; sys.meta_path = []""")530 assert testdir.runpytest().ret == 0531 def test_write_pyc(self, testdir, tmpdir, monkeypatch):532 from _pytest.assertion.rewrite import _write_pyc533 from _pytest.assertion import AssertionState534 try:535 import __builtin__ as b536 except ImportError:537 import builtins as b538 config = testdir.parseconfig([])539 state = AssertionState(config, "rewrite")540 source_path = tmpdir.ensure("source.py")541 pycpath = tmpdir.join("pyc").strpath542 assert _write_pyc(state, [1], source_path.stat(), pycpath)543 def open(*args):544 e = IOError()545 e.errno = 10546 raise e547 monkeypatch.setattr(b, "open", open)548 assert not _write_pyc(state, [1], source_path.stat(), pycpath)549 def test_resources_provider_for_loader(self, testdir):550 """551 Attempts to load resources from a package should succeed normally,552 even when the AssertionRewriteHook is used to load the modules.553 See #366 for details.554 """555 pytest.importorskip("pkg_resources")556 testdir.mkpydir('testpkg')557 contents = {558 'testpkg/test_pkg': """559 import pkg_resources560 import pytest561 from _pytest.assertion.rewrite import AssertionRewritingHook562 def test_load_resource():563 assert isinstance(__loader__, AssertionRewritingHook)564 res = pkg_resources.resource_string(__name__, 'resource.txt')565 res = res.decode('ascii')566 assert res == 'Load me please.'567 """,568 }569 testdir.makepyfile(**contents)570 testdir.maketxtfile(**{'testpkg/resource': "Load me please."})571 result = testdir.runpytest_subprocess()572 result.assert_outcomes(passed=1)573 def test_read_pyc(self, tmpdir):574 """575 Ensure that the `_read_pyc` can properly deal with corrupted pyc files.576 In those circumstances it should just give up instead of generating577 an exception that is propagated to the caller.578 """579 import py_compile580 from _pytest.assertion.rewrite import _read_pyc581 source = tmpdir.join('source.py')582 pyc = source + 'c'583 source.write('def test(): pass')584 py_compile.compile(str(source), str(pyc))585 contents = pyc.read(mode='rb')586 strip_bytes = 20 # header is around 8 bytes, strip a little more587 assert len(contents) > strip_bytes588 pyc.write(contents[:strip_bytes], mode='wb')589 assert _read_pyc(source, str(pyc)) is None # no error590 def test_reload_is_same(self, testdir):591 # A file that will be picked up during collecting.592 testdir.tmpdir.join("file.py").ensure()593 testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent("""594 [pytest]595 python_files = *.py596 """))597 testdir.makepyfile(test_fun="""598 import sys599 try:600 from imp import reload601 except ImportError:602 pass603 def test_loader():604 import file605 assert sys.modules["file"] is reload(file)606 """)607 result = testdir.runpytest('-s')608 result.stdout.fnmatch_lines([609 "* 1 passed*",610 ])611 def test_get_data_support(self, testdir):612 """Implement optional PEP302 api (#808).613 """614 path = testdir.mkpydir("foo")615 path.join("test_foo.py").write(_pytest._code.Source("""616 class Test:617 def test_foo(self):618 import pkgutil619 data = pkgutil.get_data('foo.test_foo', 'data.txt')620 assert data == b'Hey'621 """))622 path.join('data.txt').write('Hey')623 result = testdir.runpytest()624 result.stdout.fnmatch_lines('*1 passed*')625def test_issue731(testdir):626 testdir.makepyfile("""627 class LongReprWithBraces(object):628 def __repr__(self):629 return 'LongReprWithBraces({' + ('a' * 80) + '}' + ('a' * 120) + ')'630 def some_method(self):631 return False632 def test_long_repr():633 obj = LongReprWithBraces()634 assert obj.some_method()635 """)636 result = testdir.runpytest()637 assert 'unbalanced braces' not in result.stdout.str()638def test_collapse_false_unbalanced_braces():...

Full Screen

Full Screen

tools.py

Source:tools.py Github

copy

Full Screen

1#coding: utf-82# +-------------------------------------------------------------------3# | 宝塔Linux面板4# +-------------------------------------------------------------------5# | Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.6# +-------------------------------------------------------------------7# | Author: hwliang <hwl@bt.cn>8# +-------------------------------------------------------------------9#------------------------------10# 工具箱11#------------------------------12import sys,os13panelPath = '/www/server/panel/';14os.chdir(panelPath)15if not 'class/' in sys.path:16 sys.path.insert(0,'class/')17import public,time,json18#设置MySQL密码19def set_mysql_root(password):20 import db,os21 sql = db.Sql()22 23 root_mysql = '''#!/bin/bash24PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin25export PATH26pwd=$127service mysqld stop28mysqld_safe --skip-grant-tables&29echo '正在修改密码...';30echo 'The set password...';31sleep 632m_version=$(cat /www/server/mysql/version.pl|grep -E "(5.1.|5.5.|5.6.)")33if [ "$m_version" != "" ];then34 mysql -uroot -e "insert into mysql.user(Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Reload_priv,Shutdown_priv,Process_priv,File_priv,Grant_priv,References_priv,Index_priv,Alter_priv,Show_db_priv,Super_priv,Create_tmp_table_priv,Lock_tables_priv,Execute_priv,Repl_slave_priv,Repl_client_priv,Create_view_priv,Show_view_priv,Create_routine_priv,Alter_routine_priv,Create_user_priv,Event_priv,Trigger_priv,Create_tablespace_priv,User,Password,host)values('Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','root',password('${pwd}'),'127.0.0.1')"35 mysql -uroot -e "insert into mysql.user(Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Reload_priv,Shutdown_priv,Process_priv,File_priv,Grant_priv,References_priv,Index_priv,Alter_priv,Show_db_priv,Super_priv,Create_tmp_table_priv,Lock_tables_priv,Execute_priv,Repl_slave_priv,Repl_client_priv,Create_view_priv,Show_view_priv,Create_routine_priv,Alter_routine_priv,Create_user_priv,Event_priv,Trigger_priv,Create_tablespace_priv,User,Password,host)values('Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','root',password('${pwd}'),'localhost')"36 mysql -uroot -e "UPDATE mysql.user SET password=PASSWORD('${pwd}') WHERE user='root'";37else38 mysql -uroot -e "UPDATE mysql.user SET authentication_string='' WHERE user='root'";39 mysql -uroot -e "FLUSH PRIVILEGES";40 mysql -uroot -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${pwd}';";41fi42mysql -uroot -e "FLUSH PRIVILEGES";43pkill -9 mysqld_safe44pkill -9 mysqld45sleep 246service mysqld start47echo '==========================================='48echo "root密码成功修改为: ${pwd}"49echo "The root password set ${pwd} successuful"''';50 51 public.writeFile('mysql_root.sh',root_mysql)52 os.system("/bin/bash mysql_root.sh " + password)53 os.system("rm -f mysql_root.sh")54 55 result = sql.table('config').where('id=?',(1,)).setField('mysql_root',password)56 print(result);57#设置面板密码58def set_panel_pwd(password,ncli = False):59 import db60 sql = db.Sql()61 result = sql.table('users').where('id=?',(1,)).setField('password',public.md5(password))62 username = sql.table('users').where('id=?',(1,)).getField('username')63 if ncli:64 print("|-%s: " % public.GetMsg("USER_NAME") + username)65 print("|-%s: " % public.GetMsg("NEW_PASS") + password)66 else:67 print(username)68#设置数据库目录69def set_mysql_dir(path):70 mysql_dir = '''#!/bin/bash71PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin72export PATH73oldDir=`cat /etc/my.cnf |grep 'datadir'|awk '{print $3}'`74newDir=$175mkdir $newDir76if [ ! -d "${newDir}" ];then77 echo 'The specified storage path does not exist!'78 exit79fi80echo "Stopping MySQL service..."81service mysqld stop82echo "Copying files, please wait..."83\cp -r -a $oldDir/* $newDir84chown -R mysql.mysql $newDir85sed -i "s#$oldDir#$newDir#" /etc/my.cnf86echo "Starting MySQL service..."87service mysqld start88echo ''89echo 'Successful'90echo '---------------------------------------------------------------------'91echo "Has changed the MySQL storage directory to: $newDir"92echo '---------------------------------------------------------------------'93''';94 public.writeFile('mysql_dir.sh',mysql_dir)95 os.system("/bin/bash mysql_dir.sh " + path)96 os.system("rm -f mysql_dir.sh")97#封装98def PackagePanel():99 print('========================================================')100 print('|-%s...' % public.GetMsg("CLEARING_LOG")),101 public.M('logs').where('id!=?',(0,)).delete();102 print('\t\t\033[1;32m[done]\033[0m')103 print('|-%s...' % public.GetMsg("CLEARING_TASK_HISTORY")),104 public.M('tasks').where('id!=?',(0,)).delete();105 print('\t\t\033[1;32m[done]\033[0m')106 print('|-%s...' % public.GetMsg("CLEARING_NET_MO")),107 public.M('network').dbfile('system').where('id!=?',(0,)).delete();108 print('\t\033[1;32m[done]\033[0m')109 print('|-%s...' % public.GetMsg("CLEARING_CPU_MO")),110 public.M('cpuio').dbfile('system').where('id!=?',(0,)).delete();111 print('\t\033[1;32m[done]\033[0m')112 print('|-%s...' % public.GetMsg("CLEARING_DISK_MO")),113 public.M('diskio').dbfile('system').where('id!=?',(0,)).delete();114 print('\t\033[1;32m[done]\033[0m')115 print('|-%s...' % public.GetMsg('CLEARING_IP')),116 os.system('rm -f /www/server/panel/data/iplist.txt')117 os.system('rm -f /www/server/panel/data/address.pl')118 os.system('rm -f /www/server/panel/data/*.login')119 os.system('rm -f /www/server/panel/data/domain.conf')120 print('\t\033[1;32m[done]\033[0m')121 print('|-%s...' % public.GetMsg("CLEARING_SYS_HISTORY")),122 command = '''cat /dev/null > /var/log/boot.log123cat /dev/null > /var/log/btmp124cat /dev/null > /var/log/cron125cat /dev/null > /var/log/dmesg126cat /dev/null > /var/log/firewalld127cat /dev/null > /var/log/grubby128cat /dev/null > /var/log/lastlog129cat /dev/null > /var/log/mail.info130cat /dev/null > /var/log/maillog131cat /dev/null > /var/log/messages132cat /dev/null > /var/log/secure133cat /dev/null > /var/log/spooler134cat /dev/null > /var/log/syslog135cat /dev/null > /var/log/tallylog136cat /dev/null > /var/log/wpa_supplicant.log137cat /dev/null > /var/log/wtmp138cat /dev/null > /var/log/yum.log139history -c140'''141 os.system(command);142 print('\t\033[1;32m[done]\033[0m')143 public.writeFile('/www/server/panel/install.pl',"True");144 port = public.readFile('data/port.pl').strip();145 public.M('config').where("id=?",('1',)).setField('status',0);146 print('========================================================')147 print('\033[1;32m|-%s\033[0m' % public.GetMsg("PANEL_TIPS"))148 print('\033[1;41m|-%s: http://{SERVERIP}:' % public.GetMsg("PANEL_INIT_ADD")+port+'/install\033[0m')149#清空正在执行的任务150def CloseTask():151 ncount = public.M('tasks').where('status!=?',(1,)).delete();152 os.system("kill `ps -ef |grep 'python panelSafe.pyc'|grep -v grep|grep -v panelExec|awk '{print $2}'`");153 os.system("kill `ps -ef |grep 'install_soft.sh'|grep -v grep|grep -v panelExec|awk '{print $2}'`");154 os.system('/etc/init.d/bt restart');155 print(public.GetMsg("CLEAR_TASK",(int(ncount),)))156 157#自签证书158def CreateSSL():159 import OpenSSL160 key = OpenSSL.crypto.PKey()161 key.generate_key( OpenSSL.crypto.TYPE_RSA, 2048 )162 cert = OpenSSL.crypto.X509()163 cert.set_serial_number(0)164 cert.get_subject().CN = public.GetLocalIp();165 cert.set_issuer(cert.get_subject())166 cert.gmtime_adj_notBefore( 0 )167 cert.gmtime_adj_notAfter( 10*365*24*60*60 )168 cert.set_pubkey( key )169 cert.sign( key, 'md5' )170 cert_ca = OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)171 private_key = OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key)172 if len(cert_ca) > 100 and len(private_key) > 100:173 public.writeFile('ssl/certificate.pem',cert_ca)174 public.writeFile('ssl/privateKey.pem',private_key)175 print('success');176 return;177 print('error');178#创建文件179def CreateFiles(path,num):180 if not os.path.exists(path): os.system('mkdir -p ' + path);181 import time;182 for i in range(num):183 filename = path + '/' + str(time.time()) + '__' + str(i)184 open(path,'w+').close()185#计算文件数量186def GetFilesCount(path):187 i=0;188 for name in os.listdir(path): i += 1;189 return i;190#清理系统垃圾191def ClearSystem():192 count = total = 0;193 tmp_total,tmp_count = ClearMail();194 count += tmp_count;195 total += tmp_total;196 print('=======================================================================')197 tmp_total,tmp_count = ClearSession();198 count += tmp_count;199 total += tmp_total;200 print('=======================================================================')201 tmp_total,tmp_count = ClearOther();202 count += tmp_count;203 total += tmp_total;204 print('=======================================================================')205 print('\033[1;32m|-%s\033[0m' % public.GetMsg("CLEAR_RUBBISH",(str(count),ToSize(total))));206#清理邮件日志207def ClearMail():208 rpath = '/var/spool';209 total = count = 0;210 import shutil211 con = ['cron','anacron','mail'];212 for d in os.listdir(rpath):213 if d in con: continue;214 dpath = rpath + '/' + d215 print('|-正在清理' + dpath + ' ...');216 time.sleep(0.2);217 num = size = 0;218 for n in os.listdir(dpath):219 filename = dpath + '/' + n220 fsize = os.path.getsize(filename);221 print('|---['+ToSize(fsize)+'] del ' + filename),222 size += fsize223 if os.path.isdir(filename):224 shutil.rmtree(filename)225 else:226 os.remove(filename)227 print('\t\033[1;32m[OK]\033[0m')228 num += 1229 print(public.GetMsg("CLEAR_RUBBISH1",(dpath,str(num),ToSize(size))))230 total += size;231 count += num;232 print('=======================================================================')233 print(public.GetMsg('CLEAR_RUBBISH2',(str(count),ToSize(total))))234 return total,count235#清理php_session文件236def ClearSession():237 spath = '/tmp'238 total = count = 0;239 import shutil240 print(public.GetMsg("CLEAR_PHP_SESSION"));241 for d in os.listdir(spath):242 if d.find('sess_') == -1: continue;243 filename = spath + '/' + d;244 fsize = os.path.getsize(filename);245 print('|---['+ToSize(fsize)+'] del ' + filename),246 total += fsize247 if os.path.isdir(filename):248 shutil.rmtree(filename)249 else:250 os.remove(filename)251 print('\t\033[1;32m[OK]\033[0m')252 count += 1;253 print(public.GetMsg("CLEAR_PHP_SESSION1",(str(count),ToSize(total))))254 return total,count255#清空回收站256def ClearRecycle_Bin():257 import files258 f = files.files();259 f.Close_Recycle_bin(None);260 261#清理其它262def ClearOther():263 clearPath = [264 {'path':'/www/server/panel','find':'testDisk_'},265 {'path':'/www/wwwlogs','find':'log'},266 {'path':'/tmp','find':'panelBoot.pl'},267 {'path':'/www/server/panel/install','find':'.rpm'},268 {'path':'/www/server/panel/install','find':'.zip'},269 {'path':'/www/server/panel/install','find':'.gz'}270 ]271 272 total = count = 0;273 print(public.GetMsg('CLEAR_RUBBISH3'));274 for c in clearPath:275 for d in os.listdir(c['path']):276 if d.find(c['find']) == -1: continue;277 filename = c['path'] + '/' + d;278 fsize = os.path.getsize(filename);279 print('|---['+ToSize(fsize)+'] del ' + filename),280 total += fsize281 if os.path.isdir(filename):282 shutil.rmtree(filename)283 else:284 os.remove(filename)285 print('\t\033[1;32m[OK]\033[0m')286 count += 1;287 public.serviceReload();288 os.system('sleep 1 && /etc/init.d/bt reload > /dev/null &');289 print(public.GetMsg("CLEAR_RUBBISH4",(str(count),ToSize(total))))290 return total,count291#关闭普通日志292def CloseLogs():293 try:294 paths = ['/usr/lib/python2.7/site-packages/web/httpserver.py','/usr/lib/python2.6/site-packages/web/httpserver.py']295 for path in paths:296 if not os.path.exists(path): continue;297 hsc = public.readFile(path);298 if hsc.find('500 Internal Server Error') != -1: continue;299 rstr = '''def log(self, status, environ):300 if status != '500 Internal Server Error': return;''';301 hsc = hsc.replace("def log(self, status, environ):",rstr)302 if hsc.find('500 Internal Server Error') == -1: return False;303 public.writeFile(path,hsc)304 except:pass;305#字节单位转换306def ToSize(size):307 ds = ['b','KB','MB','GB','TB']308 for d in ds:309 if size < 1024: return str(size)+d;310 size = size / 1024;311 return '0b';312#随机面板用户名313def set_panel_username(username = None):314 import db315 sql = db.Sql()316 if username:317 if len(username) < 5:318 print(public.GetMsg("USER_NAME_LEN_ERR"))319 return;320 if username in ['admin','root']:321 print(public.GetMsg("EASY_NAME"))322 return;323 sql.table('users').where('id=?',(1,)).setField('username',username)324 print(public.GetMsg("NEW_NAME",(username,)))325 return;326 327 username = sql.table('users').where('id=?',(1,)).getField('username')328 if username == 'admin': 329 username = public.GetRandomString(8).lower()330 sql.table('users').where('id=?',(1,)).setField('username',username)331 print('username: ' + username)332 333#设定idc334def setup_idc():335 try:336 panelPath = '/www/server/panel'337 filename = panelPath + '/data/o.pl'338 if not os.path.exists(filename): return False339 o = public.readFile(filename).strip()340 c_url = 'http://www.bt.cn/api/idc/get_idc_info_bycode?o=%s' % o341 idcInfo = json.loads(public.httpGet(c_url))342 if not idcInfo['status']: return False343 pFile = panelPath + '/static/language/Simplified_Chinese/public.json'344 pInfo = json.loads(public.readFile(pFile))345 pInfo['BRAND'] = idcInfo['msg']['name']346 pInfo['PRODUCT'] = public.GetMsg("WITH_BT_CUSTOM_EDITION")347 pInfo['NANE'] = pInfo['BRAND'] + pInfo['PRODUCT']348 public.writeFile(pFile,json.dumps(pInfo))349 tFile = panelPath + '/data/title.pl'350 titleNew = (pInfo['BRAND'] + public.GetMsg("PANEL")).encode('utf-8')351 if os.path.exists(tFile):352 title = public.readFile(tFile).strip()353 if title == public.GetMsg("NAME") or title == '': public.writeFile(tFile,titleNew)354 else:355 public.writeFile(tFile,titleNew)356 return True357 except:pass358#将插件升级到6.0359def update_to6():360 print("====================================================")361 print(public.GetMsg("PLUG_UPDATEING"))362 print("====================================================")363 download_address = public.get_url()364 exlodes = ['gitlab','pm2','mongodb','deployment_jd','logs','docker','beta','btyw']365 for pname in os.listdir('plugin/'):366 if not os.path.isdir('plugin/' + pname): continue367 if pname in exlodes: continue368 print("|-正在升级【%s】..." % pname),369 download_url = download_address + '/install/plugin/' + pname + '/install.sh';370 to_file = '/tmp/%s.sh' % pname371 public.downloadFile(download_url,to_file);372 os.system('/bin/bash ' + to_file + ' install &> /tmp/plugin_update.log 2>&1');373 print(" \033[32m[%s]\033[0m" % public.GetMsg("SUCCESS"))374 print("====================================================")375 print("\033[32m%s\033[0m" % public.GetMsg("PLUG_UPDATE_TO_6"))376 print("====================================================")377#命令行菜单378def bt_cli():379 raw_tip = "==============================================="380 print("===============%s==================" % public.GetMsg("PANEL_SHELL"))381 print("(01) %s (08) %s" % (public.GetMsg("RESTART_PANEL"),public.GetMsg("CHANGE_PANEL_PORT")))382 print("(02) %s (09) %s" % (public.GetMsg("STOP_PANEL"),public.GetMsg("CLEAR_PANEL_CACHE")))383 print("(03) %s (10) %s" % (public.GetMsg("START_PANEL"),public.GetMsg("CLEAR_PANEL_LIMIT")))384 print("(04) %s (11) %s" % (public.GetMsg("RELOAD_PANEL"),public.GetMsg("CANCEL_ENTRY")))385 print("(05) %s (12) %s" % (public.GetMsg("CHANGE_PANEL_PASS"),public.GetMsg("CANCEL_DOMAIN_BIND")))386 print("(06) %s (13) %s" % (public.GetMsg("CHANGE_PANEL_USER"),public.GetMsg("CANCEL_IP_LIMIT")))387 print("(07) %s (14) %s" % (public.GetMsg("CHANGE_MYSQL_PASS_FORCE"),public.GetMsg("GET_PANEL_DEFAULT_MSG")))388 print("(00) %s (15) %s" % (public.GetMsg("CANCEL"),public.GetMsg("CLEAR_SYS_RUBBISH")))389 print(raw_tip)390 try:391 u_input = input(public.GetMsg("INPUT_CMD_NUM"))392 if sys.version_info[0] == 3: u_input = int(u_input)393 except: u_input = 0394 nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]395 if not u_input in nums:396 print(raw_tip)397 print(public.GetMsg("CANCELLED"))398 exit()399 print(raw_tip)400 print(public.GetMsg("EXECUTING",(u_input,)))401 print(raw_tip)402 if u_input == 1:403 os.system("/etc/init.d/bt restart")404 elif u_input == 2:405 os.system("/etc/init.d/bt stop")406 elif u_input == 3:407 os.system("/etc/init.d/bt start")408 elif u_input == 4:409 os.system("/etc/init.d/bt reload")410 elif u_input == 5:411 if sys.version_info[0] == 2:412 input_pwd = raw_input(public.GetMsg("INPUT_NEW_PASS"))413 else:414 input_pwd = input(public.GetMsg("INPUT_NEW_PASS"))415 set_panel_pwd(input_pwd.strip(),True)416 elif u_input == 6:417 if sys.version_info[0] == 2:418 input_user = raw_input(public.GetMsg("INPUT_NEW_USER"))419 else:420 input_user = input(public.GetMsg("INPUT_NEW_USER"))421 set_panel_username(input_user.strip())422 elif u_input == 7:423 if sys.version_info[0] == 2:424 input_mysql = raw_input(public.GetMsg("INPUT_NEW_MYSQL_PASS"))425 else:426 input_mysql = input(public.GetMsg("INPUT_NEW_MYSQL_PASS"))427 if not input_mysql:428 print(public.GetMsg("PASS_NOT_EMPTY"))429 return;430 if len(input_mysql) < 8:431 print(public.GetMsg("PASS_LEN_ERR"))432 return;433 import re434 rep = "^[\w@\._]+$"435 if not re.match(rep, input_mysql):436 print(public.GetMsg("PASS_SPECIAL_CHARACTRES_ERR"))437 return;438 439 print(input_mysql)440 set_mysql_root(input_mysql.strip())441 elif u_input == 8:442 input_port = input(public.GetMsg("INPUT_NEW_PANEL_PORT"))443 if sys.version_info[0] == 3: input_port = int(input_port)444 if not input_port:445 print(public.GetMsg("INPUT_PANEL_PORT_ERR"))446 return;447 if input_port in [80,443,21,20,22]:448 print(public.GetMsg("CANT_USE_USUALLY_PORT_ERR"))449 return;450 old_port = int(public.readFile('data/port.pl'))451 if old_port == input_port:452 print(public.GetMsg("NEW_PORT_SAMEAS_OLD"))453 return;454 is_exists = public.ExecShell("lsof -i:%s" % input_port)455 if len(is_exists[0]) > 5:456 print(public.GetMsg("PORT_ALREADY_IN_USE"))457 return;458 public.writeFile('data/port.pl',str(input_port))459 if os.path.exists("/usr/bin/firewall-cmd"):460 os.system("firewall-cmd --permanent --zone=public --add-port=%s/tcp" % input_port)461 os.system("firewall-cmd --reload")462 elif os.path.exists("/etc/sysconfig/iptables"):463 os.system("iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport %s -j ACCEPT" % input_port)464 os.system("service iptables save")465 else:466 os.system("ufw allow %s" % input_port)467 os.system("ufw reload")468 print("CHANGE_PORT_SUCCESS" , (input_port,))469 print("CLOUD_RELEASE_PORT" , (input_port,))470 elif u_input == 9:471 sess_file = '/dev/shm/session.db'472 if os.path.exists(sess_file): os.remove(sess_file)473 os.system("/etc/init.d/bt reload")474 elif u_input == 10:475 os.system("/etc/init.d/bt reload")476 elif u_input == 11:477 auth_file = 'data/admin_path.pl'478 if os.path.exists(auth_file): os.remove(auth_file)479 os.system("/etc/init.d/bt reload")480 print(public.GetMsg("CHANGE_LIMITED_CANCEL"))481 elif u_input == 12:482 auth_file = 'data/domain.conf'483 if os.path.exists(auth_file): os.remove(auth_file)484 os.system("/etc/init.d/bt reload")485 print(public.GetMsg("CHANGE_DOMAIN_CANCEL"))486 elif u_input == 13:487 auth_file = 'data/limitip.conf'488 if os.path.exists(auth_file): os.remove(auth_file)489 os.system("/etc/init.d/bt reload")490 print(public.GetMsg("CHANGE_IP_CANCEL"))491 elif u_input == 14:492 os.system("/etc/init.d/bt default")493 elif u_input == 15:494 ClearSystem()495if __name__ == "__main__":496 type = sys.argv[1];497 if type == 'root':498 set_mysql_root(sys.argv[2])499 elif type == 'panel':500 set_panel_pwd(sys.argv[2])501 elif type == 'username':502 set_panel_username()503 elif type == 'o':504 setup_idc()505 elif type == 'mysql_dir':506 set_mysql_dir(sys.argv[2])507 elif type == 'to':508 panel2To3()509 elif type == 'package':510 PackagePanel();511 elif type == 'ssl':512 CreateSSL();513 elif type == 'port':514 CheckPort();515 elif type == 'clear':516 ClearSystem();517 elif type == 'closelog':518 CloseLogs();519 elif type == 'update_to6':520 update_to6()521 elif type == "cli":522 bt_cli()523 else:...

Full Screen

Full Screen

yusarin.py

Source:yusarin.py Github

copy

Full Screen

...114 gotCommand(message)115 116 if message.author.id == config["owner"]:117 118 await message.reply(embed=makeEmbed(description=getMsg("shutdown", message.guild).format(message.author), color=strToColor(config["color_default"])), mention_author=False)119 os.system(f"kill -9 {str(pid)}")120 121 else:122 123 return124 125 elif message.content.startswith(f"{prefix}channel"):126 127 gotCommand(message)128 129 fullcmd = message.content.split()130 131 if message.guild is not None:132 133 if message.author.guild_permissions.administrator:134 135 try:136 137 if fullcmd[1] == "reset":138 139 if guildConfGet(message.guild, "channel") is not None:140 141 guildConfReset(message.guild, "channel")142 143 await message.reply(embed=makeEmbed(title=getMsg("reset_channel_title", message.guild), description=getMsg("reset_channel_description", message.guild).format(prefix), color=strToColor(config["color_ok"])), mention_author=False)144 145 else:146 147 await message.reply(embed=makeEmbed(title=getMsg("hint_none_channel_title", message.guild), description=getMsg("hint_none_channel_description", message.guild).format(prefix), color=strToColor(config["color_warn"])), mention_author=False)148 149 else:150 151 selected_channel = discord.utils.get(message.guild.channels, id=int(fullcmd[1]))152 if isinstance(selected_channel, discord.VoiceChannel):153 154 guildConfSet(message.guild, "channel", int(fullcmd[1]))155 156 await message.reply(embed=makeEmbed(title=getMsg("set_channel_title", message.guild), description=getMsg("set_channel_description", message.guild).format(selected_channel.name), color=strToColor(config["color_ok"])), mention_author=False)157 158 if guildConfGet(message.guild, "category") is None:159 160 await message.channel.send(embed=makeEmbed(title=getMsg("hint_none_category_title", message.guild), description=getMsg("hint_none_category_description", message.guild).format(prefix), color=strToColor(config["color_warn"])))161 162 else:163 await message.reply(embed=makeEmbed(title=getMsg("error_text_channel_title", message.guild), description=getMsg("error_text_channel_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)164 165 except Exception as exp:166 167 if debug:168 print(exp)169 170 await message.reply(embed=makeEmbed(title=getMsg("error_channel_title", message.guild), description=getMsg("error_channel_description", message.guild).format(prefix), footer=getMsg("help_notice_id", message.guild), color=strToColor(config["color_error"])), mention_author=False)171 172 else:173 174 await message.reply(embed=makeEmbed(title=getMsg("forbidden_title", message.guild), description=getMsg("forbidden_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)175 else:176 await message.reply(embed=makeEmbed(title=getMsg("dm_title", message.guild), description=getMsg("dm_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)177 178 elif message.content.startswith(f"{prefix}category"):179 180 gotCommand(message)181 182 fullcmd = message.content.split()183 184 if message.guild is not None:185 186 if message.author.guild_permissions.administrator:187 188 try:189 190 if fullcmd[1] == "reset":191 192 if guildConfGet(message.guild, "category") is not None:193 194 guildConfReset(message.guild, "category")195 196 await message.reply(embed=makeEmbed(title=getMsg("reset_category_title", message.guild), description=getMsg("reset_category_description", message.guild).format(prefix), color=strToColor(config["color_ok"])), mention_author=False)197 198 else:199 200 await message.reply(embed=makeEmbed(title=getMsg("hint_none_category_title", message.guild), description=getMsg("hint_none_category_description", message.guild).format(prefix), color=strToColor(config["color_warn"])), mention_author=False)201 202 else:203 204 selected_category = discord.utils.get(message.guild.channels, id=int(fullcmd[1]))205 206 guildConfSet(message.guild, "category", int(fullcmd[1]))207 208 await message.reply(embed=makeEmbed(title=getMsg("set_category_title", message.guild), description=getMsg("set_category_description", message.guild).format(selected_category.name), color=strToColor(config["color_ok"])), mention_author=False)209 210 if guildConfGet(message.guild, "channel") is None:211 212 await message.channel.send(embed=makeEmbed(title=getMsg("hint_none_channel_title", message.guild), description=getMsg("hint_none_channel_description", message.guild).format(prefix), color=strToColor(config["color_warn"])))213 214 except Exception as exp:215 216 if debug:217 print(exp)218 219 await message.reply(embed=makeEmbed(title=getMsg("error_category_title", message.guild), description=getMsg("error_category_description", message.guild).format(prefix), footer=getMsg("help_notice_id_category", message.guild), color=strToColor(config["color_error"])), mention_author=False)220 221 else:222 223 await message.reply(embed=makeEmbed(title=getMsg("forbidden_title", message.guild), description=getMsg("forbidden_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)224 225 else:226 await message.reply(embed=makeEmbed(title=getMsg("dm_title", message.guild), description=getMsg("dm_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)227 elif message.content.startswith(f"{prefix}prefix"):228 229 gotCommand(message)230 231 fullcmd = message.content.split()232 233 if message.guild is not None:234 235 if message.author.guild_permissions.administrator:236 237 try:238 239 if fullcmd[1] == "reset":240 241 if guildConfGet(message.guild, "prefix") is not None:242 243 guildConfReset(message.guild, "prefix")244 245 await message.reply(embed=makeEmbed(title=getMsg("reset_prefix_title", message.guild), description=getMsg("reset_prefix_description", message.guild).format(config["bot_prefix"], config["bot_prefix"]), color=strToColor(config["color_ok"])), mention_author=False)246 247 else:248 249 await message.reply(embed=makeEmbed(title=getMsg("hint_none_prefix_title", message.guild), description=getMsg("hint_none_prefix_description", message.guild).format(prefix, prefix), color=strToColor(config["color_warn"])), mention_author=False)250 251 else:252 253 guildConfSet(message.guild, "prefix", fullcmd[1])254 255 await message.reply(embed=makeEmbed(title=getMsg("set_prefix_title", message.guild), description=getMsg("set_prefix_description", message.guild).format(fullcmd[1]), color=strToColor(config["color_ok"])), mention_author=False)256 257 except:258 259 await message.reply(embed=makeEmbed(title=getMsg("error_prefix_title", message.guild), description=getMsg("error_prefix_description", message.guild).format(prefix), color=strToColor(config["color_error"])), mention_author=False)260 261 else:262 await message.reply(embed=makeEmbed(title=getMsg("forbidden_title", message.guild), description=getMsg("forbidden_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)263 264 else:265 await message.reply(embed=makeEmbed(title=getMsg("dm_title", message.guild), description=getMsg("dm_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)266 elif message.content.startswith(f"{prefix}locale"):267 268 gotCommand(message)269 270 fullcmd = message.content.split()271 272 if message.guild is not None:273 274 if message.author.guild_permissions.administrator:275 276 try:277 278 if fullcmd[1] == "reset":279 280 if guildConfGet(message.guild, "locale") is not None:281 282 guildConfReset(message.guild, "locale")283 appendLog(f"Server's locale has been reset", message.guild)284 await message.reply(embed=makeEmbed(title=getMsg("reset_locale_title", message.guild), description=getMsg("reset_locale_description", message.guild).format(getMsg("locale_name", message.guild), prefix), color=strToColor(config["color_ok"])), mention_author=False)285 286 else:287 288 await message.reply(embed=makeEmbed(title=getMsg("hint_none_locale_title", message.guild), description=getMsg("hint_none_locale_description", message.guild).format(getMsg("locale_name", message.guild), prefix), color=strToColor(config["color_warn"])), mention_author=False)289 290 else:291 292 for locale_file in os.listdir(f"{path}/locale/"):293 if locale_file[:-5] == fullcmd[1]:294 guildConfSet(message.guild, "locale", fullcmd[1])295 appendLog(f"Server's locale is now set to {fullcmd[1]}", message.guild)296 await message.reply(embed=makeEmbed(title=getMsg("set_locale_title", message.guild), description=getMsg("set_locale_description", message.guild).format(getMsg("locale_name", message.guild)), color=strToColor(config["color_ok"])), mention_author=False)297 return298 299 locales = []300 301 for locale_file in os.listdir(f"{path}/locale/"):302 locales.append(f"`{locale_file[:-5]}`")303 304 await message.reply(embed=makeEmbed(title=getMsg("error_locale_title", message.guild), description=getMsg("error_locale_description", message.guild).format(prefix, ", ".join(locales)), color=strToColor(config["color_error"])), mention_author=False)305 306 except:307 308 locales = []309 310 for locale_file in os.listdir(f"{path}/locale/"):311 locales.append(f"`{locale_file[:-5]}`")312 313 await message.reply(embed=makeEmbed(title=getMsg("error_locale_title", message.guild), description=getMsg("error_locale_description", message.guild).format(prefix, ", ".join(locales)), color=strToColor(config["color_error"])), mention_author=False)314 315 else:316 await message.reply(embed=makeEmbed(title=getMsg("forbidden_title", message.guild), description=getMsg("forbidden_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)317 318 else:319 await message.reply(embed=makeEmbed(title=getMsg("dm_title", message.guild), description=getMsg("dm_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)320 elif message.content.startswith(f"{prefix}help"):321 322 gotCommand(message)323 324 await message.reply(embed=getHelpMessage(message, version, prefix=prefix), mention_author=False)325#if loadJson("config.json")["auto_clear_trash"]:326 # run func327appendLog(f"Trying to log in...")...

Full Screen

Full Screen

test.py

Source:test.py Github

copy

Full Screen

...30class TopicTestCase(ChannelPluginTestCase):31 plugins = ('Topic','User',)32 def testRemove(self):33 self.assertError('topic remove 1')34 _ = self.getMsg('topic add foo')35 _ = self.getMsg('topic add bar')36 _ = self.getMsg('topic add baz')37 self.assertError('topic remove 0')38 self.assertNotError('topic remove 3')39 self.assertNotError('topic remove 2')40 self.assertNotError('topic remove 1')41 self.assertError('topic remove 1')42 def testRemoveMultiple(self):43 self.assertError('topic remove 1 2')44 _ = self.getMsg('topic add foo')45 _ = self.getMsg('topic add bar')46 _ = self.getMsg('topic add baz')47 _ = self.getMsg('topic add derp')48 _ = self.getMsg('topic add cheese')49 self.assertNotError('topic remove 1 2')50 self.assertNotError('topic remove -1 1')51 self.assertError('topic remove -99 1')52 def testReplace(self):53 _ = self.getMsg('topic add foo')54 _ = self.getMsg('topic add bar')55 _ = self.getMsg('topic add baz')56 self.assertRegexp('topic replace 1 oof', 'oof.*bar.*baz')57 self.assertRegexp('topic replace -1 zab', 'oof.*bar.*zab')58 self.assertRegexp('topic replace 2 lorem ipsum',59 'oof.*lorem ipsum.*zab')60 self.assertRegexp('topic replace 2 rab', 'oof.*rab.*zab')61 def testGet(self):62 self.assertError('topic get 1')63 _ = self.getMsg('topic add foo')64 _ = self.getMsg('topic add bar')65 _ = self.getMsg('topic add baz')66 self.assertRegexp('topic get 1', '^foo')67 self.assertError('topic get 0')68 def testAdd(self):69 self.assertError('topic add #floorgle')70 m = self.getMsg('topic add foo')71 self.assertEqual(m.command, 'TOPIC')72 self.assertEqual(m.args[0], self.channel)73 self.assertEqual(m.args[1], 'foo')74 m = self.getMsg('topic add bar')75 self.assertEqual(m.command, 'TOPIC')76 self.assertEqual(m.args[0], self.channel)77 self.assertEqual(m.args[1], 'foo | bar')78 def testManageCapabilities(self):79 try:80 self.irc.feedMsg(ircmsgs.mode(self.channel, args=('+o', self.nick),81 prefix=self.prefix))82 self.irc.feedMsg(ircmsgs.mode(self.channel, args=('+t'),83 prefix=self.prefix))84 world.testing = False85 origuser = self.prefix86 self.prefix = 'stuff!stuff@stuff'87 self.assertNotError('register nottester stuff', private=True)88 self.assertError('topic add foo')89 origconf = conf.supybot.plugins.Topic.requireManageCapability()90 conf.supybot.plugins.Topic.requireManageCapability.setValue('')91 self.assertNotError('topic add foo')92 finally:93 world.testing = True94 self.prefix = origuser95 conf.supybot.plugins.Topic.requireManageCapability.setValue(origconf)96 def testInsert(self):97 m = self.getMsg('topic add foo')98 self.assertEqual(m.args[1], 'foo')99 m = self.getMsg('topic insert bar')100 self.assertEqual(m.args[1], 'bar | foo')101 def testChange(self):102 _ = self.getMsg('topic add foo')103 _ = self.getMsg('topic add bar')104 _ = self.getMsg('topic add baz')105 self.assertRegexp('topic change -1 s/baz/biff/',106 r'foo.*bar.*biff')107 self.assertRegexp('topic change 2 s/bar/baz/',108 r'foo.*baz.*biff')109 self.assertRegexp('topic change 1 s/foo/bar/',110 r'bar.*baz.*biff')111 self.assertRegexp('topic change -2 s/baz/bazz/',112 r'bar.*bazz.*biff')113 self.assertError('topic change 0 s/baz/biff/')114 def testConfig(self):115 try:116 original = conf.supybot.plugins.Topic.separator()117 conf.supybot.plugins.Topic.separator.setValue(' <==> ')118 _ = self.getMsg('topic add foo')119 m = self.getMsg('topic add bar')120 self.assertTrue('<==>' in m.args[1])121 finally:122 conf.supybot.plugins.Topic.separator.setValue(original)123 def testReorder(self):124 _ = self.getMsg('topic add foo')125 _ = self.getMsg('topic add bar')126 _ = self.getMsg('topic add baz')127 self.assertRegexp('topic reorder 2 1 3', r'bar.*foo.*baz')128 self.assertRegexp('topic reorder 3 -2 1', r'baz.*foo.*bar')129 self.assertError('topic reorder 0 1 2')130 self.assertError('topic reorder 1 -2 2')131 self.assertError('topic reorder 1 2')132 self.assertError('topic reorder 2 3 4')133 self.assertError('topic reorder 1 2 2')134 self.assertError('topic reorder 1 1 2 3')135 _ = self.getMsg('topic remove 1')136 _ = self.getMsg('topic remove 1')137 self.assertError('topic reorder 1')138 _ = self.getMsg('topic remove 1')139 self.assertError('topic reorder 0')140 def testList(self):141 _ = self.getMsg('topic add foo')142 self.assertRegexp('topic list', '1: foo')143 _ = self.getMsg('topic add bar')144 self.assertRegexp('topic list', '1: foo.*2: bar')145 _ = self.getMsg('topic add baz')146 self.assertRegexp('topic list', '1: foo.* 2: bar.* and 3: baz')147 def testSet(self):148 _ = self.getMsg('topic add foo')149 self.assertRegexp('topic set -1 bar', 'bar')150 self.assertNotRegexp('topic set -1 baz', 'bar')151 self.assertResponse('topic set foo bar baz', 'foo bar baz')152 # Catch a bug we had where setting topic 1 would reset the whole topic153 orig = conf.supybot.plugins.Topic.format()154 sep = conf.supybot.plugins.Topic.separator()155 try:156 conf.supybot.plugins.Topic.format.setValue('$topic')157 self.assertResponse('topic add baz', 'foo bar baz%sbaz' % sep)158 self.assertResponse('topic set 1 bar', 'bar%sbaz' % sep)159 finally:160 conf.supybot.plugins.Topic.format.setValue(orig)161 def testRestore(self):162 self.getMsg('topic set foo')163 self.assertResponse('topic restore', 'foo')164 self.getMsg('topic remove 1')165 restoreError = 'Error: I haven\'t yet set the topic in #test.'166 self.assertResponse('topic restore', restoreError)167 def testRefresh(self):168 self.getMsg('topic set foo')169 self.assertResponse('topic refresh', 'foo')170 self.getMsg('topic remove 1')171 refreshError = 'Error: I haven\'t yet set the topic in #test.'172 self.assertResponse('topic refresh', refreshError)173 def testUndo(self):174 try:175 original = conf.supybot.plugins.Topic.format()176 conf.supybot.plugins.Topic.format.setValue('$topic')177 self.assertResponse('topic set ""', '')178 self.assertResponse('topic add foo', 'foo')179 self.assertResponse('topic add bar', 'foo | bar')180 self.assertResponse('topic add baz', 'foo | bar | baz')181 self.assertResponse('topic undo', 'foo | bar')182 self.assertResponse('topic undo', 'foo')183 self.assertResponse('topic undo', '')184 finally:...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var getMsg = require('pact-foundation-pact').getMsg;2var msg = getMsg();3console.log(msg);4var getMsg = require('pact-foundation-pact').getMsg;5var msg = getMsg();6console.log(msg);7var getMsg = require('pact-foundation-pact').getMsg;8var msg = getMsg();9console.log(msg);10var getMsg = require('pact-foundation-pact').getMsg;11var msg = getMsg();12console.log(msg);13var getMsg = require('pact-foundation-pact').getMsg;14var msg = getMsg();15console.log(msg);16var getMsg = require('pact-foundation-pact').getMsg;17var msg = getMsg();18console.log(msg);19delete require.cache[require.resolve('pact-foundation-pact')];20var getMsg = require('pact-foundation-pact').getMsg;21var msg = getMsg();22console.log(msg);

Full Screen

Using AI Code Generation

copy

Full Screen

1var pact = require('pact-foundation/pact');2var getMsg = pact.getMsg;3var msg = getMsg();4console.log(msg);5var pact = require('pact-foundation/pact');6var getMsg = pact.getMsg;7var msg = getMsg();8console.log(msg);

Full Screen

Using AI Code Generation

copy

Full Screen

1const Pact = require('pact-foundation/pact').Pact;2const getMsg = require('pact-foundation/pact').getMsg;3const msg = getMsg(Pact.mockService.baseUrl);4console.log(msg);5const { Pact } = require('@pact-foundation/pact');6const getMsg = require('@pact-foundation/pact').getMsg;7describe('Pact', () => {8 describe('getMsg', () => {9 it('returns the correct message', () => {10 const msg = getMsg(Pact.mockService.baseUrl);11 expect(msg).toEqual('Hello World!');12 });13 });14});15const Pact = require('@pact-foundation/pact').Pact;16const getMsg = require('@pact-foundation/pact').getMsg;17const msg = getMsg(Pact.mockService.baseUrl);18console.log(msg);19const { Pact } = require('@pact-foundation/pact');20const getMsg = require('@pact-foundation/pact').getMsg;21describe('Pact', () => {22 describe('getMsg', () => {23 it('returns the correct message', () => {24 const msg = getMsg(Pact.mockService.baseUrl);25 expect(msg).toEqual('Hello World!');26 });27 });28});

Full Screen

Using AI Code Generation

copy

Full Screen

1const pact = require('pact-foundation/pact');2const {getMsg} = require('./test1.js');3console.log(getMsg());4module.exports = {5 getMsg: function () {6 return 'Hello';7 }8};9Your name to display (optional):10Your name to display (optional):11Your name to display (optional):

Full Screen

Using AI Code Generation

copy

Full Screen

1const pact = require('pact-foundation-pact');2const expect = require('chai').expect;3describe('Pact', () => {4 it('returns the correct message', () => {5 expect(pact.getMsg()).to.eql('Hello World!');6 });7 it('returns a string', () => {8 expect(pact.getMsg()).to.be.a('string');9 });10});11const pact = require('pact-foundation-pact');12const expect = require('chai').expect;13describe('Pact', () => {14 it('returns the correct message', () => {15 expect(pact.getMsg()).to.eql('Hello World!');16 });17 it('returns a string', () => {18 expect(pact.getMsg()).to.be.a('string');19 });20});21const pact = require('pact-foundation-pact');22const expect = require('chai').expect;23describe('Pact', () => {24 it('returns the correct sum', () => {25 expect(pact.add(1, 2)).to.eql(3);26 });27 it('returns a number', () => {28 expect(pact.add(1, 2)).to.be.a('number');29 });30});31const pact = require('pact-foundation-pact');32const expect = require('chai').expect;33describe('Pact', () => {

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pact-foundation-pact automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful