summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Admin/Tidy.py
diff options
context:
space:
mode:
authorNarayan Desai <desai@mcs.anl.gov>2007-12-29 04:57:53 +0000
committerNarayan Desai <desai@mcs.anl.gov>2007-12-29 04:57:53 +0000
commit3610288cbcbf4d1adedefa03166cd77ee15aad96 (patch)
tree1588fb78ae1c6e51e4e4ea2dab46a006a3ece0d3 /src/lib/Server/Admin/Tidy.py
parent6bf7875bc299a1f81061782c8646c90972e06e5a (diff)
downloadbcfg2-3610288cbcbf4d1adedefa03166cd77ee15aad96.tar.gz
bcfg2-3610288cbcbf4d1adedefa03166cd77ee15aad96.tar.bz2
bcfg2-3610288cbcbf4d1adedefa03166cd77ee15aad96.zip
Refactor of bcfg2-admin (all modes moved to discrete modules in Bcfg2.Server.Admin
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4125 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib/Server/Admin/Tidy.py')
-rw-r--r--src/lib/Server/Admin/Tidy.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/lib/Server/Admin/Tidy.py b/src/lib/Server/Admin/Tidy.py
new file mode 100644
index 000000000..80307df80
--- /dev/null
+++ b/src/lib/Server/Admin/Tidy.py
@@ -0,0 +1,52 @@
+import Bcfg2.Server.Admin
+import re, os, socket
+
+class Tidy(Bcfg2.Server.Admin.Mode):
+ __shorthelp__ = 'bcfg2-admin tidy [-f] [-I]'
+ __longhelp__ = __shorthelp__ + '\n\tClean up useless files in the repo'
+ def __call__(self, args):
+ Bcfg2.Server.Admin.Mode.__call__(self, args)
+ badfiles = self.buildTidyList()
+ if '-f' in args or '-I' in args:
+ if '-I' in args:
+ for name in badfiles[:]:
+ answer = raw_input("Unlink file %s? [yN] " % name)
+ if answer not in ['y', 'Y']:
+ badfiles.remove(name)
+ for name in badfiles:
+ try:
+ os.unlink(name)
+ except IOError:
+ print "Failed to unlink %s" % name
+ else:
+ for name in badfiles:
+ print name
+
+ def buildTidyList(self):
+ '''Clean up unused or unusable files from the repository'''
+ hostmatcher = re.compile('.*\.H_(\S+)$')
+ repo = self.get_repo_path()
+ to_remove = []
+ good = []
+ bad = []
+
+ # clean up unresolvable hosts in SSHbase
+ for name in os.listdir("%s/SSHbase" % (repo)):
+ if hostmatcher.match(name):
+ hostname = hostmatcher.match(name).group(1)
+ if hostname in good + bad:
+ continue
+ try:
+ socket.gethostbyname(hostname)
+ good.append(hostname)
+ except:
+ bad.append(hostname)
+ for name in os.listdir("%s/SSHbase" % (repo)):
+ if not hostmatcher.match(name):
+ to_remove.append("%s/SSHbase/%s" % (repo, name))
+ else:
+ if hostmatcher.match(name).group(1) in bad:
+ to_remove.append("%s/SSHbase/%s" % (repo, name))
+ # clean up file~
+ # clean up files without parsable names in Cfg
+ return to_remove