]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Convert Python scripts to Python 3 and Blacken them
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 22 Apr 2019 14:13:13 +0000 (16:13 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 22 Apr 2019 14:13:13 +0000 (16:13 +0200)
Because 2019.

misc/combine-trace-files
misc/summarize-trace-files
perf/perf.py

index e0f5304f968799f211c1aa3dd31ce945fed8576f..a581270edd50bb64ccda0d683167f8c9fc295548 100755 (executable)
@@ -1,28 +1,27 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
-import sys
 import json
+import sys
 
 traces = {}
 for arg in sys.argv[1:]:
-    object = json.load(open(arg))
-    for event in object["traceEvents"]:
+    events = json.load(open(arg))["traceEvents"]
+    for event in events:
         if event["name"] == "" and event["ph"] == "I":
             time = float(event["args"]["time"])
             # print "%.6f" % time
-            traces[time] = object
+            traces[time] = events
             break
 
-times = traces.keys()
-mintime = min(times)
-times.sort()
+times = sorted(traces)
+min_time = min(times)
 
-events = []
+combined_events = []
 for time in times:
-    offset = (time - mintime) * 1000000.0
-    object = traces[time]
-    for event in object["traceEvents"]:
+    offset = (time - min_time) * 1000000.0
+    events = traces[time]
+    for event in events:
         event["ts"] = int(event["ts"] + offset)
-        events.append(event)
+        combined_events.append(event)
 
-print json.dumps({"traceEvents": events})
+print(json.dumps({"traceEvents": combined_events}))
index a52aa365de5446299fcfe81aa8dabef36d5ccb19..3c65f052d2590b9b021474fb4c141022e3ee9b92 100755 (executable)
@@ -1,20 +1,21 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
-import sys
 import json
+import sys
 
 trace = json.load(sys.stdin)
 
 events = trace["traceEvents"]
-slotevents = []
+slot_events = []
 
-events.sort(key = lambda event: event["ts"])
+events.sort(key=lambda event: event["ts"])
 
 jobs = int(sys.argv[1])
 
 pids = {}
 busy = [None] * jobs
 
+
 def find_slot(pid):
     if pid in pids:
         return pids[pid]
@@ -25,6 +26,7 @@ def find_slot(pid):
             return slot
     return None
 
+
 def end_slot(pid):
     for slot in range(jobs):
         if busy[slot] == pid:
@@ -33,50 +35,59 @@ def end_slot(pid):
             return slot
     return slot
 
+
 name = {}
 slot = -1
 for event in events:
-    cat = event['cat']
-    pid = event['pid']
-    phase = event['ph']
-    args = event['args']
+    cat = event["cat"]
+    pid = event["pid"]
+    phase = event["ph"]
+    args = event["args"]
 
-    if phase == 'M' and event['name'] == "thread_name":
+    if phase == "M" and event["name"] == "thread_name":
         name[pid] = args["name"]
     if cat != "program":
         continue
 
-    if phase == 'B' or phase == 'S':
+    if phase == "B" or phase == "S":
         slot = find_slot(pid)
-    elif phase == 'E' or phase == 'F':
+    elif phase == "E" or phase == "F":
         slot = end_slot(pid)
-    elif phase == 'M':
+    elif phase == "M":
         pass
     else:
         continue
 
-    event['pid'] = slot
-    event['tid'] = pid
+    event["pid"] = slot
+    event["tid"] = pid
 
-    slotevents.append(event)
+    slot_events.append(event)
 
-slotevents.sort(key = lambda event: event["tid"])
+slot_events.sort(key=lambda event: event["tid"])
 
-for event in slotevents:
-    if event['cat'] == "program":
-        event['cat'] = "ccache"
-        if event['tid'] in name:
-            event['name'] = name[event['tid']]
-    else:
-        if event['tid'] in name:
-            event['name'] = event['name'] + ':' + name[event['tid']]
-    del event['tid']
-    if event['ph'] == 'S':
-        event['ph'] = 'B'
-    if event['ph'] == 'F':
-        event['ph'] = 'E'
+for event in slot_events:
+    if event["cat"] == "program":
+        event["cat"] = "ccache"
+        if event["tid"] in name:
+            event["name"] = name[event["tid"]]
+    elif event["tid"] in name:
+        event["name"] = event["name"] + ":" + name[event["tid"]]
+    del event["tid"]
+    if event["ph"] == "S":
+        event["ph"] = "B"
+    elif event["ph"] == "F":
+        event["ph"] = "E"
 
 for slot in range(jobs):
-    slotevents.append({"cat":"","pid":slot,"tid":0,"ph":"M","name":"process_name","args":{"name":"Job %d" % slot}})
-
-json.dump({"traceEvents": slotevents}, sys.stdout, indent=4)
+    slot_events.append(
+        {
+            "cat": "",
+            "pid": slot,
+            "tid": 0,
+            "ph": "M",
+            "name": "process_name",
+            "args": {"name": "Job %d" % slot},
+        }
+    )
+
+json.dump({"traceEvents": slot_events}, sys.stdout, indent=4)
index acb410f2676a00e985edbed5fa506a59fad7aa52..954a7558a66ca9e161ae9ae066082d28e739f540 100755 (executable)
@@ -1,4 +1,4 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
 #
 # Copyright (C) 2010-2019 Joel Rosdahl
 #
 from optparse import OptionParser
 from os import access, environ, mkdir, getpid, X_OK
 from os.path import (
-    abspath, basename, exists, isabs, isfile, join as joinpath, realpath,
-    splitext)
+    abspath,
+    basename,
+    exists,
+    isabs,
+    isfile,
+    join as joinpath,
+    realpath,
+    splitext,
+)
 from shutil import rmtree
 from subprocess import call
 from time import time
