Skip to content

Commit 89199b9

Browse files
committed
Move tests to version 3.7 and fixed some of them with temp files to make it compatible with older pythons
1 parent fe833f1 commit 89199b9

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

.github/workflows/autotests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919

2020
- uses: actions/setup-python@v2
2121
with:
22-
python-version: '3.x'
22+
python-version: '3.7'
2323

2424
- name: Install python package dependencies
2525
run: |

mergin/client_pull.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from .common import CHUNK_SIZE, ClientError
2424
from .merginproject import MerginProject
25-
from .utils import save_to_file
25+
from .utils import cleanup_tmp_dir, save_to_file
2626

2727

2828
# status = download_project_async(...)
@@ -145,7 +145,7 @@ def download_project_async(mc, project_path, directory, project_version=None):
145145
mp.log.info("--- version: " + mc.user_agent_info())
146146
mp.log.info(f"--- start download {project_path}")
147147

148-
tmp_dir = tempfile.TemporaryDirectory(prefix="python-api-client-", ignore_cleanup_errors=True, delete=True)
148+
tmp_dir = tempfile.TemporaryDirectory(prefix="python-api-client-")
149149

150150
try:
151151
# check whether we download the latest version or not
@@ -250,7 +250,7 @@ def download_project_finalize(job):
250250
# final update of project metadata
251251
job.mp.update_metadata(job.project_info)
252252

253-
job.tmp_dir.cleanup()
253+
cleanup_tmp_dir(job.mp, job.tmp_dir)
254254

255255

256256
def download_project_cancel(job):
@@ -424,7 +424,7 @@ def pull_project_async(mc, directory):
424424
# then we just download the whole file
425425
_pulling_file_with_diffs = lambda f: "diffs" in f and len(f["diffs"]) != 0
426426

427-
tmp_dir = tempfile.TemporaryDirectory(prefix="mm-pull-", ignore_cleanup_errors=True, delete=True)
427+
tmp_dir = tempfile.TemporaryDirectory(prefix="mm-pull-")
428428
pull_changes = mp.get_pull_changes(server_info["files"])
429429
mp.log.debug("pull changes:\n" + pprint.pformat(pull_changes))
430430
fetch_files = []
@@ -646,7 +646,7 @@ def pull_project_finalize(job: PullJob):
646646
else:
647647
job.mp.log.info("--- pull finished -- at version " + job.mp.version())
648648

649-
job.tmp_dir.cleanup() # delete our temporary dir and all its content
649+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
650650
return conflicts
651651

652652

mergin/client_push.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .common import UPLOAD_CHUNK_SIZE, ClientError
2020
from .merginproject import MerginProject
2121
from .editor import filter_changes
22+
from .utils import cleanup_tmp_dir
2223

2324

2425
class UploadJob:
@@ -124,7 +125,7 @@ def push_project_async(mc, directory):
124125
changes = filter_changes(mc, project_info, changes)
125126
mp.log.debug("push changes:\n" + pprint.pformat(changes))
126127

127-
tmp_dir = tempfile.TemporaryDirectory(prefix="python-api-client-", ignore_cleanup_errors=True, delete=True)
128+
tmp_dir = tempfile.TemporaryDirectory(prefix="python-api-client-")
128129

129130
# If there are any versioned files (aka .gpkg) that are not updated through a diff,
130131
# we need to make a temporary copy somewhere to be sure that we are uploading full content.
@@ -275,7 +276,7 @@ def push_project_finalize(job):
275276
f"Upload details: {len(job.upload_queue_items)} file chunks, total size {job.total_size} bytes."
276277
)
277278
# server returns various error messages with filename or something generic
278-
# it would be better if it returned list of failed files (and reasons) whenever possible
279+
# it would be better if it returned list of failed files (and reasons) whenever possible
279280
job.mp.log.error("--- push finish failed! " + str(err))
280281

281282
# if push finish fails, the transaction is not killed, so we
@@ -296,7 +297,7 @@ def push_project_finalize(job):
296297
job.mp.log.info("--- push aborted")
297298
raise ClientError("Failed to apply push changes: " + str(e))
298299

299-
job.tmp_dir.cleanup() # delete our temporary dir and all its content
300+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
300301

301302
remove_diff_files(job)
302303

mergin/utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sqlite3
77
from datetime import datetime
88
from pathlib import Path
9+
import tempfile
910
from .common import ClientError
1011

1112

@@ -201,7 +202,7 @@ def conflicted_copy_file_name(path, user, version):
201202
head, tail = os.path.split(os.path.normpath(path))
202203
ext = "".join(Path(tail).suffixes)
203204
file_name = tail.replace(ext, "")
204-
# in case of QGIS project files we have to add "~" (tilde) to suffix
205+
# in case of QGIS project files we TemporaryDirectoryhave to add "~" (tilde) to suffix
205206
# to avoid having several QGIS project files inside Mergin Maps project.
206207
# See https://github.com/lutraconsulting/qgis-mergin-plugin/issues/382
207208
# for more details
@@ -294,3 +295,18 @@ def bytes_to_human_size(bytes: int):
294295
return f"{round( bytes / 1024.0 / 1024.0 / 1024.0, precision )} GB"
295296
else:
296297
return f"{round( bytes / 1024.0 / 1024.0 / 1024.0 / 1024.0, precision )} TB"
298+
299+
def cleanup_tmp_dir(mp, tmp_dir: tempfile.TemporaryDirectory):
300+
"""
301+
Remove temporary from tempfile.TemporaryDirectory instance
302+
This is needed beacause ignore_clanup_errors is not accepted under < Python 3.10
303+
"""
304+
305+
try:
306+
tmp_dir.cleanup()
307+
except PermissionError:
308+
mp.log.warning(f"Permission error during tmp dir cleanup: {tmp_dir.name}")
309+
pass # Ignore the error and continue
310+
except Exception as e:
311+
mp.log.error(f"Error during tmp dir cleanup: {tmp_dir.name}: {e}")
312+
pass # Ignore the error and continue

0 commit comments

Comments
 (0)