+def errmsg(msg, prog=parser.prog):
+ print("%s: %s" % (prog, msg), file=sys.stderr)
+
+# NamedTemporaryFile workalike which allows control of the file mode...
+def open_tmp(prefix="tmp", suffix="", dir=".", mode=0o600):
+ names = tempfile._get_candidate_names()
+ for seq in range(100):
+ name = os.path.join(dir, "%s%s%s" % (prefix, next(names), suffix))
+ try:
+ f = open(name, "x+", mode)
+ except FileExistsError:
+ continue
+ return tempfile._TemporaryFileWrapper(f, name)
+ return None
+
+# Given an arbitrary string, return the first substring that looks like an
+# mbid, or None if no such substring is found.
+def extract_mbid(arg):
+ xdigit = r'[0-9abcdefABCDEF]'
+ m = re.search(r'{0}{{8}}(-{0}{{4}}){{3}}-{0}{{12}}'.format(xdigit), arg)
+ if m:
+ return m.group()
+