def __parseMemberDoc(self, id, ver): doc = cdr.getDoc('guest', id, version=str(ver), getObject=True) errors = cdr.getErrors(doc, errorsExpected=False, asSequence=True) if errors: raise Exception("loading member doc: %s" % "; ".join(errors)) dom = xml.dom.minidom.parseString(doc.xml) for node in dom.documentElement.childNodes: if node.nodeName == "BoardMemberContact": for child in node.childNodes: if child.nodeName == "PersonContactID": self.contactId = cdr.getTextContent(child) elif node.nodeName == "BoardMembershipDetails": self.__parseBoardMembershipDetails(node) elif node.nodeName == "BoardMemberAssistant": self.__parseBoardMemberAssistantInfo(node)
def save(self, label, doc_str, ver, pub, val): """ Invoke the CdrRepDoc command (or its local equivalent) If what we're about to do would overwrite unversioned XML, we recurse to create a numbered version to capture the current working document before continuing with the requested save. Keep track of which versions we have saved in `self.saved`. Pass: label - string identifying what we're saving (e.g., "new cwd") doc_str - serialized CDR document object ver - "Y" or "N" (should we create a version?) pub - "Y" or "N" (should the version be publishable?) val - "Y" or "N" (should we validate the document?) """ if self.preserve: preserve = str(self.preserve) self.preserve = None self.save("old cwd", preserve, "Y", "N", "N") args = self.id, ver, pub, val, label self.job.logger.info("save(%d, ver=%r pub=%r val=%r %s)", *args) opts = dict( doc=doc_str, ver=ver, val=val, publishable=pub, reason=self.job.comment, comment=self.job.comment, show_warnings=True, tier=self.job.tier, blob=self.blob, ) doc_id, errors = cdr.repDoc(self.job.session, **opts) if not doc_id: args = self.job, doc_str, ver, pub, val, errors self.capture_transaction(*args) args = self.cdr_id, errors message = "Failure saving changes for {}: {}".format(*args) raise Exception(message) if errors: opts = dict(asSequence=True, errorsExpected=False) warnings = cdr.getErrors(errors, **opts) for warning in warnings: self.job.logger.warning("%s: %s" % (self.cdr_id, warning)) self.saved.add(label)
def __init__(self, id): self.id = id self.name = None docId = 'CDR%d' % id versions = cdr.lastVersions('guest', docId) ver = str(versions[0]) doc = cdr.getDoc('guest', docId, version=ver, getObject=True) errors = cdr.getErrors(doc, errorsExpected=False, asSequence=True) if errors: raise Exception("loading doc %d for editor in chief: %s" % (id, "; ".join(errors))) dom = xml.dom.minidom.parseString(doc.xml) for node in dom.documentElement.childNodes: if node.nodeName == "PersonNameInformation": self.name = cdrmailer.PersonalName(node) if not self.name: raise Exception("No name found for editor-in-chief %d" % id)
def versionChanges(session, docId): LOGGER.info("saving unversioned changes for CDR%d", docId) doc = cdr.getDoc(session, docId, 'Y') err = cdr.checkErr(doc) if err: LOGGER.error("failure for CDR%d: %s", docId, err) return False docId, errors = cdr.repDoc(session, doc=doc, comment=COMMENT, reason=COMMENT, val='Y', ver='Y', showWarnings='Y', checkIn='Y', verPublishable='N') if errors: for e in cdr.getErrors(errors, asSequence=True): LOGGER.error(e) return docId and True or False
def __parseBoardDoc(self, id): today = str(self.today) docId = "CDR%d" % id versions = cdr.lastVersions('guest', docId) ver = str(versions[0]) doc = cdr.getDoc('guest', docId, version=ver, getObject=True) errors = cdr.getErrors(doc, errorsExpected=False, asSequence=True) if errors: raise Exception("loading doc for board %d: %s" % (id, "; ".join(errors))) dom = xml.dom.minidom.parseString(doc.xml) for node in dom.documentElement.childNodes: if node.nodeName == "OrganizationNameInformation": for child in node.childNodes: if child.nodeName == "OfficialName": for grandchild in child.childNodes: if grandchild.nodeName == "Name": self.name = cdr.getTextContent(grandchild) elif node.nodeName == "OrganizationType": self.boardType = cdr.getTextContent(node) elif node.nodeName == "PDQBoardInformation": managerNode = None phoneNode = None emailNode = None for child in node.childNodes: if child.nodeName == "BoardManager": managerNode = child elif child.nodeName == "BoardManagerPhone": phoneNode = child elif child.nodeName == "BoardManagerEmail": emailNode = child elif child.nodeName == "BoardMeetings": for grandchild in child.childNodes: if grandchild.nodeName == 'BoardMeeting': md = self.MeetingDate(grandchild) if md.date >= today: self.meetingDates.append(md) elif child.nodeName == "AdvisoryBoardFor": edBoardId = child.getAttribute("cdr:ref") self.edBoardId = cdr.exNormalize(edBoardId)[1] self.manager = self.Manager(managerNode, phoneNode, emailNode) if not self.name or not self.name.strip(): raise Exception("no name found for board in document %d" % id) if not self.manager: raise Exception("no board manager found in document %d" % id) self.boardValues = BoardValues.findBoardValues(self.name) self.summaryType = self.boardValues.summaryType self.workingGroups = self.boardValues.workingGroupBlock self.invitePara = self.boardValues.invitationParagraph self.meetingDates.sort(key=lambda a: a.date) self.name = self.name.strip() if self.boardType.upper() == 'PDQ ADVISORY BOARD': self.advBoardId = self.id self.advBoardName = self.name self.edBoardName = self.__getBoardName(self.edBoardId) elif self.boardType.upper() == 'PDQ EDITORIAL BOARD': self.edBoardId = self.id self.edBoardName = self.name self.advBoardId = self.__findAdvBoardFor(self.id) self.advBoardName = self.__getBoardName(self.advBoardId) else: raise Exception('Board type: %s' % self.boardType)
raise Exception("Can't have more than one sweep spec document") # If the document already exists, create a new version if result: doc_id = result[0].docId args = dict(checkout="Y", getObject=True, tier=opts.tier) doc = cdr.getDoc(opts.session, doc_id, **args) error_message = cdr.checkErr(doc) if error_message: parser.error(error_message) doc.xml = xml save_opts["doc"] = str(doc) doc_id, warnings = cdr.repDoc(opts.session, **save_opts) # Otherwise, create the document (with a first version) else: doc = cdr.Doc(xml, doctype, encoding="utf-8") save_opts["doc"] = str(doc) doc_id, warnings = cdr.addDoc(opts.session, **save_opts) # Let the user know how things went if warnings: print((doc_id and "WARNINGS" or "ERRORS")) for error in cdr.getErrors(warnings, asSequence=True): print((" -->", error)) if not doc_id: print("*** DOCUMENT NOT SAVED ***") else: versions = cdr.lastVersions(opts.session, doc_id, tier=opts.tier) print(("Saved {} as version {}".format(doc_id, versions[0])))
def expireMeetingRecordings(self, testMode): """ This is a "Custom" routine that sweeps away MP3 format meeting recordings that have passed their useful life. Implemented for JIRA Issue OCECDR-3886. Pass: testMode True = Don't actually delete any blobs, just report False = Update docs and delete blobs. """ cursor = None session = None # Need a connection to the CDR Server session = cdr.login('FileSweeper', cdr.getpw('FileSweeper')) if not session: FS_LOGGER.error("FileSweeper login to CdrServer failed") # But no reason not to do the rest of the sweep return # And a read-only connection to the database try: conn = db.connect() cursor = conn.cursor() except Exception as e: FS_LOGGER.exception("attempting DB connect") # But continue with the sweep cleanSession(cursor, session) return # Today's SQL Server date try: cursor.execute("SELECT GETDATE()") now = cursor.fetchone()[0] except Exception as e: FS_LOGGER.exception("getting DB date") cleanSession(cursor, session) return # Only want YYYY-MM-DD, not HMS nowDate = str(now)[:10] # Locate all Media documents linked to meeting recordings that # are older than Oldest days. # This is done by checking for any ADD DOCUMENT transaction in the # audit trail for one of the qualifying documents. If any ADD was # performed before the Oldest value, then there was a version of # the meeting recording from before that date. # The Media doc must also be found in one of the ...blob_usage tables. # If not, then any blob associated with it has already been deleted. isoFmt = "%Y-%m-%d" earlyDate = \ datetime.datetime.fromtimestamp(self.oldSpec).strftime(isoFmt) # DEBUG msg = "Looking for meeting recordings older than %s" FS_LOGGER.debug(msg, earlyDate) qry = """ SELECT d.id, d.title FROM document d JOIN query_term qt ON qt.doc_id = d.id JOIN audit_trail at ON at.document = d.id JOIN action act ON act.id = at.action WHERE qt.path = '/Media/MediaContent/Categories/Category' AND qt.value = 'meeting recording' AND act.name = 'ADD DOCUMENT' AND at.dt <= '%s' AND ( d.id IN ( SELECT doc_id FROM doc_blob_usage ) OR d.id IN ( SELECT doc_id FROM version_blob_usage ) ) """ % earlyDate # Read the info into memory try: cursor.execute(qry) rows = cursor.fetchall() except Exception as e: FS_LOGGER.exception("attempting to locate old blobs") cleanSession(cursor, session) return # If there weren't any, that's normal and okay if len(rows) == 0: FS_LOGGER.info("No meeting recordings needed to be deleted") cleanSession(cursor, session) return # Do we need to lock and load the docs for update? checkOut = 'Y' if testMode: checkOut = 'N' #------------------------------------------------------------------- # We've got some to delete. # For each Media document: # Send a transaction to the CDR Server to do the following: # Add a ProcessingStatus to the Media document to say what happened # Delete all of the blobs. #------------------------------------------------------------------- for row in rows: docId, title = row # Fetch the original document # We'll do this even in test mode to test the xml mods try: docObj = cdr.getDoc(session, docId, checkout=checkOut, getObject=True) except Exception as e: FS_LOGGER.exception("attempting to fetch doc %d", docId) cleanSession(cursor, session) return # Test for retrieval error, e.g., locked doc err = cdr.checkErr(docObj) if err: message = "Failed getDoc for CDR ID %s: %s, continuing" FS_LOGGER.error(message, docId, err) continue # Parse the xml preparatory to modifying it mediaRoot = et.fromstring(docObj.xml) # Create the new Comment field to record what we did # Make it the last subelement of the Media document element # It has to be there comment = et.SubElement(mediaRoot, 'Comment', audience='Internal', user='******', date=nowDate) comment.text = "Removed meeting recording object after expiration" # Back to serial XML newXml = et.tostring(mediaRoot) # If we're testing, just log what we would have done if testMode: # For log file actionMsg = 'would delete' else: # Send the doc back to the database: # Wrapped in CdrDoc wrapper # With command to delete all blobs actionMsg = 'deleted' saveXml = cdr.makeCdrDoc(newXml, 'Media', docObj.id) response = cdr.repDoc(session, doc=saveXml, comment='Removed meeting recording blobs', delAllBlobVersions=True, check_in=True) # Check response if not response[0]: errors = cdr.getErrors(response[1], errorsExpected=True, asSequence=False) message = "Saving Media xml for doc %s: %s" FS_LOGGER.error(message, docObj.id, errors) FS_LOGGER.info("Aborting expireMeetingRecords()") # Stop doing this, but continue rest of file sweeps. cleanSession(cursor, session) return # Log results for this media recording args = actionMsg, docId, title msg = "FileSweeper %s blobs for cdrId: %s\n%s" FS_LOGGER.info(msg, *args) # Cleanup cleanSession(cursor, session)
def checkForProblems(response, optionsParser): errors = cdr.getErrors(response, errorsExpected=False, asSequence=True) if errors: for error in errors: sys.stderr.write("%s\n" % error) optionsParser.error("aborting")
def main(): """ Store the new version of the filter. Processing steps: 1. Parse the command-line options and arguments. 2. Load the new version of the control document from the file system. 3. Find the CDR ID which matches the control document title 4. Log into the CDR on the target server. 5. Check out the document from the target CDR server. 6. Store the new version on the target CDR server. 7. Report the number of the new version. 8. Clean up. """ #------------------------------------------------------------------ # 1. Parse the command-line options and arguments. #------------------------------------------------------------------ parser = create_parser() opts = parser.parse_args() pub = "Y" if opts.publishable else "N" # If no comment is specified the last comment used (from the # all_docs table) would be stored. # Setting the comment to something to overwrite the last comment # ----------------------------------------------------------------- comment = opts.comment or "Replaced w/o user comment" #------------------------------------------------------------------ # 2. Load the new version of the control document from the file system. #------------------------------------------------------------------ with open(opts.filename) as fp: xml = fp.read() if "]]>" in xml: parser.error("CdrDoc wrapper must be stripped from the file") #------------------------------------------------------------------ # 3. Find out what the control document's document ID is. #------------------------------------------------------------------ doc_id = get_doc_id(opts.filename, opts.tier) #------------------------------------------------------------------ # 4. Log into the CDR on the target server. #------------------------------------------------------------------ if opts.session: session = opts.session else: password = getpass.getpass() session = cdr.login(opts.user, password, tier=opts.tier) error_message = cdr.checkErr(session) if error_message: parser.error(error_message) #------------------------------------------------------------------ # 5. Check out the document from the target CDR server. #------------------------------------------------------------------ args = dict(checkout="Y", getObject=True, tier=opts.tier) doc = cdr.getDoc(session, doc_id, **args) error_message = cdr.checkErr(doc) if error_message: parser.error(error_message) #------------------------------------------------------------------ # 6. Store the new version on the target CDR server. #------------------------------------------------------------------ doc.xml = xml args = dict(doc=str(doc), checkIn="Y", reason=comment, comment=comment, ver="Y", val="Y", verPublishable=pub, tier=opts.tier, showWarnings=True) doc_id, warnings = cdr.repDoc(session, **args) if warnings: print((doc_id and "WARNINGS" or "ERRORS")) for error in cdr.getErrors(warnings, asSequence=True): print((" -->", error)) if not doc_id: print("aborting with failure") #------------------------------------------------------------------ # 7. Report the number of the latest version. #------------------------------------------------------------------ versions = cdr.lastVersions(session, doc_id, tier=opts.tier) print(("Saved {} as version {}".format(doc_id, versions[0]))) #------------------------------------------------------------------ # 8. Clean up. #------------------------------------------------------------------ if not opts.session: cdr.logout(session, tier=opts.tier)