Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH] dispatch_conf: factor out _archive_copy
Date: Mon, 12 Jan 2015 05:17:19
Message-Id: 1421039815-27793-1-git-send-email-zmedico@gentoo.org
In Reply to: Re: [gentoo-portage-dev] [PATCH] dispatch-conf: avoid symlink "File exists" error (535850) by Brian Dolbec
1 This eliminates 4 instances of duplicate code from the rcs_archive and
2 file_archive functions.
3
4 Suggested-by: Brian Dolbec <dolsen@g.o>
5 ---
6 pym/portage/dispatch_conf.py | 92 +++++++++++++++++---------------------------
7 1 file changed, 36 insertions(+), 56 deletions(-)
8
9 diff --git a/pym/portage/dispatch_conf.py b/pym/portage/dispatch_conf.py
10 index 7d55182..790eacb 100644
11 --- a/pym/portage/dispatch_conf.py
12 +++ b/pym/portage/dispatch_conf.py
13 @@ -158,6 +158,38 @@ def read_config(mandatory_opts):
14
15 return opts
16
17 +def _archive_copy(src_st, src_path, dest_path):
18 + """
19 + Copy file from src_path to dest_path. Regular files and symlinks
20 + are supported. If an EnvironmentError occurs, then it is logged
21 + to stderr.
22 +
23 + @param src_st: source file lstat result
24 + @type src_st: posix.stat_result
25 + @param src_path: source file path
26 + @type src_path: str
27 + @param dest_path: destination file path
28 + @type dest_path: str
29 + """
30 + # Remove destination file in order to ensure that the following
31 + # symlink or copy2 call won't fail (see bug #535850).
32 + try:
33 + os.unlink(dest_path)
34 + except OSError:
35 + pass
36 + try:
37 + if stat.S_ISLNK(src_st.st_mode):
38 + os.symlink(os.readlink(src_path), dest_path)
39 + else:
40 + shutil.copy2(src_path, dest_path)
41 + except EnvironmentError as e:
42 + portage.util.writemsg(
43 + _('dispatch-conf: Error copying %(src_path)s to '
44 + '%(dest_path)s: %(reason)s\n') % {
45 + "src_path": src_path,
46 + "dest_path": dest_path,
47 + "reason": e
48 + }, noiselevel=-1)
49
50 def rcs_archive(archive, curconf, newconf, mrgconf):
51 """Archive existing config in rcs (on trunk). Then, if mrgconf is
52 @@ -179,20 +211,7 @@ def rcs_archive(archive, curconf, newconf, mrgconf):
53 if curconf_st is not None and \
54 (stat.S_ISREG(curconf_st.st_mode) or
55 stat.S_ISLNK(curconf_st.st_mode)):
56 - # Remove destination file in order to ensure that the following
57 - # symlink or copy2 call won't fail (see bug #535850).
58 - try:
59 - os.unlink(archive)
60 - except OSError:
61 - pass
62 - try:
63 - if stat.S_ISLNK(curconf_st.st_mode):
64 - os.symlink(os.readlink(curconf), archive)
65 - else:
66 - shutil.copy2(curconf, archive)
67 - except(IOError, os.error) as why:
68 - print(_('dispatch-conf: Error copying %(curconf)s to %(archive)s: %(reason)s; fatal') % \
69 - {"curconf": curconf, "archive": archive, "reason": str(why)}, file=sys.stderr)
70 + _archive_copy(curconf_st, curconf, archive)
71
72 if os.path.lexists(archive + ',v'):
73 os.system(RCS_LOCK + ' ' + archive)
74 @@ -214,20 +233,7 @@ def rcs_archive(archive, curconf, newconf, mrgconf):
75 if has_branch:
76 os.rename(archive, archive + '.dist')
77
78 - # Remove destination file in order to ensure that the following
79 - # symlink or copy2 call won't fail (see bug #535850).
80 - try:
81 - os.unlink(archive)
82 - except OSError:
83 - pass
84 - try:
85 - if stat.S_ISLNK(mystat.st_mode):
86 - os.symlink(os.readlink(newconf), archive)
87 - else:
88 - shutil.copy2(newconf, archive)
89 - except(IOError, os.error) as why:
90 - print(_('dispatch-conf: Error copying %(newconf)s to %(archive)s: %(reason)s; fatal') % \
91 - {"newconf": newconf, "archive": archive, "reason": str(why)}, file=sys.stderr)
92 + _archive_copy(mystat, newconf, archive)
93
94 if has_branch:
95 if mrgconf and os.path.isfile(archive) and \
96 @@ -276,20 +282,7 @@ def file_archive(archive, curconf, newconf, mrgconf):
97 if curconf_st is not None and \
98 (stat.S_ISREG(curconf_st.st_mode) or
99 stat.S_ISLNK(curconf_st.st_mode)):
100 - # Remove destination file in order to ensure that the following
101 - # symlink or copy2 call won't fail (see bug #535850).
102 - try:
103 - os.unlink(archive)
104 - except OSError:
105 - pass
106 - try:
107 - if stat.S_ISLNK(curconf_st.st_mode):
108 - os.symlink(os.readlink(curconf), archive)
109 - else:
110 - shutil.copy2(curconf, archive)
111 - except(IOError, os.error) as why:
112 - print(_('dispatch-conf: Error copying %(curconf)s to %(archive)s: %(reason)s; fatal') % \
113 - {"curconf": curconf, "archive": archive, "reason": str(why)}, file=sys.stderr)
114 + _archive_copy(curconf_st, curconf, archive)
115
116 mystat = None
117 if newconf:
118 @@ -303,20 +296,7 @@ def file_archive(archive, curconf, newconf, mrgconf):
119 stat.S_ISLNK(mystat.st_mode)):
120 # Save off new config file in the archive dir with .dist.new suffix
121 newconf_archive = archive + '.dist.new'
122 - # Remove destination file in order to ensure that the following
123 - # symlink or copy2 call won't fail (see bug #535850).
124 - try:
125 - os.unlink(newconf_archive)
126 - except OSError:
127 - pass
128 - try:
129 - if stat.S_ISLNK(mystat.st_mode):
130 - os.symlink(os.readlink(newconf), newconf_archive)
131 - else:
132 - shutil.copy2(newconf, newconf_archive)
133 - except(IOError, os.error) as why:
134 - print(_('dispatch-conf: Error copying %(newconf)s to %(archive)s: %(reason)s; fatal') % \
135 - {"newconf": newconf, "archive": archive + '.dist.new', "reason": str(why)}, file=sys.stderr)
136 + _archive_copy(mystat, newconf, newconf_archive)
137
138 ret = 0
139 if mrgconf and os.path.isfile(curconf) and \
140 --
141 2.0.5

Replies