| 1 |
# archive_conf.py -- functionality common to archive-conf and dispatch-conf
|
| 2 |
# Copyright 2003-2004 Gentoo Foundation
|
| 3 |
# Distributed under the terms of the GNU General Public License v2
|
| 4 |
# $Header: /var/cvsroot/gentoo-src/portage/pym/dispatch_conf.py,v 1.3.2.3 2005/04/29 03:37:30 jstubbs Exp $
|
| 5 |
cvs_id_string="$Id: dispatch_conf.py,v 1.3.2.3 2005/04/29 03:37:30 jstubbs Exp $"[5:-2]
|
| 6 |
|
| 7 |
# Library by Wayne Davison <gentoo@blorf.net>, derived from code
|
| 8 |
# written by Jeremy Wohl (http://igmus.org)
|
| 9 |
|
| 10 |
from stat import *
|
| 11 |
import os, sys, commands, shutil
|
| 12 |
|
| 13 |
sys.path = ["/usr/lib/portage/pym"]+sys.path
|
| 14 |
import portage
|
| 15 |
|
| 16 |
RCS_BRANCH = '1.1.1'
|
| 17 |
RCS_LOCK = 'rcs -ko -M -l'
|
| 18 |
RCS_PUT = 'ci -t-"Archived config file." -m"dispatch-conf update."'
|
| 19 |
RCS_GET = 'co'
|
| 20 |
RCS_MERGE = 'rcsmerge -p -r' + RCS_BRANCH + ' %s >%s'
|
| 21 |
|
| 22 |
DIFF3_MERGE = 'diff3 -mE %s %s %s >%s'
|
| 23 |
|
| 24 |
def read_config(mandatory_opts):
|
| 25 |
try:
|
| 26 |
opts = portage.getconfig('/etc/dispatch-conf.conf')
|
| 27 |
except:
|
| 28 |
opts = None
|
| 29 |
|
| 30 |
if not opts:
|
| 31 |
print >> sys.stderr, 'dispatch-conf: Error reading /etc/dispatch-conf.conf; fatal'
|
| 32 |
sys.exit(1)
|
| 33 |
|
| 34 |
for key in mandatory_opts:
|
| 35 |
if not opts.has_key(key):
|
| 36 |
if key == "merge":
|
| 37 |
opts["merge"] = "sdiff --suppress-common-lines --output=%s %s %s"
|
| 38 |
else:
|
| 39 |
print >> sys.stderr, 'dispatch-conf: Missing option "%s" in /etc/dispatch-conf.conf; fatal' % (key,)
|
| 40 |
|
| 41 |
if not os.path.exists(opts['archive-dir']):
|
| 42 |
os.mkdir(opts['archive-dir'])
|
| 43 |
elif not os.path.isdir(opts['archive-dir']):
|
| 44 |
print >> sys.stderr, 'dispatch-conf: Config archive dir [%s] must exist; fatal' % (opts['archive-dir'],)
|
| 45 |
sys.exit(1)
|
| 46 |
|
| 47 |
return opts
|
| 48 |
|
| 49 |
|
| 50 |
def rcs_archive(archive, curconf, newconf, mrgconf):
|
| 51 |
"""Archive existing config in rcs (on trunk). Then, if mrgconf is
|
| 52 |
specified and an old branch version exists, merge the user's changes
|
| 53 |
and the distributed changes and put the result into mrgconf. Lastly,
|
| 54 |
if newconf was specified, leave it in the archive dir with a .dist.new
|
| 55 |
suffix along with the last 1.1.1 branch version with a .dist suffix."""
|
| 56 |
|
| 57 |
try:
|
| 58 |
os.makedirs(os.path.dirname(archive))
|
| 59 |
except:
|
| 60 |
pass
|
| 61 |
|
| 62 |
try:
|
| 63 |
shutil.copy2(curconf, archive)
|
| 64 |
except(IOError, os.error), why:
|
| 65 |
print >> sys.stderr, 'dispatch-conf: Error copying %s to %s: %s; fatal' % \
|
| 66 |
(curconf, archive, str(why))
|
| 67 |
if os.path.exists(archive + ',v'):
|
| 68 |
os.system(RCS_LOCK + ' ' + archive)
|
| 69 |
os.system(RCS_PUT + ' ' + archive)
|
| 70 |
|
| 71 |
ret = 0
|
| 72 |
if newconf != '':
|
| 73 |
os.system(RCS_GET + ' -r' + RCS_BRANCH + ' ' + archive)
|
| 74 |
has_branch = os.path.exists(archive)
|
| 75 |
if has_branch:
|
| 76 |
os.rename(archive, archive + '.dist')
|
| 77 |
|
| 78 |
try:
|
| 79 |
shutil.copy2(newconf, archive)
|
| 80 |
except(IOError, os.error), why:
|
| 81 |
print >> sys.stderr, 'dispatch-conf: Error copying %s to %s: %s; fatal' % \
|
| 82 |
(newconf, archive, str(why))
|
| 83 |
|
| 84 |
if has_branch:
|
| 85 |
if mrgconf != '':
|
| 86 |
# This puts the results of the merge into mrgconf.
|
| 87 |
ret = os.system(RCS_MERGE % (archive, mrgconf))
|
| 88 |
mystat = os.lstat(newconf)
|
| 89 |
os.chmod(mrgconf, mystat[ST_MODE])
|
| 90 |
os.chown(mrgconf, mystat[ST_UID], mystat[ST_GID])
|
| 91 |
os.rename(archive, archive + '.dist.new')
|
| 92 |
return ret
|
| 93 |
|
| 94 |
|
| 95 |
def file_archive(archive, curconf, newconf, mrgconf):
|
| 96 |
"""Archive existing config to the archive-dir, bumping old versions
|
| 97 |
out of the way into .# versions (log-rotate style). Then, if mrgconf
|
| 98 |
was specified and there is a .dist version, merge the user's changes
|
| 99 |
and the distributed changes and put the result into mrgconf. Lastly,
|
| 100 |
if newconf was specified, archive it as a .dist.new version (which
|
| 101 |
gets moved to the .dist version at the end of the processing)."""
|
| 102 |
|
| 103 |
try:
|
| 104 |
os.makedirs(os.path.dirname(archive))
|
| 105 |
except:
|
| 106 |
pass
|
| 107 |
|
| 108 |
# Archive the current config file if it isn't already saved
|
| 109 |
if os.path.exists(archive) \
|
| 110 |
and len(commands.getoutput('diff -aq %s %s' % (curconf,archive))) != 0:
|
| 111 |
suf = 1
|
| 112 |
while suf < 9 and os.path.exists(archive + '.' + str(suf)):
|
| 113 |
suf += 1
|
| 114 |
|
| 115 |
while suf > 1:
|
| 116 |
os.rename(archive + '.' + str(suf-1), archive + '.' + str(suf))
|
| 117 |
suf -= 1
|
| 118 |
|
| 119 |
os.rename(archive, archive + '.1')
|
| 120 |
|
| 121 |
try:
|
| 122 |
shutil.copy2(curconf, archive)
|
| 123 |
except(IOError, os.error), why:
|
| 124 |
print >> sys.stderr, 'dispatch-conf: Error copying %s to %s: %s; fatal' % \
|
| 125 |
(curconf, archive, str(why))
|
| 126 |
|
| 127 |
if newconf != '':
|
| 128 |
# Save off new config file in the archive dir with .dist.new suffix
|
| 129 |
try:
|
| 130 |
shutil.copy2(newconf, archive + '.dist.new')
|
| 131 |
except(IOError, os.error), why:
|
| 132 |
print >> sys.stderr, 'dispatch-conf: Error copying %s to %s: %s; fatal' % \
|
| 133 |
(newconf, archive + '.dist.new', str(why))
|
| 134 |
|
| 135 |
ret = 0
|
| 136 |
if mrgconf != '' and os.path.exists(archive + '.dist'):
|
| 137 |
# This puts the results of the merge into mrgconf.
|
| 138 |
ret = os.system(DIFF3_MERGE % (curconf, archive + '.dist', newconf, mrgconf))
|
| 139 |
mystat = os.lstat(newconf)
|
| 140 |
os.chmod(mrgconf, mystat[ST_MODE])
|
| 141 |
os.chown(mrgconf, mystat[ST_UID], mystat[ST_GID])
|
| 142 |
|
| 143 |
return ret
|
| 144 |
|
| 145 |
|
| 146 |
def rcs_archive_post_process(archive):
|
| 147 |
"""Check in the archive file with the .dist.new suffix on the branch
|
| 148 |
and remove the one with the .dist suffix."""
|
| 149 |
os.rename(archive + '.dist.new', archive)
|
| 150 |
if os.path.exists(archive + '.dist'):
|
| 151 |
# Commit the last-distributed version onto the branch.
|
| 152 |
os.system(RCS_LOCK + RCS_BRANCH + ' ' + archive)
|
| 153 |
os.system(RCS_PUT + ' -r' + RCS_BRANCH + ' ' + archive)
|
| 154 |
os.unlink(archive + '.dist')
|
| 155 |
else:
|
| 156 |
# Forcefully commit the last-distributed version onto the branch.
|
| 157 |
os.system(RCS_PUT + ' -f -r' + RCS_BRANCH + ' ' + archive)
|
| 158 |
|
| 159 |
|
| 160 |
def file_archive_post_process(archive):
|
| 161 |
"""Rename the archive file with the .dist.new suffix to a .dist suffix"""
|
| 162 |
os.rename(archive + '.dist.new', archive + '.dist')
|