Integration of Locust with Pytest - pytest

I am trying to integrate pytest and locust and facing the issue PytestCollectionWarning: cannot collect test class because it has a init constructor
import json
import pytest
import requests
from locust import SequentialTaskSet, task
request_url = ""
module_name = "shifts"
class TestShifts(SequentialTaskSet):
def get_shifts_data(self, login, request_data):
global request_url
name = "Get Shifts Data"
request_url = login.get_base_url() + request_data['path']
with self.client.get(request_url, catch_response=True, name=name, headers=login.get_header_get_request()) \
as get_shift_response:
if get_shift_response.status_code == 200:
get_shift_response.success()
return json.loads(get_shift_response.content)
else:
get_shift_response.failure("Get Shifts Failure")
#pytest.mark.usefixtures("login", "request_data")
def test_delete_shift(self, login, request_data):
global request_url
name = "Delete Shift with name :" + request_data['requestBody']['name']
shifts_data_dict = self.get_shifts_data(login, request_data)
for shift in shifts_data_dict['shifts']:
if shift['name'] == request_data['requestBody']['name']:
with self.client.delete(request_url + "/" + shift['id'], catch_response=True, name=name,
headers=login.get_header_get_request()) as shift_delete_response:
if shift_delete_response.status_code == 204:
shift_delete_response.success()
else:
shift_delete_response.failure("Shift Delete Failure")

Related

Adding files to BitBucket repository using BitBucket Server REST API

Everytime a new file is added to my server, I want that file to be added into BitBucket server. The server containing files is non-git and I want to know if this is possible programmatically using Bitbucket server rest api. Is it possible to do a remote commit and push to a certain branch using rest api? I took a look into the documentation and couldn't figure out which is the one for this scenario. Any help would be appreciated.
Here is a snippet of Python which uses the Bitbucket REST API:
#!/usr/bin/python
import os
import tempfile
import sys
import urllib2
import json
import base64
import logging
import re
import pprint
import requests
import subprocess
projectKey= "FW"
repoKey = "fw"
branch = "master"
pathToVersionProperties = "core/CruiseControl/CI_version.properties"
localVersionProperties = "CI_version.properties"
bitbucketBaseUrl = "https://bitbucket.company.com/rest/api/latest"
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
def checkPersonalAccessToken():
try:
os.environ["PAT"]
logging.info("Detected Personal Access Token")
except KeyError:
logging.error("Personal Access Token: $PAT env variable not set, update Jenkins master with correct environment variable")
sys.exit(1)
def getJenkinsPropertiesFile():
restEndpoint = "{}/projects/{}/repos/{}/raw/{}".format(bitbucketBaseUrl, projectKey, repoKey, pathToVersionProperties)
logging.info("REST endpoint : {}".format(restEndpoint))
request = urllib2.Request(restEndpoint)
request.add_header("Authorization", "Bearer %s" % os.environ["PAT"])
result = urllib2.urlopen(request).read()
return result
def extractBuildNumber(propertiesString):
m = re.search(r'BUILD_NUMBER=(\d+)', propertiesString)
if m:
logging.info("Current build number: {}".format(m.group(1)))
else:
logging.error("Failed to extract build number")
sys.exit(1)
return int(m.group(1))
def extractVersion(propertiesString):
m = re.search(r'\nVERSION=(.*)', propertiesString)
if m:
logging.info("Current version: {}".format(m.group(1)))
else:
logging.error("Failed to extract version")
sys.exit(1)
return m.group(1)
def updateBuildNumber(propertiesString, currentBuild, newBuildNumber):
buildString = "BUILD_NUMBER=%s" % currentBuild
newBuildString = "BUILD_NUMBER=%s" % newBuildNumber
return propertiesString.replace(buildString, newBuildString)
def getLatestCommit():
restEndpoint = "{}/projects/{}/repos/{}/commits?path={}".format(bitbucketBaseUrl, projectKey, repoKey, pathToVersionProperties)
logging.info("REST endpoint : {}".format(restEndpoint))
request = urllib2.Request(restEndpoint)
request.add_header("Authorization", "Bearer %s" % os.environ["PAT"])
result = json.loads(urllib2.urlopen(request).read())
latestCommit = result["values"][0]["displayId"]
if(len(latestCommit) > 0):
logging.info("Latest commit: {}".format(latestCommit))
else:
logging.error("Commit hash is empty, failed to retrieve latest commit")
sys.exit(1)
return latestCommit
def commitUpdatedBuildNumber(commitId, commitMessage, updateVersionProperties):
restEndpoint = "{}/projects/{}/repos/{}/browse/{}".format(bitbucketBaseUrl, projectKey, repoKey, pathToVersionProperties)
logging.info("REST endpoint : {}".format(restEndpoint))
tempFile = tempfile.NamedTemporaryFile(delete=False)
try:
with open(tempFile.name, "w") as f:
f.write(updateVersionProperties)
finally:
tempFile.close()
f.close()
curlCommand = "curl -s -S -X PUT -H \"Authorization: Bearer %s\" -F content=#%s -F \'message=%s\' -F branch=master -F sourceCommitId=%s %s" % (os.environ["PAT"], tempFile.name, commitMessage,commitId, restEndpoint)
try:
FNULL = open(os.devnull, 'w')
subprocess.call(curlCommand, shell=True, stdout=FNULL)
finally:
logging.info("Committed version update")
FNULL.close()
def writeCommitNumber(latestCommit):
revisionFile = "%s/%s/%s" % (os.environ["HOME"],"git_revisions", "FW_11_CI_git.revision")
logging.info("Revision file: {}".format(revisionFile))
writeString = "%s=%s\n" % ("VERSION_PROPERTIES_REVISION", latestCommit)
try:
with open(revisionFile,"w") as f:
f.write(writeString)
finally:
f.close()
checkPersonalAccessToken()
propertiesString = getJenkinsPropertiesFile()
currentBuildNumber = extractBuildNumber(propertiesString)
currentVersion = extractVersion(propertiesString)
newBuildNumber = currentBuildNumber + 1
newBuild = "%s%s%s" % (currentVersion,'B',newBuildNumber)
logging.info("New build number: {}".format(newBuild))
updatedPropertiesString = updateBuildNumber(propertiesString, currentBuildNumber, newBuildNumber)
commitMessage = "%s %s" % (newBuild, "Version Update")
latestCommit = getLatestCommit()
commitUpdatedBuildNumber(latestCommit, commitMessage, updatedPropertiesString)
writeCommitNumber(latestCommit)

