189dc7585fcf56fe268f91a350bbaed9100e6649
[platform/upstream/mic.git] / mic / pykickstart / commands / user.py
1 #
2 # Chris Lumens <clumens@redhat.com>
3 #
4 # Copyright 2007 Red Hat, Inc.
5 #
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.
12 #
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. 
19 #
20 from pykickstart.base import *
21 from pykickstart.constants import *
22 from pykickstart.errors import *
23 from pykickstart.options import *
24
25 import gettext
26 import warnings
27 _ = lambda x: gettext.ldgettext("pykickstart", x)
28
29 class FC6_UserData(BaseData):
30     removedKeywords = BaseData.removedKeywords
31     removedAttrs = BaseData.removedAttrs
32
33     def __init__(self, *args, **kwargs):
34         BaseData.__init__(self, *args, **kwargs)
35         self.groups = kwargs.get("groups", [])
36         self.homedir = kwargs.get("homedir", "")
37         self.isCrypted = kwargs.get("isCrypted", False)
38         self.name = kwargs.get("name", "")
39         self.password = kwargs.get("password", "")
40         self.shell = kwargs.get("shell", "")
41         self.uid = kwargs.get("uid", None)
42
43     def __eq__(self, y):
44         return self.name == y.name
45
46     def __str__(self):
47         retval = BaseData.__str__(self)
48
49         if self.uid != "":
50             retval += "user"
51             retval += self._getArgsAsStr() + "\n"
52
53         return retval
54
55     def _getArgsAsStr(self):
56         retval = ""
57
58         if len(self.groups) > 0:
59             retval += " --groups=%s" % ",".join(self.groups)
60         if self.homedir:
61             retval += " --homedir=%s" % self.homedir
62         if self.name:
63             retval += " --name=%s" % self.name
64         if self.password:
65             retval += " --password=%s" % self.password
66         if self.isCrypted:
67             retval += " --iscrypted"
68         if self.shell:
69             retval += " --shell=%s" % self.shell
70         if self.uid:
71             retval += " --uid=%s" % self.uid
72
73         return retval
74
75 class F8_UserData(FC6_UserData):
76     removedKeywords = FC6_UserData.removedKeywords
77     removedAttrs = FC6_UserData.removedAttrs
78
79     def __init__(self, *args, **kwargs):
80         FC6_UserData.__init__(self, *args, **kwargs)
81         self.lock = kwargs.get("lock", False)
82
83     def _getArgsAsStr(self):
84         retval = FC6_UserData._getArgsAsStr(self)
85
86         if self.lock:
87             retval += " --lock"
88
89         return retval
90
91 class F12_UserData(F8_UserData):
92     removedKeywords = F8_UserData.removedKeywords
93     removedAttrs = F8_UserData.removedAttrs
94
95     def __init__(self, *args, **kwargs):
96         F8_UserData.__init__(self, *args, **kwargs)
97         self.gecos = kwargs.get("gecos", "")
98
99     def _getArgsAsStr(self):
100         retval = F8_UserData._getArgsAsStr(self)
101
102         if self.gecos:
103             retval += " --gecos=\"%s\"" % (self.gecos,)
104
105         return retval
106
107 class FC6_User(KickstartCommand):
108     removedKeywords = KickstartCommand.removedKeywords
109     removedAttrs = KickstartCommand.removedAttrs
110
111     def __init__(self, writePriority=0, *args, **kwargs):
112         KickstartCommand.__init__(self, writePriority, *args, **kwargs)
113         self.op = self._getParser()
114
115         self.userList = kwargs.get("userList", [])
116
117     def __str__(self):
118         retval = ""
119         for user in self.userList:
120             retval += user.__str__()
121
122         return retval
123
124     def _getParser(self):
125         def groups_cb (option, opt_str, value, parser):
126             for d in value.split(','):
127                 parser.values.ensure_value(option.dest, []).append(d)
128
129         op = KSOptionParser()
130         op.add_option("--groups", dest="groups", action="callback",
131                       callback=groups_cb, nargs=1, type="string")
132         op.add_option("--homedir")
133         op.add_option("--iscrypted", dest="isCrypted", action="store_true",
134                       default=False)
135         op.add_option("--name", required=1)
136         op.add_option("--password")
137         op.add_option("--shell")
138         op.add_option("--uid", type="int")
139         return op
140
141     def parse(self, args):
142         ud = self.handler.UserData()
143         (opts, extra) = self.op.parse_args(args=args, lineno=self.lineno)
144         self._setToObj(self.op, opts, ud)
145         ud.lineno = self.lineno
146
147         # Check for duplicates in the data list.
148         if ud in self.dataList():
149             warnings.warn(_("A user with the name %s has already been defined.") % ud.name)
150
151         return ud
152
153     def dataList(self):
154         return self.userList
155
156 class F8_User(FC6_User):
157     removedKeywords = FC6_User.removedKeywords
158     removedAttrs = FC6_User.removedAttrs
159
160     def _getParser(self):
161         op = FC6_User._getParser(self)
162         op.add_option("--lock", action="store_true", default=False)
163         op.add_option("--plaintext", dest="isCrypted", action="store_false")
164         return op
165
166 class F12_User(F8_User):
167     removedKeywords = F8_User.removedKeywords
168     removedAttrs = F8_User.removedAttrs
169
170     def _getParser(self):
171         op = F8_User._getParser(self)
172         op.add_option("--gecos", type="string")
173         return op