mirror of
https://github.com/openSUSE/osc.git
synced 2025-12-08 19:34:47 +01:00
Add gitea_api.common.TemporaryDirectory class that supports 'delete' argument on python 3.6+
This commit is contained in:
@@ -4,6 +4,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@@ -101,8 +102,6 @@ def run_editor(file_path: str):
|
|||||||
|
|
||||||
|
|
||||||
def edit_message(template: Optional[str] = None) -> str:
|
def edit_message(template: Optional[str] = None) -> str:
|
||||||
import tempfile
|
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8", prefix="git_obs_message_") as f:
|
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8", prefix="git_obs_message_") as f:
|
||||||
if template:
|
if template:
|
||||||
f.write(template)
|
f.write(template)
|
||||||
@@ -138,3 +137,24 @@ def dt_sanitize(date_time: str):
|
|||||||
dt = datetime.datetime.fromisoformat(date_time)
|
dt = datetime.datetime.fromisoformat(date_time)
|
||||||
|
|
||||||
return dt.astimezone(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M")
|
return dt.astimezone(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M")
|
||||||
|
|
||||||
|
|
||||||
|
if sys.version_info[:2] >= (3, 12):
|
||||||
|
TemporaryDirectory = tempfile.TemporaryDirectory
|
||||||
|
else:
|
||||||
|
class TemporaryDirectory:
|
||||||
|
"""
|
||||||
|
A minimalist implementation of TemporaryDirectory that supports 'delete' argument
|
||||||
|
"""
|
||||||
|
def __init__(self, suffix=None, prefix=None, dir=None, delete=True):
|
||||||
|
self.name = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
|
||||||
|
self._delete = delete
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
if self._delete and os.path.isdir(self.name):
|
||||||
|
shutil.rmtree(self.name)
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
import os
|
||||||
|
import shutil
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from osc.gitea_api import dt_sanitize
|
from osc.gitea_api import dt_sanitize
|
||||||
|
from osc.gitea_api.common import TemporaryDirectory
|
||||||
|
|
||||||
|
|
||||||
class TestGiteaApiCommon(unittest.TestCase):
|
class TestGiteaApiCommon(unittest.TestCase):
|
||||||
@@ -21,5 +24,17 @@ class TestGiteaApiCommon(unittest.TestCase):
|
|||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
|
|
||||||
|
def test_TemporaryDirectory_delete(self):
|
||||||
|
with TemporaryDirectory(delete=True) as temp_dir:
|
||||||
|
pass
|
||||||
|
self.assertFalse(os.path.isdir(temp_dir))
|
||||||
|
|
||||||
|
def test_TemporaryDirectory_no_delete(self):
|
||||||
|
with TemporaryDirectory(delete=False) as temp_dir:
|
||||||
|
pass
|
||||||
|
self.assertTrue(os.path.isdir(temp_dir))
|
||||||
|
shutil.rmtree(temp_dir)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user