how to access variable from another class in python

I would like to access variable lotId & qty_selected in class 'InputDialog' and to be use in class 'Mainwindow'. I did tried to find a solution in net but unable to solve it until now. can anyone show how to make it?
Here is my current code:
from __future__ import division
from skimage.measure import compare_ssim as ssim
import matplotlib.pyplot as plt
import numpy as np
import sys
import os, glob
import cv2
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtGui import *
from inputdialog import Ui_inputDialog
from mainwindow import Ui_mainWindow
from tkinter import messagebox
class MainWindow(QtGui.QMainWindow, Ui_mainWindow):
def __init__(self, class_input):
QtGui.QMainWindow.__init__(self)
Ui_mainWindow.__init__(self)
Ui_inputDialog.__init__(self)
self.setupUi(self)
self.capture_button.clicked.connect(self.captureImage)
self.display_button.clicked.connect(self.displayImage)
self.deviceBox.activated.connect(self.selectDeviceCombo)
self.startInspectionBtn.clicked.connect(self.enterLotID)
self.display_button.clicked.connect(self.displayResults)
#self.viewResultBtn.clicked.connect(self.viewResults)
self.window2 = None
def enterLotID(self): # Dialog box will ask user to enter lot ID and wafer qty
if self.window2 is None:
self.window2 = InputDialog(self)
self.window2.isModal()
self.window2.show()
# Program need to be loop through a image file in directory
def displayImage(self, ):
os.chdir('c:\\Users\\mohd_faizal4\\Desktop\\Python\\Testing' + '\\' + lotId )
for lotId in glob.glob('*.jpeg'):
print(lotId)
sample_label = 'c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 6.jpg' # Sample image must read from current folder lot ID running the inspection
self.sample_label.setScaledContents(True)
self.sample_label.setPixmap(QtGui.QPixmap(sample_label))
def selectDeviceCombo(self):
self.var_Selected = self.deviceBox.currentText()
#print('The user selected value now is:')
print('Device = ' + self.var_Selected)
if self.var_Selected.lower() == 'xf35':
print("Great! Device Id is - " + self.var_Selected + '!')
source_label ='c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 4.jpg'
self.source_label.setScaledContents(True)
self.source_label.setPixmap(QtGui.QPixmap(source_label))
elif self.var_Selected.lower() == 'xf38':
print("Great! Device Id is - " + self.var_Selected + '!')
source_label ='c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 5.jpg'
self.source_label.setScaledContents(True)
self.source_label.setPixmap(QtGui.QPixmap(source_label))
elif self.var_Selected.lower() == 'x38c':
print("Great! Device Id is - " + self.var_Selected + '!')
source_label ='c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 7.jpg'
self.source_label.setScaledContents(True)
self.source_label.setPixmap(QtGui.QPixmap(source_label))
else:
print("Pls select device id. It's compulsory field!")
def captureImage(self): # Capture image and display on 'Sample' column under Inspection
cam = cv2.VideoCapture(0)
i = 1
while i < int(input(qty_selected)):
ret, frame = cam.read()
cv2.imshow('Please review an image', frame)
if not ret:
break
k = cv2.waitKey(0)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
if k % 256 == 32:
# SPACE pressed
img_name = "_{}.jpeg".format(i)
#print (img_name)
cv2.imwrite(img_name, frame)
#cv2.imwrite(os.path.join(dirname, img_name), frame)
print("{}".format(img_name))
i += 1
cam.release()
cv2.destroyAllWindows()
def displayResults(self): #Display image of wafer at 'Result' tab. Simultaneously with 'Inspect'
label_vid01 = 'c:/Users/mohd_faizal4/Desktop/Python/Image/Picture 7.jpg'
self.label_vid01.setScaledContents(True)
self.label_vid01.setPixmap(QtGui.QPixmap(label_vid01))
# A new class for user input dialog to enter lot information
class InputDialog (QtGui.QDialog, Ui_inputDialog):
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.setupUi(self)
self.okButton.clicked.connect(self.addLotId)
self.okButton.clicked.connect(self.selectWaferQty)
def addLotId(self):
lotId = str(self.strLotId.toPlainText())
self.strLotId.setText(lotId)
print(lotId)
path = 'c:\\Users\\mohd_faizal4\\Desktop\\Python\\Testing' + '\\' + lotId
if not os.path.exists(path):
os.makedirs(path)
else:
messagebox.showwarning('Error','Please enter required information')
QtGui.QMessageBox.show(self)
# User require to select wafer quantity upon enter the lot ID
def selectWaferQty(self):
qty_selected = self.waferQty.currentText()
#print ('The user selected value now is:')
print ('Wafer Qty = ' + qty_selected)
#if self.qty_selected() == '1':
# print('Great! Wafer Qty is - ' + self.qty_selected + '!')
#else:
# print ('Pls select wafer quantity!')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
Window = MainWindow()
Window.show()
sys.exit()
Under the class InputDialog, change lotId and qty_selected to self.lotId and self.qty_selected, respectively. And to access it inside your WindowMain class, try self.window2.lotId and self.window2.qty_selected.

