This file is indexed.

/usr/lib/python2.7/dist-packages/curtin/pack.py is in python-curtin 18.1-5-g572ae5d6-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# This file is part of curtin. See LICENSE file for copyright and license info.

import errno
import os
import shutil
import tempfile

from . import util
from . import version

CALL_ENTRY_POINT_SH_HEADER = """
#!/bin/sh
PY3OR2_MAIN="%(ep_main)s"
PY3OR2_MCHECK="%(ep_mcheck)s"
PY3OR2_PYTHONS=${PY3OR2_PYTHONS:-"%(python_exe_list)s"}
PYTHON=${PY3OR2_PYTHON}
PY3OR2_DEBUG=${PY3OR2_DEBUG:-0}
""".strip()

CALL_ENTRY_POINT_SH_BODY = """
debug() {
   [ "${PY3OR2_DEBUG}" != "0" ] || return 0
   echo "$@" 1>&2
}
fail() { echo "$@" 1>&2; exit 1; }

# if $0 is is bin/ and dirname($0)/../module exists, then prepend PYTHONPATH
mydir=${0%/*}
updir=${mydir%/*}
if [ "${mydir#${updir}/}" = "bin" -a -d "$updir/${PY3OR2_MCHECK%%.*}" ]; then
    updir=$(cd "$mydir/.." && pwd)
    case "$PYTHONPATH" in
        *:$updir:*|$updir:*|*:$updir) :;;
        *) export PYTHONPATH="$updir${PYTHONPATH:+:$PYTHONPATH}"
           debug "adding '$updir' to PYTHONPATH"
            ;;
    esac
fi

if [ ! -n "$PYTHON" ]; then
    first_exe=""
    oifs="$IFS"; IFS=":"
    best=0
    best_exe=""
    [ "${PY3OR2_DEBUG}" = "0" ] && _v="" || _v="-v"
    for p in $PY3OR2_PYTHONS; do
        command -v "$p" >/dev/null 2>&1 ||
            { debug "$p: not in path"; continue; }
        [ -z "$PY3OR2_MCHECK" ] && PYTHON=$p && break
        out=$($p -m "$PY3OR2_MCHECK" $_v -- "$@" 2>&1) && PYTHON="$p" &&
            { debug "$p is good [$p -m $PY3OR2_MCHECK $_v -- $*]"; break; }
        ret=$?
        debug "$p [$ret]: $out"
        # exit code of 1 is unuseable
        [ $ret -eq 1 ] && continue
        [ -n "$first_exe" ] || first_exe="$p"
        # higher non-zero exit values indicate more plausible usability
        [ $best -lt $ret ] && best_exe="$p" && best=$ret &&
            debug "current best: $best_exe"
    done
    IFS="$oifs"
    [ -z "$best_exe" -a -n "$first_exe" ] && best_exe="$first_exe"
    [ -n "$PYTHON" ] || PYTHON="$best_exe"
    [ -n "$PYTHON" ] ||
        fail "no availble python? [PY3OR2_DEBUG=1 for more info]"
fi
debug "executing: $PYTHON -m \\"$PY3OR2_MAIN\\" $*"
exec $PYTHON -m "$PY3OR2_MAIN" "$@"
"""


def write_exe_wrapper(entrypoint, path=None, interpreter=None,
                      deps_check_entry=None, mode=0o755):
    if not interpreter:
        interpreter = "python3:python"

    subs = {
        'ep_main': entrypoint,
        'ep_mcheck': deps_check_entry if deps_check_entry else "",
        'python_exe_list': interpreter,
    }

    content = '\n'.join(
        (CALL_ENTRY_POINT_SH_HEADER % subs, CALL_ENTRY_POINT_SH_BODY))

    if path is not None:
        with open(path, "w") as fp:
            fp.write(content)
        if mode is not None:
            os.chmod(path, mode)
    else:
        return content


