public inbox for gentoo-portage-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager
@ 2020-12-18 18:46 Florian Schmaus
  2020-12-18 18:46 ` [gentoo-portage-dev] [PATCH 2/2] env_update: use "with statement" on atomic_ofstream Florian Schmaus
  2020-12-31  2:21 ` [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager Zac Medico
  0 siblings, 2 replies; 3+ messages in thread
From: Florian Schmaus @ 2020-12-18 18:46 UTC (permalink / raw
  To: gentoo-portage-dev; +Cc: Florian Schmaus

This allows using a "with statement" together with instances of
atomic_ofstream. Allowing for more readable, less error prone and
shorter code.

Signed-off-by: Florian Schmaus <flo@geekplace.eu>
---
 lib/portage/util/__init__.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/portage/util/__init__.py b/lib/portage/util/__init__.py
index 0412b2b5911f..bedcbcfe6fcc 100644
--- a/lib/portage/util/__init__.py
+++ b/lib/portage/util/__init__.py
@@ -11,6 +11,7 @@ __all__ = ['apply_permissions', 'apply_recursive_permissions',
 	'stack_dicts', 'stack_lists', 'unique_array', 'unique_everseen', 'varexpand',
 	'write_atomic', 'writedict', 'writemsg', 'writemsg_level', 'writemsg_stdout']
 
+from contextlib import AbstractContextManager
 from copy import deepcopy
 import errno
 import io
@@ -1246,7 +1247,7 @@ def apply_secpass_permissions(filename, uid=-1, gid=-1, mode=-1, mask=-1,
 		stat_cached=stat_cached, follow_links=follow_links)
 	return all_applied
 
-class atomic_ofstream(ObjectProxy):
+class atomic_ofstream(AbstractContextManager, ObjectProxy):
 	"""Write a file atomically via os.rename().  Atomic replacement prevents
 	interprocess interference and prevents corruption of the target
 	file when the write is interrupted (for example, when an 'out of space'
@@ -1287,6 +1288,13 @@ class atomic_ofstream(ObjectProxy):
 				encoding=_encodings['fs'], errors='strict'),
 				mode=mode, **kargs))
 
+	def __exit__(self, exc_type, exc_val, exc_tb):
+		if exc_type is not None:
+			self.abort()
+		else:
+			self.close()
+		return None
+
 	def _get_target(self):
 		return object.__getattribute__(self, '_file')
 
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [gentoo-portage-dev] [PATCH 2/2] env_update: use "with statement" on atomic_ofstream
  2020-12-18 18:46 [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager Florian Schmaus
@ 2020-12-18 18:46 ` Florian Schmaus
  2020-12-31  2:21 ` [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager Zac Medico
  1 sibling, 0 replies; 3+ messages in thread
From: Florian Schmaus @ 2020-12-18 18:46 UTC (permalink / raw
  To: gentoo-portage-dev; +Cc: Florian Schmaus

Signed-off-by: Florian Schmaus <flo@geekplace.eu>
---
 lib/portage/util/env_update.py | 30 ++++++++++++------------------
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/lib/portage/util/env_update.py b/lib/portage/util/env_update.py
index dec086cf8c5b..5588931a84e7 100644
--- a/lib/portage/util/env_update.py
+++ b/lib/portage/util/env_update.py
@@ -342,18 +342,17 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
 
 	#create /etc/profile.env for bash support
 	profile_env_path = os.path.join(eroot, "etc", "profile.env")
-	outfile = atomic_ofstream(profile_env_path)
-	outfile.write(penvnotice)
-
-	env_keys = [x for x in env if x != "LDPATH"]
-	env_keys.sort()
-	for k in env_keys:
-		v = env[k]
-		if v.startswith('$') and not v.startswith('${'):
-			outfile.write("export %s=$'%s'\n" % (k, v[1:]))
-		else:
-			outfile.write("export %s='%s'\n" % (k, v))
-	outfile.close()
+	with atomic_ofstream(profile_env_path) as outfile:
+		outfile.write(penvnotice)
+
+		env_keys = [x for x in env if x != "LDPATH"]
+		env_keys.sort()
+		for k in env_keys:
+			v = env[k]
+			if v.startswith('$') and not v.startswith('${'):
+				outfile.write("export %s=$'%s'\n" % (k, v[1:]))
+			else:
+				outfile.write("export %s='%s'\n" % (k, v))
 
 	# Create the systemd user environment configuration file
 	# /etc/environment.d/10-gentoo-env.conf with the
@@ -363,8 +362,7 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
 
 	systemd_gentoo_env_path = os.path.join(systemd_environment_dir,
 						"10-gentoo-env.conf")
-	systemd_gentoo_env = atomic_ofstream(systemd_gentoo_env_path)
-	try:
+	with atomic_ofstream(systemd_gentoo_env_path) as systemd_gentoo_env:
 		senvnotice = notice + "\n\n"
 		systemd_gentoo_env.write(senvnotice)
 
@@ -384,10 +382,6 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
 			line = f"{env_key}={env_key_value}\n"
 
 			systemd_gentoo_env.write(line)
-	except:
-		systemd_gentoo_env.abort()
-		raise
-	systemd_gentoo_env.close()
 
 	#create /etc/csh.env for (t)csh support
 	outfile = atomic_ofstream(os.path.join(eroot, "etc", "csh.env"))
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager
  2020-12-18 18:46 [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager Florian Schmaus
  2020-12-18 18:46 ` [gentoo-portage-dev] [PATCH 2/2] env_update: use "with statement" on atomic_ofstream Florian Schmaus
@ 2020-12-31  2:21 ` Zac Medico
  1 sibling, 0 replies; 3+ messages in thread
From: Zac Medico @ 2020-12-31  2:21 UTC (permalink / raw
  To: gentoo-portage-dev, Florian Schmaus


[-- Attachment #1.1: Type: text/plain, Size: 524 bytes --]

On 12/18/20 10:46 AM, Florian Schmaus wrote:
> This allows using a "with statement" together with instances of
> atomic_ofstream. Allowing for more readable, less error prone and
> shorter code.
> 
> Signed-off-by: Florian Schmaus <flo@geekplace.eu>

Thanks, this is much better! Merged both patches:

https://gitweb.gentoo.org/proj/portage.git/commit/?id=e93e6d65fa1ca75f676a227f7918f8b6d747425c
https://gitweb.gentoo.org/proj/portage.git/commit/?id=1574ae127b270739c4293271c959d1d981684906
-- 
Thanks,
Zac


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 981 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-31  2:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-18 18:46 [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager Florian Schmaus
2020-12-18 18:46 ` [gentoo-portage-dev] [PATCH 2/2] env_update: use "with statement" on atomic_ofstream Florian Schmaus
2020-12-31  2:21 ` [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager Zac Medico

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox