File: util.py

package info (click to toggle)
nwsserver 2.0.0-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 304 kB
  • ctags: 510
  • sloc: python: 3,252; sh: 76; makefile: 64
file content (125 lines) | stat: -rw-r--r-- 3,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#
# Copyright (c) 2005-2009, REvolution Computing, Inc.
#
# NetWorkSpaces is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#

"""System-level utilities."""

import sys, os
import string       #pylint: disable-msg=W0402

__all__ = ['which', 'msc_quote', 'msc_argv2str']

IS_WINDOWS = sys.platform.startswith('win')
WIN_EXTS = os.environ.get('PATHEXT', '').split(os.pathsep)
NEED_QUOTING = string.whitespace + '"'

def _isexec(path):
    """Check if a path refers to an executable file."""
    return os.path.isfile(path) and os.access(path, os.X_OK)

def _exenames(name):
    """Generate the list of executable names to which ``name`` might refer."""
    names = [name]
    # only expand on Windows
    if IS_WINDOWS:
        # only expand if not extension is specified
        base, ext = os.path.splitext(name)
        if not ext:
            for ext in WIN_EXTS:
                names.append(base + ext)
    return names

def which(cmd, path=None):
    """Find the command ``cmd`` in the path, similar to shell-builtin
    'which'
    """
    if os.path.split(cmd)[0]:
        cmd = os.path.abspath(cmd)
        # print "Checking for", cmd
        matches = [x for x in _exenames(cmd) if _isexec(x)]
    else:
        matches = []
        if not path:
            path = os.environ.get('PATH', os.defpath).split(os.pathsep)
            if IS_WINDOWS:
                path.insert(0, os.curdir)
        for pelem in path:
            full = os.path.join(os.path.normpath(pelem), cmd)
            # print "Checking for", full
            matches += [x for x in _exenames(full) if _isexec(x)]

    return matches

def msc_quote(cmd):
    """Quote a command-line argument as per MSC quoting rules.  This is useful
    in conjunction with os.spawnv on Windows.  For example::

        os.spawnv(os.P_WAIT, argv[0], [msc_quote(a) for a in argv])

    This is only useful on Windows.
    """
    if not [char for char in cmd if char in NEED_QUOTING]:
        return cmd

    quoted = '"'
    nbs = 0

    for char in cmd:
        if char == '\\':
            quoted += char
            nbs += 1
        elif char == '"':
            quoted += (nbs + 1) * '\\' + char
            nbs = 0
        else:
            quoted += char
            nbs = 0

    quoted += nbs * '\\' + '"'
    return quoted

def msc_argv2str(argv):
    """Quote a command-line as per MSC quoting rules.  This is useful in
    conjunction with win32process.CreateProcess and os.system on Windows.  For
    example::

        os.system(msc_argv2str(argv))

    This is only useful on Windows.
    """
    return ' '.join([msc_quote(arg) for arg in argv])

if __name__ == '__main__':
    def _qtest(argv):
        """Simple test for msc_argv2str quoting."""
        print "quoted command string:", msc_argv2str(argv)
    
    def _wtest(argv):
        """Simple test for ``which`` functionality."""
        for arg in argv[1:]:
            plist = which(arg)
            if not plist:
                print >> sys.stderr, \
                        "error: no matches found for", arg
                continue
            if len(plist) > 1:
                print >> sys.stderr, \
                        "warning: more than one match found for", arg
            print plist[0]

    _qtest(sys.argv)