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 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 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 _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: 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 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 _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