Skip to content

Commit

Permalink
python2to3 port to b4.4 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jennfshr committed Sep 24, 2024
1 parent 25a17b2 commit da29553
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 303 deletions.
48 changes: 24 additions & 24 deletions ldms/scripts/ldms-csv-anonymize
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env python2
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
########### section II below by NTESS ################################
license_ntess="""
Expand Down Expand Up @@ -548,10 +548,10 @@ def host_substitute(h, vmap):
canon.append(head)
continue
else:
print "Fix hmap. Can't map", nonum, "in", i, "of", h
print(f"Fix hmap. Can't map {nonum} in {i} of {h}")
sys.exit(1)
else:
print "Fix hmap file. Can't map", i, "in", h
print(f"Fix hmap file. Can't map {i} in {h}")
sys.exit(1)

fin = "-".join(canon)
Expand All @@ -567,14 +567,14 @@ mapmagic = "#anonymize-csv-map"
def write_map(maps, kind, fn):
if len(maps[kind]) > 0:
with open(fn, "w") as o:
print >>o, mapmagic, kind
print(mapmagic, kind, file=o)
m = maps[kind]
for key in sorted(m):
if key == "netdomains":
if len(m[key]) > 0:
print >>o, key, ",".join(m[key])
print(key, ",".join(m[key]), file=o)
else:
print >>o, key, m[key]
print(key, m[key], file=o)

def reload_map(m, fn, kind):
"""prime m with data from prior run. input can be empty, otherwise must match kind."""
Expand All @@ -587,10 +587,10 @@ def reload_map(m, fn, kind):
(k,v) = ln.split()
if header:
if k != mapmagic:
print "error loading map- not a mapping file:", fn,
print(f"Error loading map- not a mapping file: {fn}")
sys.exit(1)
if v != kind:
print "error loading map file- wrong type.", kind, "vs", v, "in", fn
print(f"Error loading map file- wrong type. {kind} vs {v} in")
sys.exit(1)
header = False
continue
Expand All @@ -600,7 +600,7 @@ def convert_col(kstr):
"""convert extended cut index string to python notation."""
k = int(kstr)
if k == 0:
print "column numbers are 1-N (as cut(1)) or negative, not 0."
print("column numbers are 1-N (as cut(1)) or negative, not 0.")
sys.exit(1)
if k > 0:
k -= 1
Expand Down Expand Up @@ -668,12 +668,12 @@ if __name__ == "__main__":
parser.add_argument("--gen-args", default=None, help="input metric names and file")
parser.add_argument("--debug", default=False, action='store_true', help="enable debug")
if len(sys.argv) < 2:
print "need some arguments. try -h"
print("Need some arguments. Try -h")
sys.exit(1)
args = parser.parse_args()

if args.debug:
print args
print(args)
sep = args.col_sep
if not sep:
sep = ','
Expand All @@ -685,18 +685,18 @@ if __name__ == "__main__":
files.extend(i.split())
cols = []
if len(args.mappings) == 0:
print "nothing to do"
print("Nothing to do")
sys.exit(1)
for i in args.mappings:
if ":" in i:
cols.append(i)
else:
files.append(i)
if args.debug:
print files
print cols
print(files)
print(cols)
maps = dict()
for key in rewrite.keys():
for key in list(rewrite.keys()):
maps[key] = dict()
if args.imap:
reload_map(maps["int"], args.imap, "int")
Expand All @@ -707,35 +707,35 @@ if __name__ == "__main__":
maps["host"]["netdomains"] = []
if args.hmap:
reload_map(maps["host"], args.hmap, "host")
if maps["host"].has_key("netdomains"):
if "netdomains" in maps["host"]:
maps["host"]["netdomains"] = maps["host"]["netdomains"].split(",")

prefix = args.save_maps
if not prefix:
prefix = 'anonmap_'
seed = args.seed
if not seed:
seed = random.randint(1,sys.maxint)
seed = random.randint(1,sys.maxsize)
random.seed(seed)
od = args.out_dir
if not od or not os.path.isdir(od):
print "output directory not given or not found"
print("output directory not given or not found")
sys.exit(1)
for f in files:
if not os.path.isfile(f):
print "file not found", f
print("File not found {f}")
sys.exit(1)
for f in files:
if args.debug:
print f
print(f)
bname = os.path.basename(f)
with open(f, "r") as i:
out = os.path.join(od, bname)
with open(out, "w") as o:
for ln in i:
ln = ln.rstrip()
if len(ln) < 3:
print >>o, ln
print(ln, file=o)
continue
if ln[0] == '#':
x = ln.split(sep)
Expand All @@ -744,18 +744,18 @@ if __name__ == "__main__":
k = convert_col(k)
x[k] = map_header(x[k], method)
y = sep.join(x)
print >>o, y
print(y, file=o)
continue
x = ln.split(sep)
for c in cols:
(method, k) = c.split(":")
if method == "host" and args.hmap == None:
print "--hmap required for host mapping"
print("--hmap required for host mapping")
sys.exit(1)
k = convert_col(k)
x[k] = rewrite[method](x[k], maps[method])
y = sep.join(x)
print >>o, y
print(y, file=o)


# print seed file if we ever use random
Expand Down
Loading

0 comments on commit da29553

Please sign in to comment.