summaryrefslogtreecommitdiffstats
path: root/sbin/kgen
blob: fa01d5468fcd42b6f50d1c46f7b812d8c5b6c0e7 (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/opt/sunpy3/bin/python3
#coding:utf-8
import os,sys
import getopt
import sunhpc.invoke
from xml.sax._exceptions import SAXParseException
class App(sunhpc.core.database.ApplicationSQL):

    def __init__(self, argv=None):
        sunhpc.core.database.ApplicationSQL.__init__(self)

        if not argv:
            argv = sys.argv

        self.args           = []
        self.caller_args    = argv[1:]
        self.usage_name     = 'Kickstart Generator'
        self.usage_version  = '1.0'
        self.usage_command  = os.path.basename(argv[0])
        self.sections       = []

        self.os         = os.uname()[0].lower()
        self.arch       = os.uname()[4]
        osGenerator     = getattr(sunhpc.core.xmlgen, 'Generator_%s' % self.os)
        self.generator  = osGenerator()
        self.generator.setArch(self.arch)
        self.generator.setOS(self.os)

        self.getopt     = sunhpc.core.utils.Struct()
        self.getopt.s   = [('h', 'help infomation'), ('a', 'architecture')]
        self.getopt.l   = [('arch=', 'architecture'),
                           ('section=', 'name'),
                           ('postonly', 'show post'),
                          ]
    def usage(self):
        argDict = {}
        for e in self.getopt.s:
            if type(e) == type(()):
                argDict['-%s' % e[0]] = e[1]
            else:
                argDict['-%s' % e] = ''

        for l in self.getopt.l:
            if type(l) == type(()):
                argDict['--%s' % l[0]] = l[1]
            else:
                argDict['--%s' % l] = ''

        if not argDict: return
        maxlen = max(map(len, argDict.keys()))
        print ('\nUsage: %s [options] command infomations' % self.usage_command)
        for k in argDict:
            keys = k.ljust(maxlen)
            vals = argDict[k]
            print ('  %s\t%s' % (keys, vals))
        print ('If you have any questions, please contact info@sunhpc.com')

    def parseArg(self, c):
        if c[0] in ('-h', '--help'):
            self.usage()
            sys.exit(-1)
        elif c[0] in ('-a', '--arch'):
            self.generator.setArch(c[1])
        elif c[0] == '--section':
            self.sections += c[1].split()
        elif c[0] == '--postonly':
            self.sections.append('post')
        else:
            return 0
        return 1

    def parseArgs(self):
        self.parseCommandLine()

    def parseCommandLine(self):
        # 解析短参数形式
        short = ''
        for e in self.getopt.s:
            if type(e) == type(()):
                # 取参数左值
                short = short + e[0]
            else:
                short = short + e

        # 解析长参数形式
        long = []
        for e in self.getopt.l:
            if type(e) == type(()):
                # 取参数左值
                long.append(e[0])
            else:
                long.append(e)
        try:
            opts, args = getopt.getopt(self.caller_args, short, long)
        except getopt.GetoptError as msg:
            sys.stderr.write('error - %s\n' % msg)
            self.usage()
            sys.exit(1)

        for c in opts:
            self.parseArg(c)

    def run(self):

        if self.args:
            fe = open(self.args[0], 'r')
        else:
            fe = sys.stdin

        self.generator.parse(fe.read())
        print ('#')
        print ('# %s version %s' % (self.usage_name, self.usage_version))
        print ('#')

        sections = self.sections
        if not sections:
            sections = ['order', 'debug', 'main', 'packages', 'pre', 'post']

        plist = []
        for s in sections:
            plist += self.generator.generate(s)

        for line in plist:
            print (line.rstrip())

if __name__ == "__main__":
    app = App(sys.argv)
    app.parseArgs()
    try:
        app.run()
    except sunhpc.core.exceptions.KickstartError as msg:
        sys.stderr.write("kgen error - %s\n" % msg)
        sys.exit(-1)

    except SAXParseException as msg:
        sys.stderr.write("kgen XML parse exception: %s\n" % msg)
        sys.exit(-1)