Skip to content

Commit 4ad9bbf

Browse files
authored
Merge pull request #271 from MerginMaps/python-37-tests
Move tests to version 3.8 + fix tempdir handling in older python
2 parents 17c8ee0 + ee66f23 commit 4ad9bbf

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
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.8"
2323

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

mergin/client_pull.py

Lines changed: 8 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):
@@ -263,6 +263,7 @@ def download_project_cancel(job):
263263
job.is_cancelled = True
264264
job.executor.shutdown(wait=True)
265265
job.mp.log.info("--- download cancelled")
266+
cleanup_tmp_dir(job.mp, job.tmp_dir)
266267

267268

268269
class UpdateTask:
@@ -424,7 +425,7 @@ def pull_project_async(mc, directory):
424425
# then we just download the whole file
425426
_pulling_file_with_diffs = lambda f: "diffs" in f and len(f["diffs"]) != 0
426427

427-
tmp_dir = tempfile.TemporaryDirectory(prefix="mm-pull-", ignore_cleanup_errors=True, delete=True)
428+
tmp_dir = tempfile.TemporaryDirectory(prefix="mm-pull-")
428429
pull_changes = mp.get_pull_changes(server_info["files"])
429430
mp.log.debug("pull changes:\n" + pprint.pformat(pull_changes))
430431
fetch_files = []
@@ -550,6 +551,7 @@ def pull_project_cancel(job):
550551
job.is_cancelled = True
551552
job.executor.shutdown(wait=True)
552553
job.mp.log.info("--- pull cancelled")
554+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
553555

554556

555557
class FileToMerge:
@@ -637,6 +639,7 @@ def pull_project_finalize(job: PullJob):
637639
except Exception as e:
638640
job.mp.log.error("Failed to apply pull changes: " + str(e))
639641
job.mp.log.info("--- pull aborted")
642+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
640643
raise ClientError("Failed to apply pull changes: " + str(e))
641644

642645
job.mp.update_metadata(job.project_info)
@@ -646,7 +649,7 @@ def pull_project_finalize(job: PullJob):
646649
else:
647650
job.mp.log.info("--- pull finished -- at version " + job.mp.version())
648651

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

652655

mergin/client_push.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .common import UPLOAD_CHUNK_SIZE, ClientError
2727
from .merginproject import MerginProject
2828
from .editor import filter_changes
29+
from .utils import cleanup_tmp_dir
2930

3031

3132
class UploadJob:
@@ -131,7 +132,7 @@ def push_project_async(mc, directory):
131132
changes = filter_changes(mc, project_info, changes)
132133
mp.log.debug("push changes:\n" + pprint.pformat(changes))
133134

134-
tmp_dir = tempfile.TemporaryDirectory(prefix="python-api-client-", ignore_cleanup_errors=True, delete=True)
135+
tmp_dir = tempfile.TemporaryDirectory(prefix="python-api-client-")
135136

136137
# If there are any versioned files (aka .gpkg) that are not updated through a diff,
137138
# we need to make a temporary copy somewhere to be sure that we are uploading full content.
@@ -267,6 +268,7 @@ def push_project_finalize(job):
267268
job.transferred_size, job.total_size
268269
)
269270
job.mp.log.error("--- push finish failed! " + error_msg)
271+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
270272
raise ClientError("Upload error: " + error_msg)
271273

272274
if with_upload_of_files:
@@ -310,6 +312,7 @@ def push_project_finalize(job):
310312
job.mp.log.info("cancel response: " + resp_cancel.msg)
311313
except ClientError as err2:
312314
job.mp.log.info("cancel response: " + str(err2))
315+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
313316
raise err
314317

315318
job.mp.update_metadata(job.server_resp)
@@ -318,10 +321,10 @@ def push_project_finalize(job):
318321
except Exception as e:
319322
job.mp.log.error("Failed to apply push changes: " + str(e))
320323
job.mp.log.info("--- push aborted")
324+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
321325
raise ClientError("Failed to apply push changes: " + str(e))
322326

323-
job.tmp_dir.cleanup() # delete our temporary dir and all its content
324-
327+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
325328
remove_diff_files(job)
326329

327330
job.mp.log.info("--- push finished - new project version " + job.server_resp["version"])
@@ -362,7 +365,9 @@ def push_project_cancel(job):
362365
job.server_resp = resp_cancel.msg
363366
except ClientError as err:
364367
job.mp.log.error("--- push cancelling failed! " + str(err))
368+
cleanup_tmp_dir(job.mp, job.tmp_dir)
365369
raise err
370+
cleanup_tmp_dir(job.mp, job.tmp_dir) # delete our temporary dir and all its content
366371
job.mp.log.info("--- push cancel response: " + str(job.server_resp))
367372

368373

mergin/test/test_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2266,8 +2266,11 @@ def test_clean_diff_files(mc):
22662266
shutil.copy(mp.fpath("inserted_1_A.gpkg"), mp.fpath(f_updated))
22672267
mc.push_project(project_dir)
22682268

2269-
diff_files = glob.glob("*-diff-*", root_dir=os.path.split(mp.fpath_meta("inserted_1_A.gpkg"))[0])
2269+
# Get the directory path
2270+
directory = os.path.split(mp.fpath_meta("inserted_1_A.gpkg"))[0]
2271+
diff_files = [f for f in os.listdir(directory) if "-diff-" in f]
22702272

2273+
# Assert that no matching files are found
22712274
assert diff_files == []
22722275

22732276

mergin/utils.py

Lines changed: 16 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,17 @@ 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+
300+
def cleanup_tmp_dir(mp, tmp_dir: tempfile.TemporaryDirectory):
301+
"""
302+
Remove temporary from tempfile.TemporaryDirectory instance
303+
This is needed beacause ignore_clanup_errors is not accepted under < Python 3.10
304+
"""
305+
306+
try:
307+
tmp_dir.cleanup()
308+
except PermissionError:
309+
mp.log.warning(f"Permission error during tmp dir cleanup: {tmp_dir.name}")
310+
except Exception as e:
311+
mp.log.error(f"Error during tmp dir cleanup: {tmp_dir.name}: {e}")

0 commit comments

Comments
 (0)