Example #1
0
    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
Example #2
0
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")
Example #3
0
    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
Example #4
0
 def _unlock_doc(self, doc_id):
     """
     Release an existing lock on a CDR document.
     """
     id_string = cdr.normalize(doc_id)
     response = cdr.unlock(self._session, id_string, Job.COMMENT)
     if response:
         self._logger.error("failure unlocking %s: %s", id_string, response)
         return False
     return True
Example #5
0
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)
Example #6
0
    def run(self):
        """
        Transform a batch of CDR documents

        The overridden `select()` and `transform()` methods are
        invoked by this base class method.
        """

        if not self.testing:
            self.logger.info("Running in real mode, updating the database")
        else:
            self.logger.info("Saving test output to %r", self.output_directory)
        if self.tier:
            self.logger.info("Running on {}".format(self.tier))
        start = datetime.now()
        doc_ids = self.select()
        counts = dict(processed=0, saved=0, versions=0, locked=0, errors=0)
        types = {}
        self.logger.info("%d documents selected", len(doc_ids))
        self.logger.info("Purpose: %r", self.comment)
        for doc_id in doc_ids:
            self.doc = None
            needs_unlock = True
            try:
                self.doc = self.Doc(self, doc_id)
                self.logger.info("Processing %s", self.doc)
                self.doc.save_changes()
                if self.doc.saved:
                    counts["saved"] += 1
                if "cwd" in self.doc.changed:
                    counts["versions"] += 1
                if "lastv" in self.doc.changed:
                    counts["versions"] += 1
                if "lastp" in self.doc.changed:
                    counts["versions"] += 1
                for version_type in self.doc.saved:
                    types[version_type] = types.get(version_type, 0) + 1
            except Job.DocumentLocked as info:
                needs_unlock = False
                counts["locked"] += 1
                self.logger.warning(str(info))
            except Exception:
                self.logger.exception("Document %d", doc_id)
                counts["errors"] += 1
                if counts["errors"] > self.max_errors:
                    message = "Stopping after %d errors"
                    self.logger.error(message, counts["errors"])
                    break
            if needs_unlock:
                cdr.unlock(self.session, doc_id, tier=self.tier)
            counts["processed"] += 1
            if counts["processed"] >= self.max_docs:
                message = "Stopping after processing %d documents"
                self.logger.info(message, counts["processed"])
                break
        details = []
        if types:
            details = ["Specific versions saved:"]
            for key in sorted(types):
                details.append("  {} = {:d}".format(key, types[key]))
            details.append("")
        elapsed = datetime.now() - start
        self.logger.info("""\
Run completed.
   Docs examined    = {processed:d}
   Docs changed     = {saved:d}
   Versions changed = {versions:d}
   Could not lock   = {locked:d}
   Errors           = {errors:d}
   Time             = {time}
{details}""".format(details="\n".join(details), time=elapsed, **counts))
        if not self.__opts.get("session"):
            cdr.logout(self.session, tier=self.tier)
Example #7
0
#!/usr/bin/env python
#----------------------------------------------------------------------
#
# Unlock a document by its doc id.
#
#----------------------------------------------------------------------
import cdr, sys

if len(sys.argv) != 4:
    sys.stderr.write("usage: UnlockCdrDoc userId pw docId\n")
    sys.exit(1)

session = cdr.login(sys.argv[1], sys.argv[2])
if session.find("<Err") != -1:
    sys.stderr.write("Failure logging in to CDR: %s" % session)
    sys.exit(1)

# Accept doc id in any form, normalize to CDR000... form
docId = cdr.exNormalize(sys.argv[3])[0]

err = cdr.unlock(session, docId)
if err:
    sys.stderr.write("Failure unlocking %s: %s" % (docId, err))
else:
    print("unlocked " + docId)
Example #8
0
"""Unlock all documents checked out by a user.
"""

from argparse import ArgumentParser
from getpass import getpass
from sys import stderr
from cdr import login, unlock
from cdrapi import db

COMMENT = "Unlocked by UnlockDocsForUser script"

parser = ArgumentParser()
parser.add_argument("user")
parser.add_argument("--tier")
parser.add_argument("--session", required=True)
opts = parser.parse_args()
cursor = db.connect(user="******", tier=opts.tier).cursor()
query = db.Query("checkout c", "c.id").unique()
query.join("open_usr u", "u.id = c.usr")
query.where("c.dt_in IS NULL")
query.where(query.Condition("u.name", opts.user))
rows = query.execute(cursor).fetchall()
print(f"unlocking {len(rows):d} documents")
if rows:
    for row in rows:
        err = unlock(opts.session, row.id, tier=opts.tier, reason=COMMENT)
        if err:
            stderr.write(f"Failure unlocking CDR{row.id:010d}: {err}\n")
        else:
            print(f"unlocked CDR{row.id:010d}")