def __init__(self, letter, name): """ Creates a new MiscellaneousDoc object for insertion into a correspondence mailer RTF document. Pass in the reference to the object for the letter into which the contents of the miscellaneous document are to be inserted, and the title of the CDR document to be loaded. """ import cdr from lxml import etree self.letter = letter path = "/MiscellaneousDocument/MiscellaneousDocumentTitle" query = f"{path}/value eq {name}" try: resp = cdr.search("guest", query) except Exception as e: raise Exception("failure loading misc doc {name!r}: {e}") if not resp: raise Exception(f"Miscellaneous document {name!r} not found") self.docId = resp[0].docId try: doc = cdr.getDoc("guest", self.docId, getObject=True) except Exception as e: raise Exception(f"Retrieving {self.docId}: {e}") self.root = etree.fromstring(doc.xml)
def load(self, cdr_id): """ Check out the CDR Term document, parse it, and update it with the current concept information. Pass: cdr_id - unique identified of the existing CDR Term document Return: cdr.Doc object with updated terms and/or definitions """ try: self.cdr_id, self.doc_id, frag_id = cdr.exNormalize(cdr_id) if cdr.lastVersions(self.session, self.cdr_id)[1] != -1: self.published = True except: Concept.fail("invalid CDR ID %r" % cdr_id) self.concept.logger.info("updating %s", self.cdr_id) try: doc = cdr.getDoc(self.session, self.cdr_id, "Y", getObject=True) except Exception as e: Concept.fail("failure retrieving %s: %s" % (self.cdr_id, e)) try: self.root = self.parse(doc.xml) doc.xml = self.update() return doc except Exception: cdr.unlock(self.session, self.cdr_id) raise
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 __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 _lock_doc(self, doc_id): """ Check out an existing CDR document. """ # If someone else has the document locked, break the lock. locker = self._find_locker(doc_id) if locker and locker.lower() != self._uid.lower(): if not self._unlock_doc(doc_id): return None # Fetch the document with a lock. doc = cdr.getDoc(self._session, doc_id, checkout="Y", getObject=True) err = cdr.checkErr(doc) if not err: return doc args = cdr.normalize(doc_id), err self._logger.error("failure locking %s: %r", *args) return None
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 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 filter from the file system. 3. Log into the CDR on the target server. 4. Find the CDR ID which matches the filter title 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 filter 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. 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) #------------------------------------------------------------------ # 4. Find out what the filter's document ID is. #------------------------------------------------------------------ doc_id = get_doc_id(xml, opts.tier, session) #------------------------------------------------------------------ # 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.encode("utf-8") args = dict( doc=str(doc), checkIn="Y", setLinks="N", reason=comment, comment=comment, ver="Y", verPublishable=pub, tier=opts.tier ) doc_id = cdr.repDoc(session, **args) if not doc_id.startswith("CDR"): parser.error(error_message) #------------------------------------------------------------------ # 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)
def load_versions(self): """ Check out and transform the XML for this document We get the current working document (CWD), the last version (if any), and the last publishable version (if any). """ session = self.job.session checkout = "N" if self.job.testing else "Y" opts = dict(checkout=checkout, getObject=True, tier=self.job.tier) try: cwd = cdr.getDoc(session, self.id, **opts) self.doc_objects["cwd"] = cwd except Exception as e: message = "Unable to check out {}: {}".format(self.cdr_id, e) raise Job.DocumentLocked(message) last_version, last_publishable_version, changed = self.versions errors = self.preserve = None if changed == "Y" or last_version < 1: self.preserve = deepcopy(cwd) new_xml = self.transformed_xml["cwd"] = self.job.transform(cwd) if self.job.validating: args = session, cwd.type, cwd.xml, new_xml errors = cdr.valPair(*args, tier=self.job.tier) self.errors["cwd"] = errors if self.job.creating_versions: if last_version > 0: if changed == "Y": opts["version"] = last_version try: lastv = cdr.getDoc(session, self.id, **opts) self.doc_objects["lastv"] = lastv except Exception as e: msg = "Failure retrieving lastv ({:d}) for {}: {}" args = last_version, self.cdr_id, e raise Exception(msg.format(*args)) new_xml = self.job.transform(lastv) self.transformed_xml["lastv"] = new_xml if self.job.validating: args = session, lastv.type, lastv.xml, new_xml errors = cdr.valPair(*args, tier=self.job.tier) self.errors["lastv"] = errors else: lastv = self.doc_objects["lastv"] = cwd self.transformed_xml["lastv"] = new_xml self.errors["lastv"] = errors if last_publishable_version > 0: if last_publishable_version != last_version: opts["version"] = last_publishable_version try: lastp = cdr.getDoc(session, self.id, **opts) self.doc_objects["lastp"] = lastp except Exception as e: msg = "Failure retrieving lastp ({:d}) for {}: {}" args = last_publishable_version, self.cdr_id, e raise Exception(msg.format(*args)) new_xml = self.job.transform(lastp) self.transformed_xml["lastp"] = new_xml if self.job.validating: args = session, lastp.type, lastp.xml, new_xml errors = cdr.valPair(*args, tier=self.job.tier) self.errors["lastp"] = errors else: self.doc_objects["lastp"] = lastv self.transformed_xml["lastp"] = new_xml self.errors["lastp"] = errors
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)
#!/usr/bin/env python """ Fetch and lock CDR document. """ import cdr import sys if len(sys.argv) == 4: session = cdr.login(sys.argv[1], sys.argv[2]) cdr_id = sys.argv[3] else: session, cdr_id = sys.argv[1:3] print(cdr.getDoc(session, cdr_id, "Y"))
counter += 1 newname = "%s.bak-%d" % (filename, counter) try: os.rename(filename, newname) print("old %s backed up to %s" % (filename, newname)) except: print("unable to rename %s to %s" % (filename, newname)) sys.exit(1) if len(sys.argv) != 2: usage() try: doc_id = int(sys.argv[1]) except: usage() filename = "%d.xml" % doc_id if os.path.exists(filename): move_file(filename) doc_obj = cdr.getDoc("guest", doc_id, checkout="N", getObject=True) parser = etree.XMLParser(remove_blank_text=True) tree = etree.fromstring(doc_obj.xml, parser) fp = open(filename, "wb") fp.write( etree.tostring(tree, encoding="utf-8", pretty_print=True, xml_declaration=True)) fp.close() print("wrote %s" % filename)
val="Y", tier=opts.tier, showWarnings=True) # See if we already have the document installed doctype = "SweepSpecifications" query = "CdrCtl/Title contains %" result = cdr.search("guest", query, doctypes=[doctype], tier=opts.tier) if len(result) > 1: 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:
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 main(): #------------------------------------------------------------------ # 1. Parse the command-line options and arguments. #------------------------------------------------------------------ op = createOptionParser() (options, args) = op.parse_args() if len(args) != 4: op.error("incorrect number of arguments") uid, pwd, idArg, filename = args if not options.publishable: options.publishable = 'N' elif options.publishable == 'Y': options.version = 'Y' fullId, intId, fragId = cdr.exNormalize(idArg) # 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 # ----------------------------------------------------------------- if not options.comment: options.comment = "Filter title changed" #------------------------------------------------------------------ # 2. Load the new version of the filter from the file system. #------------------------------------------------------------------ fp = open(filename, 'r') docXml = fp.read() fp.close() if ']]>' in docXml: op.error("CdrDoc wrapper must be stripped from the file") #------------------------------------------------------------------ # 3. Log into the CDR on the target server. #------------------------------------------------------------------ session = cdr.login(uid, pwd) checkForProblems(session, op) #------------------------------------------------------------------ # 4. Check out the document from the target CDR server. #------------------------------------------------------------------ docObj = cdr.getDoc(session, fullId, checkout='Y', getObject=True) checkForProblems(docObj, op) #------------------------------------------------------------------ # 5. Plug in the new title for the filter. #------------------------------------------------------------------ docObj.ctrl['DocTitle'] = getNewTitle(docXml) #------------------------------------------------------------------ # 6. Store the new version on the target CDR server. #------------------------------------------------------------------ doc = str(docObj) print('Versioned: %s, Publishable: %s' % (options.version, options.publishable)) cdrId = cdr.repDoc(session, doc=doc, checkIn="Y", setLinks="N", reason=options.comment, comment=options.comment, ver=options.version, verPublishable=options.publishable) checkForProblems(cdrId, op) #------------------------------------------------------------------ # 7. Report the number of the latest version. #------------------------------------------------------------------ versions = cdr.lastVersions(session, cdrId) if options.version == "N": print("CWD for %s updated" % cdrId) else: print("Latest version of %s is %d" % (cdrId, versions[0])) print("") print("DON'T FORGET TO CHANGE THE TITLE OF THIS FILTER ON ALL TIERS!")