BackUp Odoo 8 Windows erreur zero size file

I make a specific development of the db_backup openerp 7 module to running on version 8 Odoo.
So it is properly installed and it does backup
the problem is that the sql file size is zero KB
this is the code backup_scheduler.py
import xmlrpclib
import socket
import os
import time
import base64
from openerp.osv import fields,osv,orm
from openerp import tools, netsvc
from openerp.tools.translate import _
import logging
_logger = logging.getLogger(__name__)
def execute(connector, method, *args):
res = False
try:
res = getattr(connector,method)(*args)
except socket.error,e:
raise e
return res
addons_path = tools.config['addons_path'] + '/auto_backup/DBbackups'
class db_backup(osv.Model):
_name = 'db.backup'
def get_db_list(self, cr, user, ids, host='localhost', port='8069', context={}):
uri = 'http://' + host + ':' + port
conn = xmlrpclib.ServerProxy(uri + '/xmlrpc/db')
db_list = execute(conn, 'list')
return db_list
_columns = {
'host' : fields.char('Host', size=100, required='True'),
'port' : fields.char('Port', size=10, required='True'),
'name' : fields.char('Database', size=100, required='True',help='Database you want to schedule backups for'),
'bkp_dir' : fields.char('Backup Directory', size=100, help='Absolute path for storing the backups', required='True')
}
_defaults = {
'bkp_dir' : lambda *a : addons_path,
'host' : lambda *a : 'localhost',
'port' : lambda *a : '8069'
}
def _check_db_exist(self, cr, user, ids):
for rec in self.browse(cr,user,ids):
db_list = self.get_db_list(cr, user, ids, rec.host, rec.port)
if rec.name in db_list:
return True
return False
_constraints = [
(_check_db_exist, _('Error ! No such database exists!'), [])
]
def schedule_backup(self, cr, user, context={}):
conf_ids= self.search(cr, user, [])
confs = self.browse(cr,user,conf_ids)
for rec in confs:
db_list = self.get_db_list(cr, user, [], rec.host, rec.port)
if rec.name in db_list:
try:
if not os.path.isdir(rec.bkp_dir):
os.makedirs(rec.bkp_dir)
except:
raise
bkp_file='%s_%s.sql' % (rec.name, time.strftime('%Y%m%d_%H_%M_%S'))
file_path = os.path.join(rec.bkp_dir,bkp_file)
fp = open(file_path,'wb')
uri = 'http://' + rec.host + ':' + rec.port
conn = xmlrpclib.ServerProxy(uri + '/xmlrpc/db')
bkp=''
try:
bkp = execute(conn, 'dump', tools.config['admin_passwd'], rec.name)
except:
logger.notifyChannel('backup', netsvc.LOG_INFO, "Could'nt backup database %s. Bad database administrator password for server running at http://%s:%s" %(rec.name, rec.host, rec.port))
continue
bkp = base64.decodestring(bkp)
fp.write(bkp)
fp.close()
else:
logger.notifyChannel('backup', netsvc.LOG_INFO, "database %s doesn't exist on http://%s:%s" %(rec.name, rec.host, rec.port))
db_backup()
This is probably because an error occurs on the backend that does not get propagated back to the client. If you check the server logs at the time you take a backup you will probably see the issue.
Note If you need a script to get a backup from or restore a database to a v7, v8 or v9 server take a look at https://github.com/daramousk/odoo_remote_backup
I have developed a script for this specific reason which you can use or change to resolve your issue.

