New version.
[pykickstart.git] / pykickstart / commands / sshpw.py
blob67feec5141f5423abab8f7cfbbfa287709aa9082
2 # Peter Jones <pjones@redhat.com>
4 # Copyright 2009 Red Hat, Inc.
6 # This copyrighted material is made available to anyone wishing to use, modify,
7 # copy, or redistribute it subject to the terms and conditions of the GNU
8 # General Public License v.2. This program is distributed in the hope that it
9 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
10 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
16 # trademarks that are incorporated in the source code or documentation are not
17 # subject to the GNU General Public License and may only be used or replicated
18 # with the express permission of Red Hat, Inc.
20 from pykickstart.base import BaseData, KickstartCommand
21 from pykickstart.errors import KickstartValueError, formatErrorMsg
22 from pykickstart.options import KSOptionParser
23 import warnings
25 import gettext
26 _ = lambda x: gettext.ldgettext("pykickstart", x)
28 class F13_SshPwData(BaseData):
29 removedKeywords = BaseData.removedKeywords
30 removedAttrs = BaseData.removedAttrs
32 def __init__(self, *args, **kwargs):
33 BaseData.__init__(self, *args, **kwargs)
34 self.username = kwargs.get("username", None)
35 self.isCrypted = kwargs.get("isCrypted", False)
36 self.password = kwargs.get("password", "")
37 self.lock = kwargs.get("lock", False)
39 def __eq__(self, y):
40 if not y:
41 return False
43 return self.username == y.username
45 def __ne__(self, y):
46 return not self == y
48 def __str__(self):
49 retval = BaseData.__str__(self)
51 retval += "sshpw"
52 retval += self._getArgsAsStr() + '\n'
54 return retval
56 def _getArgsAsStr(self):
57 retval = ""
59 retval += " --username=%s" % self.username
60 if self.lock:
61 retval += " --lock"
62 if self.isCrypted:
63 retval += " --iscrypted"
64 else:
65 retval += " --plaintext"
67 retval += " %s" % self.password
68 return retval
70 class F13_SshPw(KickstartCommand):
71 removedKeywords = KickstartCommand.removedKeywords
72 removedAttrs = KickstartCommand.removedAttrs
74 def __init__(self, writePriority=0, *args, **kwargs):
75 KickstartCommand.__init__(self, writePriority, *args, **kwargs)
76 self.op = self._getParser()
78 self.sshUserList = kwargs.get("sshUserList", [])
80 def __str__(self):
81 retval = ""
82 for user in self.sshUserList:
83 retval += user.__str__()
85 return retval
87 def _getParser(self):
88 op = KSOptionParser()
89 op.add_option("--username", dest="username", required=True)
90 op.add_option("--iscrypted", dest="isCrypted", action="store_true",
91 default=False)
92 op.add_option("--plaintext", dest="isCrypted", action="store_false")
93 op.add_option("--lock", dest="lock", action="store_true", default=False)
94 return op
96 def parse(self, args):
97 ud = self.handler.SshPwData()
98 (opts, extra) = self.op.parse_args(args=args, lineno=self.lineno)
99 self._setToObj(self.op, opts, ud)
100 ud.lineno = self.lineno
102 if len(extra) != 1:
103 raise KickstartValueError(formatErrorMsg(self.lineno, msg=_("A single argument is expected for the %s command") % "sshpw"))
104 ud.password = extra[0]
106 if ud in self.dataList():
107 warnings.warn(_("An ssh user with the name %s has already been defined.") % ud.name)
109 return ud
111 def dataList(self):
112 return self.sshUserList