-
Notifications
You must be signed in to change notification settings - Fork 1
/
dnf-count-rpm-words.py
executable file
·216 lines (199 loc) · 7.88 KB
/
dnf-count-rpm-words.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
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python3
import dnf
from collections import Counter, defaultdict
# A dictionary of known "words" from RPM names.
#
# Partly this is useful to generate nosplit_words, so "gnome-shell" doesn't get
# broken into "gnome" and "shell".
# It also gets used to guess the "meaning" of each part of the name.
#
# NOTE: These should be listed from least to most specific categories, so when
# we make the all_words reverse-lookup dict the result for, say, "gtk" is
# "framework" instead of just "project name".
known_words = {
"project name": [
"texlive", "eclipse", "glibc", "libreoffice", "drupal7", "pcp", "vim",
"globus", "root", "coin-or", "asterisk", "uwsgi", "gimp", "gcc",
"qemu", "boost", "tesseract", "gallery2", "nagios", "maven",
"jboss", "apache-commons", "glassfish", "jetty", "collectd", "kde",
"kf5", "qt5", "qt", "plasma", "gnome", "gnome-shell", "sugar", "mate",
"lxqt", "xfce4", "hunspell", "aspell", "autocorr", "fawkes",
"openrdf-sesame", "google-noto", "fence", "jenkins",
"emacs", "ibus", "derelict", "django", "qpid", "libvirt", "lodash",
"matreshka", "plexus", "shrinkwrap", "soletta", "springframework",
"trytond", "oslo", "pulp", "geany", "NetworkManager", "yum", "felix",
"fusionforge", "opensips", "jackson", "vdsm", "horde", "sblim",
"gcompris", "glite", "arquillian", "geronimo", "binutils",
"asterisk-sounds-core", "xorg-x11", "xorg", "man-pages",
"libjpeg-turbo", "util-linux", "xkb-utils", "gnome-getting-started",
"subscription-manager", "virt-manager", "gtk", "gtk2", "gtk3",
"aws-sdk",
],
# FUTURE: could get clever and use this to merge variant words?
"variants": {
"python": ["python2", "python3"],
"mingw": ["mingw32", "mingw64"],
"gtk": ["gtk2", "gtk3"],
"qt": ["qt4", "qt5"],
},
"programming language": [
"perl", "python", "python2", "python3", "js", "ruby",
"rubygem", "php", "go", "ocaml", "R", "erlang", "rust", "java", "lua",
"tcl", "ghc", "ocaml", "mono", "gambas3", "octave", "gap", "c++",
"cpp", "c", "sharp",
],
# FUTURE: could get clever and ignore "words" after a packaging prefix
# (until we hit a known suffix...)
"package prefix": [
"perl", "php-pear", "php-pecl", "gap-pkg", "rubygem", "golang",
"nodejs", "ghc", "XStatic",
],
"vendor": [
"google", "fedora", "redhat", "hashicorp",
],
"framework": [
"gtk", "gtk2", "gtk3", "glib", "dbus",
"qt", "qt4", "qt5", "kf5", "plasma",
"ibus", "boost", "lodash", "grunt", "django",
"openmpi", "mpich",
"sqlite", "mysql", "pgsql", "postgresql",
"selinux", "device-mapper", "git", "docker",
"syntastic", "zendframework", "zend", "gl", "flask",
"maven", "trac", "nautilus", "lv2",
],
"protocol": [
"http", "ldap", "x11", "ssh",
],
"data format": [
"json", "yaml", "xml", "el", "latex", "html", "HTML", "XML", "text",
],
"environment": [
"gnome", "kde", "mate", "lxde", "lxqt", "xfce", "xfce4", "sugar",
],
"build-variant": [
"mingw32", "mingw64", "python2", "python3",
"compat", "static", "debug", "linux-gnu",
],
"file-type": [
"devel", "doc", "docs", "javadoc", "fonts", "libs", "bin", "data",
"info", "parent", "theme", "lib", "config", "filesystem", "firmware",
"macros", "backgrounds", "bundle", "icon", "rpm-macros", "srpm-macros",
"source", "mythes",
],
"font-type": [
"sans", "serif",
],
"file-purpose": [
"tests", "test", "unit-test", "unit-tests", "client", "server",
"tools", "examples", "cli", "gui", "daemon", "util", "utils",
"daemon", "manual", "api", "demo", "web", "console", "log",
"agent", "manager", "parser", "runtime", "cache", "bridge",
"session-manager", "power-manager", "font-manager", "help",
"agents", "proxy", "desktop", "sounds",
],
# FUTURE: could get clever and use the (singular, plural) pairings
"extends/enhances": [
"plugin", "plugins",
"plug-in", "plug-ins",
"extension", "extensions",
"provider", "providers",
"module", "modules",
"driver", "drivers",
"addon", "addons",
],
"core/extras": [
"core", "common", "base", "extra", "extras", "others", "contrib",
],
"release name": [
"spherical-cow", "beefy-miracle", "schroedinger-cat",
],
"concept": [
"file", "system", "stream", "net", "map", "path", "app",
],
"translation": [
"langpack", "langpacks", "i18n", "l10n",
"fr", "ru", "it", "es", "de", "en", "ja", "pl", "cs", "gb",
"utf-8", "KOI8-R",
],
}
all_words = {w:desc for desc,words in known_words.items() for w in words}
nosplit_words = set(w for w in all_words if '-' in w)
def wordsplit(rpmname):
# empty string
if not rpmname:
return []
# nothing to split
if '-' not in rpmname or rpmname in nosplit_words:
return [rpmname]
# check for nosplit_words
for w in nosplit_words:
p = w+'-'
if rpmname.startswith(p):
rest = rpmname[len(p):]
return [w] + wordsplit(rest)
# otherwise just split on dashes
w, rest = rpmname.split('-', 1)
return [w] + wordsplit(rest)
# --- Okay, ready to start doing stuff? Here we go!
print("fetching DNF metadata..")
b = dnf.Base()
b.read_all_repos()
b.repos.all().disable()
b.repos['fedora'].enable()
b.repos['updates'].enable()
sack = b.fill_sack(load_system_repo=False)
print("analyzing package names..")
names = dict()
subpkgs = defaultdict(list)
namewords = list()
wordnames = defaultdict(set)
for pkg in sack.query():
if pkg.name in names:
continue
name = pkg.name
src = pkg.source_name
words = wordsplit(name)
# name -> src
names[name] = src
# src -> [name, name, ...]
subpkgs[src].append(name)
# [[word, word, ...], ...]
namewords.append(words)
# word -> [name, name, ...]
for w in words:
wordnames[w].add(name)
# Count raw number of uses for every word in every package name
words = Counter(w for nw in namewords for w in nw)
prefixes = Counter(nw[0] for nw in namewords if len(nw) > 1)
suffixes = Counter(nw[-1] for nw in namewords if len(nw) > 1)
# How many different source packages - i.e. spec files - use each word?
# (this reduces noise from things like texlive...)
srcwords = Counter({w:len(set(names[n] for n in wordnames[w])) for w in words})
# A wordlist that includes every word in the top 100 of each category
commonwords = set(w for cat in (words, srcwords, prefixes, suffixes)
for w,c in cat.most_common(100))
# bonus stuff for finding words like "coin-or"
def iterpairs(iterable):
i = iter(iterable)
left = next(i)
for right in i:
yield left, right
left = right
pairs = Counter(p for nw in namewords for p in iterpairs(nw))
def dumpcsv(fobj, wordlist, key=srcwords.get):
fobj.write("word,specfiles,rpms,as prefix,as suffix,meaning\n")
for word in sorted(wordlist, key=key, reverse=True):
fobj.write("{},{},{},{},{},{}\n".format(word,
srcwords.get(word, 0),
words.get(word, 0),
prefixes.get(word, 0),
suffixes.get(word, 0),
all_words.get(word, '')))
# run with "ipython3 -i dnf-count-rpm-words.py" to do interactive exploration!
if __name__ == '__main__':
with open("rpm-name-word-counts.csv", 'wt') as fobj:
print("writing {}...".format(fobj.name))
dumpcsv(fobj, words)
with open("rpm-name-word-counts-common.csv", 'wt') as fobj:
print("writing {}...".format(fobj.name))
dumpcsv(fobj, commonwords)