root/livinglogic.python.xist/src/ll/scripts/ucat.py @ 4422:fe09ca906d4e

Revision 4422:fe09ca906d4e, 3.6 KB (checked in by Walter Doerwald <walter@…>, 9 years ago)

Bump copyright year. Change encoding of remaining files to UTF-8. Remove trailing whitespace.

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