-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin-gallery-generator
executable file
·1507 lines (1390 loc) · 55.7 KB
/
plugin-gallery-generator
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
The plugin gallery generator retrieves Munin plugins from configured sources.
The plugins are parsed and relevant meta data is extracted (programming language, capabilities,
graph categories).
The plugin data is used for generating a static website via the static website generator "hugo".
The default configuration supplied with this generator (siehe "config.yml")
is used for the Munin Plugin Gallery (https://gallery.munin-monitoring.org/).
Copyright 2020, Lars Kruse <[email protected]>
SPDX-License-Identifier: GPL-3.0-or-later
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, either version 3 of the License, or
(at your option) any later version.
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, see <https://www.gnu.org/licenses/>.
"""
import argparse
import asyncio
import asyncio.subprocess
import collections
import datetime
import enum
import json
import logging
import multiprocessing
import os
import pathlib
import re
import shutil
import sys
import tempfile
import time
import urllib.request
import aiohttp
import yaml
EXAMPLE_GRAPH_DIRECTORY_NAME = "example-graphs"
INDEXING_IGNORE_WORDS_FILE = os.path.join(
os.path.dirname(__file__), "indexing-ignore-words.txt"
)
SPDX_LICENSE_DATA_URL = (
"https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json"
)
class RepositorySourceType(enum.Enum):
GIT = "git"
ARCHIVE = "archive"
DIRECTORY = "directory"
class YamlDataDumper(yaml.Dumper):
"""provide representations for a few non-default data types"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_representer(
collections.OrderedDict,
lambda dumper, data: dumper.represent_dict(data.items()),
)
self.add_representer(tuple, lambda dumper, data: dumper.represent_list(data))
class MuninPluginExampleGraph(
collections.namedtuple("MuninPluginExampleGraph", "key filename")
):
def _get_sort_key(self):
"""calculate a sorting weight based on the "key"
The special keys for daily, weekly, monthly and yearly graphs are supposed to appear first.
Numeric keys follow.
All other keys are used as sorting keys without further processing.
"""
try:
return (
{"day": -4, "weeky": -3, "month": -2, "year": -1}[self.key.lower()],
"",
)
except KeyError:
pass
try:
return (int(self.key), "")
except ValueError:
pass
return (100, self.key)
def __lt__(self, other):
return self._get_sort_key() < other._get_sort_key()
class MuninPluginRepositoryProcessingError(IOError):
"""any kind of error happened while processing a plugin source"""
class ConfigurationImportError(ValueError):
"""any kind of data validation problem encountered while processing the configuration file"""
class LicenseInformation(
collections.namedtuple("LicenseInformation", ("name", "key", "url"))
):
pass
class LicenseParser:
"""Determine the license of a given source code.
TODO: add detection based on non-SPDX keywords
"""
MANUAL_KEYWORD_LICENSE_MAP = (
("Public Domain", "GPL-2.0-only"),
("GPLv2", "GPL-2.0-only"),
("GPL v2", "GPL-2.0-only"),
("GPL (v2)", "GPL-2.0-only"),
("GPLv2+", "GPL-2.0-or-later"),
("GPL v2+", "GPL-2.0-or-later"),
("GNU General Public License, version 2 or later", "GPL-2.0-or-later"),
("GNU General Public License, version 2", "GPL-2.0-only"),
("Free Software Foundation; version 2 only", "GPL-2.0-only"),
("version 2 dated June,", "GPL-2.0-only"),
("either version 3 of the License,", "GPL-2.0-or-later"),
("either version 3 of the License,", "GPL-3.0-or-later"),
("GPLv3", "GPL-3.0-only"),
("GPL v3", "GPL-3.0-only"),
("GPL (v3)", "GPL-3.0-only"),
("GPLv3+", "GPL-3.0-or-later"),
("GPL v3+", "GPL-3.0-or-later"),
("LGPL", "LGPL-2.0-only"),
("LGPLv3", "LGPL-3.0-only"),
("LGPL v3", "LGPL-3.0-only"),
("LGPL (v3)", "LGPL-3.0-only"),
("use, copy, and modify this software with or without fee, provided", "ISC"),
("Attribution-ShareAlike 1.0", "CC-BY-1.0"),
("Attribution-ShareAlike 2.0", "CC-BY-2.0"),
("Attribution-ShareAlike 2.5", "CC-BY-2.5"),
("http://creativecommons.org/licenses/by-sa/3.0/", "CC-BY-3.0"),
("Attribution-ShareAlike 3.0", "CC-BY-3.0"),
("http://creativecommons.org/licenses/by-sa/4.0/", "CC-BY-3.0"),
("Attribution-ShareAlike 4.0", "CC-BY-4.0"),
)
def __init__(self):
licenses = {}
for item in self._download_license_data():
license_id = item["licenseId"]
name = item["name"]
references = item["seeAlso"]
url = references[0] if references else None
licenses[license_id] = LicenseInformation(name, license_id, url)
regexes = []
for license in licenses.values():
regex = re.compile(r"\b{}\b".format(re.escape(license.key)))
regexes.append((regex, 10, license))
# add optional alias for "*-or-later"
if license.key.endswith("+"):
regex = re.compile(
r"\b{}-or-later\b".format(re.escape(license.key[:-1]))
)
regexes.append((regex, 10, license))
if license.key.endswith("-or-later"):
regex = re.compile(r"\b{}\+\b".format(re.escape(license.key[:-9])))
regexes.append((regex, 10, license))
for keyword, license_id in self.MANUAL_KEYWORD_LICENSE_MAP:
regex = re.compile(r"\b{}\b".format(re.escape(keyword)))
regexes.append((regex, 5, licenses[license_id]))
# sort regexes by priority (higher first) and length (higher first)
regexes.sort(key=lambda item: (-item[1], -len(item[0].pattern)))
# remove priority from list (it was only used for sorting)
self.license_regexes = tuple(
(regex, license) for regex, priority, license in regexes
)
@staticmethod
def _download_license_data():
try:
with urllib.request.urlopen(SPDX_LICENSE_DATA_URL) as download:
raw = download.read()
except IOError as exc:
logging.warning(
"Failed to download SPDX license data (%s): %s",
SPDX_LICENSE_DATA_URL,
exc,
)
return {}
try:
data = json.loads(raw)
except ValueError as exc:
logging.warning(
"Failed to parse SPDX license data (%s): %s", SPDX_LICENSE_DATA_URL, exc
)
return {}
try:
return data["licenses"]
except KeyError:
logging.warning(
"Invalid SPDX license data format (%s): missing key 'licenses'",
SPDX_LICENSE_DATA_URL,
)
return {}
def parse_code(self, code):
# join lines and remove line comment indicators
processed = " ".join(re.sub(r"^#\s*", "", line) for line in code.splitlines())
for regex, license in self.license_regexes:
if regex.search(processed):
return license
else:
return None
class MuninPlugin:
# special periods (day, week, month, year) and numbers are supported
EXAMPLE_GRAPH_SUFFIX_REGEX = r"-(day|week|month|year|\d+).png"
# the "stable-2.0" branch of the core repository uses a ".in" suffix for all plugin files
OPTIONAL_PLUGIN_FILENAME_SUFFIXES = (".in",)
FAMILY_REGEX = re.compile(r"^.*#%#\s*family\s*=\s*(.+)$")
CAPABILITIES_HEADER_REGEX = re.compile(r"^.*#%#\s*capabilities\s*=\s*(.+)$")
# the following words are just good indicators of capabilities- not a real proof
CAPABILITIES_INDICATOR_REGEXES = {
"multigraph": re.compile(
r"\b(need_multigraph|multigraph)\b", flags=re.IGNORECASE
),
"dirtyconfig": re.compile(r"\bMUNIN_CAP_DIRTYCONFIG\b"),
}
# Most plugins contain a description in the first few lines ("NAME - SUMMARY ...").
# Some irrelevant tokens (e.g. the prefix "Munin Plugin to" or a trailing dot) are ignored.
SUMMARY_REGEX = re.compile(
r"^[\w\-\\]+\s+-\s+(Munin )?((Plugin|Script) )?(to )?(?P<summary>.*?)\.?$",
flags=re.IGNORECASE,
)
CATEGORY_LINE_BLACKLIST_REGEXES = (
re.compile(r"(?:label|documentation|\bthe\b|filterwarnings)"),
# ignore existing ambiguous word combinations
re.compile(
r"(?:env\.category|/category/|category queries|category\.|force_category)"
),
# ignore SQL expressions
re.compile(r"select.*from.*(?:join|where)"),
# ignore any kind of comments
re.compile(r"^\s*(?:#|//|/\*)"),
# no variable may be part of the category name
re.compile(r"category.*[&\$]"),
)
CATEGORY_REGEX = re.compile(
r"^(?P<line>.*[^$.]category[^\w\n]+(?P<category>\w+).*)$", flags=re.MULTILINE
)
KEYWORDS_REMOVAL_REGEXES = (
# the munin repository groups plugins by operating system
re.compile(r"^node\.d\."),
# omit the platform-independent plugin directory name used in the munin repository
re.compile(r"^node\.d$"),
# remove the "current directory"
re.compile(r"^\.$"),
)
# http://guide.munin-monitoring.org/en/latest/reference/graph-category.html#well-known-categories
WELL_KNOWN_CATEGORIES = {
"1sec",
"antivirus",
"appserver",
"auth",
"backup",
"chat",
"cloud",
"cms",
"cpu",
"db",
"devel",
"disk",
"dns",
"filetransfer",
"forum",
"fs",
"fw",
"games",
"htc",
"loadbalancer",
"mail",
"mailinglist",
"memory",
"munin",
"network",
"other",
"printing",
"processes",
"radio",
"san",
"search",
"security",
"sensors",
"spamfilter",
"streaming",
"system",
"time",
"tv",
"virtualization",
"voip",
"webserver",
"wiki",
"wireless",
}
# the list of mappings is ordered
IMPLEMENTATION_LANGUAGE_REGEXES = {
"awk": re.compile(r"\W(g|m)?awk(\W|$)"),
"bash": re.compile(r"\Wbash(\W|$)"),
"ksh": re.compile(r"\Wksh(\W|$)"),
"perl": re.compile(r"\Wperl(\W|$)"),
"php": re.compile(r"\Wphp"),
"python2": re.compile(r"\Wpython2?(\W|$)"),
"python3": re.compile(r"\Wpython3"),
"ruby": re.compile(r"\Wj?ruby"),
"sh": re.compile(r"\Wsh(\W|$)"),
"zsh": re.compile(r"\Wzsh(\W|$)"),
}
HEADING_REGEX = re.compile(r"^(#+.*)$", flags=re.MULTILINE)
CAPITALIZATION_UPPER = {"IP", "TCP", "UDP"}
CAPITALIZATION_LOWER = {"a", "the", "in", "for", "to", "and"}
PREPROCESSING_SHEBANG_SUBSTITUTIONS = (
(re.compile(r"^#!@@BASH@@"), "#!/bin/bash"),
(re.compile(r"^#!@@GOODSH@@"), "#!/bin/sh"),
(re.compile(r"^#!@@PERL@@"), "#!/usr/bin/perl"),
(re.compile(r"^#!@@PYTHON@@"), "#!/usr/bin/env python3"),
(re.compile(r"^#!@@RUBY@@"), "#!/usr/bin/ruby"),
)
COPYRIGHT_REGEX = re.compile(
r"^.{0,15}(?:Copyright|Copyleft|copying|\(c\)|Author:)(?:\s+\(c\))?(?:\s+\d+(?:-\d+)?,?)?"
r"(?:\s+-)?\s+((?:\w[\w.-]*\s){0,2}\w[\w.-]*?)(?:,.*|\s+[\d\-,<\(].*|\.|)$",
flags=re.IGNORECASE,
)
AUTHOR_HEADING_START_REGEX = re.compile(
r"^=head1 (AUTHORS?|COPYRIGHT)$", flags=re.IGNORECASE
)
AUTHOR_HEADING_END_REGEX = re.compile(r"^=(head|cut)", flags=re.IGNORECASE)
# we expect up to three name components - anything else is probably a textual description
AUTHOR_BARE_NAME_REGEX = re.compile(
r"^\s*"
r"(?:(?:.*:|(?:Copyright|Copyleft|copying)(?:\s+\(c\))?|\(c\)|\(\d+\)|contributed by)\s+)?"
r"(?:\d+(?:-\d+)?,?\s+)?(?:-\s+)?"
r"((?:\w[\w.-]*\s){0,2}\w[\w.-]*?)"
r"(?:\s+[\d\-,<\(].*|\.|)?$",
flags=re.IGNORECASE,
)
def __init__(
self,
plugin_filename,
repository_source=None,
name=None,
language=None,
license_parser=None,
):
self.plugin_filename = plugin_filename
self.repository_source = repository_source
self.name = os.path.basename(plugin_filename) if name is None else name
self.implementation_language = language
for suffix in self.OPTIONAL_PLUGIN_FILENAME_SUFFIXES:
if self.name.endswith(suffix):
self.name = self.name[: -len(suffix)]
self.example_graphs = self._find_images()
self.license_parser = license_parser
self._is_initialized = False
def _find_images(self):
example_graphs = []
example_graph_directory = os.path.join(
os.path.dirname(self.plugin_filename), EXAMPLE_GRAPH_DIRECTORY_NAME
)
example_graph_filename_pattern = re.compile(
self.name + self.EXAMPLE_GRAPH_SUFFIX_REGEX
)
try:
graph_filenames = os.listdir(example_graph_directory)
except OSError:
graph_filenames = []
for graph_filename in graph_filenames:
match = example_graph_filename_pattern.match(graph_filename)
if match:
image_key = match.groups()[0]
example_graphs.append(
MuninPluginExampleGraph(
image_key, os.path.join(example_graph_directory, graph_filename)
)
)
example_graphs.sort()
return example_graphs
async def initialize(self):
if not self._is_initialized:
with open(self.plugin_filename, "rb") as raw:
raw_content = raw.read().decode(errors="ignore")
self.plugin_code = self._preprocess_raw_code(raw_content)
self.documentation = await self._parse_documentation()
self.family = self._parse_family()
self.capabilities = self._parse_capabilities()
self.categories = self._parse_categories()
if self.repository_source:
self.changed_timestamp = (
await self.repository_source.get_file_timestamp(
self.plugin_filename
)
)
else:
self.changed_timestamp = None
self.path_keywords = tuple(self._get_keywords())
self.summary = self._guess_summary()
self.authors = await self._guess_authors()
self.license = self.license_parser.parse_code(self.plugin_code)
if self.implementation_language is None:
self.implementation_language = self._parse_implementation_language()
self._is_initialized = True
@classmethod
def _preprocess_raw_code(cls, raw_code):
"""replace specific patterns (e.g. the pre-substituted shebangs for munin-2.0 plugins)"""
result = raw_code
for pattern, replacement in cls.PREPROCESSING_SHEBANG_SUBSTITUTIONS:
result = pattern.sub(replacement, result)
return result
def _get_keywords(self):
if self.repository_source:
relative_path = self.repository_source.get_relative_path(
os.path.dirname(self.plugin_filename)
)
else:
relative_path = ""
for token in relative_path.lower().split(os.path.sep):
for regex in self.KEYWORDS_REMOVAL_REGEXES:
token = regex.sub("", token)
if token:
yield token
def _guess_summary(self):
# we expect the summary within the first few lines of the documentation
if not self.documentation:
return None
for line in self.documentation.splitlines()[:10]:
match = self.SUMMARY_REGEX.search(line)
if match:
return match.groupdict()["summary"]
else:
return None
async def _guess_authors(self):
def split_and_maybe_add(result, text):
"""split the text into into multiple authors and add new authors to the list"""
tokens = [text]
for tokenizer in (",", "/", " and "):
new_tokens = []
for token in tokens:
new_tokens.extend(token.split(tokenizer))
tokens = new_tokens
for token in tokens:
# remove "year" from copyright statements
token = re.sub(r"\b\d[\d-]+\b", "", token)
token = token.strip()
token = token.strip(".")
for ignore_suffix in [" and others", ", changed by me"]:
if token.endswith(ignore_suffix):
token = token[: -len(ignore_suffix)]
if token in {"HOLDERS BE LIABLE FOR ANY CLAIM", "LICENSE", "Copyright"}:
# ignore misleading terms following the word "COPYRIGHT"
pass
elif not token:
# ignore empty strings
pass
elif token[0].islower():
# terms starting with a lowercase letter are probably not names
pass
elif token in result:
# do not add duplicates
pass
else:
result.append(token)
result = []
is_in_author_header = False
for line in self.plugin_code.splitlines():
match = self.COPYRIGHT_REGEX.search(line)
if match:
split_and_maybe_add(result, match.groups()[0].strip())
elif not is_in_author_header:
match = self.AUTHOR_HEADING_START_REGEX.search(line)
if match:
is_in_author_header = True
elif is_in_author_header:
match = self.AUTHOR_HEADING_END_REGEX.search(line)
if match:
is_in_author_header = False
else:
match = self.AUTHOR_BARE_NAME_REGEX.search(line)
if match:
author_name = match.groups()[0].strip()
if author_name not in result:
for item in (
"known",
"contribut",
"bears no resemblance",
"license",
"all rights reserved",
"left join",
"linpro as",
"_",
):
if item in author_name.lower():
break
else:
split_and_maybe_add(result, author_name)
return result
@classmethod
def _rewrite_match_capitalization(cls, match):
"""downgrade the capitalization of each word of the match"""
result = []
for token in match.groups()[0].split():
if token.upper() in cls.CAPITALIZATION_UPPER:
# upper case for specific words (e.g. "IP")
token = token.upper()
elif token.lower() in cls.CAPITALIZATION_LOWER:
# lower case for all trivial words
token = token.lower()
else:
# capitalize only the first letter for all other words
token = token.title()
result.append(token)
return " ".join(result)
async def _parse_documentation(self):
"""parse the documentation and return a markdown formatted text"""
# quickly scan the file content in order to skip "perldoc" for files without documentation
if "=head1" not in self.plugin_code:
return None
if "ruby" in self.plugin_code[:20]:
# Ruby's multiline comment format ends with "=end" instead of "=cut". Sadly there
# seems to be no way to embed an "=end" without breaking the ruby interpreting.
# Without adding "=cut", the markdown conversion would end with the full plugin code.
result_lines = []
for line in self.plugin_code.splitlines():
if line == "=begin":
# "=begin" is a special string for ruby, but is invalid perlpod syntax, since
# it lacks the format specified. Thus we replace it with a generic "start"
# marker suitable for perlpod.
result_lines.append("=pod")
elif line == "=end":
# The "=end" directive closes a previous "=begin" directive. It indicates the
# end of the documentation (for ruby). Thus we replace it with perlpod's
# marker for the end of documentation.
result_lines.append("=cut")
else:
result_lines.append(line)
plugin_code = os.linesep.join(result_lines)
else:
plugin_code = self.plugin_code
# Enforce utf8 input encoding (if no encoding was specified).
# Otherwise perldoc would complain about non-utf8 characters.
if "=encoding" not in plugin_code:
plugin_code = re.sub(
r"^=head1",
os.linesep.join(("=encoding utf8", "", "=head1")),
plugin_code,
count=1,
flags=re.MULTILINE,
)
try:
process = await asyncio.subprocess.create_subprocess_exec(
*("pod2markdown", "--utf8"),
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
except OSError as exc:
logging.warning("Failed to execute pod2markdown: {}".format(exc))
return None
stdout, stderr = await process.communicate(plugin_code.encode())
if process.returncode != 0:
logging.info(
"Failed to generate documentation for plugin '%s': %s",
self.name,
stderr.decode(),
)
return None
documentation = stdout.decode()
# remove empty lines and whitespace and the beginning and end
documentation = documentation.strip()
# fix all-uppercase style (e.g. "NAME" -> "Name")
documentation = self.HEADING_REGEX.sub(
self._rewrite_match_capitalization, documentation
)
# reduce the level of all headings (the template applies level 1 to the plugin title)
documentation = re.sub(r"^#", "##", documentation, flags=re.MULTILINE)
# TODO: add some post-processing
return documentation
def _parse_capabilities(self):
result = set()
for line in self.plugin_code.splitlines():
match = self.CAPABILITIES_HEADER_REGEX.search(line)
if match:
result.update(match.groups()[0].strip().lower().split())
break
for capability, regex in self.CAPABILITIES_INDICATOR_REGEXES.items():
if regex.search(self.plugin_code):
result.add(capability)
# The "wildcard" configuration ability is not really a capability. But this is the least
# unsuitable place to indicate this behaviour
if self.plugin_filename.endswith("_"):
result.add("wildcard")
return tuple(sorted(result))
def _parse_family(self):
for line in self.plugin_code.splitlines():
match = self.FAMILY_REGEX.search(line)
if match:
return match.groups()[0].strip().lower()
else:
return None
def _parse_categories(self):
categories = set()
for line, category in self.CATEGORY_REGEX.findall(self.plugin_code):
if len(line.splitlines()) != 1:
continue
if any(
blacklist_regex.search(line)
for blacklist_regex in self.CATEGORY_LINE_BLACKLIST_REGEXES
):
continue
categories.add(category.lower())
return tuple(sorted(categories))
def _parse_implementation_language(self):
first_line = self.plugin_code.splitlines()[0]
for name, regex in self.IMPLEMENTATION_LANGUAGE_REGEXES.items():
if regex.search(first_line):
return name
else:
return None
def get_unexpected_categories(self):
return sorted(set(self.categories).difference(self.WELL_KNOWN_CATEGORIES))
def get_details(self):
return {
"documentation": bool(self.documentation),
"family": self.family,
"capabilities": self.capabilities,
"categories": set(self.categories),
"keywords": set(self.path_keywords),
"unexpected_categories": self.get_unexpected_categories(),
"authors": tuple(self.authors),
"image_filenames": dict(self._image_filenames),
"changed_timestamp": self.changed_timestamp,
}
def __str__(self):
if self._image_filenames:
return "Plugin '{:s}' ({:d} example graphs)".format(
self.name, len(self._image_filenames)
)
else:
return "Plugin '{:s}'".format(self.name)
class MuninPluginSource:
def __init__(
self,
name,
source_type,
location,
git_branch=None,
source_path=None,
ignore_files=None,
):
self.name = name
self._source_type = source_type
self._source_location = location
self._branch = git_branch
self._filter_path = source_path or os.path.curdir
self._ignore_files = set(ignore_files or [])
self._is_downloaded = False
async def initialize(self):
if not self._is_downloaded:
self._extract_directory = tempfile.mkdtemp(prefix="munin-gallery-")
if self._source_type == RepositorySourceType.GIT:
self._plugins_directory = await self._import_git_repository(
self._extract_directory,
self._source_location,
self._branch,
path=self._filter_path,
)
elif self._source_type == RepositorySourceType.ARCHIVE:
self._plugins_directory = await self._import_archive(
self._extract_directory,
self._source_location,
path=self._filter_path,
)
elif self._source_type == RepositorySourceType.DIRECTORY:
self._plugins_directory = os.path.join(
self._source_location, self._filter_path
)
else:
raise ValueError("Invalid source type: {}".format(self._source_type))
self._is_downloaded = True
def __del__(self):
if self._is_downloaded:
shutil.rmtree(self._extract_directory, ignore_errors=True)
del self._extract_directory
del self._plugins_directory
self._is_downloaded = False
async def _get_git_file_timestamp(self, filename):
"""retrieve the timestamp of the most recent commit affecting the filename"""
dirname, basename = os.path.dirname(filename), os.path.basename(filename)
try:
process = await asyncio.subprocess.create_subprocess_exec(
*(
"git",
"log",
"-n",
"1",
"--no-merges",
"--format=format:%aI",
"--",
basename,
),
cwd=dirname,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
except OSError:
logging.warning(
"Failed to run 'git log' while retrieving the timestamp of '{}'.".format(
filename
)
)
return None
timestamp_raw, error_output = await process.communicate()
if process.returncode != 0:
logging.warning(
"Failed to retrieve the timestamp of '{}' via 'git log': {}".format(
filename, error_output.decode()
)
)
return None
try:
return datetime.datetime.fromisoformat(timestamp_raw.decode())
except ValueError:
logging.warning(
"Failed to parse file timestamp of '{}': {}".format(
filename, timestamp_raw
)
)
return None
async def get_file_timestamp(self, filename):
if self._source_type == RepositorySourceType.GIT:
return await self._get_git_file_timestamp(filename)
elif self._source_type == RepositorySourceType.ARCHIVE:
# github's tar archive does not support proper file timestamps
return None
elif self._source_type == RepositorySourceType.DIRECTORY:
stat = os.stat(filename, follow_symlinks=True)
return datetime.datetime.fromtimestamp(stat.st_ctime).astimezone()
else:
raise ValueError("Invalid source type: {}".format(self._source_type))
@staticmethod
async def _import_git_repository(
target_directory, repository_url, branch, path=None
):
if path.rstrip(os.path.sep) == os.path.curdir:
path = None
try:
# we cannot use "--depth=1", since we are interested in the file timestamps
process = await asyncio.subprocess.create_subprocess_exec(
*(
"git",
"clone",
"--single-branch",
"--branch",
branch,
repository_url,
target_directory,
),
stderr=asyncio.subprocess.PIPE,
)
except OSError as exc:
raise MuninPluginRepositoryProcessingError(
"Failed to spawn process for repository retrieval (git): {}".format(exc)
)
await process.wait()
if process.returncode == 0:
return os.path.join(target_directory, path) if path else target_directory
else:
raise MuninPluginRepositoryProcessingError(
"Failed to extract source archive ({}): {}".format(
repository_url, (await process.stderr.read()).decode()
)
)
@staticmethod
async def _import_archive(target_directory, archive_url, path=None):
if path.rstrip(os.path.sep) == os.path.curdir:
path = None
# Strip the top-level path before extracting. Github assembles the name of this path
# component by combining the repository name and the branch name.
extract_command = [
"tar",
"--extract",
"--gzip",
"--strip-components=1",
"--directory",
target_directory,
]
if path:
# extract the specified path and ignore the top-level directory of github's archive
extract_command.extend(["--wildcards", os.path.join("*", path)])
try:
process = await asyncio.subprocess.create_subprocess_exec(
*extract_command,
stdin=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
except OSError as exc:
raise MuninPluginRepositoryProcessingError(
"Failed to spawn process for archival extraction (tar): {}".format(exc)
)
try:
async with aiohttp.ClientSession() as session:
async with session.get(archive_url) as response:
while True:
chunk = await response.content.read(256 * 1024)
if chunk:
process.stdin.write(chunk)
else:
break
process.stdin.close()
except IOError as exc:
raise MuninPluginRepositoryProcessingError(
"Failed to download source archive from '{}': {}'".format(
archive_url, exc
)
)
await process.wait()
if process.returncode == 0:
return os.path.join(target_directory, path if path else os.path.curdir)
else:
raise MuninPluginRepositoryProcessingError(
"Failed to extract source archive ({}): {}".format(
archive_url, (await process.stderr.read()).decode()
)
)
async def get_plugins(self, license_parser=None):
await self.initialize()
for dirpath, dirnames, filenames in os.walk(self._plugins_directory):
if os.path.basename(dirpath) in {
EXAMPLE_GRAPH_DIRECTORY_NAME,
"node.d.debug",
}:
# example graph directories are not expected to contain plugins
continue
for filename in filenames:
full_path = os.path.join(dirpath, filename)
relative_path = str(
pathlib.Path(full_path).relative_to(self._plugins_directory)
)
if relative_path in self._ignore_files:
continue
try:
status = os.stat(full_path, follow_symlinks=False)
except OSError:
pass
# every executable file is assumed to be a plugin
if status.st_mode & 0o100 > 0:
yield MuninPlugin(full_path, self, license_parser=license_parser)
elif filename.endswith(".in"):
# the plugin files in the stable-2.0 repository are not executable
yield MuninPlugin(
full_path,
repository_source=self,
name=filename[:-3],
license_parser=license_parser,
)
elif filename.endswith(".c"):
yield MuninPlugin(
full_path,
repository_source=self,
name=filename[:-2],
language="c",
license_parser=license_parser,
)
elif filename.endswith(".cpp"):
yield MuninPlugin(
full_path,
repository_source=self,
name=filename[:-4],
language="cpp",
license_parser=license_parser,
)
else:
# this file is probably not a plugin
pass
def get_relative_path(self, path):
return str(pathlib.Path(path).relative_to(self._plugins_directory))
class ContentIndexer:
"""a trivial content indexer for reducing the given text to a minimal set of words
Specific lines, special characters and superfluous whitespace is removed.
"""
# ignore lines with headings and magic markers
IGNORE_LINE_REGEX = re.compile(r"(^#|^\s+#%#)")
REMOVAL_REGEXES = (
# Remove common (very unspecific) words. These words are parsed from a separate file.
re.compile(
r"\b({})\b".format(
"|".join(open(INDEXING_IGNORE_WORDS_FILE, "r").read().splitlines())
),
flags=re.IGNORECASE,
),
# remove single-letter words/digits and dots
re.compile(r"\b(\.+|\w)\b", flags=re.IGNORECASE),
# remove all special characters
re.compile(r"[^\w\s.]"),
re.compile(r"\bSPDX-License-Identifier:\b"),
)
MERGE_WHITESPACE_REGEX = re.compile(r"\s+")
@classmethod
def get_indexing_content(cls, text):
result = []
for line in text.splitlines():
if cls.IGNORE_LINE_REGEX.search(line):
continue
for regex in cls.REMOVAL_REGEXES:
line = regex.sub(" ", line)
line = cls.MERGE_WHITESPACE_REGEX.sub(" ", line)
line = line.strip()
if line:
result.append(line)
return " ".join(result)
class MuninPluginsHugoExport:
MISSING_DOCUMENTATION_TEXT = "Sadly there is no documentation for this plugin."
PLUGINS_SUBDIRECTORY = "plugins"
def __init__(
self,
hugo_directory,
export_directory,
baseurl="http://localhost/",
hugo_environment="production",
clean_export_directory=True,
):
self._hugo_directory = hugo_directory
self._baseurl = baseurl
self._hugo_environment = hugo_environment
self._export_directory = export_directory
self.plugins = []
if clean_export_directory:
if os.path.exists(self._export_directory):
shutil.rmtree(self._export_directory)
async def _run_hugo(self, action=None, hide_output=True):
call_args = [
"hugo",
"--baseURL",
self._baseurl,
"--environment",
self._hugo_environment,