Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tsm-orchestration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
UFZ TSM
tsm-orchestration
Commits
917470d2
Commit
917470d2
authored
1 year ago
by
Bert Palm
Browse files
Options
Downloads
Patches
Plain Diff
added port, speedup by using list_iter on dest
parent
0062fc7f
No related branches found
No related tags found
1 merge request
!148
sftp sync script
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cron/scripts/sftp_sync/sftp_sync.py
+31
-26
31 additions, 26 deletions
cron/scripts/sftp_sync/sftp_sync.py
with
31 additions
and
26 deletions
cron/scripts/sftp_sync/sftp_sync.py
+
31
−
26
View file @
917470d2
...
...
@@ -46,8 +46,11 @@ def get_external_ftp(conn, thing_id) -> FtpMeta:
def
connect_ftp
(
ftp
:
FtpMeta
)
->
SFTPClient
:
ssh
=
SSHClient
()
ssh
.
set_missing_host_key_policy
(
WarningPolicy
)
host
,
*
port
=
ftp
.
uri
.
split
(
"
:
"
)
port
=
port
[
0
]
if
port
else
22
ssh
.
connect
(
hostname
=
ftp
.
uri
,
hostname
=
host
,
port
=
port
,
username
=
ftp
.
username
,
password
=
ftp
.
password
or
None
,
key_filename
=
ftp
.
keyfile_path
or
None
,
...
...
@@ -63,6 +66,25 @@ def connect_ftp(ftp: FtpMeta) -> SFTPClient:
return
sftp
def
get_files
(
sftp
,
path
)
->
dict
:
# Note that directories always appear
# before any files from that directory
# appear.
files
=
{}
dirs
=
[]
# we must avoid calling listdir_iter multiple
# times, otherwise it might cause a deadlock.
# That's why we do not recurse within the loop.
for
attrs
in
sftp
.
listdir_iter
(
path
):
file_path
=
f
"
{
path
}
/
{
attrs
.
filename
}
"
if
stat
.
S_ISDIR
(
attrs
.
st_mode
):
dirs
.
append
(
file_path
)
files
[
file_path
]
=
attrs
for
dir_
in
dirs
:
files
.
update
(
get_files
(
sftp
,
dir_
))
return
files
def
sync
(
src
:
SFTPClient
,
dest
:
SFTPClient
):
def
is_dir
(
attrs
:
SFTPAttributes
)
->
bool
:
...
...
@@ -86,15 +108,14 @@ def sync(src: SFTPClient, dest: SFTPClient):
dest
.
utime
(
path
,
(
src_attrs
.
st_atime
,
src_attrs
.
st_mtime
))
def
needs_sync
(
path
,
src_attrs
)
->
bool
:
# general:
# return True if path not exist on dest
# for directories:
# return True if path exist and
# also is a directory on dest
# return True if path also is a directory on dest
# for files:
# return True if path exist and
# file has same size and mtime on dest
try
:
dest_attrs
=
dest
.
lstat
(
path
)
except
FileNotFoundError
:
# return True if file has same size and mtime on dest
dest_attrs
=
dest_files
.
get
(
path
,
None
)
if
dest_attrs
is
None
:
return
True
if
is_dir
(
src_attrs
)
and
is_dir
(
dest_attrs
):
...
...
@@ -109,25 +130,9 @@ def sync(src: SFTPClient, dest: SFTPClient):
else
:
return
True
def
get_files
(
path
)
->
dict
:
# Note that directories always appear
# before any files from that directory
# appear.
files
=
{}
dirs
=
[]
# we must avoid calling listdir_iter multiple
# times, otherwise it might cause a deadlock.
# That's why we do not recurse within the loop.
for
attrs
in
src
.
listdir_iter
(
path
):
file_path
=
f
"
{
path
}
/
{
attrs
.
filename
}
"
if
is_dir
(
attrs
):
dirs
.
append
(
file_path
)
files
[
file_path
]
=
attrs
for
dir_
in
dirs
:
files
.
update
(
get_files
(
dir_
))
return
files
dest_files
=
get_files
(
dest
,
"
.
"
)
for
path
,
attrs
in
get_files
(
"
.
"
).
items
():
for
path
,
attrs
in
get_files
(
src
,
"
.
"
).
items
():
if
needs_sync
(
path
,
attrs
):
if
is_dir
(
attrs
):
mk_dir
(
path
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment