创建 BagIt 包,从上游来源收集数据
项目描述
bagit-创建
根据CERN 提交信息包规范,“BagIt Create”是一种以一致格式导出包中的数字存储库记录的工具。
支持由 Invenio v1、Invenio v3、Invenio RDM、CERN Open Data 和 Indico 提供支持的数字存储库以及本地找到的文件夹。
快速开始:
# Install
pip install bagit-create
# Create bag for CDS record 2728246
bic --recid 2728246 --source cds
目录
安装
先决条件:
# On CentOS
yum install gcc krb5-devel python3-devel
如果您只需要从命令行运行 BagIt Create:
# Install from PyPi
pip install bagit-create
# Check installed version
bic --version
# Create bag for CDS record 2728246
bic --recid 2728246 --source cds
LXPLUS
BagIt-Create 可以在 LXPLUS 上轻松安装和使用(例如,如果您需要访问已安装的 EOS 文件夹):
pip3 install bagit-create --user
检查.local/bin(pip 放置可执行文件的位置)是否在路径中。如果没有export PATH=$PATH:~/.local/bin。
发展
克隆此存储库,然后使用以下-e标志安装软件包:
# Clone the repository
git clone https://gitlab.cern.ch/digitalmemory/bagit-create
cd bagit-create
# Create a virtual environment and activate it
python -m venv env
source env/bin/activate
# Install the project in editable mode
pip install -e .
# Check installed version
bic --version
# You're done! Create a SIP for a CDS resource from its URL:
bic --url http://cds.cern.ch/record/2665537
# Install additional packages for testing
pip install pytest oais_utils
# Run tests
python -m pytest
代码使用黑色格式化,并用 flake8 进行了绒毛处理。为方便起见,提供了 VSCode 设置文件。
用法
您通常只需要指定您尝试为其创建包的记录的位置。
您可以通过指定“来源”(请参阅支持的来源)和记录 ID 来做到这一点:
bic --recid 2728246 --source cds
或传递 URL(目前仅适用于 CDS、Zenodo 和 CERN 开放数据链接):
bic --url http://cds.cern.ch/record/2665537
例子
光盘:
# (Expect error) Removed resource
bic --recid 1 --source cds
# (Expect error) Public resource but metadata requires authorisation
bic --recid 1000 --source cds
# Resource with a lot of large videos, light bag
bic --recid 1000571 --source cds --dry-run
# Resource with just a PDF
bic --recid 2728246 --source cds
文件:
# ilcdoc #
bic --source ilcdoc --recid 62959 --verbose
bic --source ilcdoc --recid 34794 --verbose
芝诺多
bic --recid 3911261 --source zenodo --verbose
bic --recid 3974864 --source zenodo --verbose
靛蓝
bic --recid 1024767 --source indico
欧洲核子研究中心开放数据
bic --recid 1 --source cod --dry-run --verbose
bic --recid 8884 --source cod --dry-run --verbose --alternate-uri
bic --recid 8884 --source cod --dry-run --verbose
bic --recid 5200 --source cod --dry-run --verbose
bic --recid 8888 --source cod --dry-run --verbose
bic --recid 10101 --source cod --dry-run --verbose
bic --recid 10102 --source cod --dry-run --verbose
bic --recid 10103 --source cod --dry-run --verbose
bic --recid 10104 --source cod --dry-run --verbose
bic --recid 10105 --source cod --dry-run --verbose
bic --recid 10101 --source cod --verbose
bic --recid 10102 --source cod --verbose
bic --recid 10103 --source cod --verbose
bic --recid 10104 --source cod --verbose
bic --recid 10105 --source cod --verbose
选项
--version Show the version and exit.
--recid TEXT Record ID of the resource the upstream
digital repository. Required by every
pipeline but local.
-s, --source [cds|ilcdoc|cod|zenodo|inveniordm|indico|local|ilcagenda]
Select source pipeline from the supported
ones.
-u, --url TEXT Provide an URL for the Record
[Works with CDS, Open Data and Zenodo links]
-d, --dry-run Skip downloads and create a `light` bag,
without any payload.
-a, --alternate-uri Use alternative uri instead of https for
fetch.txt (e.g. root endpoints for CERN
Open Data instead of http).
-v, --verbose Enable basic logging (verbose, 'info'
level).
-vv, --very-verbose Enable verbose logging (very verbose,
'debug' level).
-b, --bibdoc [ONLY for Supported Invenio v1 pipelines]
Get metadata for a CDS record from the
bibdocfile utility
(`/opt/cdsweb/bin/bibdocfile` must be
available in the system).
--bd-ssh-host TEXT [ONLY for Supported Invenio v1 pipelines]
Specify SSH host to run bibdocfile. Access
must be promptless. (See documentation for
usage and configuration). By default uses
the local machine.
-t, --target TEXT Output folder for the generated SIP. By
default uses the same folder the tool is
being executed from.
-sp, --source-path TEXT [Local source ONLY, required] Set path of
the local folder to use as a source.
-u, --author TEXT [Local source ONLY] Specify the Author of
data.
-sbp, --source-base-path TEXT [Local source ONLY] Specify a part of the
path as relevant for extracting an
hierachy.
-ic, --invcookie TEXT [Invenio v1.x ONLY] Use custom
INVENIOSESSION cookie value to authenticate.
Useful for local accounts.
-ss, --skipssl [Invenio v1.x ONLY] Skip SSL authentication
in HTTP requests. Useful for misconfigured
or deprecated instances.
-c, --cert TEXT [Invenio v1.x ONLY] Full path to the
certificate to use to authenticate. Don't
specify extension, only the file name. A
'.key' and a '.pem' will be loaded. Read
documentation (CERN SSO authentication) to
learn more on how to generate it.
--help Show this message and exit.
特征
支持的来源
| 姓名 | 来源 ID | 网址 | 管道 |
|---|---|---|---|
| CERN 文件服务器 | 光盘 | https://cds.cern.ch/ | Invenio v1.x |
| ILC 文件服务器 | 文档 | http://ilcdoc.linearcollider.org | Invenio v1.x |
| 欧洲核子研究中心开放数据 | 鳕鱼 | https://opendata.cern.ch/ | 欧洲核子研究中心开放数据 |
| 芝诺多 | 泽诺多 | https://zenodo.org/ | Invenio v3.x |
| 欧洲核子研究组织 | 靛蓝 | https://indico.cern.ch/ | 靛蓝 v3.0.x |
| 国际劳工大会议程 | 伊尔卡根达 | https://agenda.linearcollider.org/ | 靛蓝 v3.0.x |
| CodiMD | 密码 | https://codimd.web.cern.ch/ | CodiMD |
可能需要额外的配置(例如受限事件)。
网址解析
--url您可以只使用带有选项的记录 URL,而不是传递源 + 记录 ID 。
灯袋
通过该--dry-run选项,BIC 可以创建“轻量”包,跳过任何有效负载下载(即附加文件),但生成相同的清单(公开上游文件位置和 URL),从而允许将来“填充”完整包。
配置
一些管道需要额外的配置(例如验证)。
靛蓝
要使用任何 Indico 管道,您需要一个 API 令牌。从您的浏览器登录到 Indico 实例,转到“首选项”,然后转到“API 令牌”。创建新令牌,名称可以是任何东西。选择(至少)Everything (all methods)和Classic API (read only)作为范围。记下令牌并将INDICO_KEY环境变量设置为它。
export INDICO_KEY=<INDICO_API_TOKEN>
这也将允许您针对您有权访问的受限事件运行该工具。
Invenio v1.x
欧洲核子研究中心 SSO
通过 cern -sso-python工具获取 CERN SSO HTTP cookie,BIC 可以在 Invenio v1.x 管道(例如 CDS)的“身份验证”模式下运行。
为此,您需要提供从有权访问所需受限记录的帐户的CERN 证书颁发机构获得的网格用户证书。
下载.p12证书后,您需要处理证书文件以删除密码并将密钥和证书分开:
openssl pkcs12 -clcerts -nokeys -in myCert.p12 -out myCert.pem
# A passphrase is required here (after the Import one)
openssl pkcs12 -nocerts -in myCert.p12 -out myCert.tmp.key
openssl rsa -in ~/private/myCert.tmp.key -out myCert.key
警告:openssl rsa.. 命令从私钥中删除密码。将其保存在安全的位置。
一旦你有了你的myCert.key和myCert.pem文件,你可以运行 BagIt-Create--cert选项,提供这些文件的路径(没有扩展名,因为假设你的证书和密钥文件具有相同的基本名称并且位于同一个文件夹中,并且密钥具有文件结尾.key和证书.pem)。例如:
bic --source cds --recid 2748063 --cert /home/avivace/Downloads/myCert
将使该工具查找“ /home/avivace/Downloads/myCert.key ”和“ /home/avivace/Downloads/myCert.pem ”,并且管道将使用获得的 Cookie 对每个 HTTP 请求进行身份验证,生成一个 SIP想要的受限记录。
有关更多信息,请查看cern-sso-python文档。
当地的
要使用本地帐户进行身份验证(即没有 CERN SSO),请使用浏览器登录您的 Invenio v1.x 实例以及您的INVENIOSESSIONcookie 设置。
在 Firefox 上,打开开发者工具,进入“存储”选项卡并选择“Cookies”,您应该会看到一个INVENIOSESSIONcookie。复制其值并将其传递给 BagIt Create 并使用以下--token选项:
bic --source cds --recid 2748063 --token <INVENIOSESSION_value_here>
CodiMD
要从 CodiMD 文档创建包,请转到https://codimd.web.cern.ch/,进行身份验证并在重定向到主页后打开浏览器开发人员工具(CTRL+SHIFT+I),转到“存储" 选项卡并在 cookie 下复制 cookie 的值connect.sid。
CodiMD 文档的“记录 ID”是主域地址之后的 url 部分(例如,https://codimd.web.cern.ch/KabpdG3TTHKOsig2lq8tnw#在 recid 中KabpdG3TTHKOsig2lq8tnw)
bic --source codimd --recid vgGgOxGQU --token <connect.sid_value_here>
转储完整历史
此存储库中包含一个小脚本,examples/codimd_history.py其中将转储您的整个 CodiMD“历史记录”(您在主页上看到的相同历史记录),为每个文档创建一个包。
connect.sid在运行脚本之前将 CODIMD_SESSION 环境变量设置为 cookie 的值:
CODIMD_SESSION=<connect.sid_value_here> python examples/codimd_history.py
高级用法
模块
BIC 可以很容易地在其他 Python 脚本中运行。只需将其导入并使用process可以传递给 CLI 的相同参数的方法。
例如,此代码段为从 ID 2728246 到 27282700 的 CDS 资源创建 SIP 包。
import bagit_create
for i in range(2728246, 27282700):
result = bagit_create.main.process(
source="cds", recid=i, loglevel=3
)
if result["status"] == 0:
print("Success")
else:
print("Error")
访问 CERN 防火墙网站
如果您尝试访问的上游源有防火墙,您可以通过 LXPLUS 的 SSH 隧道设置 SOCKS5 代理,然后使用或bic之类的工具运行它。例如:proxychainstsocks
启动 SSH 隧道:
ssh -D 1337 -q -N -f -C lxplus.cern.ch
代理将在socks5://localhost:1337. 安装后tsocks,编辑其配置文件 ( /etc/tsocks.conf),如下所示:
[...]
server = localhost
server_type = 5
server_port = 1337
[...]
现在,只需bic按照此处记录的方式运行,但tsocks添加到命令之前:
tsocks bic --recid 1024767 --source indico -vv
文档文件
bibdocfile命令行实用程序可用于获取 CDS 的元数据,公开通常无法通过 CDS API 获得的内部文件路径和哈希值。
如果可执行文件在路径中可用(即您可以运行/opt/cdsweb/bin/bibdocfile),只需附加--bibdoc:
bic --recid 2751237 --source cds --bibdoc -v
如果不是这种情况,您可以传递一个--bd-ssh-host参数,指定 SSH 配置连接的名称,指向能够为您运行命令的机器。请注意,您的机器必须能够在没有任何用户交互的情况下建立此类连接(脚本将运行ssh <THE_PROVIDED_SSH_HOST> bibdocfile ..args)。
由于在正常的 CERN 场景中,由于需要 ProxyJumps/OTP 身份验证步骤,这是不可能的,因此您可以使用ControlMaster任何最新版本的 OpenSSH 的功能,允许重用套接字进行连接:
添加一个条目~/.ssh/config以设置与能够以bibdocfile下列方式运行的远程计算机的 SSH 连接:
Host <SSH_NAME>
User <YOUR_USER>
Hostname <HOSTNAME.cern.ch>
ProxyJump <LXPLUS_or_AIADM>
ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r
然后,ssh <SSH_NAME>在 shell 中运行,进行身份验证并保持打开状态。OpenSSH 现在将在您每次运行时重用此套接字,如果您传递参数,则<SSH_NAME>允许 BagItCreate 工具为您运行此 ssh 连接:bibdocfilebd-ssh-host
bic --recid 2751237 --source cds --bibdoc --bd-ssh-host=<SSH_NAME> -v