From 91365814b865b63ae2d95cf3df2eacd410685101 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Thu, 11 Oct 2012 00:33:14 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ catarc | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 50 +++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 catarc create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2fc8ce3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build +dist +*.egg-info diff --git a/catarc b/catarc new file mode 100644 index 0000000..bc8e66c --- /dev/null +++ b/catarc @@ -0,0 +1,126 @@ +#!/usr/bin/env python + +# This script will run the appropriate command to print archive contents to stdout +# Written by Sven Slootweg, Licensed under WTFPL - in other words, feel free to reuse for whatever purpose you desire. + +import os, argparse, sys, subprocess + +parser = argparse.ArgumentParser(description='Prints the uncompressed contents of archives to stdout, based on certain criteria.') + +parser.add_argument('pattern', metavar='FILES', type=str, nargs='+', + help='files to parse') + +parser.add_argument('-s', dest='size', action='store', default=None, + help='size requirement that has to be satisfied for a file to be printed (use < and >)') + +args = parser.parse_args() +options = vars(args) + +def exit_script(): + sys.stderr.write("Exiting...\n") + exit(1) + +def to_bytes(size): + size = size.lower().strip() + + if size.endswith("t"): + return int(size[:-1]) * 1024 * 1024 * 1024 * 1024 + elif size.endswith("g"): + return int(size[:-1]) * 1024 * 1024 * 1024 + elif size.endswith("m"): + return int(size[:-1]) * 1024 * 1024 + elif size.endswith("k"): + return int(size[:-1]) * 1024 + else: + return int(size) + +def from_bytes(size, unit): + size = int(size) + unit = unit.lower() + + if unit == 't': + return size * 1.0 / (1024 * 1024 * 1024 * 1024) + elif unit == 'g': + return size * 1.0 / (1024 * 1024 * 1024) + elif unit == 'm': + return size * 1.0 / (1024 * 1024) + elif unit == 'k': + return size * 1.0 / (1024) + else: + return size + +specifications = {} + +if options['size'] is not None: + # Parse size specification + for specification in options['size'].split(","): + if specification[:1] == "<": + specifications['<'] = specification[1:].strip() + elif specification[:1] == ">": + specifications['>'] = specification[1:].strip() + elif specification[:1] == "=": + specifications['='] = specification[1:].strip() + else: + sys.stderr.write("Incorrect size specification: %s\n" % specification) + exit_script() + +# Select all files matching the given pattern +file_list = options['pattern'] + +for item in file_list: + data = os.stat(item) + filesize = data.st_size + + try: + if filesize >= to_bytes(specifications['<']): + continue + except KeyError, e: + pass + + + try: + if filesize <= to_bytes(specifications['>']): + continue + except KeyError, e: + pass + + + try: + if int(from_bytes(filesize, specifications['='][-1:])) != specifications['=']: + continue + except KeyError, e: + pass + + # Passed all size tests, let's process it + with open(os.devnull, 'w') as stfu: + if item.endswith(".7z"): + processor = "7zip" + returncode = subprocess.call(['7z', 'e', '-so', item], stderr=stfu) + elif item.endswith(".tar"): + processor = "tar" + returncode = subprocess.call(['tar', '-Oxf', item], stderr=stfu) + elif item.endswith(".tar.gz"): + processor = "tar/gzip" + returncode = subprocess.call(['tar', '-Oxzf', item], stderr=stfu) + elif item.endswith(".tar.bz2"): + processor = "tar/bzip2" + returncode = subprocess.call(['tar', '-Oxjf', item], stderr=stfu) + elif item.endswith(".gz"): + processor = "gzip" + returncode = subprocess.call(['gzip', '-cd', item], stderr=stfu) + elif item.endswith(".bz2"): + processor = "bzip2" + returncode = subprocess.call(['bzip2', '-cd', item], stderr=stfu) + elif item.endswith(".zip"): + processor = "unzip" + returncode = subprocess.call(['unzip', '-p', item], stderr=stfu) + else: + sys.stderr.write("WARNING: Skipping item %s, not a recognized archive type\n" % item) + continue + + if returncode == 0: + sys.stderr.write("Successfully output %s\n" % item) + elif returncode == 2: + sys.stderr.write("ERROR: Could not run the needed command - are you sure you have %s installed?\n" % processor) + else: + sys.stderr.write("ERROR: Failed to output %s\n" % item) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..ca8c8b8 --- /dev/null +++ b/setup.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +""" +distutils/setuptools install script. See inline comments for packaging documentation. +""" + +import os +import sys + +import seesaw + +try: + from setuptools import setup + # hush pyflakes + setup +except ImportError: + from distutils.core import setup + +try: + from distutils.command.build_py import build_py_2to3 as build_py +except ImportError: + from distutils.command.build_py import build_py + +packages = [] + +package_dir = {} + +package_data = {} + +scripts = [ + 'catarc' +] + + +setup( + name='catarc', + version='1.0', + maintainer='Sven Slootweg', + maintainer_email='admin@cryto.net', + description='Tool to output uncompressed contents of (multiple) archives to stdout, without writing to disk', + url='http://www.cryto.net/projects/catarc/', + packages=packages, + package_dir=package_dir, + package_data=package_data, + include_package_data=True, + scripts=scripts, + install_requires=[], + cmdclass={'build_py': build_py} +) +