From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id E912C138A1A for ; Mon, 12 Jan 2015 05:17:19 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id E3896E0851; Mon, 12 Jan 2015 05:17:17 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 44F5AE084F for ; Mon, 12 Jan 2015 05:17:17 +0000 (UTC) Received: from localhost.localdomain (99.sub-70-209-195.myvzw.com [70.209.195.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: zmedico) by smtp.gentoo.org (Postfix) with ESMTPSA id BADBC340554; Mon, 12 Jan 2015 05:17:15 +0000 (UTC) From: Zac Medico To: gentoo-portage-dev@lists.gentoo.org Cc: Zac Medico Subject: [gentoo-portage-dev] [PATCH] dispatch_conf: factor out _archive_copy Date: Sun, 11 Jan 2015 21:16:55 -0800 Message-Id: <1421039815-27793-1-git-send-email-zmedico@gentoo.org> X-Mailer: git-send-email 2.0.5 In-Reply-To: <20150111180725.68bde153.dolsen@gentoo.org> References: <20150111180725.68bde153.dolsen@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-portage-dev@lists.gentoo.org Reply-to: gentoo-portage-dev@lists.gentoo.org X-Archives-Salt: 8f0372c2-a900-448f-8269-b4e096d1d555 X-Archives-Hash: a300fcdef81c154a7974b7395f27dfcd This eliminates 4 instances of duplicate code from the rcs_archive and file_archive functions. Suggested-by: Brian Dolbec --- pym/portage/dispatch_conf.py | 92 +++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 56 deletions(-) diff --git a/pym/portage/dispatch_conf.py b/pym/portage/dispatch_conf.py index 7d55182..790eacb 100644 --- a/pym/portage/dispatch_conf.py +++ b/pym/portage/dispatch_conf.py @@ -158,6 +158,38 @@ def read_config(mandatory_opts): return opts +def _archive_copy(src_st, src_path, dest_path): + """ + Copy file from src_path to dest_path. Regular files and symlinks + are supported. If an EnvironmentError occurs, then it is logged + to stderr. + + @param src_st: source file lstat result + @type src_st: posix.stat_result + @param src_path: source file path + @type src_path: str + @param dest_path: destination file path + @type dest_path: str + """ + # Remove destination file in order to ensure that the following + # symlink or copy2 call won't fail (see bug #535850). + try: + os.unlink(dest_path) + except OSError: + pass + try: + if stat.S_ISLNK(src_st.st_mode): + os.symlink(os.readlink(src_path), dest_path) + else: + shutil.copy2(src_path, dest_path) + except EnvironmentError as e: + portage.util.writemsg( + _('dispatch-conf: Error copying %(src_path)s to ' + '%(dest_path)s: %(reason)s\n') % { + "src_path": src_path, + "dest_path": dest_path, + "reason": e + }, noiselevel=-1) def rcs_archive(archive, curconf, newconf, mrgconf): """Archive existing config in rcs (on trunk). Then, if mrgconf is @@ -179,20 +211,7 @@ def rcs_archive(archive, curconf, newconf, mrgconf): if curconf_st is not None and \ (stat.S_ISREG(curconf_st.st_mode) or stat.S_ISLNK(curconf_st.st_mode)): - # Remove destination file in order to ensure that the following - # symlink or copy2 call won't fail (see bug #535850). - try: - os.unlink(archive) - except OSError: - pass - try: - if stat.S_ISLNK(curconf_st.st_mode): - os.symlink(os.readlink(curconf), archive) - else: - shutil.copy2(curconf, archive) - except(IOError, os.error) as why: - print(_('dispatch-conf: Error copying %(curconf)s to %(archive)s: %(reason)s; fatal') % \ - {"curconf": curconf, "archive": archive, "reason": str(why)}, file=sys.stderr) + _archive_copy(curconf_st, curconf, archive) if os.path.lexists(archive + ',v'): os.system(RCS_LOCK + ' ' + archive) @@ -214,20 +233,7 @@ def rcs_archive(archive, curconf, newconf, mrgconf): if has_branch: os.rename(archive, archive + '.dist') - # Remove destination file in order to ensure that the following - # symlink or copy2 call won't fail (see bug #535850). - try: - os.unlink(archive) - except OSError: - pass - try: - if stat.S_ISLNK(mystat.st_mode): - os.symlink(os.readlink(newconf), archive) - else: - shutil.copy2(newconf, archive) - except(IOError, os.error) as why: - print(_('dispatch-conf: Error copying %(newconf)s to %(archive)s: %(reason)s; fatal') % \ - {"newconf": newconf, "archive": archive, "reason": str(why)}, file=sys.stderr) + _archive_copy(mystat, newconf, archive) if has_branch: if mrgconf and os.path.isfile(archive) and \ @@ -276,20 +282,7 @@ def file_archive(archive, curconf, newconf, mrgconf): if curconf_st is not None and \ (stat.S_ISREG(curconf_st.st_mode) or stat.S_ISLNK(curconf_st.st_mode)): - # Remove destination file in order to ensure that the following - # symlink or copy2 call won't fail (see bug #535850). - try: - os.unlink(archive) - except OSError: - pass - try: - if stat.S_ISLNK(curconf_st.st_mode): - os.symlink(os.readlink(curconf), archive) - else: - shutil.copy2(curconf, archive) - except(IOError, os.error) as why: - print(_('dispatch-conf: Error copying %(curconf)s to %(archive)s: %(reason)s; fatal') % \ - {"curconf": curconf, "archive": archive, "reason": str(why)}, file=sys.stderr) + _archive_copy(curconf_st, curconf, archive) mystat = None if newconf: @@ -303,20 +296,7 @@ def file_archive(archive, curconf, newconf, mrgconf): stat.S_ISLNK(mystat.st_mode)): # Save off new config file in the archive dir with .dist.new suffix newconf_archive = archive + '.dist.new' - # Remove destination file in order to ensure that the following - # symlink or copy2 call won't fail (see bug #535850). - try: - os.unlink(newconf_archive) - except OSError: - pass - try: - if stat.S_ISLNK(mystat.st_mode): - os.symlink(os.readlink(newconf), newconf_archive) - else: - shutil.copy2(newconf, newconf_archive) - except(IOError, os.error) as why: - print(_('dispatch-conf: Error copying %(newconf)s to %(archive)s: %(reason)s; fatal') % \ - {"newconf": newconf, "archive": archive + '.dist.new', "reason": str(why)}, file=sys.stderr) + _archive_copy(mystat, newconf, newconf_archive) ret = 0 if mrgconf and os.path.isfile(curconf) and \ -- 2.0.5