|
| 1 | +# This file is part of Invenio. |
| 2 | +# Copyright (C) 2015 CERN. |
| 3 | +# |
| 4 | +# Invenio is free software; you can redistribute it and/or |
| 5 | +# modify it under the terms of the GNU General Public License as |
| 6 | +# published by the Free Software Foundation; either version 2 of the |
| 7 | +# License, or (at your option) any later version. |
| 8 | +# |
| 9 | +# Invenio is distributed in the hope that it will be useful, but |
| 10 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +# General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with Invenio; if not, write to the Free Software Foundation, Inc., |
| 16 | +# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 17 | + |
| 18 | +"""Invenio Bibliographic Tasklet for updating CERN People Collection on the |
| 19 | +CERN Document Server. The collection is based on data fetched from CERN LDAP, |
| 20 | +including the Inspire-ID from ATLAS GLANCE. |
| 21 | +
|
| 22 | +Usage: |
| 23 | +$bibtasklet -N bibauthority-people -T bst_bibauthority_people_updater [-a file |
| 24 | + [default: invenio.bibauthority_people_config.CFG_RECORDS_JSON_FILE]] |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +from sys import stderr |
| 29 | +from invenio.bibauthority_people_config import ( |
| 30 | + CFG_BIBAUTHORITY_LDAP_ATTRLIST, CFG_BIBAUTHORITY_LDAP_SEARCHFILTER, |
| 31 | + CFG_BIBAUTHORITY_RECORDS_JSON_FILE, CFG_BIBAUTHORITY_RECORDS_UPDATES_FILE) |
| 32 | +from invenio.bibauthority_people_mapper import Mapper, MapperError |
| 33 | +from invenio.bibauthority_people_utils import (bibupload, diff_records, |
| 34 | + export_json, json_to_list, UtilsError) |
| 35 | +from invenio.bibtask import write_message |
| 36 | +from invenio.ldap_cern import get_users_records_data, LDAPError |
| 37 | + |
| 38 | + |
| 39 | +def update(records_updates): |
| 40 | + """Map updated records in record_diff and upload to CDS. |
| 41 | +
|
| 42 | + :param list records_updates: list of tuples (status, record), where status |
| 43 | + is 'add', 'remove', or 'change' |
| 44 | + """ |
| 45 | + write_message("{0} updated record(s) detected".format( |
| 46 | + len(records_updates))) |
| 47 | + if len(records_updates): |
| 48 | + try: |
| 49 | + # Map updated records |
| 50 | + mapper = Mapper() |
| 51 | + mapper.update_ldap_records(records_updates) |
| 52 | + |
| 53 | + # Write updates to XML |
| 54 | + mapper.write_marcxml(CFG_BIBAUTHORITY_RECORDS_UPDATES_FILE, 0) |
| 55 | + |
| 56 | + # Upload updates to CDS using --replace and --insert |
| 57 | + task_id = bibupload(CFG_BIBAUTHORITY_RECORDS_UPDATES_FILE, "-ri", |
| 58 | + "bibauthority-people-update") |
| 59 | + if task_id: |
| 60 | + write_message( |
| 61 | + "Task (identifier: {0}) is correctly enqueued" |
| 62 | + .format(task_id)) |
| 63 | + else: |
| 64 | + write_message("Error: failed to enqueue task", |
| 65 | + stderr) |
| 66 | + except MapperError as e: |
| 67 | + write_message(e, stderr) |
| 68 | + |
| 69 | + |
| 70 | +def bst_bibauthority_people_updater(file=CFG_BIBAUTHORITY_RECORDS_JSON_FILE): |
| 71 | + """Update CDS records with current CERN LDAP records. |
| 72 | +
|
| 73 | + :param filepath file: path to JSON file containing records |
| 74 | + """ |
| 75 | + try: |
| 76 | + # Fetch CERN LDAP records |
| 77 | + records_ldap = get_users_records_data( |
| 78 | + CFG_BIBAUTHORITY_LDAP_SEARCHFILTER, CFG_BIBAUTHORITY_LDAP_ATTRLIST) |
| 79 | + write_message("{0} records fetched from CERN LDAP" |
| 80 | + .format(len(records_ldap))) |
| 81 | + try: |
| 82 | + records_local = json_to_list(file) |
| 83 | + |
| 84 | + # records_diff contains updated records (changed, added, or |
| 85 | + # removed on LDAP) |
| 86 | + records_diff = diff_records(records_ldap, records_local) |
| 87 | + |
| 88 | + try: |
| 89 | + update(records_diff) |
| 90 | + # Update local file with current LDAP records |
| 91 | + export_json(records_ldap, file) |
| 92 | + except UtilsError as e: |
| 93 | + write_message(e, stderr) |
| 94 | + except UtilsError as e: |
| 95 | + write_message(e, stderr) |
| 96 | + except LDAPError as e: |
| 97 | + write_message(e, stderr) |
0 commit comments