def __init__(self, document_key=None, item=None):
     self.jama_client = JamaClient()
     if document_key is not None:
         self.document_key = document_key
         self.item = self.jama_client.get_item_for_documentkey(document_key)
         self.item_id = self.item["id"]
     elif item is not None:
         self.item = item
         self.item_id = item["id"]
     self.children = self.jama_client.get_children(self.item_id)
     self.item["hasChildren"] = len(self.children) != 0
示例#2
0
class Delete_Duplicates():
    def __init__(self):
        self.deleted_list = {}
        self.ignore_list = {}
        self.deleted_item_counter = 0
        self.ignored_item_counter = 0
        self.jamaClient = JamaClient()

    def delete(self, rows):
        for row in rows:
            item = self.jamaClient.get_item_for_documentkey(row)
            if item != None:
                if self.stripString(str(rows.get(row))) != self.stripString(
                        item["fields"]["name"]):
                    # if str(rows.get(row)).strip("]").strip("[").__contains__(item["fields"]["name"]) == False:
                    self.ignore_list.__setitem__(row, item)
                    print(
                        "Name from csv file did not match retrieved item's name. Item with documentKey: "
                        + row + " will not be deleted.")
                    self.ignored_item_counter = self.ignored_item_counter + 1
                else:
                    self.deleted_list.__setitem__(row, item)
                    print("deleting item with id: " + str(item["id"]) +
                          " and documentKey: " + row)
                    if self.jamaClient.delete_item(item["id"]) == True:
                        self.deleted_item_counter = self.deleted_item_counter + 1

        print("Total number of items deleted: " +
              str(self.deleted_item_counter))
        print("ToTal number of items ignored: " +
              str(self.ignored_item_counter))

        if self.ignored_item_counter > 0:
            with open('ignored_items.json', 'wb') as fp:
                json.dump(self.ignore_list, fp)

        if self.deleted_item_counter > 0:
            with open("deleted_items.json", "wb") as fp:
                json.dump(self.deleted_list, fp)

    def stripString(self, row):
        return row.replace("\"", "").replace("\'",
                                             "").replace("[", "").replace(
                                                 "]", "").replace("\\\\", "")
class ItemSet:
    def __init__(self, document_key=None, item=None):
        self.jama_client = JamaClient()
        if document_key is not None:
            self.document_key = document_key
            self.item = self.jama_client.get_item_for_documentkey(document_key)
            self.item_id = self.item["id"]
        elif item is not None:
            self.item = item
            self.item_id = item["id"]
        self.children = self.jama_client.get_children(self.item_id)
        self.item["hasChildren"] = len(self.children) != 0

    def __iter__(self):
        print "item"
        yield self.item
        for child in self.children:
            for item in ItemSet(item=child):
                yield item
class ItemType:
    def __init__(self, itemTypeId):
        self.jama_client = JamaClient()
        self.jama_config = JamaConfig()
        self.itemTypeId = itemTypeId
        self.itemType = json.loads(
            self.jama_client.get(self.jama_config.rest_url + "itemtypes/" +
                                 str(itemTypeId)).text)["data"]
        self.field_map = {}
        self.assemble_itemType()

    def assemble_itemType(self):
        fields = self.itemType.get("fields")
        for field in fields:
            self.assemble_and_add_to_field_map(field)

    def assemble_and_add_to_field_map(self, field):
        if field.get("readOnly") == True:
            return
        object = {}
        object.__setitem__("name", field.get("name"))
        if field.get("pickList") != None:
            isPicklist = True
        else:
            isPicklist = False
        object.__setitem__("isPicklist", isPicklist)
        if isPicklist == True:
            options = {}
            list_of_options = json.loads(
                self.jama_client.get(self.jama_config.rest_url + "picklists/" +
                                     str(field.get("pickList")) +
                                     "/options").text)["data"]
            for option in list_of_options:
                options.__setitem__(option.get("name"), option.get("id"))
            object.__setitem__("options", options)

        if field.get("label") == "DbID":
            self.field_map.__setitem__("UniqueID", object)
        else:
            self.field_map.__setitem__(field.get("label"), object)

    def get_field(self, field_label):
        return self.field_map.get(field_label)
class PostProcess:
    def __init__(self):
        self.macro = Macro()
        self.jama_client = JamaClient()
        self.items = []

    def process(self, document_keys):
        for document_key in document_keys:
            self.process_set(document_key)

    def process_set(self, document_key):
        # items = [item for item in ItemTree(document_key=document_key)]
        # items = [item for item in ItemSet(document_key=document_key)]

        itemTree = ItemTree(document_key=document_key)
        self.items = itemTree.get_tree_list()

        if len(self.items) == 1:
            return

        self.items = self.macro.run_list(self.items)

        iter_items = iter(self.items)
        next(iter_items)
        for item in iter_items:
            # print "loop"
            self.macro.run(item)
            self.jama_client.put(item)
        self.jama_client.clean_up()
        print("done")

    def establish_all_links(self, document_keys):
        for document_key in document_keys:
            self.establish_links(document_key)

    def establish_links(self, document_key):
        print "links"
        # time.sleep(60)
        print "resume"

        itemTree = ItemTree(document_key=document_key)
        processed_items = itemTree.get_tree_list()

        # processed_items = [item for item in ItemTree(document_key=document_key)]
        # processed_items = self.items
        if len(processed_items) == 1:
            return

        processed_items = self.macro.run_clean(processed_items)
        iter_items = iter(processed_items)
        next(iter_items)
        for item in iter_items:
            print "link_loop"
            try:
                del item["fields"]["id"]
            except KeyError:
                pass
            self.jama_client.put_item_raw(item)