McAfee Update download script

I'm setting PC with McAfee install on them and be told that I need to stop the program going on line to download update (DAT). I need to create a script to download dat file from McAfee web site and put this file on server where McAfee can access and install this.
Has anyone done this in past.
I actually have done this. I haven't tested this script in a year or two but here is what I was using. This isn't written in Powershell but if you change the directories I think this can run on Windows.
#!/usr/bin/python
import ftplib
import tarfile
import shutil
import os
import re
import time
scannerDir = "/usr/local/uvscan/"
tmp = "/tmp/avscanner/"
def downloadDat():
datfile = ""
r = re.compile("^avvdat")
ftp = ftplib.FTP("ftp.nai.com", "anonymous", "email#yourdomain.com")
ftp.cwd("/pub/datfiles/english")
list = ftp.nlst()
for x in list:
if r.search(x):
datFile = x
f = open(tmp + "datfile", 'wb')
ftp.retrbinary("RETR " + datFile, f.write)
f.close()
ftp.quit()
def unpackDat():
tFile = tarfile.open(tmp + "datfile", 'r')
for f in tFile.getnames():
tFile.extract(f, tmp)
def createDirs():
if os.path.isdir(tmp) == False:
os.mkdir(tmp, 0700)
os.chown(tmp, 0, 95)
os.chmod(tmp, 0755)
def doCleanup():
shutil.rmtree(tmp)
def installFiles():
shutil.copyfile(tmp + "/avvclean.dat", scannerDir + "/avvclean.dat")
shutil.copyfile(tmp + "/avvnames.dat", scannerDir + "/avvnames.dat")
shutil.copyfile(tmp + "/avvscan.dat", scannerDir + "/avvscan.dat")
def isOld():
if os.path.isfile(scannerDir + "/avvclean.dat"):
if time.time() - os.path.getctime(scannerDir + "/avvclean.dat") < 80000:
return True
else:
return False
else:
return True
def main():
if isOld():
createDirs()
downloadDat()
unpackDat()
installFiles()
doCleanup()
if __name__ == "__main__":
main()

web automation - auto check link

I'm new to web app and I want to check when there's a new version of dota map, I'll check links in getdota.com.
How can I do this and which language, I want it checks every time you start warcraft, and auto download new map to specific folder.
My question is : Can you give a link to a specific article about web automation or something like that.
Thanks first :)
Below is an example in Python.
It parses getdota.com page, reads parameters for POST request for downloading a map, gets the file and saves it in configured directory (by default current directory).
#!/usr/bin/env python
import urllib
import urllib2
import sgmllib
from pprint import pprint
import os.path
import sys
url = 'http://www.getdota.com/'
download_url = 'http://www.getdota.com/app/getmap/'
chunk = 10000
directory = '' #directory where file should be saved, if empty uses current dir
class DotaParser(sgmllib.SGMLParser):
def parse(self, s):
self.feed(s)
self.close()
def __init__(self, verbose=0):
sgmllib.SGMLParser.__init__(self, verbose)
self.URL = ''
self.post_args = {}
def getArgs(self):
return self.post_args
def start_input(self, attributes):
d = dict(attributes)
if d.get('id', None) == None:
return
if d['id'] in ["input_mirror2", "input_file_name2", "input_map_id2", "input_language2", "input_language_id2"]:
self.post_args[d['name']] = d['value']
if __name__ == '__main__':
dotap = DotaParser()
data = urllib2.urlopen(urllib2.Request('http://www.getdota.com/')).read()
dotap.parse(data)
data = urllib.urlencode(dotap.getArgs())
request = urllib2.Request(download_url, data)
response = urllib2.urlopen(request)
page = response.read()
#download file
fname = directory + page.split('/')[-1]
if os.path.isfile(fname):
print "No newer file available"
sys.exit(0)
f = open(fname, 'w')
print "New file available. Saving in: %s" % fname
webFile = urllib.urlopen(page)
c = webFile.read(chunk)
while(c):
f.write(c)
c = webFile.read(chunk)
f.close()
webFile.close()