def pack(fdout=None, command=None, paths=None, copy_files=None,
         add_files=None):
    # write to 'fdout' a self extracting file to execute 'command'
    # if fdout is None, return content that would be written to fdout.
    # add_files is a list of (archive_path, file_content) tuples.
    # copy_files is a list of (archive_path, file_path) tuples.
    if paths is None:
        paths = util.get_paths()

    if add_files is None:
        add_files = []

    if copy_files is None:
        copy_files = []

    tmpd = None
    try:
        tmpd = tempfile.mkdtemp()
        exdir = os.path.join(tmpd, 'curtin')

        os.mkdir(exdir)
        bindir = os.path.join(exdir, 'bin')
        os.mkdir(bindir)

        def not_dot_py(input_d, flist):
            # include .py files and directories other than __pycache__
            return [f for f in flist if not
                    (f.endswith(".py") or
                     (f != "__pycache__" and
                      os.path.isdir(os.path.join(input_d, f))))]

        shutil.copytree(paths['helpers'], os.path.join(exdir, "helpers"))
        shutil.copytree(paths['lib'], os.path.join(exdir, "curtin"),
                        ignore=not_dot_py)
        write_exe_wrapper(entrypoint='curtin.commands.main',
                          path=os.path.join(bindir, 'curtin'),
                          deps_check_entry="curtin.deps.check")

        packed_version = version.version_string()
        ver_file = os.path.join(exdir, 'curtin', 'version.py')
        util.write_file(
            ver_file,
            util.load_file(ver_file).replace("@@PACKED_VERSION@@",
                                             packed_version))

        for archpath, filepath in copy_files:
            target = os.path.abspath(os.path.join(exdir, archpath))
            if not target.startswith(exdir + os.path.sep):
                raise ValueError("'%s' resulted in path outside archive" %
                                 archpath)
            try:
                os.mkdir(os.path.dirname(target))
            except OSError as e:
                if e.errno == errno.EEXIST:
                    pass

            if os.path.isfile(filepath):
                shutil.copy(filepath, target)
            else:
                shutil.copytree(filepath, target)

        for archpath, content in add_files:
            target = os.path.abspath(os.path.join(exdir, archpath))
            if not target.startswith(exdir + os.path.sep):
                raise ValueError("'%s' resulted in path outside archive" %
                                 archpath)
            try:
                os.mkdir(os.path.dirname(target))
            except OSError as e:
                if e.errno == errno.EEXIST:
                    pass

            with open(target, "w") as fp:
                fp.write(content)

        archcmd = os.path.join(paths['helpers'], 'shell-archive')

        archout = None

        args = [archcmd]
        if fdout is not None:
            archout = os.path.join(tmpd, 'output')
            args.append("--output=%s" % archout)

        args.extend(["--bin-path=_pwd_/bin", "--python-path=_pwd_", exdir,
                     "curtin", "--"])
        if command is not None:
            args.extend(command)

        (out, _err) = util.subp(args, capture=True)

        if fdout is None:
            if isinstance(out, bytes):
                out = out.decode()
            return out

        else:
            with open(archout, "r") as fp:
                while True:
                    buf = fp.read(4096)
                    fdout.write(buf)
                    if len(buf) != 4096:
                        break
    finally:
        if tmpd:
            shutil.rmtree(tmpd)


def pack_install(fdout=None, configs=None, paths=None,
                 add_files=None, copy_files=None, args=None,
                 install_deps=True):

    if configs is None:
        configs = []

    if add_files is None:
        add_files = []

    if args is None:
        args = []

    if install_deps:
        dep_flags = ["--install-deps"]
    else:
        dep_flags = []

    command = ["curtin"] + dep_flags + ["install"]

    my_files = []
    for n, config in enumerate(configs):
        apath = "configs/config-%03d.cfg" % n
        my_files.append((apath, config),)
        command.append("--config=%s" % apath)

    command += args

    return pack(fdout=fdout, command=command, paths=paths,
                add_files=add_files + my_files, copy_files=copy_files)

# vi: ts=4 expandtab syntax=python