Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add no overwrite feature #9

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def main():
from_filter = os.environ.get("FROMFILEFILTER")
to_delete = os.environ.get("TODELETE")
new_filename = os.environ.get("NEW_FILENAME")
file_overwrite = os.environ.get("FILE_OVERWRITE")

filepass_logger = logging.getLogger("filepass_logger")
filepass_logger.setLevel(logging.DEBUG)
Expand All @@ -93,6 +94,7 @@ def main():
"to_delete": to_delete,
"integration": "filepass",
"filepass_name": os.environ.get("INTEGRATION_NAME"),
"file_overwrite": file_overwrite,
"new_filename": new_filename,
},
)
Expand All @@ -104,6 +106,7 @@ def main():
from_filter,
to_conn,
to_delete,
file_overwrite,
new_filename,
)
except:
Expand Down
47 changes: 29 additions & 18 deletions src/filepass/filepass.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import fs
import fs.ftpfs
import fs.smbfs
Expand Down Expand Up @@ -94,6 +96,7 @@ def file_pass(
from_filter,
to_conn: ConnectionDetails,
to_delete,
file_overwrite,
new_filename=None,
):
connection_functions = {
Expand All @@ -118,7 +121,7 @@ def file_pass(
for path in walker.files(from_fs):
logger.debug("File to move: {}".format(path))

if to_delete == "yes" and to_fs.exists(path):
if to_delete.upper() == "YES" and to_fs.exists(path):
logger.debug("delete (to): {}".format(path))
if to_fs.exists(path):
try:
Expand All @@ -130,26 +133,34 @@ def file_pass(
else:
logger.debug("No delete (to): {}".format(path))

# Confirm if single file mode condition is satisfied
if len(total_files) == 1 and new_filename:
should_rename = True
# No overwrite feature
# Check the environment variable status and if the file exists
if file_overwrite.upper() == "NO" and to_fs.exists(path):
logger.debug(f"File overwrite is disabled. \nFile {path} not transferred")
pass
else:
should_rename = False

transfer_file(from_fs, to_fs, path, should_rename, new_filename=new_filename)

if from_delete == "yes" and from_fs.exists(path):
logger.debug("delete (from): {}".format(path))
if from_fs.exists(path):
try:
from_fs.remove(path)
except fs.errors.ResourceNotFound:
logger.warning("ResourceNotFound: {}".format(path))
# Confirm if single file mode condition is satisfied
if len(total_files) == 1 and new_filename:
should_rename = True
else:
logger.warning("file {} not found".format(path))
should_rename = False

transfer_file(
from_fs, to_fs, path, should_rename, new_filename=new_filename
)

if from_delete.upper() == "YES" and from_fs.exists(path):
logger.debug("delete (from): {}".format(path))
if from_fs.exists(path):
try:
from_fs.remove(path)
except fs.errors.ResourceNotFound:
logger.warning("ResourceNotFound: {}".format(path))
else:
logger.warning("file {} not found".format(path))

else:
logger.debug("No delete (from): {}".format(path))
else:
logger.debug("No delete (from): {}".format(path))

from_fs.close()

Expand Down
Loading