You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.4 KiB

  1. #-*- coding: utf-8 -*-
  2. # Copyright 2010 Calculate Ltd. http://www.calculate-linux.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from cl_ldap_service import ldapService, __app__, __version__
  16. from cl_opt import opt
  17. import sys
  18. from cl_share_cmd import share_cmd
  19. # Перевод сообщений для программы
  20. from cl_lang import lang
  21. lang().setLanguage(sys.modules[__name__])
  22. # Использование программы
  23. USAGE = _("%prog [options]")
  24. # Коментарии к использованию программы
  25. COMMENT_EXAMPLES = _("Create LDAP tree")
  26. # Пример использования программы
  27. EXAMPLES = _("%prog")
  28. # Описание программы (что делает программа)
  29. DESCRIPTION = _("Creating LDAP tree")
  30. # Опции командной строки
  31. CMD_OPTIONS = [{'shortOption':"f",
  32. 'longOption':"force",
  33. 'help':_("forced setup service ldap")},
  34. {'longOption':"install",
  35. 'help':_("configure the system to install this package")},
  36. {'longOption':"uninstall",
  37. 'help':_("configure the system to uninstall this package")}]
  38. class ldap_setup_cmd(share_cmd):
  39. def __init__(self):
  40. # Объект опций командной строки
  41. self.optobj = opt(package=__app__,
  42. version=__version__,
  43. usage=USAGE,
  44. examples=EXAMPLES,
  45. comment_examples=COMMENT_EXAMPLES,
  46. description=DESCRIPTION,
  47. option_list=CMD_OPTIONS +\
  48. opt.variable_control+opt.color_control,
  49. check_values=self.checkOpts)
  50. # Создаем объект логики
  51. self.logicObj = ldapService()
  52. # Создаем переменные
  53. self.logicObj.createClVars()
  54. # Названия несовместимых опций
  55. self.optionsNamesIncompatible = ["install", "uninstall", "f"]
  56. def getIncompatibleOptions(self, optObj):
  57. """Получаем несовместимые опции"""
  58. retList = []
  59. for nameOpt in self.optionsNamesIncompatible:
  60. retList.append(getattr(optObj, nameOpt))
  61. return retList
  62. def _getNamesAllSetOptions(self):
  63. """Выдает словарь измененных опций"""
  64. setOptDict = self.optobj.values.__dict__.items()
  65. defaultOptDict = self.optobj.get_default_values().__dict__.items()
  66. return dict(set(setOptDict) - set(defaultOptDict)).keys()
  67. def getStringIncompatibleOptions(self):
  68. """Форматированная строка несовместимых опций разделенных ','"""
  69. listOpt = list(set(self.optionsNamesIncompatible) &\
  70. set(self._getNamesAllSetOptions()))
  71. return ", ".join(map(lambda x: len(x) == 1 and "'-%s'"%x or "'--%s'"%x,\
  72. listOpt))
  73. def checkOpts(self, optObj, args):
  74. """Check command line opt and arg"""
  75. if len(filter(lambda x: x, self.getIncompatibleOptions(optObj)))>1:
  76. errMsg = _("incompatible options")+":"+" %s"\
  77. %self.getStringIncompatibleOptions()
  78. self.optobj.error(errMsg)
  79. return False
  80. if args:
  81. errMsg = _("incorrect argument") + ":" + " %s" %" ".join(args)
  82. self.optobj.error(errMsg)
  83. return False
  84. return optObj, args
  85. def setup(self, force=False):
  86. """Setup program"""
  87. return self.logicObj.setup(force=force)
  88. def install(self):
  89. """Инсталяция программы"""
  90. return self.logicObj.installProg()
  91. def uninstall(self):
  92. """Удаление программы"""
  93. return self.logicObj.uninstallProg()