diff options
author | Andrew Gaffney <agaffney@gentoo.org> | 2006-04-15 20:12:05 +0000 |
---|---|---|
committer | Andrew Gaffney <agaffney@gentoo.org> | 2006-04-15 20:12:05 +0000 |
commit | 9660b9c9a59cccf4e30f199036fd8198423db487 (patch) | |
tree | a15809f5c4357fe4e24a5718941f9a6c9f192e5d /webgli | |
parent | missing ; (diff) | |
download | scire-9660b9c9a59cccf4e30f199036fd8198423db487.tar.gz scire-9660b9c9a59cccf4e30f199036fd8198423db487.tar.bz2 scire-9660b9c9a59cccf4e30f199036fd8198423db487.zip |
working versions of XMLParser and webgliIP
svn path=/; revision=17
Diffstat (limited to 'webgli')
-rw-r--r-- | webgli/XMLParser.php | 21 | ||||
-rw-r--r-- | webgli/webgliIP.php | 315 |
2 files changed, 121 insertions, 215 deletions
diff --git a/webgli/XMLParser.php b/webgli/XMLParser.php index 842fda1..4ebe13e 100644 --- a/webgli/XMLParser.php +++ b/webgli/XMLParser.php @@ -3,7 +3,6 @@ class XMLParser { function start_handler($parser, $name, $attr) { -# if(substr($this->xmlpath, 0, 1) == "/") $this->xmlpath = substr($this->xmlpath, 1); $tmpxmlpath = $this->xmlpath ? explode("/", $this->xmlpath) : array(); $tmpxmlpath[] = $name; $this->xmlpath = implode("/", $tmpxmlpath); @@ -11,12 +10,26 @@ class XMLParser { } function end_handler($parser, $name) { -# $this->chardata = preg_replace('/(^\s+|\s+$)/', '', $this->chardata); if($this->xmlpaths[$this->xmlpath]) { if($this->values[$this->xmlpaths[$this->xmlpath]]) { - $this->set($this->xmlpaths[$this->xmlpath], trim($this->chardata)); + $this->chardata = trim($this->chardata); + switch($this->values[$this->xmlpaths[$this->xmlpath]]['type']) { + case 'string': + case 'uri': + break; + case 'bool': + $this->chardata = $this->chardata == "True" ? 1 : 0; + break; + case 'int': + $this->chardata = intval($this->chardata); + break; + case 'array': + $this->chardata = explode(" ", $this->chardata); + if(count($this->chardata) == 1 && !$this->chardata[0]) $this->chardata = array(); + break; + } + $this->set($this->xmlpaths[$this->xmlpath], $this->chardata); } else { - # Let's ignore this for now call_user_func(array($this, $this->xmlpaths[$this->xmlpath]), trim($this->chardata), array_pop($this->attrs)); } } else { diff --git a/webgli/webgliIP.php b/webgli/webgliIP.php index cbe46cf..0eee0d5 100644 --- a/webgli/webgliIP.php +++ b/webgli/webgliIP.php @@ -22,14 +22,14 @@ class InstallProfile extends XMLParser { $this->values = array( 'boot_device' => array( 'type' => 'string', value => '' ), 'bootloader_kernel_args' => array( 'type' => 'string', 'value' => "" ), - 'boot_loader_mbr' => array( 'type' => 'bool', 'value' => 1 ), - 'boot_loader_pkg' => array( 'type' => 'string', 'value' => 'grub' ), + 'bootloader_mbr' => array( 'type' => 'bool', 'value' => 1 ), + 'bootloader_pkg' => array( 'type' => 'string', 'value' => 'grub' ), 'cron_daemon_pkg' => array( 'type' => 'string', value => 'vixie-cron' ), - 'default_gateway' => array( 'type' => 'array', 'value' => array(), 'serialize_func' => 'seriailize_default_gateway' ), + 'default_gateway' => array( 'type' => 'array', 'value' => array(), 'serialize_func' => 'serialize_default_gateway' ), 'dns_servers' => array( 'type' => 'array', 'value' => array() ), 'domainname' => array( 'type' => 'string', 'value' => "localdomain" ), 'dynamic_stage3' => array( 'type' => 'bool', 'value' => 0 ), - 'etc_files' => array( 'type' => 'array', 'value' => array() ), + 'etc_files' => array( 'type' => 'array', 'value' => array(), 'serialize_func' => 'serialize_etc_files' ), 'filesystem_tools' => array( 'type' => 'array', 'value' => array() ), 'ftp_proxy' => array( 'type' => 'string', 'value' => "" ), 'grp_install' => array( 'type' => 'bool', 'value' => 0 ), @@ -49,9 +49,9 @@ class InstallProfile extends XMLParser { 'kernel_source_pkg' => array( 'type' => 'string', 'value' => "livecd-kernel" ), 'mta_pkg' => array( 'type' => 'string', 'value' => "" ), 'network_mounts' => array( 'type' => 'array', 'value' => array() ), - 'network_interfaces' => array( 'type' => 'array', 'value' => array() ), + 'network_interfaces' => array( 'type' => 'array', 'value' => array(), 'serialize_func' => 'serialize_network_interfaces' ), 'nisdomainname' => array( 'type' => 'string', 'value' => "" ), - 'partition_tables' => array( 'type' => 'array', 'value' => array() ), + 'partition_tables' => array( 'type' => 'array', 'value' => array(), 'serialize_func' => 'serialize_partition_tables' ), 'portage_tree_snapshot_uri' => array( 'type' => 'uri', 'value' => "" ), 'portage_tree_sync_type' => array( 'type' => 'string', 'value' => "sync" ), 'post_install_script_uri' => array( 'type' => 'uri', 'value' => "" ), @@ -60,11 +60,11 @@ class InstallProfile extends XMLParser { 'services' => array( 'type' => 'array', 'value' => array() ), 'stage_tarball_uri' => array( 'type' => 'uri', 'value' => "" ), 'time_zone' => array( 'type' => 'string', 'value' => "UTC" ), - 'users' => array( 'type' => 'array', 'value' => array() ), + 'users' => array( 'type' => 'array', 'value' => array(), 'serialize_func' => 'serialize_users' ), ); $this->xmlpaths = array( - 'gli-profile/bootloader' => 'boot_loader_pkg', + 'gli-profile/bootloader' => 'bootloader_pkg', 'gli-profile/boot-device' => 'boot_device', 'gli-profile/bootloader-kernel-args' => 'bootloader_kernel_args', 'gli-profile/bootloader-mbr' => 'bootloader_mbr', @@ -133,7 +133,7 @@ class InstallProfile extends XMLParser { break; } } - switch ($value['type']) { + switch($value['type']) { case 'string': case 'uri': $this->xmldoc .= sprintf("<%s>%s</%s>", $xmlname, $this->get($name), $xmlname); @@ -164,7 +164,7 @@ class InstallProfile extends XMLParser { return sprintf("<default-gateway interface=\"%s\">%s</default-gateway>", $gw[0], $gw[1]); } - function add_etc_files_file_entry($value, $attr) { + function parse_etc_files_file_entry($value, $attr) { if($attr['name']) { if(!$this->_temp_etc_file) $this->_temp_etc_file = array(); $this->_temp_etc_file[$attr['name']] = $value; @@ -174,13 +174,13 @@ class InstallProfile extends XMLParser { } } - function add_etc_files_file($value, $attr) { + function parse_etc_files_file($value, $attr) { $this->etc_files[$attr['name']] = $this->_temp_etc_file; $this->_temp_etc_file = array(); } function serialize_etc_files() { - $this->xmldoc = "<etc-files>"; + $this->xmldoc .= "<etc-files>"; foreach($this->get("etc_files") as $etc_file => $values) { $this->xmldoc .= sprintf("<file name=\"%s\">", $etc_file); $isalist = (array_keys($values) === range(0, count($values) - 1)) ? true : false; @@ -198,13 +198,13 @@ class InstallProfile extends XMLParser { $this->xmldoc .= "</etc-files>"; } - function add_network_interface($device, $attr) { + function parse_network_interface($device, $attr) { $options = null; $ip = $broadcast = $netmask = $dhcp_options = null; $dhcp = true; foreach($attr as $name => $value) { - switch ($name) { + switch($name) { case 'ip': $ip = $value; break; @@ -247,212 +247,105 @@ class InstallProfile extends XMLParser { } else { $this->xmldoc .= sprintf("<device ip=\"%s\" broadcast=\"%s\" netmask=\"%s\">%s</device>", $interface[0], $interface[1], $interface[2], $iname); } + } $this->xmldoc .= "</network-interfaces>"; } } - function add_netmount(self, xml_path, unused, attr): - netmount_entry = {'export': '', 'host': '', 'mountopts': '', 'mountpoint': '', 'type': ''} - if type(attr) == tuple: - netmount_entry['export'] = attr[0] - netmount_entry['host'] = attr[1] - netmount_entry['mountopts'] = attr[2] - netmount_entry['mountpoint'] = attr[3] - netmount_entry['type'] = attr[4] - else: - if "export" in attr.getNames(): - for attrName in attr.getNames(): - netmount_entry[attrName] = str(attr.getValue(attrName)) - self._network_mounts.append(netmount_entry) - - ## - # Serializes network mounts - function serialize_network_mounts(self): - if self.get_network_mounts() != {}: - netmounts = self.get_network_mounts() - self.xmldoc += "<network-mounts>" - for mount in netmounts: - self.xmldoc += "<netmount host=\"%s\" export=\"%s\" type=\"%s\" mountpoint=\"%s\" mountopts=\"%s\" />" % (mount['host'], mount['export'], mount['type'], mount['mountpoint'], mount['mountopts']) - self.xmldoc += "</network-mounts>" + function parse_netmount($unused, $attr) { + $netmount_entry = array('export' => '', 'host' => '', 'mountopts' => '', 'mountpoint' => '', 'type' => ''); + foreach($attr as $name => $value) { + $netmount_entry[$name] = $value; + } + $network_mounts = $this->get("network_mounts"); + $network_mounts[] = $netmount_entry; + $this->set("network_mounts", $network_mounts); + } - ## - # FIXME: agaffney - # @param xml_path Used internally by the XML parser. Should be None when calling directly - # @param unused Parameter description - # @param attr Parameter description - function add_partitions_device(self, xml_path, unused, attr): - devnode = None - if type(attr) == tuple: - devnode = attr[0] - disklabel = attr[1] - else: - if "devnode" in attr.getNames(): - devnode = str(attr.getValue("devnode")) - if "disklabel" in attr.getNames(): - disklabel = str(attr.getValue("disklabel")) - else: - disklabel = "" - self._partition_current_device = devnode - self._partition_tables[devnode] = GLIStorageDevice.Device(devnode) - self._partition_tables[devnode].set_disklabel(disklabel) - self._partition_tables[devnode].set_partitions_from_install_profile_structure(self._temp_partition_table) - self._temp_partition_table = [] + function serialize_network_mounts() { + $network_mounts = $this->get("network_mounts"); + if($network_mounts) { + $this->xmldoc .= "<network-mounts>"; + foreach($network_mounts as $mount) { + $this->xmldoc .= sprintf("<netmount host=\"%s\" export=\"%s\" type=\"%s\" mountpoint=\"%s\" mountopts=\"%s\" />", $mount['host'], $mount['export'], $mount['type'], $mount['mountpoint'], $mount['mountopts']); + } + $this->xmldoc .= "</network-mounts>"; + } + } - ## - # FIXME: agaffney - # @param xml_path Used internally by the XML parser. Should be None when calling directly - # @param unused Parameter description - # @param attr Parameter description - function add_partitions_device_partition(self, xml_path, unused, attr): - part_entry = {'end': 0, 'format': None, 'mb': 0, 'minor': 0, 'mountopts': '', 'mountpoint': '', 'origminor': '', 'start': 0, 'type': ''} -# if type(attr) == tuple: -# part_entry['end'] = attr[0] -# part_entry['format'] = attr[1] -# part_entry['mb'] = attr[2] -# part_entry['minor'] = attr[3] -# part_entry['mountopts'] = attr[4] -# part_entry['mountpoint'] = attr[5] -# part_entry['origminor'] = attr[6] -# part_entry['start'] = attr[7] -# part_entry['type'] = attr[8] -# else: - if "minor" in attr.getNames(): - for attrName in attr.getNames(): - part_entry[attrName] = str(attr.getValue(attrName)) - if type(part_entry['format']) == str: part_entry['format'] = GLIUtility.strtobool(part_entry['format']) - if type(part_entry['resized']) == str: part_entry['resized'] = GLIUtility.strtobool(part_entry['resized']) -# if GLIUtility.is_numeric(part_entry['end']): part_entry['end'] = long(part_entry['end']) -# if GLIUtility.is_numeric(part_entry['start']): part_entry['start'] = long(part_entry['start']) - if GLIUtility.is_numeric(part_entry['mb']): part_entry['mb'] = long(part_entry['mb']) - if GLIUtility.is_numeric(part_entry['minor']): -# if part_entry['type'] == "free": - part_entry['minor'] = float(part_entry['minor']) - if int(part_entry['minor']) == part_entry['minor']: - part_entry['minor'] = int(part_entry['minor']) - if GLIUtility.is_numeric(part_entry['origminor']): part_entry['origminor'] = int(part_entry['origminor']) - self._temp_partition_table.append(part_entry) - - ## - # Serializes partition tables - function serialize_partition_tables(self): - if self.get_partition_tables() != {}: - partitions = self.get_partition_tables() - self.xmldoc += "<partitions>"; - for device in partitions.keys(): - self.xmldoc += "<device devnode=\"%s\" disklabel=\"%s\">" % (device, partitions[device].get_disklabel()) - ips = partitions[device].get_install_profile_structure() - for part in ips: -# part = ips[minor] - self.xmldoc += "<partition minor=\"%s\" origminor=\"%s\" mb=\"%s\" type=\"%s\" mountpoint=\"%s\" mountopts=\"%s\" format=\"%s\" mkfsopts=\"%s\" resized=\"%s\" />" % (str(part['minor']), str(part['origminor']), str(part['mb']), str(part['type']), str(part['mountpoint']), str(part['mountopts']), str(part['format']), str(part['mkfsopts']), str(part['resized'])) - self.xmldoc += "</device>" - self.xmldoc += "</partitions>" + function parse_partitions_device($unused, $attr) { + $devnode = ""; + $disklable = ""; + foreach($attr as $name => $value) { + if($name == "devnode") { + $devnode = $value; + } elseif($name == "disklabel") { + $disklabel = $value; + } + } + $partition_tables = $this->get("partition_tables"); + $partition_tables[$devnode] = $this->_temp_partition_table; + $this->_temp_partition_table = array(); + $this->set("partition_tables", $partition_tables); + } - ############################################################################ - #### Users + function parse_partitions_device_partition($unused, $attr) { + $part_entry = array(); + foreach($attr as $name => $value) { + $part_entry[$name] = $value; + } + $part_entry['format'] = $part_entry['format'] == "True" ? true : false; + $part_entry['resized'] = $part_entry['resized'] == "True" ? true : false; +# if GLIUtility.is_numeric(part_entry['mb']): part_entry['mb'] = long(part_entry['mb']) +# if GLIUtility.is_numeric(part_entry['minor']): +# part_entry['minor'] = float(part_entry['minor']) +# if int(part_entry['minor']) == part_entry['minor']: +# part_entry['minor'] = int(part_entry['minor']) +# if GLIUtility.is_numeric(part_entry['origminor']): part_entry['origminor'] = int(part_entry['origminor']) + $this->_temp_partition_table[] = $part_entry; + } - ## - # Adds a user to the list of users - # @param xml_path Used internally by the XML parser. Should be None when calling directly - # @param username name of user to be added - # @param attr=None parameters for the user. - function add_user(self, xml_path, username, attr=None): - """ - This will take a username (that is a string) and a set of attributes and it will verify everything is valid - and convert it into a 7-tuple set. Then it adds this tuple into the users list. - username and hash are manditory. All other attributes are optional. Or this method will - take a 7-tuple set, verify it's correctness and then append it to the _users list. - All items are strings except <uid>, which is an integer, and groups, which is a tuple. - - The finished tuples look like this: - ( <user name>, <password hash>, (<tuple of groups>), <shell>, <home directory>, <user id>, <user comment> ) - - """ - hash = '' - shell = None - groups = None - shell = None - homedir = None - uid = None - comment = None - - if type(username) == tuple: - if len(username) != 7: - raise GLIException("UserError", 'fatal', 'add_user', "Wrong format for user tuple!") - - username_tmp = username[0] - hash = username[1] - groups = username[2] - shell = username[3] - homedir = username[4] - uid = username[5] - comment = username[6] - username = username_tmp - - if type(groups) != tuple: - if groups != None: - groups = tuple(groups.split(',')) - else: - for attrName in attr.keys(): - if attrName == 'groups': - groups = tuple(str(attr[attrName]).split(',')) - elif attrName == 'shell': - shell = str(attr[attrName]) - elif attrName == 'hash': - hash = str(attr[attrName]) - elif attrName == 'homedir': - homedir = str(attr[attrName]) - elif attrName == 'uid': - if attr[attrName]: - uid = int(attr[attrName]) - elif attrName == 'comment': - comment = str(attr[attrName]) - - allowable_nonalphnum_characters = '_-' - - if not GLIUtility.is_realstring(username): - raise GLIException("UserError", 'fatal', 'add_user', "username must be a non-empty string") - - if username[0] not in (string.lowercase + string.uppercase): - raise GLIException("UsersError", 'fatal', 'add_user', "A username must start with a letter!") - - for x in username: - if x not in (string.lowercase + string.uppercase + string.digits + allowable_nonalphnum_characters): - raise GLIException("UsersError", 'fatal', 'add_user', "A username must contain only letters, numbers, or these symbols: " + allowable_nonalphnum_characters) - - for user in self._users: - if username == user[0]: - raise GLIException("UserError", 'fatal', 'add_user', "This username already exists!") - - if (hash == None) or (hash == ''): - raise GLIException("UserError", 'fatal', 'add_user', "A password hash must be given for every user!") - - self._users.append((username,hash,groups,shell,homedir,uid,comment)) - - ## - # Serializes users - function serialize_users(self): - if self.get_users() != []: - self.xmldoc += "<users>" - users = self.get_users() - for user in users: - attrstr = "" - username = user[0] + function serialize_partition_tables() { + $partitions = $this->get("partition_tables"); + if($partitions) { + $this->xmldoc .= "<partitions>"; + foreach($partitions as $device => $parts) { + $this->xmldoc .= sprintf("<device devnode=\"%s\" disklabel=\"%s\">", $device, "msdos"); #partitions[device].get_disklabel()) + foreach($parts as $part) { + $this->xmldoc .= sprintf("<partition minor=\"%s\" origminor=\"%s\" mb=\"%s\" type=\"%s\" mountpoint=\"%s\" mountopts=\"%s\" format=\"%s\" mkfsopts=\"%s\" resized=\"%s\" />", $part['minor'], $part['origminor'], $part['mb'], $part['type'], $part['mountpoint'], $part['mountopts'], $part['format'], $part['mkfsopts'], $part['resized']); + } + $this->xmldoc .= "</device>"; + } + $this->xmldoc .= "</partitions>"; + } + } - if user[1] != None: - attrstr += "hash=\"%s\" " % user[1] - if user[2] != None: - attrstr += "groups=\"%s\" " % string.join(user[2],',') - if user[3] != None: - attrstr += "shell=\"%s\" " % user[3] - if user[4] != None: - attrstr += "homedir=\"%s\" " % user[4] - if user[5] != None: - attrstr += "uid=\"%s\" " % user[5] - if user[6] != None: - attrstr += "comment=\"%s\" " % user[6] + function parse_user($username, $attr) { + $tmp_user = array(); + foreach($attr as $name => $value) { + $tmp_user[$name] = $value; + } + $users = $this->get("users"); + $users[] = array($username, $tmp_user['hash'], $tmp_user['groups'], $tmp_user['shell'], $tmp_user['homedir'], $tmp_user['uid'], $tmp_user['comment']); + $this->set("users", $users); + } - self.xmldoc += "<user %s>%s</user>" % (string.strip(attrstr), username) - self.xmldoc += "</users>" -*/ + function serialize_users() { + $users = $this->get("users"); + if($users) { + $this->xmldoc .= "<users>"; + foreach($users as $user) { + $attrstr = ""; + if($user[1]) $attrstr .= sprintf("hash=\"%s\" ", $user[1]); + if($user[2]) $attrstr .= sprintf("groups=\"%s\" ", implode(",", $user[2])); + if($user[3]) $attrstr .= sprintf("shell=\"%s\" ", $user[3]); + if($user[4]) $attrstr .= sprintf("homedir=\"%s\" ", $user[4]); + if($user[5]) $attrstr .= sprintf("uid=\"%s\" ", $user[5]); + if($user[6]) $attrstr .= sprintf("comment=\"%s\" ", $user[6]); + $this->xmldoc .= sprintf("<user %s>%s</user>", trim($attrstr), $user[0]); + } + $this->xmldoc .= "</users>"; + } + } } ?> |