summaryrefslogtreecommitdiffstats
path: root/pyfirstaidkit/utils
diff options
context:
space:
mode:
authorMartin Sivak <msivak@redhat.com>2008-03-14 15:55:08 +0100
committerMartin Sivak <msivak@redhat.com>2008-03-14 15:55:08 +0100
commit4862af18ff6d062022edf3a1222180fe66af0687 (patch)
tree624cf3664e5efc5dce2478ca7a685902480a2e8d /pyfirstaidkit/utils
parent630e99fd66b393d934c0e654673313aaf6f28b7f (diff)
downloadfirstaidkit-4862af18ff6d062022edf3a1222180fe66af0687.tar.gz
firstaidkit-4862af18ff6d062022edf3a1222180fe66af0687.tar.xz
firstaidkit-4862af18ff6d062022edf3a1222180fe66af0687.zip
Introduce the common Backup interface and move utils package into directory
Diffstat (limited to 'pyfirstaidkit/utils')
-rw-r--r--pyfirstaidkit/utils/__init__.py45
-rw-r--r--pyfirstaidkit/utils/backup.py98
-rw-r--r--pyfirstaidkit/utils/errors.py25
3 files changed, 168 insertions, 0 deletions
diff --git a/pyfirstaidkit/utils/__init__.py b/pyfirstaidkit/utils/__init__.py
new file mode 100644
index 0000000..a6a1086
--- /dev/null
+++ b/pyfirstaidkit/utils/__init__.py
@@ -0,0 +1,45 @@
+# First Aid Kit - diagnostic and repair tool for Linux
+# Copyright (C) 2007 Martin Sivak <msivak@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+import os
+import sys
+import subprocess
+from backup import *
+from errors import *
+
+def chroot_func(dir):
+ def do_chroot():
+ return os.chroot(dir)
+
+ if os.path.abspath(dir)=="/":
+ return lambda: True
+ else:
+ return do_chroot
+
+def spawnvch(executable, args, chroot, env = None):
+ """Use Popen to launch program in chroot
+ executable - path to binary to execute (in chroot!)
+ args - it's parameters
+ chroot - directory to chroot to
+
+Returns the subprocess.Popen object"""
+
+ return subprocess.Popen(executable = executable, args = args, preexec_fn = chroot_func(chroot), env = env,
+ stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
+
+
+
diff --git a/pyfirstaidkit/utils/backup.py b/pyfirstaidkit/utils/backup.py
new file mode 100644
index 0000000..b59308b
--- /dev/null
+++ b/pyfirstaidkit/utils/backup.py
@@ -0,0 +1,98 @@
+# File name: backup.py
+# Date: 2008/03/14
+# Author: Martin Sivak <msivak at redhet dot com>
+#
+# Copyright (C) Red Hat 2008
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# in a file called COPYING along with this program; if not, write to
+# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
+# 02139, USA.
+from errors import NotImplemented
+import os
+
+class BackupException(Exception):
+ pass
+
+class BackupStoreIterface(object):
+ class Backup:
+ def __init__(self, id):
+ raise NotImplemented()
+
+ def backupPath(self, path, name = None):
+ raise NotImplemented()
+ def backupValue(self, value, name):
+ raise NotImplemented()
+
+ def restorePath(self, name, path = None):
+ raise NotImplemented()
+ def restoreValue(self, name):
+ raise NotImplemented()
+
+ def delete(self, name):
+ raise NotImplemented()
+ def cleanup(self):
+ raise NotImplemented()
+
+ def __init__(self):
+ raise NotImplemented()
+
+ def getBackup(self, id):
+ raise NotImplemented()
+
+ def closeBackup(self, id):
+ raise NotImplemented()
+
+ def __del__(self):
+ raise NotImplemented()
+
+class FileBackupStore(BackupStoreIterface):
+ _singleton = None
+
+ class Backup(BackupStoreIterface.Backup):
+ def __init__(self, id, path):
+ self._id = id
+ self._path = path
+
+ def __init__(self, path):
+ if self.__class__._singleton:
+ raise BackupException("BackupStore with %s type can have only one instance" % (self.__name__,))
+
+ assert self.__class__._singleton==None
+
+ self.__class__._singleton = self
+ self._path = path
+ self._backups = {}
+
+ def getBackup(self, id):
+ if not self._backups.has_key(id):
+ self._backups[id] = self.Backup(id, self._path+"/"+id+"/")
+ return self._backups[id]
+
+ def closeBackup(self, id):
+ if not self._backups.has_key(id):
+ raise BackupException("Backup with id %s does not exist" % (id,))
+ self._backups[id].cleanup()
+ del self._backups[id]
+
+ def __del__(self):
+ for id,backup in self._backups[id].iteritems():
+ backup.cleanup()
+
+ @classmethod
+ def get(cls, path):
+ if cls._singleton:
+ return cls._singleton
+ else:
+ return cls(path)
+
diff --git a/pyfirstaidkit/utils/errors.py b/pyfirstaidkit/utils/errors.py
new file mode 100644
index 0000000..ee5db41
--- /dev/null
+++ b/pyfirstaidkit/utils/errors.py
@@ -0,0 +1,25 @@
+# File name: errors.py
+# Date: 2008/03/14
+# Author: Martin Sivak <msivak at redhat dot com>
+#
+# Copyright (C) Red Hat 2008
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# in a file called COPYING along with this program; if not, write to
+# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
+# 02139, USA.
+
+
+class NotImplemented(Exception):
+ pass
+