Gentoo Archives: gentoo-portage-dev

From: Florian Schmaus <flo@×××××××××.eu>
To: gentoo-portage-dev@l.g.o
Cc: Florian Schmaus <flo@×××××××××.eu>
Subject: [gentoo-portage-dev] [PATCH 2/2] env_update: use "with statement" on atomic_ofstream
Date: Fri, 18 Dec 2020 18:47:00
Message-Id: 20201218184639.361607-2-flo@geekplace.eu
In Reply to: [gentoo-portage-dev] [PATCH 1/2] Make atomic_ofstream a Context Manager by Florian Schmaus
1 Signed-off-by: Florian Schmaus <flo@×××××××××.eu>
2 ---
3 lib/portage/util/env_update.py | 30 ++++++++++++------------------
4 1 file changed, 12 insertions(+), 18 deletions(-)
5
6 diff --git a/lib/portage/util/env_update.py b/lib/portage/util/env_update.py
7 index dec086cf8c5b..5588931a84e7 100644
8 --- a/lib/portage/util/env_update.py
9 +++ b/lib/portage/util/env_update.py
10 @@ -342,18 +342,17 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
11
12 #create /etc/profile.env for bash support
13 profile_env_path = os.path.join(eroot, "etc", "profile.env")
14 - outfile = atomic_ofstream(profile_env_path)
15 - outfile.write(penvnotice)
16 -
17 - env_keys = [x for x in env if x != "LDPATH"]
18 - env_keys.sort()
19 - for k in env_keys:
20 - v = env[k]
21 - if v.startswith('$') and not v.startswith('${'):
22 - outfile.write("export %s=$'%s'\n" % (k, v[1:]))
23 - else:
24 - outfile.write("export %s='%s'\n" % (k, v))
25 - outfile.close()
26 + with atomic_ofstream(profile_env_path) as outfile:
27 + outfile.write(penvnotice)
28 +
29 + env_keys = [x for x in env if x != "LDPATH"]
30 + env_keys.sort()
31 + for k in env_keys:
32 + v = env[k]
33 + if v.startswith('$') and not v.startswith('${'):
34 + outfile.write("export %s=$'%s'\n" % (k, v[1:]))
35 + else:
36 + outfile.write("export %s='%s'\n" % (k, v))
37
38 # Create the systemd user environment configuration file
39 # /etc/environment.d/10-gentoo-env.conf with the
40 @@ -363,8 +362,7 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
41
42 systemd_gentoo_env_path = os.path.join(systemd_environment_dir,
43 "10-gentoo-env.conf")
44 - systemd_gentoo_env = atomic_ofstream(systemd_gentoo_env_path)
45 - try:
46 + with atomic_ofstream(systemd_gentoo_env_path) as systemd_gentoo_env:
47 senvnotice = notice + "\n\n"
48 systemd_gentoo_env.write(senvnotice)
49
50 @@ -384,10 +382,6 @@ def _env_update(makelinks, target_root, prev_mtimes, contents, env,
51 line = f"{env_key}={env_key_value}\n"
52
53 systemd_gentoo_env.write(line)
54 - except:
55 - systemd_gentoo_env.abort()
56 - raise
57 - systemd_gentoo_env.close()
58
59 #create /etc/csh.env for (t)csh support
60 outfile = atomic_ofstream(os.path.join(eroot, "etc", "csh.env"))
61 --
62 2.26.2