@@ -49,7 +56,8 @@ PHASES = [
     "with ccache, direct mode, cache miss",
     "with ccache, direct mode, cache hit",
     "with ccache, depend mode, cache miss",
-    "with ccache, depend mode, cache hit"]
+    "with ccache, depend mode, cache hit",
+]
 
 verbose = False
 
@@ -80,10 +88,11 @@ def test(tmp_dir, options, compiler_args, source_file):
 
     progress("Creating source code\n")
     for i in range(times):
-        fp = open("%s/%d%s" % (src_dir, i, extension), "w")
-        fp.write(open(source_file).read())
-        fp.write("\nint ccache_perf_test_%d;\n" % i)
-        fp.close()
+        with open("%s/%d%s" % (src_dir, i, extension), "w") as fp:
+            with open(source_file) as fp2:
+                content = fp2.read()
+            fp.write(content)
+            fp.write("\nint ccache_perf_test_%d;\n" % i)
 
     environment = {"CCACHE_DIR": ccache_dir, "PATH": environ["PATH"]}
     environment["CCACHE_COMPILERCHECK"] = options.compilercheck
@@ -111,7 +120,8 @@ def test(tmp_dir, options, compiler_args, source_file):
             env["CCACHE_DEPEND"] = "1"
         if call(args, env=env) != 0:
             sys.stderr.write(
-                'Error running "%s"; please correct\n' % " ".join(args))
+                'Error running "%s"; please correct\n' % " ".join(args)
+            )
             sys.exit(1)
 
     # Warm up the disk cache.
@@ -201,25 +211,29 @@ def test(tmp_dir, options, compiler_args, source_file):
 
 
 def print_result_as_text(result):
-    for (i, x) in enumerate(PHASES):
-        print "%-43s %6.2f s (%6.2f %%) (%5.2f x)" % (
-            x.capitalize() + ":",
-            result[i],
-            100 * (result[i] / result[0]),
-            result[0] / result[i])
+    for i, x in enumerate(PHASES):
+        print(
+            "%-43s %6.2f s (%6.2f %%) (%5.2f x)"
+            % (
+                x.capitalize() + ":",
+                result[i],
+                100 * (result[i] / result[0]),
+                result[0] / result[i],
+            )
+        )
 
 
 def print_result_as_xml(result):
-    print '<?xml version="1.0" encoding="UTF-8"?>'
-    print "<ccache-perf>"
-    for (i, x) in enumerate(PHASES):
-        print "<measurement>"
-        print "<name>%s</name>" % x.capitalize()
-        print "<seconds>%.2f</seconds>" % result[i]
-        print "<percent>%.2f</percent>" % (100 * (result[i] / result[0]))
-        print "<times>%.2f</times>" % (result[0] / result[i])
-        print "</measurement>"
-    print "</ccache-perf>"
+    print('<?xml version="1.0" encoding="UTF-8"?>')
+    print("<ccache-perf>")
+    for i, x in enumerate(PHASES):
+        print("<measurement>")
+        print("<name>%s</name>" % x.capitalize())
+        print("<seconds>%.2f</seconds>" % result[i])
+        print("<percent>%.2f</percent>" % (100 * (result[i] / result[0])))
+        print("<times>%.2f</times>" % (result[0] / result[i]))
+        print("</measurement>")
+    print("</ccache-perf>")
 
 
 def on_off(x):
