-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummon-update.py
98 lines (86 loc) · 3.09 KB
/
summon-update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# puts MARC file to our Summon SFTP server
# https://knowledge.exlibrisgroup.com/Summon/Product_Documentation/Configuring_The_Summon_Service/Working_with_Local_Collections_in_the_Summon_Service/Getting_Local_Collections_Loaded_into_the_Summon_Index/Summon%3A_Exporting_Catalog_Holdings_-_Uploading_to_Summon
from datetime import datetime
import logging
import os
from typing import Literal
import click
from dotenv import dotenv_values
from pymarc import MARCReader, Record
import pysftp
config = {
**dotenv_values(".env"), # load shared development variables
**os.environ, # override loaded values with environment variables
}
# log to data/log.txt and stdout
logformat = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
logging.basicConfig(
filename=config.get("SUMMON_LOG_FILE", "data/log.txt"),
level=logging.INFO,
format=logformat,
)
# also to stdout
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter(logformat)
console.setFormatter(formatter)
logging.getLogger("summon_update").addHandler(console)
logger: logging.Logger = logging.getLogger("summon_update")
# fix pysftp bug "AttributeError: 'Connection' object has no attribute '_sftp_live'"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
def rename(filetype: str) -> str:
"""rename MARC file using Summon CDI conventions
Args:
filetype (str): type of update (deletes, updates, full)
Returns:
str: filename formatted for Summon SFTP server
"""
return f"cca-catalog-{filetype}-{datetime.now().strftime('%F-%H-%M-%S')}.mrc"
@click.command()
@click.argument("file_path")
@click.help_option("--help", "-h")
@click.option(
"-t",
"--type",
"filetype",
help="type of update",
required=True,
type=click.Choice(["updates", "deletes", "full"]),
)
@click.option(
"-d", "--debug", is_flag=True, help="enable SFTP debug logging", flag_value=1
)
def put_file(
file_path: str,
filetype: Literal["updates", "deletes", "full"],
debug: Literal[1, None] = None,
) -> None:
"""
Puts a file to the Summon SFTP server.
"""
# we need to know the number of records for Summon admin
with open(file_path, "rb") as fh:
reader = MARCReader(fh)
# count=1 for non-MARC files if we don't include the isinstance check
count: int = sum(1 for record in reader if isinstance(record, Record))
if count and count != 0:
logger.info(f"Number of records in {file_path}: {count}")
else:
logger.error(
f"No records found in {file_path}. Are you sure it's a MARC file?"
)
exit()
remote_path: str = f"{filetype}/{rename(filetype)}"
with pysftp.Connection(
cnopts=cnopts,
host=config["SUMMON_SFTP_HOST"],
log=debug, # type: ignore
port=int(config.get("SUMMON_SFTP_PORT", 22)),
private_key=config["SUMMON_SFTP_KEY"],
username=config["SUMMON_SFTP_USER"],
) as sftp:
sftp.put(file_path, remote_path)
logger.info(f"File {file_path} put to {remote_path}")
if __name__ == "__main__":
put_file()