summaryrefslogtreecommitdiffstats
path: root/base/deploy/src/scriptlets/pkimanifest.py
blob: dfd18fbafd92134b3ad90df2bc2dacd65c612119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/python -t
# Authors:
#     Matthew Harmsen <mharmsen@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2012 Red Hat, Inc.
# All rights reserved.
#

# System Imports
from collections import namedtuple
import csv


# PKI Deployment Manifest Constants
RECORD_TYPE_DIRECTORY = "directory"
RECORD_TYPE_FILE = "file"
RECORD_TYPE_SYMLINK = "symlink"


# PKI Deployment Manifest Record Class
class record(object):
    __slots__= "name",\
               "type",\
               "user",\
               "group",\
               "uid",\
               "gid",\
               "permissions",\
               "acls",

    def items(self):
        "dict style items"
        return [
            (field_name, getattr(self, field_name))
            for field_name in self.__slots__]

    def __iter__(self):
        "iterate over fields tuple/list style"
        for field_name in self.__slots__:
            yield getattr(self, field_name)

    def __getitem__(self, index):
        "tuple/list style getitem"
        return getattr(self, self.__slots__[index])


# PKI Deployment Manifest File Class
class file:
    """FUTURE:  Consider creating a single manifest file
       that is always overwritten."""
    global database
    filename = None

    def register(self, name):
        self.filename = name

    def write(self):
        try:
            fd = open(self.filename, "wt")
            c = csv.writer(fd)
            for record in database:
                c.writerow(tuple(record))
            fd.close()
        except IOError as exc:
            config.pki_log.error(log.PKI_IOERROR_1, exc,
                                 extra=config.PKI_INDENTATION_LEVEL_1)
            sys.exit(1)

    def read(self):
        try:
            fd = open(self.filename, "rt")
            cr = csv.reader(fd)
            for row in cr:
                print tuple(row)
            fd.close()
        except IOError as exc:
            config.pki_log.error(log.PKI_IOERROR_1, exc,
                                 extra=config.PKI_INDENTATION_LEVEL_1)
            sys.exit(1)


# PKI Deployment Global Named Tuples
database = []
file = file()