-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathblobs_mv.py
More file actions
319 lines (272 loc) · 12.4 KB
/
blobs_mv.py
File metadata and controls
319 lines (272 loc) · 12.4 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
# --------------------------------------------------------------------------- #
# The MIT License (MIT) #
# #
# Copyright (c) 2021 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> #
# #
# Permission is hereby granted, free of charge, to any person obtaining #
# a copy of this software and associated documentation files #
# (the "Software"), to deal in the Software without restriction, including #
# without limitation the rights to use, copy, modify, merge, publish, #
# distribute, sublicense, and/or sell copies of the Software, and to permit #
# persons to whom the Software is furnished to do so, subject to the #
# following conditions: #
# #
# The above copyright notice and this permission notice shall be included #
# in all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL #
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
# --------------------------------------------------------------------------- #
"""Functions to help move the blobs from downloaded LBRY content."""
import os
import shutil
import lbrytools.funcs as funcs
import lbrytools.sort as sort
import lbrytools.blobs as blobs
def blobs_move(uri=None, cid=None, name=None,
move_dir=None, blobfiles=None, print_missing=False,
action="copy",
server="http://localhost:5279"):
"""Copy or move the binary blobs of a downloaded claim.
Parameters
----------
uri: str
A unified resource identifier (URI) to a claim on the LBRY network.
It can be full or partial.
::
uri = 'lbry://@MyChannel#3/some-video-name#2'
uri = '@MyChannel#3/some-video-name#2'
uri = 'some-video-name'
The URI is also called the `'canonical_url'` of the claim.
cid: str, optional
A `'claim_id'` for a claim on the LBRY network.
It is a 40 character alphanumeric string.
name: str, optional
A name of a claim on the LBRY network.
It is normally the last part of a full URI.
::
uri = 'lbry://@MyChannel#3/some-video-name#2'
name = 'some-video-name'
move_dir: str, optional
It defaults to `$HOME`.
The path to where the blobs will be copied or moved.
They will be placed inside their own subdirectory which will be named
after the channel's name, and the claim's name.
blobfiles: str, optional
It defaults to `'$HOME/.local/share/lbry/lbrynet/blobfiles'`.
The path to the directory where the blobs were downloaded.
This is normally seen with `lbrynet settings get`, under `'data_dir'`.
It can be any other directory if it is symbolically linked
to it, such as `'/opt/lbryblobfiles'`
print_missing: bool, optional
It defaults to `False`.
If it is `True` it will print all blobs
that don't exist in `blobfiles`, and thus that cannot be copied
or moved to `move_dir`.
action: str, optional
It defaults to `'copy'`, in which case the blobs are copied to the
`move_dir` path.
If it is `'move'` the blobs will be moved, so they won't be
in `blobfiles` any more.
server: str, optional
It defaults to `'http://localhost:5279'`.
This is the address of the `lbrynet` daemon, which should be running
in your computer before using any `lbrynet` command.
Normally, there is no need to change this parameter from its default
value.
Returns
-------
bool
Returns `True` if the blobs were copied successfully.
It returns `False` if the claim does not exist,
or if the `'sd_hash'` does not exist in the `blobfiles` directory,
of if at least one blob is missing.
The last point may mean that the claim was not downloaded fully,
so it needs to be redownloaded.
"""
if not funcs.server_exists(server=server):
return False
if (not move_dir or not isinstance(move_dir, str)
or move_dir == "~" or not os.path.exists(move_dir)):
move_dir = os.path.expanduser("~")
print("Destination directory should exist; "
f"set to move_dir='{move_dir}'")
if (not blobfiles or not isinstance(blobfiles, str)
or not os.path.exists(blobfiles)):
print("Copy or move the blobs from the blobfiles directory")
print(f"blobfiles={blobfiles}")
print("This is typically '$HOME/.local/share/lbry/lbrynet/blobfiles'")
home = os.path.expanduser("~")
blobfiles = os.path.join(home,
".local", "share",
"lbry", "lbrynet", "blobfiles")
if not os.path.exists(blobfiles):
print(f"Blobfiles directory does not exist: {blobfiles}")
return False
if (not isinstance(action, str)
or action not in ("copy", "move")):
print(">>> Error: action can only be 'copy', 'move'")
print(f"action={action}")
return False
blob_info = blobs.count_blobs(uri=uri, cid=cid, name=name,
blobfiles=blobfiles, print_each=False,
server=server)
print(80 * "-")
if "error_not_found" in blob_info or "error_no_sd_hash" in blob_info:
print("Not copying or moving blobs.")
return False
name = blob_info["name"]
channel = blob_info["channel"]
subdir = os.path.join(move_dir, channel + "_" + name)
if not os.path.exists(subdir):
try:
os.mkdir(subdir)
except (FileNotFoundError, PermissionError) as err:
print(f"Cannot open directory for writing; {err}")
return False
sd_hash_f = os.path.join(blobfiles, blob_info["sd_hash"])
if "copy" in action:
shutil.copy(sd_hash_f, subdir)
for blob in blob_info["blobs"]:
num = blob[0]
hsh = blob[1]
blob_f = os.path.join(blobfiles, hsh)
if os.path.exists(blob_f):
shutil.copy(blob_f, subdir)
else:
if print_missing:
print(f"{num:3d}, {hsh}, missing, not copied")
print("Finished copying blobs.")
elif "move" in action:
shutil.move(sd_hash_f, subdir)
for blob in blob_info["blobs"]:
num = blob[0]
hsh = blob[1]
blob_f = os.path.join(blobfiles, hsh)
if os.path.exists(blob_f):
shutil.move(blob_f, subdir)
else:
if print_missing:
print(f"{num:3d}, {hsh}, missing, not moved")
print("Finished moving blobs.")
if not blob_info["all_present"]:
print("Some blobs are missing, wait for the download to complete, "
"or redownload this claim.")
return False
return True
def blobs_move_all(move_dir=None, blobfiles=None, print_missing=False,
action="copy",
channel=None, start=1, end=0,
server="http://localhost:5279"):
"""Copy or move all blobs of all downloaded claims.
Parameters
----------
move_dir: str, optional
It defaults to `$HOME`.
The path to where the blobs will be copied or moved.
They will be placed inside their own subdirectory which will be named
after the channel's name, and the claim's name.
blobfiles: str, optional
It defaults to `'$HOME/.local/share/lbry/lbrynet/blobfiles'`.
The path to the directory where the blobs were downloaded.
This is normally seen with `lbrynet settings get`, under `'data_dir'`.
It can be any other directory if it is symbolically linked
to it, such as `'/opt/lbryblobfiles'`
print_missing: bool, optional
It defaults to `False`.
If it is `True` it will print all blobs
that don't exist in `blobfiles`, and thus that cannot be copied
or moved to `move_dir`.
action: str, optional
It defaults to `'copy'`, in which case the blobs are copied to the
`move_dir` path.
If it is `'move'` the blobs will be moved, so they won't be
in `blobfiles` any more.
channel: str, optional
It defaults to `None`.
A channel's name, full or partial:
`'@MyChannel#5'`, `'MyChannel#5'`, `'MyChannel'`
If a simplified name is used, and there are various channels
with the same name, the one with the highest LBC bid will be selected.
Enter the full name to choose the right one.
start: int, optional
It defaults to 1.
Move the blobs from claims starting from this index
in the list of items.
end: int, optional
It defaults to 0.
Move the blobs from claims until and including this index
in the list of items.
If it is 0, it is the same as the last index in the list.
server: str, optional
It defaults to `'http://localhost:5279'`.
This is the address of the `lbrynet` daemon, which should be running
in your computer before using any `lbrynet` command.
Normally, there is no need to change this parameter from its default
value.
Returns
-------
list of bool
Each item in the list refers to a claim.
It is `True` if the blobs were copied successfully.
It is `False` if the claim does not exist,
or if the `'sd_hash'` does not exist in the `blobfiles` directory,
of if at least one blob is missing.
The last point may mean that the claim was not downloaded fully,
so it needs to be redownloaded.
"""
if not funcs.server_exists(server=server):
return False
if (not move_dir or not isinstance(move_dir, str)
or move_dir == "~" or not os.path.exists(move_dir)):
move_dir = os.path.expanduser("~")
print("Destination directory should exist; "
f"set to move_dir='{move_dir}'")
if (not blobfiles or not isinstance(blobfiles, str)
or not os.path.exists(blobfiles)):
print("Copy or move the blobs from the blobfiles directory")
print(f"blobfiles={blobfiles}")
print("This is typically '$HOME/.local/share/lbry/lbrynet/blobfiles'")
home = os.path.expanduser("~")
blobfiles = os.path.join(home,
".local", "share",
"lbry", "lbrynet", "blobfiles")
if not os.path.exists(blobfiles):
print(f"Blobfiles directory does not exist: {blobfiles}")
return False
if channel and not isinstance(channel, str):
print("Channel must be a string. Set to 'None'.")
print(f"channel={channel}")
channel = None
if channel:
if not channel.startswith("@"):
channel = "@" + channel
items = sort.sort_items(channel=channel,
server=server)
if not items:
return False
n_items = len(items)
print()
list_blobs_info = []
print("Copy/move all blob files")
print(80 * "-")
for it, item in enumerate(items, start=1):
if it < start:
continue
if end != 0 and it > end:
break
print(f"Claim {it}/{n_items}, {item['claim_name']}")
blob_m = blobs_move(cid=item["claim_id"],
move_dir=move_dir, blobfiles=blobfiles,
print_missing=print_missing,
action=action,
server=server)
list_blobs_info.append(blob_m)
print()
return list_blobs_info