Skip to content

Commit

Permalink
stat implementations 1st draft for file, https, sftp
Browse files Browse the repository at this point in the history
  • Loading branch information
petersilva committed Aug 11, 2024
1 parent 9a9238c commit 479b9a4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions sarracenia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ def durationToSeconds(str_value, default=None) -> float:
206: "Partial Content: received and inserted.",
304: "Not modified (Checksum validated, unchanged, so no download resulted.)",
307: "Insertion deferred (writing to temporary part file for the moment.)",
410: "Gone: server data different from notification message",
417: "Expectation Failed: invalid notification message (corrupt headers)",
422: "Unprocessable Content: could not determine path to transfer to",
499: "Failure: Not Copied. SFTP/FTP/HTTP download problem",
Expand Down
4 changes: 4 additions & 0 deletions sarracenia/transfer/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ def getAccelerated(self, msg, remote_file, local_file, length=0, remote_offset=0
def getcwd(self):
return self.cwd

def stat(self,path,message=None):
spath = path if path[0] == '/' else self.cwd + '/' + path
return sarracenia.stat(spath)

# ls
def ls(self):
logger.debug("sr_file ls")
Expand Down
50 changes: 50 additions & 0 deletions sarracenia/transfer/https.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#
#

import datetime
import logging
import os
import requests
import sarracenia
import ssl
import subprocess
Expand Down Expand Up @@ -366,3 +368,51 @@ def __open__(self, path, remote_offset=0, length=0):

alarm_cancel()
return False

def stat(self,path,msg) -> sarracenia.filemetadata.FmdStat:
st = sarracenia.filemetadata.FmdStat()


url = msg['baseUrl']
if msg['baseUrl'][-1] != '/' and self.cwd[0] != '/' :
url += '/'
url += self.cwd
logger.critical( f" 1 url={url}")
if url[-1] != '/':
url += '/'
url += path
logger.critical( f" 2 url={url}")

try:
resp = requests.head(url)
resp.raise_for_status()

# parse the HTTP header response into the stat object used by sr3
# {... 'Content-Length': '549872', ... , 'Last-Modified': 'Mon, 03 Jun 2024 15:42:02 GMT', ... }
# logger.debug(f"{resp} {resp.headers}")
if resp.status_code == 200:
have_metadata = False
if "Last-Modified" in resp.headers:
# Mon, 03 Jun 2024 15:42:02 GMT
lm = datetime.datetime.strptime(resp.headers['Last-Modified'], '%a, %d %b %Y %H:%M:%S GMT')
lm = lm.timestamp()
# logger.debug(f"parsed date: {lm}")
st.st_atime = lm
st.st_mtime = lm
have_metadata = True
if "Content-Length" in resp.headers:
size = int(resp.headers['Content-Length'])
# logger.debug(f"file size: {size}")
st.st_size = size
have_metadata = True

if have_metadata:
return st

if not have_metadata :
logger.warning(f"HEAD request returned {resp.status_code} but metadata was " +
f"not available for {url}, not posting")
return None
except Exception as e:
logger.info(f"Failed to get metadata for {url} ({e}), posting anyways")
return None
5 changes: 5 additions & 0 deletions sarracenia/transfer/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from paramiko import *
from stat import *

import sarracenia
from sarracenia.transfer import Transfer
from sarracenia.transfer import alarm_cancel, alarm_set, alarm_raise
from urllib.parse import unquote
Expand Down Expand Up @@ -528,6 +529,10 @@ def rmdir(self, path):
self.sftp.rmdir(path)
alarm_cancel()

#when sftp is active, paramiko is present... STFPAttributes is then the same as FmdStat.
def stat(self, path, msg=None) -> sarracenia.filemetadata.FmdStat:
return self.sftp.stat(path)

# utime
def utime(self, path, tup):
logger.debug("sr_sftp utime %s %s " % (path, tup))
Expand Down

0 comments on commit 479b9a4

Please sign in to comment.