summaryrefslogtreecommitdiff
path: root/assert
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2009-10-31 18:04:25 +0200
committerLars Wirzenius <liw@liw.fi>2009-10-31 18:04:25 +0200
commit8c560fe4939e351b49fff7f38773a4afb860055c (patch)
tree27be4e22194d6f3853482dd02e75647f36bd9141 /assert
parent930028fbde9ded60443e74860444587b5bf11e0a (diff)
downloadextrautils-8c560fe4939e351b49fff7f38773a4afb860055c.tar.gz
Added assert command.
Diffstat (limited to 'assert')
-rwxr-xr-xassert89
1 files changed, 89 insertions, 0 deletions
diff --git a/assert b/assert
new file mode 100755
index 0000000..4b31b5d
--- /dev/null
+++ b/assert
@@ -0,0 +1,89 @@
+#!/usr/bin/python
+
+
+import gnomevfs
+import optparse
+import os
+import subprocess
+import sys
+
+
+def setup_option_parser():
+ parser = optparse.OptionParser()
+
+ parser.add_option("--file", metavar="FILE",
+ help="Report FILE as being failing the assertion.")
+
+ return parser
+
+
+def equal(options, args):
+ return args[0] == args[1], "%s != %s" % (repr(args[0]), repr(args[1]))
+
+
+def mime_type_is(options, args):
+ wanted_type = args[0]
+ args = args[1:]
+ for arg in args:
+ uri = gnomevfs.get_uri_from_local_path(os.path.abspath(arg))
+ actual_type = gnomevfs.get_mime_type(uri)
+ if actual_type != wanted_type:
+ options.file = arg
+ return False, "%s != %s" % (wanted_type, actual_type)
+ return True, None
+
+
+def file_type_is(options, args):
+ wanted_type = args[0]
+ filenames = args[1:]
+ for filename in filenames:
+ p = subprocess.Popen(["file", "-0", filename], stdout=subprocess.PIPE)
+ stdout, stderr = p.communicate()
+ if p.returncode == 0:
+ filename, actual_type = stdout.split("\0")
+ actual_type = actual_type.strip()
+ if actual_type != wanted_type:
+ return False, "%s != %s" % (wanted_type, actual_type)
+ return True, None
+
+
+def main():
+ parser = setup_option_parser()
+ options, args = parser.parse_args()
+
+ opers = {
+ "equal": (2, 2, equal),
+ "file-type-is": (2, None, file_type_is),
+ "mime-type-is": (2, None, mime_type_is),
+ }
+
+ if not args:
+ raise Exception("No operation on command line.")
+ elif args[0] in opers:
+ name = args[0]
+ args = args[1:]
+ min_count, max_count, func = opers[name]
+ if (len(args) < min_count or
+ (max_count is not None and len(args) > max_count)):
+ raise Exception("Operation %s must have %d..%d "
+ "arguments (got %d)." %
+ (name, min_count, max_count or "infinity",
+ len(args)))
+ ok, msg = func(options, args)
+ if not ok:
+ parts = []
+ if options.file:
+ parts.append("%s: " % options.file)
+ parts.append("Assertion failed: ")
+ parts.append(msg or " ".join(sys.argv[1:]))
+ raise Exception("".join(parts))
+ else:
+ raise Exception("Unknown operation '%s'." % args[0])
+
+
+if __name__ == "__main__":
+ try:
+ main()
+ except Exception, e:
+ sys.stderr.write("%s\n" % str(e))
+ sys.exit(1)