File: shell.py

package info (click to toggle)
python-rjsmin 1.2.5%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,256 kB
  • sloc: javascript: 8,503; python: 5,315; ansic: 821; sh: 100; makefile: 19
file content (611 lines) | stat: -rw-r--r-- 16,467 bytes parent folder | download
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# -*- coding: ascii -*-
#
# Copyright 2007 - 2025
# Andr\xe9 Malo or his licensors, as applicable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
=================
 Shell utilities
=================

Shell utilities.
"""
from __future__ import absolute_import

__author__ = "Andr\xe9 Malo"

import contextlib as _contextlib
import errno as _errno
import fnmatch as _fnmatch
import functools as _ft
import glob as _glob
import os as _os
import re as _re
import shutil as _shutil
import sys as _sys
import tempfile as _tempfile

from . import util as _util

root = _os.path.dirname(
    _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__)))
)


@_contextlib.contextmanager
def root_dir():
    """ Context manager to change into the root directory """
    assert root is not None

    old = _os.getcwd()
    try:
        _os.chdir(root)
        yield root
    finally:
        _os.chdir(old)


def _make_split_command():
    """
    Make split_command function

    The command splitter splits between tokens. Tokens are non-whitespace
    sequences or double quoted strings. Inside those double quotes can be
    escaped with a backslash. So have to be backslashes.

    Stolen from <http://opensource.perlig.de/svnmailer/>.

    Returns:
      callable: Parser for generic commandlines
    """
    argre = r'[^"\s]\S*|"[^\\"]*(?:\\[\\"][^\\"]*)*"'
    check = _re.compile(
        r'\s*(?:%(arg)s)(?:\s+(?:%(arg)s))*\s*$' % dict(arg=argre)
    ).match
    split = _re.compile(argre).findall
    strip = _ft.partial(_re.compile(r'\\([\\"])').sub, r'\1')

    def split_command(command):  # pylint: disable = redefined-outer-name
        """
        Split generic commandline into single arguments

        The command splitter splits between tokens. Tokens are non-whitespace
        sequences or double quoted strings. Inside those double quotes can be
        escaped with a backslash. So have to be backslashes.

        Stolen from <http://opensource.perlig.de/svnmailer/>.

        Parameters:
          command (str or iterable):
            The command string to split. If it's not a string, it's taken as a
            generic iterable and returned as a list.

        Returns:
          list: Command splitted parts
        """
        if not isinstance(command, _util.basestring_):
            return list(command)

        if not check(command):
            raise ValueError("Invalid command string %r" % (command,))

        return [
            strip(arg[1:-1]) if arg.startswith('"') else arg
            for arg in split(command)
        ]

    return split_command

split_command = _make_split_command()  # noqa


def _make_formatter(*args, **kwargs):
    """
    Make args / kwargs formatter

    Either args or kwargs or neither of them can be set. There cannot be set
    both of them.

    Returns:
      callable: Formatter, using either args or kwargs
    """
    # pylint: disable = no-else-return

    assert not (args and kwargs)

    if args:
        # tuples are given for the whole command string but applied per token.
        # We need to supply only the tuples which are needed for the current
        # token.
        args = list(args[::-1])
        pcents = _re.compile(r'%[^%]').findall

        def formatter(value):
            """ Tuple formatter """
            count = len(pcents(value))
            torepl = []
            while len(torepl) < count:
                torepl.append(args.pop())
            return value % tuple(torepl)
        return formatter

    elif kwargs:
        return lambda x: x % kwargs

    return lambda x: x


def _make_win32_command():
    r"""
    Make win32_command function

    >>> x = win32_command(r'''
    ...     command arg "arg 2" "" "arg %3"
    ...     "malic'ious argument\\\"&whoami"
    ... ''')
    >>> print(x[:42])
    command arg ^"arg^ 2^" ^"^" ^"arg^ ^%3^" ^
    >>> print(x[41:])
    ^"malic'ious^ argument\\\^"^&whoami^"

    """
    wsp, meta = r'\r\n\t\x0b\x0c\x08 ', r'()%!^"<>&|'
    slashsub = _ft.partial(_re.compile(r'(\\+)("|$)').sub, r'\1\1\2')
    metasub = _ft.partial(_re.compile(r'([%s%s])' % (wsp, meta)).sub, r'^\1')
    qsearch = _re.compile(r'[%s"]' % (wsp,)).search
    needq = lambda x: not x or qsearch(x)  # noqa pylint: disable = unnecessary-lambda-assignment

    def win32_command(command, *args, **kwargs):
        """
        Return a win32/cmd.exe suitable commandline

        :See: https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/
              2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/

        Either args or kwargs or neither of them can be set. There cannot be
        set both of them.

        Parameters:
          command (str):
            Generic commandline, possibly containing substitutions, filled by
            args or kwargs. See `split_command` for generic commandline
            syntax.

          *args:
            Substitution positional arguments

          **kwargs
            Substitution keyword arguments

        Returns:
          str: Strictly quoted shell commandline for ``cmd.exe``
        """
        # pylint: disable = redefined-outer-name
        return ' '.join([metasub(
            '"%s"' % (slashsub(token).replace('"', '\\"'),)
            if needq(token) else token
        ) for token in map(_make_formatter(*args, **kwargs),
                           split_command(command))])

    return win32_command

win32_command = _make_win32_command()  # noqa


def _make_posix_command():
    r"""
    Make posix_command function

    >>> x = posix_command(r'''
    ...     command arg "arg 2" "" "arg $3"
    ...     "malic'ious argument\\\"&whoami"
    ... ''')
    >>> print(x)
    command arg 'arg 2' '' 'arg $3' 'malic'\''ious argument\"&whoami'

    """
    qsearch = _re.compile(r'[^a-zA-Z\d_./-]').search
    needq = lambda x: not x or qsearch(x)  # noqa pylint: disable = unnecessary-lambda-assignment

    def posix_command(command, *args, **kwargs):
        """
        Return a POSIX shell suitable commandline

        Either args or kwargs or neither of them can be set. There cannot be
        set both of them.

        Parameters:
          command (str):
            Generic commandline, possibly containing substitutions, filled by
            args or kwargs. See `split_command` for generic commandline
            syntax.

          *args:
            Substitution positional arguments

          **kwargs
            Substitution keyword arguments

        Returns:
          str: Strictly quoted shell commandline for POSIX shells
        """
        # pylint: disable = redefined-outer-name
        return ' '.join([
            "'%s'" % (token.replace("'", "'\\''")) if needq(token) else token
            for token in map(_make_formatter(*args, **kwargs),
                             split_command(command))
        ])
    return posix_command

posix_command = _make_posix_command()  # noqa

command = win32_command if _sys.platform.lower() == 'win32' else posix_command


def native(path):
    """
    Convert slash path to native

    Parameters:
      path (str):
        Path relative to the checkout root

    Returns:
      str: The native path
    """
    path = _os.path.sep.join(path.split('/'))
    return _os.path.normpath(_os.path.join(root, path))


def relative(path):
    """
    Convert native path into root-relative

    Parameters:
      path (str):
        Path relative to the checkout root

    Returns:
      str: The relative path - with slashes
    """
    return "/".join(pathparts(_os.path.relpath(native(path), native(root))))


def cp(src, dest):
    """
    Copy src to dest

    Parameters:
      src (str):
        Source path, relative to the checkout root

      dest (str):
        Dest path, relative to the checkout root
    """
    _shutil.copy2(native(src), native(dest))


def cp_r(src, dest, ignore=None):
    """
    Copy -r src to dest

    Parameters:
      src (str):
        Source path, relative to the checkout root

      dest (str):
        Dest path, relative to the checkout root

      ignore (callable):
        Ignore callback
    """
    _shutil.copytree(native(src), native(dest), ignore=ignore)


def rm(*dest):
    """
    Remove one or more files, ENOENT is not considered an error

    Parameters:
      *dest:
        Files to remove
    """
    if len(dest) == 1 and not isinstance(dest[0], _util.basestring_):
        dest = dest[0]

    for name in dest:
        try:
            _os.unlink(native(name))
        except OSError as e:
            if _errno.ENOENT != e.errno:
                raise


def rm_rf(*dest):
    """
    Remove one or more trees

    :Parameters:
      *dest:
        Paths to remove
    """
    if len(dest) == 1 and not isinstance(dest[0], _util.basestring_):
        dest = dest[0]

    for name in dest:
        name = native(name)
        if _os.path.islink(name):
            _os.unlink(name)

        elif _os.path.isfile(name):
            rm(name)

        elif _os.path.exists(name):
            for path in files(name, '*'):
                if not _os.path.islink(native(path)):
                    _os.chmod(native(path), 0o644)
            _shutil.rmtree(name)


def mkdir_p(*dirnames):
    """
    Create one or more directories

    Parameters:
      *dirnames:
        Directory names (the leaf directory)
    """
    if len(dirnames) == 1 and not isinstance(dirnames[0], _util.basestring_):
        dirnames = dirnames[0]

    for dirname in dirnames:
        try:
            _os.makedirs(dirname)
        except OSError as e:
            # makedirs throws OSError if the last dir segment exists
            if e.errno != _errno.EEXIST:
                raise


mkstemp = _tempfile.mkstemp
walk = _os.walk


def glob(*pattern, **kwargs):
    """
    glob(*pattern, prune=None, prune_base=('.git', '.svn', 'CVS')):

    Find files or directories with glob pattern (python 3.6+)

    Parameters:
      *pattern:
        Glob to match against

      prune (iterable):
        List of directory basenames to ignore.
        Default: None. Can be empty or ``None`` (meaning not to prune anything
        except prune_base)

      prune_base (iterable):
        Extra list of directory basenames to ignore.
        Default: ('.git', '.svn', 'CVS'). Can be empty or ``None`` (meaning
        not to prune anything except prune)

    Returns:
      iterable: Iterator over matching pathnames
    """
    for single in pattern:
        # pylint: disable = use-yield-from
        for item in _glob_single(single, **kwargs):
            yield item


def glob_escape(value):
    """
    Escape string for use in glob pattern (python 3.6+)

    Parameters:
      value (str):
        value to escape

    Returns:
      str: The escaped value
    """
    return _glob.escape(value)


def _glob_single(pattern, prune=None, prune_base=('.git', '.svn', 'CVS')):
    """
    Find files or directories with glob pattern (python 3.6+)

    Parameters:
      pattern (str):
        Glob to match against

      prune (iterable):
        List of directory basenames to ignore.
        Default: None. Can be empty or ``None`` (meaning not to prune anything
        except prune_base)

      prune_base (iterable):
        Extra list of directory basenames to ignore.
        Default: ('.git', '.svn', 'CVS'). Can be empty or ``None`` (meaning
        not to prune anything except prune)

    Returns:
      iterable: Iterator over matching pathnames
    """
    normroot = native(root + "/")
    pattern = _os.path.join(_glob.escape(normroot), pattern)
    toprune = set(prune or ()) | set(prune_base or ())

    for found in _glob.iglob(pattern, recursive=True):
        assert found.startswith(normroot)
        parts = pathparts(found[len(normroot):])
        if set(parts) & toprune:
            continue
        yield "/".join(parts)


def pathparts(path):
    """
    Return path parts

    If the path ends with a slash the last part will be an empty string

    Parameters:
      path (str):
        The path to split into parts

    Returns:
      list: The path parts
    """
    aslist = []
    head, tail = _os.path.split(path)
    if not tail:
        aslist.append(tail)
        head, tail = _os.path.split(head)
    while tail:
        aslist.append(tail)
        head, tail = _os.path.split(head)
    aslist.reverse()
    return aslist


def files(base, wildcard='[!.]*', recursive=1, prune=None,
          prune_base=('.git', '.svn', 'CVS')):
    """
    Determine a filelist

    Parameters:
      base (str):
        Base path to start from

      wildcard (str):
        Glob to match against

      recursive (bool):
        Deep walk into the tree? Default: true

      prune (iterable):
        List of directory basenames to ignore.
        Default: None. Can be empty or ``None`` (meaning not to prune anything
        except prune_base)

      prune_base (iterable):
        Extra list of directory basenames to ignore.
        Default: ('.git', '.svn', 'CVS'). Can be empty or ``None`` (meaning
        not to prune anything except prune)

    Returns:
      iterable: Iterator over matching pathnames
    """
    prune = tuple(prune_base or ()) + tuple(prune or ())
    for dirpath, dirnames, filenames in walk(native(base)):
        for item in prune:
            if item in dirnames:
                dirnames.remove(item)

        filenames.sort()
        for name in _fnmatch.filter(filenames, wildcard):
            dest = _os.path.join(dirpath, name)
            if dest.startswith(root):
                dest = dest.replace(root, '', 1)
            yield '/'.join(pathparts(dest))

        if not recursive:
            break
        dirnames.sort()


def dirs(base, wildcard='[!.]*', recursive=1, prune=None,
         prune_base=('.git', '.svn', 'CVS')):
    """
    Determine a directory list

    Parameters:
      base (str):
        Base path to start from

      wildcard (str):
        Glob to match against

      recursive (bool):
        Deep walk into the tree? Default: true

      prune (iterable):
        List of directory basenames to ignore.
        Default: None. Can be empty or ``None`` (meaning not to prune anything
        except prune_base)

      prune_base (iterable):
        Extra list of directory basenames to ignore.
        Default: ('.git', '.svn', 'CVS'). Can be empty or ``None`` (meaning
        not to prune anything except prune)

    Returns:
      iterable: Iterator over matching pathnames
    """
    prune = tuple(prune_base or ()) + tuple(prune or ())
    for dirpath, dirnames, _ in walk(native(base)):
        for item in prune:
            if item in dirnames:
                dirnames.remove(item)

        dirnames.sort()
        for name in _fnmatch.filter(dirnames, wildcard):
            dest = _os.path.join(dirpath, name)
            if dest.startswith(root):
                dest = dest.replace(root, '', 1)
            yield '/'.join(pathparts(dest))

        if not recursive:
            break


def frompath(executable):
    """
    Find executable in PATH

    Parameters:
      executable (str):
        Command to search for

    Returns:
      str: Full path or ``None``
    """
    # Based on distutils.spawn.find_executable.
    path = _os.environ.get('PATH', '')
    paths = [
        _os.path.expanduser(item)
        for item in path.split(_os.pathsep)
    ]
    ext = _os.path.splitext(executable)[1]
    exts = ['']
    if _sys.platform == 'win32' or _os.name == 'os2':
        eext = ['.exe', '.bat', '.py']
        if ext not in eext:
            exts.extend(eext)

    for ext in exts:
        if not _os.path.isfile(executable + ext):
            for path in paths:
                fname = _os.path.join(path, executable + ext)
                if _os.path.isfile(fname):
                    # the file exists, we have a shot at spawn working
                    return fname
        else:
            return executable + ext

    return None