-
Notifications
You must be signed in to change notification settings - Fork 2
/
pkglist.py
206 lines (173 loc) · 6.65 KB
/
pkglist.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/python
# pkglist.py - get a repository's metadata, the actual list of packages
import os
import re
import sys
from lxml import etree
from version import Version
from lfs.checksum import Checksum
#-------------------------------------------------------------------------------
# PkgTime -
#-------------------------------------------------------------------------------
class PkgTime():
def __init__(self, file, build):
self.file = file
self.build = build
def __str__(self):
return f'time: file={self.file}, build={self.build}\n'
def to_csv(self):
return f'{self.file}\t{self.build}'
@classmethod
def csv_header(cls):
return f'file\tbuild'
#-------------------------------------------------------------------------------
# Size -
#-------------------------------------------------------------------------------
class Size():
def __init__(self, package, archive, installed):
self.package = package
self.archive = archive
self.installed = installed
def __str__(self):
return (f'size: package={self.package}, archive={self.archive}'
+ f', installed={self.installed}\n')
def to_csv(self):
return f'{self.package}\t{self.archive}\t{self.installed}'
@classmethod
def csv_header(cls):
return f'package\tarchive\tinstalled'
#-------------------------------------------------------------------------------
# Pkg -
#-------------------------------------------------------------------------------
class Pkg():
def __init__(self, type, name, arch, version, checksum, summary, description,
packager, url, pkg_time, size, location, format):
self.type = type
self.name = name
self.arch = arch
self.version = version
self.checksum = checksum
self.summary = summary
self.description = description
self.packager = packager
self.url = url
self.pkg_time = pkg_time
self.size = size
self.location = location
self.format = format
def __str__(self):
s = f'{self.name}\n'
s += f' type: {self.type}\n'
s += f' arch: {self.arch}\n'
s += f' {str(self.version)}'
s += f' {str(self.checksum)}'
s += f' summary: {self.summary}\n'
s += f' description: {self.description}\n'
s += f' packager: {self.packager}\n'
s += f' url: {self.url}\n'
s += f' {str(self.pkg_time)}'
s += f' {str(self.size)}'
s += f' location: {self.location}\n'
s += '\n'
return s
def to_csv(self):
# Don't put multiline values in cells
x = self.description.split('\n', maxsplit=1)[0] if self.description else ''
s = f'{self.name}\t{self.type}\t{self.arch}'
s += f'\t{self.version.to_csv()}'
s += f'\t{self.checksum.to_csv()}'
# s += f'\t{self.summary}\t{self.description}\t{self.packager}\t{self.url}'
s += f'\t{self.summary}\t{x}\t{self.packager}\t{self.url}'
s += f'\t{self.pkg_time.to_csv()}\t{self.size.to_csv()}'
s += f'\t{self.location}'
return s
@classmethod
def csv_header(cls):
s = f'name\ttype\tarch'
s += f'\t{Version.csv_header()}'
s += f'\t{Checksum.csv_header()}'
s += f'\tsummary\tdescription\tpackager\turl'
s += f'\t{PkgTime.csv_header()}\t{Size.csv_header()}'
s += f'\tlocation'
return s
#-------------------------------------------------------------------------------
# PkgList - metalinks for repository metadata access
#-------------------------------------------------------------------------------
class PkgList():
def __init__(self):
self.packages = []
def __str__(self):
s = ''
for p in self.packages:
s += f'{p}'
return s
def to_csv(self, filepath):
with open(filepath, 'w', encoding='utf-8') as f:
f.write(f'{Pkg.csv_header()}\n')
for p in self.packages:
f.write(f'{p.to_csv()}\n')
def handle_pkg(nd):
type = nd.attrib['type']
for k in nd:
tag = etree.QName(k.tag).localname
# Store in local variables during the 'for' loop
if tag == 'name':
name = k.text
if tag == 'arch':
arch = k.text
if tag == 'version':
version = Version(k.attrib['epoch'], k.attrib['ver'], k.attrib['rel'])
if tag == 'checksum':
checksum = Checksum(k.attrib['type'], k.text, pkgid=k.attrib['pkgid'])
elif tag == 'summary':
summary = k.text
elif tag == 'description':
description = k.text
elif tag == 'packager':
packager = k.text
elif tag == 'url':
url = k.text
elif tag == 'time':
pkg_time = PkgTime(k.attrib['file'], k.attrib['build'])
elif tag == 'size':
size = Size(k.attrib['package'], k.attrib['archive'],
k.attrib['installed'])
elif tag == 'location':
location = k.attrib['href']
p = Pkg(type, name, arch, version, checksum, summary, description,
packager, url, pkg_time, size, location, None)
return p
def parse_root(nd):
pl= PkgList()
for k in nd:
tag = etree.QName(k.tag).localname
if tag == 'package':
p = PkgList.handle_pkg(k)
pl.packages.append(p)
return pl
@classmethod
def from_file(cls, filepath):
"""Return a PkgList instance from a primary.xml file."""
root = etree.parse(filepath).getroot()
return PkgList.parse_root(root)
#===============================================================================
# main
#===============================================================================
if __name__ == '__main__':
# print("""This module is not meant to run directly.""")
if len(sys.argv) != 2:
print(f'Usage: {sys.argv[0]} <filepath>')
exit(-1)
filepath = sys.argv[1]
print(f'Parsing file "{filepath}"')
pl = PkgList.from_file(filepath)
print(f'Parsing done, found {len(pl.packages)} packages.')
# # Print out a text output
# print(f'String to be printed is {len(str(pl))} bytes long.')
# filename = 'toto.txt'
# print(f'Printing to file {filename}')
# with open(filename, 'w', encoding='utf-8') as f:
# f.write(str(pl))
# Create a .csv file
print(f'Creating file toto.txt')
pl.to_csv('toto.txt')