示例#6
0
 def __init__(self):
     self.deleted_list = {}
     self.ignore_list = {}
     self.deleted_item_counter = 0
     self.ignored_item_counter = 0
     self.jamaClient = JamaClient()
示例#7
0
 def __init__(self):
     self.macro = Macro()
     self.jama_client = JamaClient()
     self.items = []
class PostProcess:
    def __init__(self):
        self.jama_client = JamaClient()
        self.items = []
        self.jama_config = JamaConfig()
        self.projectId = 0
        self.itemTypeId = 0
        self.itemType = None
        self.LOG_FILENAME = "failures.json"
        self.success = 0
        self.failure = 0

    def process(self):
        try:
            self.itemTypeId = self.jama_config.itemType
            self.projectId = self.jama_config.projectId
            if (self.itemTypeId != 0):
                self.retrieve_items()
                self.jama_config.logger.info("Retrieved [" +
                                             str(self.items.__len__()) +
                                             "] items of itemType [" +
                                             str(self.itemTypeId) + "]")
                self.process_items()
                self.jama_config.logger.info("Successfully processed [" +
                                             str(self.success) + "] items")
                self.jama_config.logger.error("Failed to process [" +
                                              str(self.failure) + "] items")

        except AttributeError:
            self.jama_config.logger.critical(
                "itemType is not set in jamaconfig.py")
        print "Finished..."

    def retrieve_items(self):
        self.itemType = ItemType(self.itemTypeId)
        if (self.itemType != None):
            self.jama_config.logger.info("Retrieving all items of itemType [" +
                                         str(self.itemTypeId) + "]...")
            self.items = self.jama_client.get_all("abstractitems?project=" +
                                                  str(self.projectId) +
                                                  "&itemType=" +
                                                  str(self.itemTypeId))
        else:
            self.jama_config.logger.error(
                "Something went wrong when retrieving itemType [" +
                str(self.itemTypeId) + "]")
            exit(1)

    def process_items(self):
        for item in self.items:
            attribute_key_values = self.name_contains_attributes(item)
            if attribute_key_values != None:
                self.jama_config.logger.info(
                    "Found an item [" + str(item["id"]) +
                    "] with attributes embedded in name...")
                self.extract_and_patch(item, attribute_key_values)
                self.jama_config.logger.info("Patched item [" +
                                             str(item["id"]) + "]")
            else:
                self.jama_config.logger.warning(
                    "Item [" + str(item["id"]) +
                    "] does not have attributes embedded in name - SKIPPING")

    def name_contains_attributes(self, item):
        name = item["fields"]["name"]
        pattern = "##.*#"
        found = re.search(pattern=pattern, string=name)
        if (found):
            print "Found!"
            return found.group()
        print "No found :("
        return None

    def extract_and_patch(self, item, attribute_key_values):
        attributes = attribute_key_values.replace("##", "").split(",")
        key_value_array = []
        for key_value_pair in attributes:
            assembled_key_value_pair = self.assemble_patch_op(key_value_pair)
            if assembled_key_value_pair != None:
                key_value_array.append(assembled_key_value_pair)
        if key_value_array.__len__() > 0:
            updated_name_op = self.assemble_updated_name(
                item, attribute_key_values)
            key_value_array.append(updated_name_op)
            response = self.jama_client.patch_item(item["id"], key_value_array)
            if response.status_code == 200:
                self.jama_config.logger.info("Successfully updated item [" +
                                             str(item["id"]) + "]")
                self.success = self.success + 1
            else:
                self.jama_config.logger.error("Failed to update item [" +
                                              str(item["id"]) + "]")
                self.failure = self.failure + 1

    def assemble_patch_op(self, key_value_pair):
        to_return = {}
        elements = key_value_pair.split(":")
        if elements.__len__() < 2:
            return None
        if (str(elements[1]) == " ") == True:
            return None
        # if(elements[0].lstrip(" ").rstrip(" ") == "UniqueID"):
        #     field = self.itemType.get_field("dbid")
        # else:
        field = self.itemType.get_field(elements[0].lstrip(" ").rstrip(" "))
        field_name = None
        field_value = None
        if field != None:
            field_name = field.get("name")
            if (field.get("isPicklist") == True):
                field_value = field.get("options").get(
                    elements[1].lstrip(" ").rstrip(" "))
            else:
                field_value = elements[1]
        else:
            self.jama_config.logger.warning("Field not found for [" +
                                            str(key_value_pair) +
                                            "] - skipping")

        if field_value != None and field_name != None:
            to_return.__setitem__("op", "add")
            to_return.__setitem__("path", "/fields/" + field_name)
            to_return.__setitem__("value", field_value)
            return to_return
        else:
            return None

    def assemble_updated_name(self, item, attribute_value_pair):
        name = item.get("fields").get("name").replace(attribute_value_pair, "")
        object = {}
        object.__setitem__("op", "add")
        object.__setitem__("path", "/fields/name")
        object.__setitem__("value", name)
        return object