def set_retention_product(self, job_t_id, retention_tag_name, product_name): """ XML-RPC method to update a job's retention tag, product, or both. There is an important distinction between product_name of None, which means do not change the existing value, vs. empty string, which means clear the existing product. """ job = TaskBase.get_by_t_id(job_t_id) if job.can_change_product(identity.current.user) and \ job.can_change_retention_tag(identity.current.user): if retention_tag_name and product_name: retention_tag = RetentionTag.by_name(retention_tag_name) product = Product.by_name(product_name) result = Utility.update_retention_tag_and_product(job, retention_tag, product) elif retention_tag_name and product_name == '': retention_tag = RetentionTag.by_name(retention_tag_name) result = Utility.update_retention_tag_and_product(job, retention_tag, None) elif retention_tag_name: retention_tag = RetentionTag.by_name(retention_tag_name) result = Utility.update_retention_tag(job, retention_tag) elif product_name: product = Product.by_name(product_name) result = Utility.update_product(job, product) elif product_name == '': result = Utility.update_product(job, None) else: result = {'success': False, 'msg': 'Nothing to do'} if not result['success'] is True: raise BeakerException('Job %s not updated: %s' % (job.id, result.get('msg', 'Unknown reason'))) else: raise BeakerException('No permission to modify %s' % job)
def update(self, id, **kw): # XXX Thus function is awkward and needs to be cleaned up. try: job = Job.by_id(id) except InvalidRequestError: raise cherrypy.HTTPError(status=400, message='Invalid job id %s' % id) if not job.can_change_product(identity.current.user) or not \ job.can_change_retention_tag(identity.current.user): raise cherrypy.HTTPError(status=403, message="You don't have permission to update job id %s" % id) returns = {'success' : True, 'vars':{}} if 'retentiontag' in kw and 'product' in kw: retention_tag = RetentionTag.by_id(kw['retentiontag']) if int(kw['product']) == ProductWidget.product_deselected: product = None else: product = Product.by_id(kw['product']) returns.update(Utility.update_retention_tag_and_product(job, retention_tag, product)) elif 'retentiontag' in kw: retention_tag = RetentionTag.by_id(kw['retentiontag']) returns.update(Utility.update_retention_tag(job, retention_tag)) elif 'product' in kw: if int(kw['product']) == ProductWidget.product_deselected: product = None else: product = Product.by_id(kw['product']) returns.update(Utility.update_product(job, product)) if 'whiteboard' in kw: job.whiteboard = kw['whiteboard'] return returns
def set_retention_product(self, job_t_id, retention_tag_name, product_name): """ XML-RPC method to update a job's retention tag, product, or both. There is an important distinction between product_name of None, which means do not change the existing value, vs. empty string, which means clear the existing product. """ job = TaskBase.get_by_t_id(job_t_id) if job.can_change_product(identity.current.user) and \ job.can_change_retention_tag(identity.current.user): if retention_tag_name and product_name: retention_tag = RetentionTag.by_name(retention_tag_name) product = Product.by_name(product_name) old_tag = job.retention_tag if job.retention_tag else None result = Utility.update_retention_tag_and_product( job, retention_tag, product) job.record_activity(user=identity.current.user, service=u'XMLRPC', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif retention_tag_name and product_name == '': retention_tag = RetentionTag.by_name(retention_tag_name) old_tag = job.retention_tag if job.retention_tag else None result = Utility.update_retention_tag_and_product( job, retention_tag, None) job.record_activity(user=identity.current.user, service=u'XMLRPC', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif retention_tag_name: retention_tag = RetentionTag.by_name(retention_tag_name) old_tag = job.retention_tag if job.retention_tag else None result = Utility.update_retention_tag(job, retention_tag) job.record_activity(user=identity.current.user, service=u'XMLRPC', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif product_name: product = Product.by_name(product_name) result = Utility.update_product(job, product) elif product_name == '': result = Utility.update_product(job, None) else: result = {'success': False, 'msg': 'Nothing to do'} if not result['success'] is True: raise BeakerException( 'Job %s not updated: %s' % (job.id, result.get('msg', 'Unknown reason'))) else: raise BeakerException('No permission to modify %s' % job)
def update_products_from_xml(xml_file): xml = lxml.etree.parse(xml_file) with session.begin(): for element in xml.xpath('//cpe'): # lxml returns text as str if it's ASCII, else unicode... if isinstance(element.text, str): cpe = element.text.decode('ascii') else: cpe = element.text if cpe: Product.lazy_create(name=cpe)
def test_loads_cpe_identifiers_from_json_url(self): with open(os.path.join(self.product_docroot, 'product.json'), 'wb') as json_file: json_file.write("""\ [ {"id": 1, "cpe": "cpe:/a:redhat:jboss_data_virtualization:6.2.0"}, {"id": 2, "cpe": "cpe:/a:redhat:jboss_operations_network:3.2.0"}, {"id": 3, "cpe": ""}, {"id": 4} ] """) run_command('product_update.py', 'product-update', ['--product-url', 'http://localhost:19998/product.json']) with session.begin(): Product.by_name(u'cpe:/a:redhat:jboss_data_virtualization:6.2.0') Product.by_name(u'cpe:/a:redhat:jboss_operations_network:3.2.0')
def update(self, id, **kw): # XXX Thus function is awkward and needs to be cleaned up. try: job = Job.by_id(id) except InvalidRequestError: raise cherrypy.HTTPError(status=400, message='Invalid job id %s' % id) if not job.can_change_product(identity.current.user) or not \ job.can_change_retention_tag(identity.current.user): raise cherrypy.HTTPError( status=403, message="You don't have permission to update job id %s" % id) returns = {'success': True, 'vars': {}} if 'retentiontag' in kw and 'product' in kw: retention_tag = RetentionTag.by_id(kw['retentiontag']) if int(kw['product']) == ProductWidget.product_deselected: product = None else: product = Product.by_id(kw['product']) old_tag = job.retention_tag if job.retention_tag else None returns.update( Utility.update_retention_tag_and_product( job, retention_tag, product)) job.record_activity(user=identity.current.user, service=u'WEBUI', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif 'retentiontag' in kw: retention_tag = RetentionTag.by_id(kw['retentiontag']) old_tag = job.retention_tag if job.retention_tag else None returns.update(Utility.update_retention_tag(job, retention_tag)) job.record_activity(user=identity.current.user, service=u'WEBUI', field=u'Retention Tag', action='Changed', old=old_tag.tag, new=retention_tag.tag) elif 'product' in kw: if int(kw['product']) == ProductWidget.product_deselected: product = None else: product = Product.by_id(kw['product']) returns.update(Utility.update_product(job, product)) if 'whiteboard' in kw: job.whiteboard = kw['whiteboard'] return returns
def update_task_product(cls, job, retentiontag_id=None, product_id=None): if product_id is ProductWidget.product_deselected: product = product_id elif product_id is not None: try: product = Product.by_id(product_id) except NoResultFound: raise ValueError('%s is not a valid product' % product_id) else: product=None if retentiontag_id: try: retentiontag = RetentionTag.by_id(retentiontag_id) except NoResultFound: raise ValueError('%s is not a valid retention tag' % retentiontag_id) else: retentiontag = None if retentiontag is None and product is None: return {'success': False} if retentiontag is not None and product is None: #trying to update retentiontag only return cls.check_retentiontag_job(job, retentiontag) elif retentiontag is None and product is not None: #only product return cls.check_product_job(job, product) else: #updating both return cls._update_task_product(job, product, retentiontag)
def update_products(xml_file): dom = etree.parse(xml_file) xpath_string = "//cpe" cpes = dom.xpath(xpath_string) session.begin() try: to_add = {} dupe_errors = [] for cpe in cpes: cpe_text = cpe.text if cpe_text in to_add: dupe_errors.append(cpe_text) else: to_add[cpe_text] = 1 for cpe_to_add in to_add: try: prod = Product.by_name(u"%s" % cpe_to_add) except NoResultFound: session.add(Product(u"%s" % cpe_to_add)) continue session.commit() finally: session.rollback()
def test_ignores_duplicate_cpe_identifiers(self): xml_file = tempfile.NamedTemporaryFile() xml_file.write("""\ <products> <product> <cpe>cpe:/a:redhat:ceph_storage:69</cpe> </product> <product> <cpe>cpe:/a:redhat:ceph_storage:69</cpe> </product> </products> """) xml_file.flush() run_command('product_update.py', 'product-update', ['-f', xml_file.name]) with session.begin(): Product.by_name(u'cpe:/a:redhat:ceph_storage:69')
def _process_job_tag_product(self, retention_tag=None, product=None, *args, **kw): """ Process job retention_tag and product """ retention_tag = retention_tag or RetentionTag.get_default().tag try: tag = RetentionTag.by_tag(retention_tag.lower()) except InvalidRequestError: raise BX(_("Invalid retention_tag attribute passed. Needs to be one of %s. You gave: %s" % (','.join([x.tag for x in RetentionTag.get_all()]), retention_tag))) if product is None and tag.requires_product(): raise BX(_("You've selected a tag which needs a product associated with it, \ alternatively you could use one of the following tags %s" % ','.join([x.tag for x in RetentionTag.get_all() if not x.requires_product()]))) elif product is not None and not tag.requires_product(): raise BX(_("Cannot specify a product with tag %s, please use %s as a tag " % (retention_tag,','.join([x.tag for x in RetentionTag.get_all() if x.requires_product()])))) else: pass if tag.requires_product(): try: product = Product.by_name(product) return (tag, product) except ValueError: raise BX(_("You entered an invalid product name: %s" % product)) else: return tag, None
def test_loads_cpe_identifiers_from_xml_url(self): with open(os.path.join(self.product_docroot, 'product.xml'), 'wb') as xml_file: xml_file.write("""\ <products> <product> <cpe>cpe:/o:redhat:enterprise_linux:7.0</cpe> </product> <product> <cpe>cpe:/o:redhat:enterprise_linux:7:2</cpe> </product> </products> """) run_command('product_update.py', 'product-update', ['--product-url', 'http://localhost:19998/product.xml']) with session.begin(): Product.by_name(u'cpe:/o:redhat:enterprise_linux:7.0') Product.by_name(u'cpe:/o:redhat:enterprise_linux:7:2')
def test_loads_cpe_identifiers_from_xml_file(self): xml_file = tempfile.NamedTemporaryFile() xml_file.write("""\ <products> <product> <cpe>cpe:/a:redhat:ceph_storage:2</cpe> </product> <product> <cpe>cpe:/o:redhat:enterprise_linux:4:update8</cpe> </product> </products> """) xml_file.flush() run_command('product_update.py', 'product-update', ['-f', xml_file.name]) with session.begin(): # check that the products have been inserted into the db Product.by_name(u'cpe:/a:redhat:ceph_storage:2') Product.by_name(u'cpe:/o:redhat:enterprise_linux:4:update8')
def test_cloning_recipeset_from_job_with_product(self): with session.begin(): job = data_setup.create_job() job.retention_tag = RetentionTag.list_by_requires_product()[0] job.product = Product(u'product_name') b = self.browser login(b) b.get(get_server_base() + 'jobs/clone?job_id=%s' % job.id) cloned_from_job = b.find_element_by_xpath('//textarea[@name="textxml"]').text b.get(get_server_base() + 'jobs/clone?recipeset_id=%s' % job.recipesets[0].id) cloned_from_rs = b.find_element_by_xpath('//textarea[@name="textxml"]').text self.assertEqual(cloned_from_job,cloned_from_rs)
def test_search_product(self): with session.begin(): my_job = data_setup.create_job() new_product = Product(name=data_setup.unique_name('myproduct%s')) my_job.product = new_product b = self.browser # Test with product. b.get(get_server_base() + 'jobs') b.find_element_by_link_text('Show Search Options').click() b.find_element_by_xpath("//select[@id='jobsearch_0_table'] \ /option[@value='Product']").click() b.find_element_by_xpath("//select[@id='jobsearch_0_operation'] \ /option[@value='is']").click() b.find_element_by_xpath("//select[@id='jobsearch_0_value']/" "option[normalize-space(text())='%s']" % new_product.name).click() b.find_element_by_id('searchform').submit() job_search_result = \ b.find_element_by_xpath('//table[@id="widget"]').text self.assert_('J:%s' % my_job.id in job_search_result) with session.begin(): my_job.product = None # Test without product b.find_element_by_xpath("//select[@id='jobsearch_0_table'] \ /option[@value='Product']").click() b.find_element_by_xpath("//select[@id='jobsearch_0_operation'] \ /option[@value='is']").click() b.find_element_by_xpath( "//select[@id='jobsearch_0_value']/" "option[normalize-space(text())='None']").click() b.find_element_by_link_text('Add').click() b.find_element_by_xpath("//select[@id='jobsearch_1_table'] \ /option[@value='Id']").click() b.find_element_by_xpath("//select[@id='jobsearch_1_operation'] \ /option[@value='is']").click() b.find_element_by_xpath("//input[@id='jobsearch_1_value']"). \ send_keys(str(my_job.id)) b.find_element_by_id('searchform').submit() job_search_result = \ b.find_element_by_xpath('//table[@id="widget"]').text self.assert_('J:%s' % my_job.id in job_search_result)
def _process_job_tag_product(self, retention_tag=None, product=None, *args, **kw): """ Process job retention_tag and product """ retention_tag = retention_tag or RetentionTag.get_default().tag try: tag = RetentionTag.by_tag(retention_tag.lower()) except InvalidRequestError: raise BX( _("Invalid retention_tag attribute passed. Needs to be one of %s. You gave: %s" % (','.join([x.tag for x in RetentionTag.get_all() ]), retention_tag))) if product is None and tag.requires_product(): raise BX( _("You've selected a tag which needs a product associated with it, \ alternatively you could use one of the following tags %s" % ','.join([ x.tag for x in RetentionTag.get_all() if not x.requires_product() ]))) elif product is not None and not tag.requires_product(): raise BX( _("Cannot specify a product with tag %s, please use %s as a tag " % (retention_tag, ','.join([ x.tag for x in RetentionTag.get_all() if x.requires_product() ])))) else: pass if tag.requires_product(): try: product = Product.by_name(product) return (tag, product) except ValueError: raise BX(_("You entered an invalid product name: %s" % product)) else: return tag, None
def update_products(xml_file): dom = etree.parse(xml_file) xpath_string = '//cpe' cpes = dom.xpath(xpath_string) session.begin() try: to_add = {} dupe_errors = [] for cpe in cpes: cpe_text = cpe.text if cpe_text in to_add: dupe_errors.append(cpe_text) else: to_add[cpe_text] = 1 for cpe_to_add in to_add: prod = Product.lazy_create(name=unicode(cpe_to_add)) session.commit() finally: session.rollback()
def update_products_from_json(json_file): entries = json.load(json_file) for entry in entries: if isinstance(entry, dict) and entry.get('cpe'): assert isinstance(entry['cpe'], unicode) Product.lazy_create(name=entry['cpe'])
def create_product(product_name=None): if product_name is None: product_name = unique_name(u'product%s') return Product.lazy_create(name=product_name)