@@ -241,55 +255,52 @@ def main(argv):
     op = OptionParser(usage=USAGE, description=DESCRIPTION)
     op.disable_interspersed_args()
     op.add_option(
-        "--ccache",
-        help="location of ccache (default: %s)" % DEFAULT_CCACHE)
+        "--ccache", help="location of ccache (default: %s)" % DEFAULT_CCACHE
+    )
     op.add_option(
-        "--compilercheck",
-        help="specify compilercheck (default: mtime)")
+        "--compilercheck", help="specify compilercheck (default: mtime)"
+    )
+    op.add_option("--compression", help="use compression", action="store_true")
     op.add_option(
-        "--compression",
-        help="use compression",
-        action="store_true")
-    op.add_option(
-        "-d", "--directory",
+        "-d",
+        "--directory",
         help=(
             "where to create the temporary directory with the cache and other"
-            " files (default: %s)" % DEFAULT_DIRECTORY))
-    op.add_option(
-        "--hardlink",
-        help="use hard links",
-        action="store_true")
+            " files (default: %s)" % DEFAULT_DIRECTORY
+        ),
+    )
+    op.add_option("--hardlink", help="use hard links", action="store_true")
     op.add_option(
         "--hit-factor",
         help=(
             "how many times more to compile the file for cache hits (default:"
-            " %d)" % DEFAULT_HIT_FACTOR),
-        type="int")
+            " %d)" % DEFAULT_HIT_FACTOR
+        ),
+        type="int",
+    )
     op.add_option(
-        "--nostats",
-        help="don't write statistics",
-        action="store_true")
+        "--nostats", help="don't write statistics", action="store_true"
+    )
     op.add_option(
-        "-n", "--times",
+        "-n",
+        "--times",
         help=(
-            "number of times to compile the file (default: %d)"
-            % DEFAULT_TIMES),
-        type="int")
-    op.add_option(
-        "-v", "--verbose",
-        help="print progress messages",
-        action="store_true")
+            "number of times to compile the file (default: %d)" % DEFAULT_TIMES
+        ),
+        type="int",
+    )
     op.add_option(
-        "--xml",
-        help="print result as XML",
-        action="store_true")
+        "-v", "--verbose", help="print progress messages", action="store_true"
+    )
+    op.add_option("--xml", help="print result as XML", action="store_true")
     op.set_defaults(
         ccache=DEFAULT_CCACHE,
         compilercheck="mtime",
         directory=DEFAULT_DIRECTORY,
         hit_factor=DEFAULT_HIT_FACTOR,
-        times=DEFAULT_TIMES)
-    (options, args) = op.parse_args(argv[1:])
+        times=DEFAULT_TIMES,
+    )
+    options, args = op.parse_args(argv[1:])
     if len(args) < 2:
         op.error("Missing arguments; pass -h/--help for help")
 
@@ -304,16 +315,18 @@ def main(argv):
     if "ccache" in basename(realpath(compiler)):
         op.error(
             "%s seems to be a symlink to ccache; please specify the path to"
-            " the real compiler instead" % compiler)
+            " the real compiler instead" % compiler
+        )
 
     if not options.xml:
-        print "Compilation command: %s -c -o %s.o" % (
-            " ".join(args),
-            splitext(argv[-1])[0])
-        print "Compilercheck:", options.compilercheck
-        print "Compression:", on_off(options.compression)
-        print "Hardlink:", on_off(options.hardlink)
-        print "Nostats:", on_off(options.nostats)
+        print(
+            "Compilation command: %s -c -o %s.o"
+            % (" ".join(args), splitext(argv[-1])[0])
+        )
+        print("Compilercheck:", options.compilercheck)
+        print("Compression:", on_off(options.compression))
+        print("Hardlink:", on_off(options.hardlink))
+        print("Nostats:", on_off(options.nostats))
 
     tmp_dir = "%s/perfdir.%d" % (abspath(options.directory), getpid())
     recreate_dir(tmp_dir)