root/livinglogic.python.xist/src/ll/scripts/ucat.py @ 4437:6f3d4e845072

Revision 4437:6f3d4e845072, 3.7 KB (checked in by Walter Doerwald <walter@…>, 8 years ago)

Fix typos in script documentation. Add examples. Bump version number.

Line 
1#!/usr/local/bin/python
2# -*- coding: utf-8 -*-
3
4
5## Copyright 2007-2011 by LivingLogic AG, Bayreuth/Germany.
6## Copyright 2007-2011 by Walter Dörwald
7##
8## All Rights Reserved
9##
10## See ll/__init__.py for the license
11
12
13"""
14Purpose
15-------
16
17``ucat`` is a script for printing files. It is an URL-enabled version of the
18``cat`` system command. Via :mod:`ll.url` and :mod:`ll.orasql` ``ucat`` supports
19``ssh`` and ``oracle`` URLs too.
20
21
22Options
23-------
24
25``ucat`` supports the following options:
26
27    ``urls``
28        One or more URLs to be printed.
29
30    ``-r``, ``--recursive`` : ``false``, ``no``, ``0``, ``true``, ``yes`` or ``1``
31        Prints directory content recursively.
32
33    ``-x``, ``--ignoreerrors`` : ``false``, ``no``, ``0``, ``true``, ``yes`` or ``1``
34        Ignores errors occuring during the copy process. (Otherwise the copy
35        process is aborted.)
36
37    ``-i``, ``--include`` : regular expression
38        Only copy files that contain the regular expression.
39
40    ``-e``, ``--exclude`` : regular expression
41        Don't copy files that contain the regular expression.
42
43    ``-a``, ``--all`` : ``false``, ``no``, ``0``, ``true``, ``yes`` or ``1``
44        Include dot files (i.e. files whose name starts with a ``.``). Not that
45        the content of directories whose name starts with a dot will still be
46        printed.
47
48
49Examples
50--------
51Print a file::
52
53    $ ucat foo.txt
54
55Print a remote file::
56
57    $ ucat ssh://user@www.example.org/~/foo.txt
58
59Print the SQL source code of the procedure ``FOO`` in an Oracle database::
60
61    $ ucat oracle://user:pwd@oracle.example.org/procedure/FOO
62
63"""
64
65
66import sys, re, argparse, contextlib, errno
67
68from ll import misc, url
69
70try:
71    import astyle
72except ImportError:
73    from ll import astyle
74
75try:
76    from ll import orasql # activate the oracle scheme
77except ImportError:
78    pass
79
80
81__docformat__ = "reStructuredText"
82
83
84def main(args=None):
85    def match(url):
86        strurl = str(url)
87        if args.include is not None and args.include.search(strurl) is None:
88            return False
89        if args.exclude is not None and args.exclude.search(strurl) is not None:
90            return False
91        if not args.all:
92            if url.file:
93                name = url.file
94            elif len(url.path) >=2:
95                name = url.path[-2]
96            else:
97                name = ""
98            if name.startswith("."):
99                return False
100        return True
101
102    def catone(urlread):
103        if urlread.isdir():
104            if args.recursive:
105                for u in urlread.listdir():
106                    catone(urlread/u)
107            else:
108                raise IOError(errno.EISDIR, "Is a directory", str(urlread))
109        else:
110            if match(urlread):
111                try:
112                    with contextlib.closing(urlread.open("rb")) as fileread:
113                        size = 0
114                        while True:
115                            data = fileread.read(262144)
116                            if data:
117                                sys.stdout.write(data)
118                            else:
119                                break
120                except Exception:
121                    if not args.ignoreerrors:
122                        raise
123
124    p = argparse.ArgumentParser(description="print URL content on the screen")
125    p.add_argument("urls", metavar="url", help="URLs to be printed", nargs="+", type=url.URL)
126    p.add_argument("-r", "--recursive", dest="recursive", help="Copy stuff recursively? (default: %(default)s)", action=misc.FlagAction, default=False)
127    p.add_argument("-x", "--ignoreerrors", dest="ignoreerrors", help="Ignore errors? (default: %(default)s)", action=misc.FlagAction, default=False)
128    p.add_argument("-i", "--include", dest="include", metavar="PATTERN", help="Include only URLs matching PATTERN (default: %(default)s)", type=re.compile)
129    p.add_argument("-e", "--exclude", dest="exclude", metavar="PATTERN", help="Exclude URLs matching PATTERN (default: %(default)s)", type=re.compile)
130    p.add_argument("-a", "--all", dest="all", help="Include dot files? (default: %(default)s)", action=misc.FlagAction, default=False)
131
132    args = p.parse_args(args)
133    with url.Context():
134        for u in args.urls:
135            catone(u)
136
137
138if __name__ == "__main__":
139    sys.exit(main())
Note: See TracBrowser for help on using the browser.