def _add_doc(self, doc_type, doc_title, doc_xml): """ Add a document which was lost because it was not on PROD. """ self._logger.info("adding %r document %r", doc_type, doc_title) # Wrap the document XML in the CdrDoc wrapper and create it. doc = cdr.makeCdrDoc(doc_xml, doc_type, ctrl={"DocTitle": doc_title}) doc_id = cdr.addDoc(self._session, doc=doc, checkIn="N", comment=Job.COMMENT, reason=Job.COMMENT) err = cdr.checkErr(doc_id) if err: self._logger.error("failure creating document: %s", err) return # Newly created document need to be versioned and unlocked separately. doc = self._lock_doc(doc_id) if doc: response = cdr.repDoc(self._session, doc=str(doc), checkIn="Y", val="Y", ver="Y", reason=Job.COMMENT, comment=Job.COMMENT) err = cdr.checkErr(response) if err: self._logger.error("failure unlocking %s: %s", doc_id, err)
def main(): parser = create_parser() opts = parser.parse_args() title = opts.title if not title: parser.error("empty title argument") if not isinstance(title, unicode): title = unicode(title.strip(), "latin-1") if "--" in title: parser.error("filter title cannot contain --") if not opts.session: password = getpass.getpass() session = cdr.login(opts.user, password, tier="PROD") error = cdr.checkErr(session) if error: parser.error(error) else: session = opts.session stub = u"""\ <?xml version="1.0" ?> <!-- Filter title: {} --> <xsl:transform xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" xmlns:cdr = "cips.nci.nih.gov/cdr" version = "1.0"> <xsl:output method = "xml" encoding = "utf-8"/> <xsl:param name = "sample-param" select = "'default-value'"/> <!-- Sample template --> <xsl:template match = "@*|node()"> <xsl:copy> <xsl:apply-templates select = "@*|node()"/> </xsl:copy> </xsl:template> </xsl:transform> """.format(escape(title)).encode("utf-8") title = title.encode("utf-8") ctrl = dict(DocTitle=title) doc_opts = dict(doctype="Filter", ctrl=ctrl, encoding="utf-8") doc = cdr.Doc(stub, **doc_opts) cdr_id = cdr.addDoc(session, doc=str(doc), tier="PROD") error = cdr.checkErr(cdr_id) if error: parser.error(error) response = cdr.unlock(session, cdr_id, tier="PROD") error = cdr.checkErr(response) if error: parser.error(error) name = cdr_id + ".xml" with open(name, "wb") as fp: fp.write(stub) print(("Created {}".format(name))) if not opts.session: cdr.logout(session, tier="PROD")
def main(): """ Top-level entry point """ # Process the command-line arguments. parser = create_parser() opts = parser.parse_args() # Make sure we're not doing this on the production server. if not opts.tier and cdr.isProdHost() or opts.tier == "PROD": parser.error(""" This program can only be used to install a filter on the development or test server, not production. Use CreateFilter.py to create the filter in the production database, then use InstallFilter.py to install it in test or development with the same title/name and (almost certainly) a different local CDR ID. """) # If we don't already have a session ID, make one. if not opts.session: password = getpass.getpass() session = cdr.login(opts.user, password, tier=opts.tier) error = cdr.checkErr(session) if error: parser.error(error) else: session = opts.session # Load the document. info = DocInfo(opts.filename, parser) # Make sure the filter isn't already installed info.check_unique_title(opts.tier, parser) # All checks passed: add the document. ctrl = dict(DocTitle=info.title.encode("utf-8")) doc = cdr.Doc(info.xml, doctype="Filter", ctrl=ctrl, encoding="utf-8") comment = "New filter install" add_opts = dict(doc=str(doc), comment=comment, tier=opts.tier) cdr_id = cdr.addDoc(session, **add_opts) error = cdr.checkErr(cdr_id) if error: parser.error(error) # Unlock the document and display its ID. response = cdr.unlock(session, cdr_id, tier=opts.tier) error = cdr.checkErr(response) if error: parser.error(error) else: print(cdr_id)
def save(self): """ Create a version of the CDR Term document in the repository. Return: String describing successful creation of a new Term document (including the CDR ID for the new document); or sequence of strings describing changes made to an existing Term document. Raises an exception on failure. """ verb = self.doc.id and "Updating" or "Importing" opts = { "doc": str(self.doc), "comment": "%s Term document from NCI Thesaurus" % verb, "val": "Y", "ver": "Y", "verPublishable": self.published and "Y" or "N", "showWarnings": True } try: if self.doc.id: if self.changes: result = cdr.repDoc(self.session, **opts) Concept.logger.info("repDoc() result: %r", result) cdr_id, errors = result if not cdr_id: Concept.fail("failure versioning %s: %s", self.cdr_id, errors) response = self.changes or None else: result = cdr.addDoc(self.session, **opts) Concept.logger.info("addDoc() result: %r", result) self.cdr_id, errors = result if not self.cdr_id: Concept.fail("failure adding new document: %s" % errors) response = "Added %s as %s" % (self.concept.code, self.cdr_id) except Exception: if self.cdr_id: cdr.unlock(self.session, self.cdr_id) raise cdr.unlock(self.session, self.cdr_id) return response
def add(self): """ Add the document to the CDR repository (if not testing). Return True, which is bubbled up to the main loop in `run()`. """ if self.control.opts.test: self.control.logger.info("%s is new", self.name) return True comment = "Added by install-docset.py" ctrl = { "DocTitle": self.title } opts = { "type": self.doctype, "encoding": "utf-8", "ctrl": ctrl } cdr_doc = cdr.Doc(self.xml, **opts) opts = dict(doc=str(cdr_doc), checkIn="Y", ver="Y", comment=comment) opts["publishable"] = self.control.PUBLISHABLE cdr_id = cdr.addDoc(self.control.session, **opts) error = cdr.checkErr(cdr_id) if error: self.control.logger.error(error) sys.exit(1) self.control.logger.info("added %s as %s", self.name, cdr_id) return True
def addMailerTrackingDoc(self, doc, recipient, mailerType, remailerFor=None, protOrgId=None, email=None): """ Parameters: doc - Object of type Document, defined below recipient - Object of type Recipient, defined below mailerType - String containing a values matching the list of valid values for MailerType enumerated in the schema for Mailer docs. remailerFor - optional integer for the document ID of an earlier mailer that was sent out and never responded to, and for which this is a followup remailer. protOrgId - string or integer form of CDR ID for a protocol's lead organization (the one to which this mailer is being sent); used to distinguish between Status and Participant mailers for the same protocol in the same job. email - address used for electronic mailer. Return value: Integer ID for the newly inserted Mailer document. """ if remailerFor: remailerFor = "\n <RemailerFor cdr:ref='%s'/>" % \ cdr.normalize(remailerFor) else: remailerFor = "" if protOrgId: protOrg = "\n <ProtocolOrg cdr:ref='%s'/>" % \ cdr.normalize(protOrgId) else: protOrg = "" if email: mode = "Web-based" address = """\ <MailerAddress> <Email>%s</Email> </MailerAddress>""" % email else: mode = "Mail" address = recipient.getAddress().getXml() recipId = "CDR%010d" % recipient.getId() docId = "CDR%010d" % doc.getId() xml = """\ <CdrDoc Type="Mailer"> <CdrDocCtl> <DocTitle>Mailer for document %s sent to %s</DocTitle> </CdrDocCtl> <CdrDocXml><![CDATA[ <Mailer xmlns:cdr="cips.nci.nih.gov/cdr"> <Type Mode='%s'>%s</Type>%s <JobId>%d</JobId> <Recipient cdr:ref="%s">%s</Recipient>%s %s <Document cdr:ref="%s">%s</Document> <Sent>%s</Sent> <Deadline>%s</Deadline> </Mailer>]]> </CdrDocXml> </CdrDoc> """ % (docId, recipId, mode, mailerType, remailerFor, self.__id, recipId, recipient.getName(), protOrg, address, docId, doc.getTitle(), self.__now, self.getDeadline()) rsp = cdr.addDoc(self.__session, doc=xml.encode('utf-8'), checkIn="Y", ver="Y", val='Y') match = self.__ERR_PATTERN.search(rsp) if match: err = match.group(1) raise Exception("failure adding tracking document for %s: %s" % (docId, err)) self.__nMailers += 1 digits = re.sub("[^\d]", "", rsp) return int(digits)
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])))