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
|
#!/usr/bin/python
#-*- encoding: utf-8 -*-
"""
File name: convert_ks.py
Python Version: 2.7.3
Licensed under the terms of the GNU GPL License version 2
"""
import tempfile
import shutil
import argparse
from lxml import etree
def write_package(convert_ks, xmlfile, groups, extra_pkgs):
"""
Arguments:
- convert_ks: temporary file for converting
- xmlfile: group.xml from 'package-groups'
- groups: Group list from original ks file
- extra_pkgs: ExtraPackages and RemovePackages from original ks file
Function:
- convert group name to rpm list and write to ks file
getting from xmlfile has group information.
Return value:
- True: success
- False: fail caused by KeyError
"""
xmltree = etree.parse(xmlfile)
root = xmltree.getroot()
group_info = dict()
for group in root:
pkg_list = list()
for pkgname in group.find('packagelist').findall('packagereq'):
pkg_list.append(pkgname.text)
group_info[group.find('name').text] = pkg_list
for group in groups:
try:
convert_ks.write('# @ ' + group + '\n')
for pkg in sorted(group_info[group]):
convert_ks.write(pkg + '\n')
except KeyError:
print '\nin convert_ks.py: '
print 'error: Could not find @ ' + group + ' in package-groups\n'
return False
convert_ks.write('# Others\n')
for pkg in extra_pkgs:
convert_ks.write(pkg + '\n')
return True
def main(origin_file, group_file):
"""
Arguments:
- origin_file: *.ks file wrote by Groups name
- group_file: group.xml from 'package-groups'
Function:
- Read origin ks file and write to temp file line by line.
In pkg group session, call write_package() to convert
group name to rpm file list.
"""
origin_ks = file(origin_file, 'r')
(file_flag, convert_file) = tempfile.mkstemp()
convert_ks = file(convert_file, 'w') #temporary file for converting
groups = list()
extra_pkgs = list()
pkg_session = False
for line in origin_ks.readlines():
line_plaintext = line.strip()
if not pkg_session and line_plaintext == "%packages":
convert_ks.write(line + '\n')
pkg_session = True
elif pkg_session and line_plaintext == "%end":
# write_package writes packages' list when pkg_session ends
if not write_package(convert_ks, group_file, groups, extra_pkgs):
# KeyError in write_package: close files and exit with error
origin_ks.close()
convert_ks.close()
exit(1)
convert_ks.write('\n')
pkg_session = False
if pkg_session:
if line_plaintext.startswith('@'): # Read Group
groups.append(line_plaintext[1:])
elif not line_plaintext.startswith('%'): # Read Extra/RemovePackage
extra_pkgs.append(line_plaintext)
else:
convert_ks.write(line)
origin_ks.close()
convert_ks.close()
shutil.move(convert_file, origin_file)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='''
Replace group names with list of packages they contain.
''',
)
parser.add_argument('ksfilename', type=str, help='ks file to modify')
parser.add_argument('pkggroup', type=str,
help='group.xml file from package-groups package')
args = parser.parse_args()
main(args.ksfilename, args.pkggroup)
|