Skip to content

Commit 41eef5c

Browse files
authored
fix metalink size for url (#581)
* fix metalink size for url * use ubuntu 18.04 in ci
1 parent bbca1e4 commit 41eef5c

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on: [ push, pull_request ]
44

55
jobs:
66
main:
7-
runs-on: ubuntu-latest
7+
runs-on: ubuntu-18.04
88
strategy:
99
matrix:
1010
python-version: [3.6, 3.7, 3.8, 3.9]

pywps/inout/basic.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ def url(self):
336336
import pathlib
337337
return pathlib.PurePosixPath(self.file).as_uri()
338338

339+
@property
340+
def size(self):
341+
"""Length of the linked content in octets."""
342+
return os.stat(self.file).st_size
343+
339344
def _openmode(self, data=None):
340345
openmode = 'r'
341346
# in Python 3 we need to open binary files in binary mode.
@@ -483,6 +488,16 @@ def post_data(self):
483488
def post_data(self, value):
484489
self._post_data = value
485490

491+
@property
492+
def size(self):
493+
"""Get content-length of URL without download"""
494+
req = requests.head(self.url)
495+
if req.ok:
496+
size = int(req.headers.get('content-length', '0'))
497+
else:
498+
size = 0
499+
return size
500+
486501
@staticmethod
487502
def _openurl(href, data=None):
488503
"""Open given href.

pywps/inout/outputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def name(self):
361361
@property
362362
def size(self):
363363
"""Length of the linked content in octets."""
364-
return os.stat(self.file).st_size
364+
return self._output.size
365365

366366
@property
367367
def urls(self):

tests/test_inout.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,12 @@ def metafile(self):
772772
mf._set_workdir(self.tmp_dir)
773773
return mf
774774

775+
def metafile_with_url(self):
776+
mf = MetaFile('identifier', 'title', fmt=FORMATS.JSON)
777+
mf.url = "https://pywps.org/"
778+
mf._set_workdir(self.tmp_dir)
779+
return mf
780+
775781
def test_metafile(self):
776782
mf = self.metafile()
777783
self.assertEqual('identifier', mf.identity)
@@ -821,6 +827,11 @@ def test_hash(self):
821827
ml4.checksums = True
822828
assert 'hash' in ml4.xml
823829

830+
def test_size(self):
831+
ml4 = self.metalink4()
832+
ml4.append(self.metafile_with_url())
833+
assert 'size' in ml4.xml
834+
824835

825836
def load_tests(loader=None, tests=None, pattern=None):
826837
if not loader:

0 commit comments

Comments
 (0)