|
| 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 | +"""BibAuthority CERN people collection updater (Invenio Bibliographic Tasklet). |
| 19 | +
|
| 20 | +The collection is based on data fetched from CERN LDAP, including the |
| 21 | +Inspire-ID from ATLAS GLANCE. |
| 22 | +
|
| 23 | +Usage: |
| 24 | +$bibtasklet -N bibauthority-people |
| 25 | + -T bst_bibauthority_people_updater [-a json_file |
| 26 | + [default: invenio.bibauthority_people_config.CFG_RECORDS_JSON_FILE]] |
| 27 | +""" |
| 28 | + |
| 29 | +from sys import stderr |
| 30 | + |
| 31 | +from invenio.bibauthority_people_config import ( |
| 32 | + CFG_BIBAUTHORITY_LDAP_ATTRLIST, CFG_BIBAUTHORITY_LDAP_SEARCHFILTER, |
| 33 | + CFG_BIBAUTHORITY_RECORDS_JSON_FILE, CFG_BIBAUTHORITY_RECORDS_UPDATES_FILE) |
| 34 | +from invenio.bibauthority_people_mapper import Mapper, MapperError |
| 35 | +from invenio.bibauthority_people_utils import ( |
| 36 | + bibupload, diff_records, export_json, get_data_from_json, UtilsError, |
| 37 | + version_file) |
| 38 | +from invenio.bibtask import write_message |
| 39 | +from invenio.ldap_cern import get_users_records_data, LDAPError |
| 40 | + |
| 41 | + |
| 42 | +def update(records_updates): |
| 43 | + """Map updated records in record_diff and upload to CDS. |
| 44 | +
|
| 45 | + :param list records_updates: list of tuples (status, record), where status |
| 46 | + is 'add', 'remove', or 'change' |
| 47 | + """ |
| 48 | + write_message("{0} updated record(s) detected".format( |
| 49 | + len(records_updates))) |
| 50 | + if records_updates: |
| 51 | + try: |
| 52 | + # Map updated records |
| 53 | + mapper = Mapper() |
| 54 | + mapper.update_ldap_records(records_updates) |
| 55 | + |
| 56 | + # Write updates to XML |
| 57 | + mapper.write_marcxml(CFG_BIBAUTHORITY_RECORDS_UPDATES_FILE, 0) |
| 58 | + |
| 59 | + # Upload updates to CDS using --replace and --insert |
| 60 | + task_id = bibupload(CFG_BIBAUTHORITY_RECORDS_UPDATES_FILE, "-ri", |
| 61 | + "bibauthority-people-update") |
| 62 | + if task_id: |
| 63 | + write_message( |
| 64 | + "Task (identifier: {0}) is correctly enqueued".format( |
| 65 | + task_id)) |
| 66 | + else: |
| 67 | + write_message("Error: failed to enqueue task", |
| 68 | + stderr) |
| 69 | + except MapperError as e: |
| 70 | + write_message(e, stderr) |
| 71 | + |
| 72 | + |
| 73 | +def bst_bibauthority_people_updater( |
| 74 | + json_file=CFG_BIBAUTHORITY_RECORDS_JSON_FILE): |
| 75 | + """Update CDS records with current CERN LDAP records. |
| 76 | +
|
| 77 | + :param filepath json_file: path to JSON file containing records |
| 78 | + """ |
| 79 | + try: |
| 80 | + # Fetch CERN LDAP records |
| 81 | + records_ldap = get_users_records_data( |
| 82 | + CFG_BIBAUTHORITY_LDAP_SEARCHFILTER, |
| 83 | + CFG_BIBAUTHORITY_LDAP_ATTRLIST, |
| 84 | + "utf-8") |
| 85 | + write_message("{0} records fetched from CERN LDAP".format( |
| 86 | + len(records_ldap))) |
| 87 | + try: |
| 88 | + records_local = get_data_from_json(json_file) |
| 89 | + |
| 90 | + # records_diff contains updated records (changed, added, or |
| 91 | + # removed on LDAP) |
| 92 | + records_diff = diff_records(records_ldap, records_local) |
| 93 | + |
| 94 | + try: |
| 95 | + update(records_diff) |
| 96 | + # Version existing file |
| 97 | + version_file(json_file) |
| 98 | + # Update local file with current LDAP records |
| 99 | + export_json(records_ldap, json_file) |
| 100 | + except UtilsError as e: |
| 101 | + write_message(e, stderr) |
| 102 | + except UtilsError as e: |
| 103 | + write_message(e, stderr) |
| 104 | + except LDAPError as e: |
| 105 | + write_message(e, stderr) |
0 commit comments