File: utils.py

package info (click to toggle)
python-setuptools-git 1.2-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 140 kB
  • sloc: python: 492; makefile: 25
file content (149 lines) | stat: -rw-r--r-- 4,136 bytes parent folder | download | duplicates (4)
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
import sys
import os
import stat
import shutil
import unicodedata
import posixpath

if sys.version_info >= (3,):
    from urllib.parse import quote as url_quote
    unicode = str
else:
    from urllib import quote as url_quote

__all__ = ['check_call', 'check_output', 'rmtree',
           'b', 'posix', 'fsdecode', 'hfs_quote', 'compose', 'decompose']


try:
    from subprocess import CalledProcessError
except ImportError:
    # BBB for Python < 2.5
    class CalledProcessError(Exception):
        """
        This exception is raised when a process run by check_call() or
        check_output() returns a non-zero exit status.

        The exit status will be stored in the returncode attribute;
        check_output() will also store the output in the output attribute.
        """
        def __init__(self, returncode, cmd, output=None):
            self.returncode = returncode
            self.cmd = cmd
            self.output = output

        def __str__(self):
            return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)


try:
    from subprocess import check_call
except ImportError:
    # BBB for Python < 2.5
    def check_call(*popenargs, **kwargs):
        from subprocess import call
        retcode = call(*popenargs, **kwargs)
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        if retcode:
            raise CalledProcessError(retcode, cmd)
        return retcode


try:
    from subprocess import check_output
except ImportError:
    # BBB for Python < 2.7
    def check_output(*popenargs, **kwargs):
        from subprocess import PIPE
        from subprocess import Popen
        if 'stdout' in kwargs:
            raise ValueError(
                    'stdout argument not allowed, it will be overridden.')
        process = Popen(stdout=PIPE, *popenargs, **kwargs)
        output, unused_err = process.communicate()
        retcode = process.poll()
        if retcode:
            cmd = kwargs.get("args")
            if cmd is None:
                cmd = popenargs[0]
            raise CalledProcessError(retcode, cmd)
        return output


# Windows cannot delete read-only Git objects
def rmtree(path):
    if sys.platform == 'win32':
        def onerror(func, path, excinfo):
            os.chmod(path, stat.S_IWRITE)
            func(path)
        shutil.rmtree(path, False, onerror)
    else:
        shutil.rmtree(path, False)


# Fake byte literals for Python < 2.6
def b(s, encoding='utf-8'):
    if sys.version_info >= (3,):
        return s.encode(encoding)
    return s


# Convert path to POSIX path on Windows
def posix(path):
    if sys.platform == 'win32':
        return path.replace(os.sep, posixpath.sep)
    return path


# Decode path from fs encoding under Python 3
def fsdecode(path):
    if sys.version_info >= (3,):
        if not isinstance(path, str):
            if sys.platform == 'win32':
                errors = 'strict'
            else:
                errors = 'surrogateescape'
            return path.decode(sys.getfilesystemencoding(), errors)
    return path


# HFS Plus quotes unknown bytes like so: %F6
def hfs_quote(path):
    if isinstance(path, unicode):
        raise TypeError('bytes are required')
    try:
        path.decode('utf-8')
    except UnicodeDecodeError:
        path = url_quote(path) # Not UTF-8
        if sys.version_info >= (3,):
            path = path.encode('ascii')
    return path


# HFS Plus uses decomposed UTF-8
def compose(path):
    if isinstance(path, unicode):
        return unicodedata.normalize('NFC', path)
    try:
        path = path.decode('utf-8')
        path = unicodedata.normalize('NFC', path)
        path = path.encode('utf-8')
    except UnicodeError:
        pass # Not UTF-8
    return path


# HFS Plus uses decomposed UTF-8
def decompose(path):
    if isinstance(path, unicode):
        return unicodedata.normalize('NFD', path)
    try:
        path = path.decode('utf-8')
        path = unicodedata.normalize('NFD', path)
        path = path.encode('utf-8')
    except UnicodeError:
        pass # Not UTF-8
    return path