/usr/lib/python2.7/dist-packages/curtin/pack.py is in python-curtin 0.1.0~bzr126-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 | # Copyright (C) 2013 Canonical Ltd.
#
# Author: Scott Moser <scott.moser@canonical.com>
#
# Curtin is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# Curtin 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Curtin. If not, see <http://www.gnu.org/licenses/>.
import errno
import os
import shutil
import tempfile
from . import util
CALL_ENTRY_POINT_PY = """
def call_entry_point(name):
import os, sys
(istr, dot, ent) = name.rpartition('.')
try:
__import__(istr)
except ImportError as e:
# if that import failed, check dirname(__file__/..)
# to support ./bin/curtin with modules in .
_tdir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(_tdir)
try:
__import__(istr)
except ImportError as e:
sys.stderr.write("Unable to find %s\\n" % name)
sys.exit(2)
sys.exit(getattr(sys.modules[istr], ent)())
"""
MAIN_FOOTER = """
if __name__ == '__main__':
call_entry_point("%s")
"""
def write_exe_wrapper(entrypoint, path=None, interpreter="/usr/bin/python",
mode=0o755):
content = '\n'.join((
"#! %s" % interpreter,
CALL_ENTRY_POINT_PY,
MAIN_FOOTER % entrypoint,
))
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.main',
path=os.path.join(bindir, 'curtin'))
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):
if configs is None:
configs = []
if add_files is None:
add_files = []
if args is None:
args = []
command = ["curtin", "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
|