Skip to content

Commit 4fe1894

Browse files
OsOperations::get_file_size(self, filename) is added
It is a function to get a size of file.
1 parent 88371d1 commit 4fe1894

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

testgres/operations/local_ops.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ def isfile(self, remote_file):
325325
def isdir(self, dirname):
326326
return os.path.isdir(dirname)
327327

328+
def get_file_size(self, filename):
329+
assert filename is not None
330+
assert type(filename) == str # noqa: E721
331+
return os.path.getsize(filename)
332+
328333
def remove_file(self, filename):
329334
return os.remove(filename)
330335

testgres/operations/os_ops.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def read_binary(self, filename, start_pos):
107107
def isfile(self, remote_file):
108108
raise NotImplementedError()
109109

110+
def get_file_size(self, filename):
111+
raise NotImplementedError()
112+
110113
# Processes control
111114
def kill(self, pid, signal):
112115
# Kill the process

testgres/operations/remote_ops.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,70 @@ def isdir(self, dirname):
360360
response = self.exec_command(cmd)
361361
return response.strip() == b"True"
362362

363+
def get_file_size(self, filename):
364+
C_ERR_SRC = "RemoteOpertions::get_file_size"
365+
366+
assert filename is not None
367+
assert type(filename) == str # noqa: E721
368+
cmd = "du -b " + __class__._escape_path(filename)
369+
370+
s = self.exec_command(cmd, encoding=get_default_encoding())
371+
assert type(s) == str # noqa: E721
372+
373+
if len(s) == 0:
374+
raise Exception(
375+
"[BUG CHECK] Can't get size of file [{2}]. Remote operation returned an empty string. Check point [{0}][{1}].".format(
376+
C_ERR_SRC,
377+
"#001",
378+
filename
379+
)
380+
)
381+
382+
i = 0
383+
384+
while i < len(s) and s[i].isdigit():
385+
assert s[i] >= '0'
386+
assert s[i] <= '9'
387+
i += 1
388+
389+
if i == 0:
390+
raise Exception(
391+
"[BUG CHECK] Can't get size of file [{2}]. Remote operation returned a bad formatted string. Check point [{0}][{1}].".format(
392+
C_ERR_SRC,
393+
"#002",
394+
filename
395+
)
396+
)
397+
398+
if i == len(s):
399+
raise Exception(
400+
"[BUG CHECK] Can't get size of file [{2}]. Remote operation returned a bad formatted string. Check point [{0}][{1}].".format(
401+
C_ERR_SRC,
402+
"#003",
403+
filename
404+
)
405+
)
406+
407+
if not s[i].isspace():
408+
raise Exception(
409+
"[BUG CHECK] Can't get size of file [{2}]. Remote operation returned a bad formatted string. Check point [{0}][{1}].".format(
410+
C_ERR_SRC,
411+
"#004",
412+
filename
413+
)
414+
)
415+
416+
r = 0
417+
418+
for i2 in range(0, i):
419+
ch = s[i2]
420+
assert ch >= '0'
421+
assert ch <= '9'
422+
# Here is needed to check overflow or that it is a human-valid result?
423+
r = (r * 10) + ord(ch) - ord('0')
424+
425+
return r
426+
363427
def remove_file(self, filename):
364428
cmd = "rm {}".format(filename)
365429
return self.exec_command(cmd)

tests/test_local.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,24 @@ def test_read_binary__spec__unk_file(self):
9797

9898
with pytest.raises(FileNotFoundError, match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
9999
self.operations.read_binary("/dummy", 0)
100+
101+
def test_get_file_size(self):
102+
"""
103+
Test LocalOperations::get_file_size.
104+
"""
105+
filename = __file__ # current file
106+
107+
sz0 = os.path.getsize(filename)
108+
assert type(sz0) == int # noqa: E721
109+
110+
sz1 = self.operations.get_file_size(filename)
111+
assert type(sz1) == int # noqa: E721
112+
assert sz1 == sz0
113+
114+
def test_get_file_size__unk_file(self):
115+
"""
116+
Test LocalOperations::get_file_size.
117+
"""
118+
119+
with pytest.raises(FileNotFoundError, match=re.escape("[Errno 2] No such file or directory: '/dummy'")):
120+
self.operations.get_file_size("/dummy")

tests/test_remote.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,27 @@ def test_read_binary__spec__unk_file(self):
224224
with pytest.raises(ExecUtilException, match=re.escape("tail: cannot open '/dummy' for reading: No such file or directory")):
225225
self.operations.read_binary("/dummy", 0)
226226

227+
def test_get_file_size(self):
228+
"""
229+
Test LocalOperations::get_file_size.
230+
"""
231+
filename = __file__ # current file
232+
233+
sz0 = os.path.getsize(filename)
234+
assert type(sz0) == int # noqa: E721
235+
236+
sz1 = self.operations.get_file_size(filename)
237+
assert type(sz1) == int # noqa: E721
238+
assert sz1 == sz0
239+
240+
def test_get_file_size__unk_file(self):
241+
"""
242+
Test LocalOperations::get_file_size.
243+
"""
244+
245+
with pytest.raises(ExecUtilException, match=re.escape("du: cannot access '/dummy': No such file or directory")):
246+
self.operations.get_file_size("/dummy")
247+
227248
def test_touch(self):
228249
"""
229250
Test touch for creating a new file or updating access and modification times of an existing file.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy