[SCM] GUI front-end for Debian Live. branch, master, updated. 11e67a56b24563c3f7a488602604f9ba897740c3
Chris Lamb
chris at chris-lamb.co.uk
Tue Mar 4 03:01:19 UTC 2008
The following commit has been merged in the master branch:
commit 5093bd2a1f5b0c45f2269a7e61522901d682acb6
Author: Chris Lamb <chris at chris-lamb.co.uk>
Date: Tue Mar 4 02:59:42 2008 +0000
First steps to a seperate configuration API - remove Live Magic models.
Signed-off-by: Chris Lamb <chris at chris-lamb.co.uk>
diff --git a/LiveMagic/models/__init__.py b/LiveMagic/models/__init__.py
deleted file mode 100644
index fa449ff..0000000
--- a/LiveMagic/models/__init__.py
+++ /dev/null
@@ -1,4 +0,0 @@
-from LiveMagic.models.live_helper_configuration import LiveHelperConfiguration
-from LiveMagic.models.key_var_config_file import KeyVarConfigFile
-from LiveMagic.models.folder_of_files import FolderOfFiles
-from LiveMagic.models.sources_list import SourcesList
diff --git a/LiveMagic/models/folder_of_files.py b/LiveMagic/models/folder_of_files.py
deleted file mode 100644
index 0747821..0000000
--- a/LiveMagic/models/folder_of_files.py
+++ /dev/null
@@ -1,129 +0,0 @@
-import glob
-import os
-
-from os.path import join
-
-class FolderOfFiles(object):
- """
- Represents a folder containing a number of files.
- """
-
- def __init__(self, dir):
- self.dir = dir
-
- self._stale = set()
- self.files = {}
-
- # No file has been deleted
- self.file_deleted = False
-
- self.load()
-
- def __getitem__(self, k):
- return self.files[k]
-
- def __contains__(self, k):
- return k in self.files
-
- def __delitem__(self, k):
- del self.files[k]
-
- def __setitem__(self, k, v):
- self._stale.add(k)
- self.files[k] = v
-
- def _config_exists(self, file):
- try:
- os.stat(join(self.dir, file))
- return True
- except OSError:
- return False
-
- def load(self):
- """
- Loads files.
- """
- self.deleted = False
- self._stale.clear()
- self.files.clear()
- for name in glob.glob(join(self.dir, '*')):
- key = name.split('/')[-1]
- try:
- f = open(name, 'r')
- self.files[key] = f.read()
- f.close()
- except IOError, e:
- # "Is a directory"
- if e.errno == 21:
- continue
- raise e
-
- def altered(self):
- """
- Returns True if this configuration file has changed since last save.
- """
- return len(self._stale) > 0 or self.file_deleted == True
-
- def save(self):
- """
- Update all updated files in this directory.
- """
- for filename in self._stale:
- pathname = join(self.dir, filename)
- f = open(pathname, 'w+')
- f.write(self[filename])
- f.close()
-
- self._stale.clear()
-
- def delete(self, hook_name):
- if self._config_exists(hook_name):
- os.remove(join(self.dir, hook_name))
- del self[hook_name]
- if hook_name in self._stale: self._stale.remove(hook_name)
- self.file_deleted = True
-
- def rename(self, orig, new):
- """
- Throws ValueError if 'new' already exists.
- """
- if self._config_exists(new):
- raise ValueError
- if self._config_exists(orig):
- os.rename(join(self.dir, orig), join(self.dir, new))
- if orig in self._stale: self._stale.remove(orig)
- self[new] = self[orig]
- del self[orig]
-
- def import_file(self, source):
- """
- Imports the specified file into the current configuration, using a
- unique name. The file is not saved.
- """
- f = open(source, 'r')
- source_contents = f.read()
- f.close()
-
- target_name = self._gen_import_name(source)
- self[target_name] = source_contents
-
- return target_name
-
- def _gen_import_name(self, filename):
- """
- Generates a unique name of the imported file.
- """
- # Use existing filename as the root
- root = filename.split(os.sep)[-1]
-
- if root in self:
- # Keep adding a number to the end until it doesn't exist.
- i = 1
- while True:
- tmpnam = "%s-%d" % (root, i)
- if not tmpnam in self:
- return tmpnam
- i += 1
- else:
- # Just use the root name
- return root
diff --git a/LiveMagic/models/key_var_config_file.py b/LiveMagic/models/key_var_config_file.py
deleted file mode 100644
index 01f5dff..0000000
--- a/LiveMagic/models/key_var_config_file.py
+++ /dev/null
@@ -1,203 +0,0 @@
-import re
-import os
-
-class KeyVarConfigFile(object):
- """
- Represents a shell KEY=VAR configuration file.
- """
-
- # Escape mappings (<unescaped>, <escaped>)
- escapes = (
- (r'\ '[:-1], r'\\ '[:-1]),
- (r'"', r'\"'),
- (r'`', r'\`'),
- (r'$', r'\$'),
- (r"'", r'\''),
- )
-
- def __init__(self, filename, spec):
- self.filename = filename
- self.spec = spec
-
- self.shortname = filename.split(os.sep)[-1]
- self._line_numbers = {}
- self._stale = set()
-
- self.load()
-
- @staticmethod
- def is_lh_variable(var):
- """
- Returns True if var is a configuration variable for Debian Live.
- """
- return var.startswith('LH_') or var.startswith('LH_')
-
- def __setattr__(self, k, v):
- if self.is_lh_variable(k):
- self._stale.add(k)
- self.__dict__[k] = v
-
- def __iter__(self):
- return filter(self.is_lh_variable, self.__dict__.keys()).__iter__()
-
- def load(self):
- """
- Loads and parses file.
- """
- # Clear old state
- self._line_numbers.clear()
- self._stale.clear()
- for var in self.__dict__.keys():
- if self.is_lh_variable(var): del self.__dict__[var]
-
- lineno = 1
- regex = re.compile(r"""^\s*(\w+)=(?:(["\'])(([^\\\2]|\\.)*|)\2|((\w|\\["'])*))\s*(?:#.*)?$""")
- f = open(self.filename, 'r')
- for line in f:
-
- # Check and parse key=value lines
- match = regex.match(line)
- if match:
- key = match.groups()[0]
-
- # Find the correct match group
- for m in match.groups()[2:]:
- if m is not None:
- val = m
- break
-
- # Unescape value
- for to, from_ in self.escapes:
- val = val.replace(from_, to)
-
- # Save line number
- self._line_numbers[key] = lineno
-
- # Mutate to file type
- val_type = self.spec.get(key, 'string')
- typed_val = {
- 'int' : self._parse_int,
- 'list' : self._parse_list,
- 'string' : lambda k, v: v,
- 'boolean' : lambda k, v: {'enabled' : True, 'disabled' : False, 'yes' : True, 'no' : False}.get(v, None),
- }[val_type](key, val)
- self.__dict__[key] = typed_val
-
- lineno += 1
-
- def _parse_list(self, k, v):
- if v == '':
- return list_observer([], self._stale.add, k)
- else:
- return list_observer(v.split(' '), self._stale.add, k)
-
- def _parse_int(self, k, v):
- if v == '':
- return None
- else:
- return int(v)
-
- def altered(self):
- """Returns True if this configuration file has changed since last save."""
- return len(self._stale) != 0
-
- def save(self):
- """
- Update all updated entries in the file.
- """
- if len(self._stale) == 0:
- return
-
- f = open(self.filename, 'r+')
- lines = f.readlines()
-
- for k in self._stale:
- val = getattr(self, k)
-
- # Escape value
- if type(val) in (list, list_observer):
- for from_, to in self.escapes:
- val = map(lambda x: x.replace(from_, to), val)
- val = map(str.strip, val)
- elif type(val) is str:
- for from_, to in self.escapes:
- val = val.replace(from_, to)
-
- # Format value depending on its type
- line_value = {
- list : lambda v: " ".join(val),
- bool : lambda v: {True: 'enabled', False: 'disabled'}.get(val, None),
- str : lambda v: v,
- type(None) : lambda v: "",
- }[type(val)](val)
-
- line = '%s="%s"\n' % (k, line_value)
-
- try:
- # Overwrite original line in file
- lines[self._line_numbers[k] - 1] = line
- except KeyError:
- # Append line to end of file
- lines.append("\n# The following option was added by live-magic\n")
- lines.append(line)
- f.close()
-
- f = open(self.filename, 'w')
- f.writelines(lines)
- f.close()
-
- self._stale.clear()
-
-
-class list_observer(list):
- def __init__ (self, value, observer, observer_arg):
- list.__init__(self, value)
- self.observer = observer
- self.observer_arg = observer_arg
-
- def __iter__(self):
- return list.__iter__(self)
-
- def __setitem__(self,key,value):
- list.__setitem__(self, key, value)
- self.observer(self.observer_arg)
-
- def __delitem__(self,key):
- list.__delitem__(self, key)
- self.observer(self.observer_arg)
-
- def __setslice__(self, i, j, sequence):
- list.__setslice__(self, i, j, sequence)
- self.observer(self.observer_arg)
-
- def __delslice__(self, i, j):
- list.__delslice__(self, i, j)
- self.observer(self.observer_arg)
-
- def append(self, value):
- list.append(self, value)
- self.observer(self.observer_arg)
-
- def pop(self):
- self.observer(self.observer_arg)
- return list.pop(self)
-
- def extend(self, newvalue):
- self.observer(self.observer_arg)
- list.extend(self, newvalue)
-
- def insert(self, i, element):
- list.insert(self, i, element)
- self.observer(self.observer_arg)
-
- def remove(self, element):
- list.remove(self, element)
- self.observer(self.observer_arg)
-
- def reverse(self):
- list.reverse(self)
- self.observer(self.observer_arg)
-
- def sort(self, cmpfunc=None):
- list.sort(self, cmpfunc)
- self.observer(self.observer_arg)
diff --git a/LiveMagic/models/live_helper_configuration.py b/LiveMagic/models/live_helper_configuration.py
deleted file mode 100644
index 6a421d9..0000000
--- a/LiveMagic/models/live_helper_configuration.py
+++ /dev/null
@@ -1,177 +0,0 @@
-import os
-import commands
-import time
-
-from key_var_config_file import KeyVarConfigFile
-from folder_of_files import FolderOfFiles
-
-CONFIG_SPEC = {
- 'folder_of_files': {
- 'hooks': 'chroot_local-hooks',
- },
- 'key_value': {
- 'binary': {
- 'LH_BINARY_IMAGES': 'string',
- 'LH_BINARY_INDICES': 'boolean',
- 'LH_BOOTAPPEND': 'string',
- 'LH_BOOTLOADER': 'string',
- 'LH_DEBIAN_INSTALLER': 'boolean',
- 'LH_ENCRYPTION': 'string',
- 'LH_GRUB_SPLASH': 'string',
- 'LH_HOSTNAME': 'string',
- 'LH_ISO_APPLICATION': 'string',
- 'LH_ISO_PREPARER': 'string',
- 'LH_ISO_PUBLISHER': 'string',
- 'LH_ISO_VOLUME': 'string',
- 'LH_MEMTEST': 'string',
- 'LH_NET_PATH': 'string',
- 'LH_NET_SERVER': 'string',
- 'LH_SYSLINUX_SPLASH': 'string',
- 'LH_USERNAME': 'string',
- },
- 'bootstrap': {
- 'LH_ARCHITECTURE': 'list',
- 'LH_BOOTSTRAP_CONFIG': 'string',
- 'LH_BOOTSTRAP_FLAVOUR': 'string',
- 'LH_BOOTSTRAP_KEYRING': 'string',
- 'LH_DISTRIBUTION': 'string',
- 'LH_MIRROR_BINARY': 'string',
- 'LH_MIRROR_BINARY_SECURITY': 'string',
- 'LH_MIRROR_BOOTSTRAP': 'string',
- 'LH_MIRROR_BOOTSTRAP_SECURITY': 'string',
- 'LH_SECTIONS': 'list',
- },
- 'chroot': {
- 'LH_CHROOT_FILESYSTEM': 'string',
- 'LH_HOOKS': 'list',
- 'LH_INTERACTIVE': 'boolean',
- 'LH_KEYRING_PACKAGES': 'list',
- 'LH_LANGUAGE': 'string',
- 'LH_LINUX_FLAVOURS': 'list',
- 'LH_LINUX_PACKAGES': 'list',
- 'LH_PACKAGES': 'list',
- 'LH_PACKAGES_LISTS': 'string',
- 'LH_PRESEED': 'string',
- 'LH_SECURITY': 'boolean',
- 'LH_SYMLINKS': 'boolean',
- 'LH_SYSVINIT': 'boolean',
- 'LH_TASKS': 'list',
- 'LH_UNION_FILESYSTEM': 'string',
- },
- 'common': {
- 'LH_APT': 'string',
- 'LH_APT_FTPPROXY': 'string',
- 'LH_APT_HTTPPROXY': 'string',
- 'LH_APT_PDIFFS': 'boolean',
- 'LH_APT_PIPELINE': 'int',
- 'LH_APT_RECOMMENDS': 'boolean',
- 'LH_APT_SECURE': 'boolean',
- 'LH_BOOTSTRAP': 'string',
- 'LH_BREAKPOINTS': 'boolean',
- 'LH_CACHE_INDICES': 'boolean',
- 'LH_CACHE_PACKAGES': 'boolean',
- 'LH_CACHE_STAGES': 'list',
- 'LH_DEBCONF_FRONTEND': 'string',
- 'LH_DEBCONF_NOWARNINGS': 'boolean',
- 'LH_DEBCONF_PRIORITY': 'string',
- 'LH_DEBUG': 'boolean',
- 'LH_FORCE': 'boolean',
- 'LH_GENISOIMAGE': 'string',
- 'LH_INCLUDES': 'string',
- 'LH_INITRAMFS': 'string',
- 'LH_LOSETUP': 'string',
- 'LH_MODE': 'string',
- 'LH_QUIET': 'boolean',
- 'LH_ROOT': 'string',
- 'LH_ROOT_COMMAND': 'string',
- 'LH_TASKSEL': 'string',
- 'LH_TEMPLATES': 'string',
- 'LH_VERBOSE': 'boolean',
- },
- 'source': {
- 'LH_SOURCE': 'boolean', 'LH_SOURCE_IMAGES': 'list',
- }
- }
-}
-
-class LiveHelperConfiguration(object):
- """
- Represents a configuration for a Debian Live system.
- """
-
- def __init__(self, dir=None):
- if dir is None:
- dir = os.path.expanduser("~/DebianLive/%s" % time.strftime('%Y-%m-%d-%H%M%S'))
- self.dir = dir
-
- self.children = []
- self._load_observers = []
- self.first_load = True
-
- def new(self, tempdir=None):
- """
- Creates and initialises a new configuration in a temporary folder.
- """
- if not os.path.exists(self.dir):
- os.makedirs(self.dir)
-
- res, out = commands.getstatusoutput('cd %s; lh_config' % self.dir)
- if res != 0: raise IOError, out
- self._load()
-
- def open(self, dir):
- """
- Discards the current configuration, and then loads and initialises
- the configuration from the specified directory.
- """
- self.dir = dir
- self._load()
-
- def reload(self):
- """
- Reloads the current configuration.
- """
- self._load()
-
- def save(self):
- """
- Saves the current configuration.
- """
- for conf in self.children:
- conf.save()
-
- def altered(self):
- """
- Returns True if the state of the configuration has changed since last save.
- """
- for conf in self.children:
- if conf.altered() == True:
- return True
- return False
-
- def _load(self):
- self.children = []
-
- for filename, file_spec in CONFIG_SPEC['key_value'].iteritems():
- kv = KeyVarConfigFile(os.path.join(self.dir, 'config', filename), file_spec)
- self.children.append(kv)
- setattr(self, filename, kv)
-
- for name, dir in CONFIG_SPEC['folder_of_files'].iteritems():
- fof = FolderOfFiles(os.path.join(self.dir, 'config', dir))
- setattr(self, name, fof)
- self.children.append(fof)
-
- # Notify all the observers, but avoid double load when the view is ready
- if not self.first_load:
- self.notify_load_observers()
- self.first_load = False
-
- def notify_load_observers(self):
- """
- Notify all the registered observers that we have reloaded.
- """
- map(apply, self._load_observers)
-
- def attach_load_observer(self, fn):
- self._load_observers.append(fn)
diff --git a/LiveMagic/models/sources_list.py b/LiveMagic/models/sources_list.py
deleted file mode 100644
index 588fcd4..0000000
--- a/LiveMagic/models/sources_list.py
+++ /dev/null
@@ -1,45 +0,0 @@
-import os
-import re
-
-class SourcesList(object):
- """
- Represents /etc/apt/sources.list
- """
-
- comments = re.compile(r'\s*#')
- patterns = (
- re.compile(r'http://ftp.?\..{2}\.debian\.org[^\s]*'),
- re.compile(r'http://(localhost|127\.0\.0\.1)[^\s]*'),
- re.compile(r'http://[^\s]*'),
- )
-
- def __init__(self, filename=None):
- if filename is None:
- self.filename = r'/etc/apt/sources.list'
- else:
- self.filename = filename
-
- def get_mirror(self):
- result = r'http://ftp.us.debian.org/'
-
- f = open(self.filename, 'r')
- try:
- try:
- for line in f.readlines():
-
- if self.comments.match(line):
- continue
-
- for pat in self.patterns:
- m = pat.search(line)
- if not m:
- continue
-
- result = m.group(0)
-
- finally:
- f.close()
- except IOError:
- pass
-
- return result
--
GUI front-end for Debian Live.
More information about the debian-live-changes
mailing list