def validate(self, validate_this): ''' Load and perform validation on file given as input. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(validate_this, not self.OVERLAY) mim.validate()
def test_commit_2(self): ''' Test that validation takes place when requested. ''' self.create_small_xml() mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.IN_XML_FILE, not self.OVERLAY) self.assertRaises(milib.MimDTDInvalid, mim.commit)
def test_commit_2(self): ''' Test that validation takes place when requested. ''' self.create_small_xml() mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.IN_XML_FILE, not self.OVERLAY) self.assertRaises(milib.MimDTDInvalid, mim.commit)
def test_commit_3(self): ''' Test that file gets written without validation when requested. ''' self.create_small_xml() mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.IN_XML_FILE, not self.OVERLAY) mim.commit(validate=False)
def test_overlay_7(self): ''' Try to overlay a leaf element (id by attr) where it does not belong. Give element an attribute to identify it. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) self.assertRaises(milib.MimInvalidError, mim.load, self.OVERLAY_XML_FILE, self.OVERLAY)
def test_overlay_7(self): ''' Try to overlay a leaf element (id by attr) where it does not belong. Give element an attribute to identify it. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) self.assertRaises(milib.MimInvalidError, mim.load, self.OVERLAY_XML_FILE, self.OVERLAY)
def test_overlay_6(self): ''' Try to overlay a leaf element (id by value) where it does not belong. Give element a value to identify it. ''' # Note: giving a bogus attribute is not checked, only a bogus element. mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) self.assertRaises(milib.MimInvalidError, mim.load, self.OVERLAY_XML_FILE, self.OVERLAY)
def test_overlay_6(self): ''' Try to overlay a leaf element (id by value) where it does not belong. Give element a value to identify it. ''' # Note: giving a bogus attribute is not checked, only a bogus element. mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) self.assertRaises(milib.MimInvalidError, mim.load, self.OVERLAY_XML_FILE, self.OVERLAY)
def test_overlay_5(self): ''' Overlay same-tagged non-leaf element with new attr where not allowed. Same-tagged non-leaf elements are not allowed. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) # Verify auto_reboot attribute was added to existing element. (value, path) = mim.get("/auto_install/ai_instance@name") self.assertEquals(value, "firstname", "Error finding original attribute of existing node") self.assertEquals( path, "/auto_install[1]/ai_instance[1]", "Error: incorrect pathname returned when getting" + "\"name\" attr") (value, path) = mim.get("/auto_install/ai_instance@auto_reboot") self.assertEquals(value, "true", "Error adding new attribute to existing node") self.assertEquals( path, "/auto_install[1]/ai_instance[1]", "Error: incorrect pathname returned when getting " + "\"auto_reboot\" attr")
def test_overlay_9(self): ''' Add new leaf elem where same-tagged elements are allowed and none exist ''' self.mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) self.mim.load(self.MAIN_XML_FILE, not self.OVERLAY) self.mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) # Software[1] means a first software element was (properly) added. (value, path) = self.mim.get("/auto_install/ai_instance/software") self.assertEquals( path, "/auto_install[1]/ai_instance[1]/software[1]", "Error adding leaf element where like-tagged " + "elements are allowed.")
def check_insertion_order(self): ''' Verify that target, sofware, add_drivers nodes are present and in order ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) ai_instance_node = mim._xpath_search("/auto_install[1]/ai_instance[1]") found_target = found_software = found_add_drivers = False for child in ai_instance_node[0]: if child.tag == "target": self.assertTrue( not found_software and not found_add_drivers, "Target element not added before software " + "or add_drivers elements") found_target = True continue if child.tag == "software": self.assertTrue( found_target and not found_add_drivers, "Software element not added between target " + "and add_drivers elements") found_software = True continue if child.tag == "add_drivers": self.assertTrue( found_target and found_software, "Add_drivers element not added after target " + "and software elements") return self.assertTrue(found_target, "Target element not added") self.assertTrue(found_software, "Software element not added") self.assertTrue(found_add_drivers, "Add_drivers element not added")
def test_overlay_15(self): ''' Interleaves two files with same nodes. Verifies order. ''' values_order = [ "target2", "software1", "software2", "source1", "source2" ] mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) ai_instance_node = mim._xpath_search("/auto_install[1]/ai_instance[1]") values_order_index = 0 for child in ai_instance_node[0]: self.assertEquals(child.text, values_order[values_order_index], ("Child \"%s\" is out of order. " + "Found \"%s\" in its place at index %d.\n") % (child.tag, values_order[values_order_index], values_order_index)) values_order_index += 1 self.assertEquals( values_order_index, len(values_order), "Only %d of %d were found.\n" % (values_order_index, len(values_order)))
def test_overlay_14(self): ''' Verify that add_drivers goes after target. Normally it would go after software, but software is missing and software comes after target. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) ai_instance_node = mim._xpath_search("/auto_install[1]/ai_instance[1]") found_target = found_add_drivers = False for child in ai_instance_node[0]: if child.tag == "target": self.assertTrue( not found_add_drivers, "Target element not added before software " + "or add_drivers elements") found_target = True continue if child.tag == "add_drivers": self.assertTrue( found_target, "Add_drivers element not added after target " + "and software elements") return self.assertTrue(found_target, "Target element not added") self.assertTrue(found_add_drivers, "Add_drivers element not added")
def test_overlay_1(self): ''' Put original manifest together from pieces, and verify it. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE) mim.load(self.TARGET_XML, not self.OVERLAY) mim.load(self.ADD_DRIVER_XML, self.OVERLAY) mim.load(self.SOFTWARE_XML, self.OVERLAY) mim.commit() TestOverlayA.strip_blank_lines(self.AIM_MANIFEST_FILE) # Raises an exception if diff command finds differences from original. Popen.check_call([self.DIFF, self.FULL_XML, self.AIM_MANIFEST_FILE])
class TestOverlay9(TestMIMOverlayCommon): def setUp(self): ''' Create needed files. ''' TestMIMOverlayCommon.setUp(self) self.create_starting_file() with open(self.OVERLAY_XML_FILE, "w") as ovrl_xml: ovrl_xml.write('<auto_install>\n') ovrl_xml.write(' <ai_instance>\n') ovrl_xml.write(' <software/>\n') ovrl_xml.write(' </ai_instance>\n') ovrl_xml.write('</auto_install>\n') def tearDown(self): ''' Destroy files created for these tests. ''' TestMIMOverlayCommon.tearDown(self) self.destroy_starting_file() if os.path.exists(self.OVERLAY_XML_FILE): os.unlink(self.OVERLAY_XML_FILE) def test_overlay_9(self): ''' Add new leaf elem where same-tagged elements are allowed and none exist ''' self.mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) self.mim.load(self.MAIN_XML_FILE, not self.OVERLAY) self.mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) # Software[1] means a first software element was (properly) added. (value, path) = self.mim.get("/auto_install/ai_instance/software") self.assertEquals(path, "/auto_install[1]/ai_instance[1]/software[1]", "Error adding leaf element where like-tagged " + "elements are allowed.")
def test_overlay_14(self): ''' Verify that add_drivers goes after target. Normally it would go after software, but software is missing and software comes after target. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) ai_instance_node = mim._xpath_search("/auto_install[1]/ai_instance[1]") found_target = found_add_drivers = False for child in ai_instance_node[0]: if child.tag == "target": self.assertTrue(not found_add_drivers, "Target element not added before software " + "or add_drivers elements") found_target = True continue if child.tag == "add_drivers": self.assertTrue(found_target, "Add_drivers element not added after target " + "and software elements") return self.assertTrue(found_target, "Target element not added") self.assertTrue(found_add_drivers, "Add_drivers element not added")
def check_insertion_order(self): ''' Verify that target, sofware, add_drivers nodes are present and in order ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) ai_instance_node = mim._xpath_search("/auto_install[1]/ai_instance[1]") found_target = found_software = found_add_drivers = False for child in ai_instance_node[0]: if child.tag == "target": self.assertTrue(not found_software and not found_add_drivers, "Target element not added before software " + "or add_drivers elements") found_target = True continue if child.tag == "software": self.assertTrue(found_target and not found_add_drivers, "Software element not added between target " + "and add_drivers elements") found_software = True continue if child.tag == "add_drivers": self.assertTrue(found_target and found_software, "Add_drivers element not added after target " + "and software elements") return self.assertTrue(found_target, "Target element not added") self.assertTrue(found_software, "Software element not added") self.assertTrue(found_add_drivers, "Add_drivers element not added")
def validate(self, validate_this): ''' Load and perform validation on file given as input. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(validate_this, not self.OVERLAY) mim.validate()
class TestOverlay9(TestMIMOverlayCommon): def setUp(self): ''' Create needed files. ''' TestMIMOverlayCommon.setUp(self) self.create_starting_file() with open(self.OVERLAY_XML_FILE, "w") as ovrl_xml: ovrl_xml.write('<auto_install>\n') ovrl_xml.write(' <ai_instance>\n') ovrl_xml.write(' <software/>\n') ovrl_xml.write(' </ai_instance>\n') ovrl_xml.write('</auto_install>\n') def tearDown(self): ''' Destroy files created for these tests. ''' TestMIMOverlayCommon.tearDown(self) self.destroy_starting_file() if os.path.exists(self.OVERLAY_XML_FILE): os.unlink(self.OVERLAY_XML_FILE) def test_overlay_9(self): ''' Add new leaf elem where same-tagged elements are allowed and none exist ''' self.mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) self.mim.load(self.MAIN_XML_FILE, not self.OVERLAY) self.mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) # Software[1] means a first software element was (properly) added. (value, path) = self.mim.get("/auto_install/ai_instance/software") self.assertEquals( path, "/auto_install[1]/ai_instance[1]/software[1]", "Error adding leaf element where like-tagged " + "elements are allowed.")
def test_overlay_9(self): ''' Add new leaf elem where same-tagged elements are allowed and none exist ''' self.mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) self.mim.load(self.MAIN_XML_FILE, not self.OVERLAY) self.mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) # Software[1] means a first software element was (properly) added. (value, path) = self.mim.get("/auto_install/ai_instance/software") self.assertEquals(path, "/auto_install[1]/ai_instance[1]/software[1]", "Error adding leaf element where like-tagged " + "elements are allowed.")
def test_commit_3(self): ''' Test that file gets written without validation when requested. ''' self.create_small_xml() mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.IN_XML_FILE, not self.OVERLAY) mim.commit(validate=False)
def test_overlay_1(self): ''' Put original manifest together from pieces, and verify it. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE) mim.load(self.TARGET_XML, not self.OVERLAY) mim.load(self.ADD_DRIVER_XML, self.OVERLAY) mim.load(self.SOFTWARE_XML, self.OVERLAY) mim.commit() TestOverlayA.strip_blank_lines(self.AIM_MANIFEST_FILE) # Raises an exception if diff command finds differences from original. Popen.check_call([self.DIFF, self.FULL_XML, self.AIM_MANIFEST_FILE])
def do_test(self): ''' Load main, then overlay and test. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) (value, path) = mim.get("/auto_install/ai_instance@name") self.assertEquals( path, "/auto_install[1]/ai_instance[1]", "Error: incorrect pathname returned when getting " + "\"name\" attribute") self.assertEquals( value, "secondname", "Error changing existing attribute " + "of non-leaf node.")
def do_test(self): ''' Load main, then overlay and test. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) (value, path) = mim.get("/auto_install/ai_instance@name") self.assertEquals(path, "/auto_install[1]/ai_instance[1]", "Error: incorrect pathname returned when getting " + "\"name\" attribute") self.assertEquals(value, "secondname", "Error changing existing attribute " + "of non-leaf node.")
def test_overlay_8(self): ''' Add a new non-leaf element where same-tagged elements are allowed. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) (value, path) = mim.get("/auto_install/ai_instance/" "software/software_data@action") self.assertEquals(value, "install", "Error adding new same-tagged element") # Target[2] means a second element was (properly) added. self.assertEquals( path, "/auto_install[1]/ai_instance[1]/software[2]" + "/software_data[1]", "Error: incorrect pathname returned when getting " + "\"action\" attr.")
def test_overlay_8(self): ''' Add a new non-leaf element where same-tagged elements are allowed. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) (value, path) = mim.get("/auto_install/ai_instance/" "software/software_data@action") self.assertEquals(value, "install", "Error adding new same-tagged element") # Target[2] means a second element was (properly) added. self.assertEquals(path, "/auto_install[1]/ai_instance[1]/software[2]" + "/software_data[1]", "Error: incorrect pathname returned when getting " + "\"action\" attr.")
def test_overlay_4(self): ''' Leaf overlayed where same-tagged elements not allowed ... replaces existing element and any subtree from it. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) # Verify the old element with name attribute is gone. self.assertRaises(milib.MimMatchError, mim.get, "/auto_install/ai_instance@name") # Verify the new element with auto_reboot attribute is present. (value, path) = mim.get("/auto_install/ai_instance@auto_reboot") self.assertEquals(value, "true", "Error adding new element with new attribute") self.assertEquals( path, "/auto_install[1]/ai_instance[1]", "Error: incorrect pathname returned when getting " + "\"auto_reboot\" attribute")
def test_overlay_5(self): ''' Overlay same-tagged non-leaf element with new attr where not allowed. Same-tagged non-leaf elements are not allowed. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) # Verify auto_reboot attribute was added to existing element. (value, path) = mim.get("/auto_install/ai_instance@name") self.assertEquals(value, "firstname", "Error finding original attribute of existing node") self.assertEquals(path, "/auto_install[1]/ai_instance[1]", "Error: incorrect pathname returned when getting" + "\"name\" attr") (value, path) = mim.get("/auto_install/ai_instance@auto_reboot") self.assertEquals(value, "true", "Error adding new attribute to existing node") self.assertEquals(path, "/auto_install[1]/ai_instance[1]", "Error: incorrect pathname returned when getting " + "\"auto_reboot\" attr")
def test_overlay_4(self): ''' Leaf overlayed where same-tagged elements not allowed ... replaces existing element and any subtree from it. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) # Verify the old element with name attribute is gone. self.assertRaises(milib.MimMatchError, mim.get, "/auto_install/ai_instance@name") # Verify the new element with auto_reboot attribute is present. (value, path) = mim.get("/auto_install/ai_instance@auto_reboot") self.assertEquals(value, "true", "Error adding new element with new attribute") self.assertEquals(path, "/auto_install[1]/ai_instance[1]", "Error: incorrect pathname returned when getting " + "\"auto_reboot\" attribute")
def test_overlay_15(self): ''' Interleaves two files with same nodes. Verifies order. ''' values_order = ["target2", "software1", "software2", "source1", "source2"] mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) mim.load(self.MAIN_XML_FILE, not self.OVERLAY) mim.load(self.OVERLAY_XML_FILE, self.OVERLAY) ai_instance_node = mim._xpath_search("/auto_install[1]/ai_instance[1]") values_order_index = 0 for child in ai_instance_node[0]: self.assertEquals(child.text, values_order[values_order_index], ("Child \"%s\" is out of order. " + "Found \"%s\" in its place at index %d.\n") % (child.tag, values_order[values_order_index], values_order_index)) values_order_index += 1 self.assertEquals(values_order_index, len(values_order), "Only %d of %d were found.\n" % (values_order_index, len(values_order)))
def test_commit_1(self): ''' Test proper handling of an empty tree. ''' mim = ManifestInput(self.AIM_MANIFEST_FILE, self.SCHEMA) self.assertRaises(milib.MimEmptyTreeError, mim.commit)
def _do_aimanifest(argv): ''' Main. See usage for argv details. ''' usage_str = usage(argv) if len(argv) <= 1: AIM_LOGGER.error(_("Error: Missing subcommand")) print >> sys.stderr, _("Usage:\n") + usage_str return errno.EINVAL parser = AimOptionParser(usage=usage_str) parser.add_option("-i", "--incremental", dest="is_incremental", default=False, action="store_true", help=_("Do not clear data before adding new data")) parser.add_option("-r", "--return-path", dest="show_path", default=False, action="store_true", help=_("Return unique path to affected node")) (options, args) = parser.parse_args(argv[1:]) len_args = len(args) command = args[0] path = args[1] if (len_args > 1) else None value = args[2] if (len_args > 2) else None cmds_r_option = ["add", "set", "get"] cmds_w_value_arg = ["add", "set"] cmds_wo_value_arg = ["get", "load"] cmds_w_no_args = ["validate"] if ((command in cmds_w_value_arg and (len_args < 3)) or (command in cmds_wo_value_arg and (len_args < 2))): parser.error_w_errno(errno.EINVAL, _("missing argument")) if ((command in cmds_w_value_arg and (len_args > 3)) or (command in cmds_wo_value_arg and (len_args > 2)) or (command in cmds_w_no_args and (len_args > 1))): parser.error_w_errno(errno.EINVAL, _("extra arguments given")) if (command != "load") and options.is_incremental: parser.error_w_errno(errno.EINVAL, _("-i is not applicable for command given")) if command not in cmds_r_option and options.show_path: parser.error_w_errno(errno.EINVAL, _("-r is not applicable for command given")) # Pass AIM_MANIFEST as the output file. try: mim = ManifestInput(os.environ.get("AIM_MANIFEST"), os.environ.get("AIM_DTD")) except (milib.MimError, IOError) as err: return (_handle_error(err)) if (command == "set") or (command == "add"): AIM_LOGGER.info( _("command:%(mcommand)s, path:%(mpath)s, " "value:%(mvalue)s") % { "mcommand": command, "mpath": path, "mvalue": value }) try: if command == "set": path = mim.set(path, value) else: path = mim.add(path, value) mim.commit(NO_VALIDATE) except (milib.MimError, IOError) as err: return (_handle_error(err)) _log_final_status(mim, path) if options.show_path: # Localization not needed here. print path elif command == "get": AIM_LOGGER.info( _("command:%(mcommand)s, path:%(mpath)s") % { "mcommand": command, "mpath": path }) try: (value, path) = mim.get(path) except (milib.MimError, IOError) as err: return (_handle_error(err)) if value is None or not len(value): value = "\"\"" AIM_LOGGER.info( _("successful: returns value:%(mvalue)s, " "path:%(mpath)s") % { "mvalue": value, "mpath": path }) if not options.show_path: path = "" # Localization not needed here. print "%s %s" % (value, path) elif (command == "load"): # path arg holds the filename AIM_LOGGER.info( _("command:%(mcommand)s, " "incremental:%(mincr)s, file:%(mfile)s") % { "mcommand": command, "mincr": str(options.is_incremental), "mfile": path }) try: mim.load(path, options.is_incremental) mim.commit(NO_VALIDATE) except (milib.MimError, IOError) as err: return (_handle_error(err)) _log_final_status(mim, path) elif (command == "validate"): AIM_LOGGER.info(_("Command:%s") % command) try: mim.validate() AIM_LOGGER.info(_("Validation successful")) except (milib.MimError, IOError) as err: return (_handle_error(err)) else: AIM_LOGGER.error(_("Invalid subcommand \"%s\"") % command) print >> sys.stderr, _("Usage:\n") + usage_str return errno.EINVAL return 0 # No errors
def _do_aimanifest(argv): ''' Main. See usage for argv details. ''' usage_str = usage(argv) if len(argv) <= 1: AIM_LOGGER.error(_("Error: Missing subcommand")) print >> sys.stderr, _("Usage:\n") + usage_str return errno.EINVAL parser = AimOptionParser(usage=usage_str) parser.add_option("-i", "--incremental", dest="is_incremental", default=False, action="store_true", help=_("Do not clear data before adding new data")) parser.add_option("-r", "--return-path", dest="show_path", default=False, action="store_true", help=_("Return unique path to affected node")) (options, args) = parser.parse_args(argv[1:]) len_args = len(args) command = args[0] path = args[1] if (len_args > 1) else None value = args[2] if (len_args > 2) else None cmds_r_option = ["add", "set", "get"] cmds_w_value_arg = ["add", "set"] cmds_wo_value_arg = ["get", "load"] cmds_w_no_args = ["validate"] if ((command in cmds_w_value_arg and (len_args < 3)) or (command in cmds_wo_value_arg and (len_args < 2))): parser.error_w_errno(errno.EINVAL, _("missing argument")) if ((command in cmds_w_value_arg and (len_args > 3)) or (command in cmds_wo_value_arg and (len_args > 2)) or (command in cmds_w_no_args and (len_args > 1))): parser.error_w_errno(errno.EINVAL, _("extra arguments given")) if (command != "load") and options.is_incremental: parser.error_w_errno(errno.EINVAL, _("-i is not applicable for command given")) if command not in cmds_r_option and options.show_path: parser.error_w_errno(errno.EINVAL, _("-r is not applicable for command given")) # Pass AIM_MANIFEST as the output file. try: mim = ManifestInput(os.environ.get("AIM_MANIFEST"), os.environ.get("AIM_DTD")) except (milib.MimError, IOError) as err: return (_handle_error(err)) if (command == "set") or (command == "add"): AIM_LOGGER.info(_("command:%(mcommand)s, path:%(mpath)s, " "value:%(mvalue)s") % {"mcommand": command, "mpath": path, "mvalue": value}) try: if command == "set": path = mim.set(path, value) else: path = mim.add(path, value) mim.commit(NO_VALIDATE) except (milib.MimError, IOError) as err: return (_handle_error(err)) _log_final_status(mim, path) if options.show_path: # Localization not needed here. print path elif command == "get": AIM_LOGGER.info(_("command:%(mcommand)s, path:%(mpath)s") % {"mcommand": command, "mpath": path}) try: (value, path) = mim.get(path) except (milib.MimError, IOError) as err: return (_handle_error(err)) if value is None or not len(value): value = "\"\"" AIM_LOGGER.info(_("successful: returns value:%(mvalue)s, " "path:%(mpath)s") % {"mvalue": value, "mpath": path}) if not options.show_path: path = "" # Localization not needed here. print "%s %s" % (value, path) elif (command == "load"): # path arg holds the filename AIM_LOGGER.info(_("command:%(mcommand)s, " "incremental:%(mincr)s, file:%(mfile)s") % {"mcommand": command, "mincr": str(options.is_incremental), "mfile": path}) try: mim.load(path, options.is_incremental) mim.commit(NO_VALIDATE) except (milib.MimError, IOError) as err: return (_handle_error(err)) _log_final_status(mim, path) elif (command == "validate"): AIM_LOGGER.info(_("Command:%s") % command) try: mim.validate() AIM_LOGGER.info(_("Validation successful")) except (milib.MimError, IOError) as err: return (_handle_error(err)) else: AIM_LOGGER.error(_("Invalid subcommand \"%s\"") % command) print >> sys.stderr, _("Usage:\n") + usage_str return errno.EINVAL return 0 # No errors