YODA-connect.ipynb 6.11 KB
Newer Older
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Experimental python-irodsclient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install --upgrade python-irodsclient"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check if the module is available:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import irods\n",
    "irods.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to SURF iRODS servers (YODA, NPEC, UNLOCK, ...)\n",
    "To connect to an iROD instance you need to know the URL of the iRODS server, your user credentials and in som cases also som SSL settings. Usually iRODS connections are coommunicated by a json file like this:\n",
    "\n",
    "```\n",
    "{\n",
    "    \"irods_host\": \"wur-yoda.irods.surfsara.nl\",\n",
    "    \"irods_port\": 1247,\n",
    "    \"irods_user_name\": \"christine.staiger@wur.nl\",\n",
    "    \"irods_zone_name\": \"wur\",\n",
    "    \"irods_home\": \"/wur/home\",\n",
    "    \"irods_authentication_scheme\": \"pam\",\n",
    "    \"irods_encryption_num_hash_rounds\": 16,\n",
    "    \"irods_client_server_policy\": \"CS_NEG_REQUIRE\",\n",
    "    \"irods_encryption_algorithm\": \"AES-256-CBC\",\n",
    "    \"irods_encryption_salt_size\": 8,\n",
    "    \"irods_ssl_verify_server\": \"none\",\n",
    "    \"irods_encryption_key_size\": 32\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from irods.session import iRODSSession\n",
    "import json\n",
    "\n",
    "with open('/home/WUR/staig001/.irods/irods_environment.json') as f:\n",
    "    ienv = json.load(f)\n",
    "with open('/home/WUR/staig001/.irods/passwd') as f:\n",
    "    passwd = f.readline()\n",
    "session = iRODSSession(**ienv, password=passwd)\n",
    "print(session.zone)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coll = session.collections.get('/'+session.zone+'/home/christine.staiger@wur.nl')\n",
    "print(coll.path)\n",
    "print(coll.data_objects)\n",
    "print(coll.subcollections)\n",
    "print(\"Your current collection:\", coll.path)\n",
    "iHome = coll.path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Metadata hooks for NPEC and UNLOCK\n",
    "## 1. The resource metadata tag \"free_space\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from irods.models import Collection, DataObject, Resource, CollectionMeta, DataObjectMeta, ResourceMeta\n",
    "from pprint import pprint\n",
    "\n",
    "query = session.query(Resource.name, Resource.free_space)\n",
    "print(query.all())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
Staiger, Christine's avatar
Staiger, Christine committed
117
    "Both, bundleResc and demoResc have been disabled. One cannot put data into them:"
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import irods.keywords as kw\n",
    "options = {kw.RESC_NAME_KW: 'demoResc'}\n",
    "%ls\n",
    "iPath = iHome+'/testUpload.txt'\n",
    "session.data_objects.put('hello.py', iPath, **options)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. The data tag \"RESOURCE\"\n",
Staiger, Christine's avatar
Staiger, Christine committed
138
    "The metadata key \"RESOURCE\" is reserved for data objects only and can only have the values 'disk', 'archive' and 'dual'. It moves the data objects' bitstreams to the respective data resource in iRODS."
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coll.metadata.add(\"RESOURCE\", \"disk\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj = session.data_objects.get(coll.path+\"/demo.txt\")\n",
    "obj.metadata.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.metadata.add(\"RESOURCE\", \"dual\")\n",
    "print(obj.metadata.items())\n",
    "vars(obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in obj.metadata.items():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You see that two metadata items are created with the key \"RESOURCE\". So metadata items are not updated but aggregated. In this case, remove the old metadata entry first and then create a new one with the new value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.metadata.remove(\"RESOURCE\", \"disk\")\n",
    "obj.metadata.remove(\"RESOURCE\", \"dual\")\n",
    "for item in obj.metadata.items():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Nonsense value\n",
    "obj.metadata.add(\"RESOURCE\", \"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in obj.metadata.items():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.metadata.add(\"RESOURCE\", \"archive\")\n",
    "for item in obj.metadata.items():\n",
    "    print(item)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python 3.8.5",
   "language": "python",
   "name": "python3.8.5"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}