Skip to content

Commit

Permalink
Merge branch 'context' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
RKrahl committed Mar 17, 2024
2 parents 8ed62cc + 96f4341 commit 0c66e8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Internal changes
----------------

+ `#74`_, `#80`_: Review build tool chain.
+ `#83`_: Simplify implementation of :func:`archive.tools.tmp_chdir`
and :func:`archive.tools.tmp_umask` using :mod:`contextlib`.

.. _#74: https://github.com/RKrahl/archive-tools/pull/74
.. _#75: https://github.com/RKrahl/archive-tools/pull/75
Expand All @@ -37,6 +39,7 @@ Internal changes
.. _#80: https://github.com/RKrahl/archive-tools/pull/80
.. _#81: https://github.com/RKrahl/archive-tools/issues/81
.. _#82: https://github.com/RKrahl/archive-tools/pull/82
.. _#83: https://github.com/RKrahl/archive-tools/pull/83


0.6 (2021-12-12)
Expand Down
49 changes: 18 additions & 31 deletions src/archive/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
keep anything in here compatible between different versions.
"""

from contextlib import contextmanager
import datetime
import hashlib
import os
Expand Down Expand Up @@ -93,41 +94,27 @@ def __ne__(self, other):
return super().__ne__(other)


class tmp_chdir():
@contextmanager
def tmp_chdir(dir):
"""A context manager to temporarily change directory.
"""
def __init__(self, dir):
self.save_dir = None
self.dir = dir
def __enter__(self):
self.save_dir = os.getcwd()
os.chdir(self.dir)
def _restore_dir(self):
if self.save_dir:
os.chdir(self.save_dir)
self.save_dir = None
def __exit__(self, type, value, tb):
self._restore_dir()
def __del__(self):
self._restore_dir()


class tmp_umask():
save_dir = os.getcwd()
os.chdir(dir)
try:
yield dir
finally:
os.chdir(save_dir)


@contextmanager
def tmp_umask(mask):
"""A context manager to temporarily set the umask.
"""
def __init__(self, mask):
self.save_mask = None
self.mask = mask
def __enter__(self):
self.save_mask = os.umask(self.mask)
def _restore_mask(self):
if self.save_mask:
os.umask(self.save_mask)
self.save_mask = None
def __exit__(self, type, value, tb):
self._restore_mask()
def __del__(self):
self._restore_mask()
save_mask = os.umask(mask)
try:
yield mask
finally:
os.umask(save_mask)


def date_str_rfc5322(dt):
Expand Down

0 comments on commit 0c66e8e

Please sign in to comment.