This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
timeline.py
99 lines (76 loc) · 2.89 KB
/
timeline.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
#!/usr/bin/env python2
"""
timeline metadata from a WMI repository.
author: Willi Ballenthin
email: [email protected]
"""
import sys
import logging
import collections
import argparse
import cim
import cim.objects
logger = logging.getLogger(__name__)
def format_ts(ts):
'''
format a timestamp object nicely.
Args:
ts (datetime.datetime): the timestamp instance.
Returns:
str: the formatted timestamp string.
'''
return ts.isoformat("T") + "Z"
TimelineEntry = collections.namedtuple("TimelineEntry", ["ts", "label", "key"])
def timeline(repo):
'''
extract the timestamps from a wmi repository into an ordered list.
Args:
repo (cim.CIM): the repository form which to extract timestamps.
Returns:
List[TimelineEntry]: the timeline of timestamps in the repository.
'''
entries = []
def rec(namespace):
for klass in namespace.classes:
entries.append(TimelineEntry(ts=klass.cd.header.timestamp,
label="ClassDefinition.timestamp",
key=str(klass)))
for instance in klass.instances:
entries.append(TimelineEntry(ts=instance.ci.ts1,
label="ClassInstance.timestamp1",
key=str(instance)))
entries.append(TimelineEntry(ts=instance.ci.ts2,
label="ClassInstance.timestamp2",
key=str(instance)))
for ns in namespace.namespaces:
rec(ns)
tree = cim.objects.Tree(repo)
rec(tree.root)
return sorted(entries, key=lambda e: e.ts)
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(description="Timeline metadata in a WMI repository.")
parser.add_argument("input", type=str,
help="Path to input file")
parser.add_argument("-v", "--verbose", action="store_true",
help="Enable debug logging")
parser.add_argument("-q", "--quiet", action="store_true",
help="Disable all output but errors")
args = parser.parse_args(args=argv)
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
logging.getLogger().setLevel(logging.DEBUG)
elif args.quiet:
logging.basicConfig(level=logging.ERROR)
logging.getLogger().setLevel(logging.ERROR)
else:
logging.basicConfig(level=logging.INFO)
logging.getLogger().setLevel(logging.INFO)
repo = cim.CIM.from_path(args.input)
for entry in timeline(repo):
print('{ts:s}\t{entry.label:s}\t{entry.key:s}'.format(entry=entry, ts=format_ts(entry.ts)))
return 0
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main(sys.argv[1:])