/usr/bin/starch is in coop-computing-tools 4.0-1.1.
This file is owned by root:root, with mode 0o755.
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 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 | #! /usr/bin/python
# Copyright (c) 2010- The University of Notre Dame.
# This software is distributed under the GNU General Public License.
# See the file COPYING for details.
""" Starch """
from cStringIO import StringIO
from optparse import OptionParser
from subprocess import Popen, PIPE
from tarfile import REGTYPE, TarInfo, open as Tar
from time import time
from tempfile import NamedTemporaryFile
from shutil import copyfile, copyfileobj
from ConfigParser import ConfigParser, NoSectionError, NoOptionError
import os
import sys
# Todo
"""
1. Keep track of files added to avoid duplication (efficiency).
"""
# Global variables
STARCH_AUTODETECT = True
STARCH_VERBOSE = False
STARCH_PLATFORM = 'Linux'
# Shell scripts
SFX_SH='''#!/bin/sh
SFX_FILE=$0
SFX_EXIT_STATUS=0
if [ -z "$SFX_DIR" ]; then
basename=`basename $SFX_FILE`
SFX_DIR=`hostname`.$USER.$basename.dir
if [ ! -z "$CONDOR_SCRATCH_DIR" ]; then
SFX_DIR=$CONDOR_SCRATCH_DIR/$SFX_DIR
elif [ ! -z "$_CONDOR_SCRATCH_DIR" ]; then
SFX_DIR=$_CONDOR_SCRATCH_DIR/$SFX_DIR
elif [ ! -z "$XGRID_AGENT_NAME" ]; then
SFX_DIR=`pwd`/$SFX_DIR
else
SFX_DIR=/tmp/$SFX_DIR
fi
if [ ! -z "$SFX_UNIQUE" ] && [ "$SFX_UNIQUE" = 1 ]; then
SFX_DIR=`mktemp -d -u $SFX_DIR.XXXXXX`
fi
fi
extract() {
if [ ! -d "$SFX_DIR" -o "$SFX_EXTRACT_FORCE" -eq 1 ]; then
mkdir -p $SFX_DIR 2> /dev/null
archive=`awk '/^__ARCHIVE__/ {print NR + 1; exit 0; }' $SFX_FILE`
tail -n+$archive $SFX_FILE | tar xj -C $SFX_DIR
SFX_EXIT_STATUS=$?
fi
}
run() {
$SFX_DIR/run.sh $@
SFX_EXIT_STATUS=$?
}
if [ -z "$SFX_EXTRACT_ONLY" ]; then
SFX_EXTRACT_ONLY=0
fi
if [ -z "$SFX_EXTRACT_FORCE" ]; then
SFX_EXTRACT_FORCE=0
fi
if [ -z "$SFX_KEEP" ]; then
SFX_KEEP=0
fi
if [ "$SFX_EXTRACT_ONLY" -eq 1 ]; then
extract
echo $SFX_DIR
SFX_KEEP=1
else
extract && run $@
fi
if [ "$SFX_KEEP" -ne 1 ]; then
rm -fr $SFX_DIR
fi
exit $SFX_EXIT_STATUS
__ARCHIVE__
'''
RUN_SH = '''#!/bin/sh
SFX_DIR=`dirname $0`
if [ -d "$SFX_DIR/env" ]; then
for f in $SFX_DIR/env/*; do
. $f
done
fi
if [ -d "$SFX_DIR/bin" ]; then
export PATH=$SFX_DIR/bin:$PATH
fi
if [ -d "$SFX_DIR/lib" ]; then
if [ x"`uname -s`" == x"Darwin" ]; then
if [ -z "$DYLD_LIBRARY_PATH" ]; then
export DYLD_LIBRARY_PATH=$SFX_DIR/lib
else
export DYLD_LIBRARY_PATH=$SFX_DIR/lib:$DYLD_LIBRARY_PATH
fi
else
if [ -z "$LD_LIBRARY_PATH" ]; then
export LD_LIBRARY_PATH=$SFX_DIR/lib
else
export LD_LIBRARY_PATH=$SFX_DIR/lib:$LD_LIBRARY_PATH
fi
fi
fi
%s
'''
# Create SFX
def create_sfx(sfx_path, executables, libraries, data, environments, command):
""" Create self-extracting executable
Directory structure:
bin Executables
lib Libraries
env Environment Scripts (static)
run.sh Script that contains command
"""
tmp_file = NamedTemporaryFile()
tmp_file.close()
arc_path = tmp_file.name
archive = Tar(arc_path, 'w:bz2')
debug('adding executables...')
executables = find_executables(executables)
for exe_path, real_path in executables:
exe_name = os.path.basename(exe_path)
exe_info = archive.gettarinfo(real_path, os.path.join('bin', exe_name))
exe_info.mode = 0755
debug(' adding executable: %s (%s)' % (exe_name, real_path))
archive.addfile(exe_info, open(real_path))
debug('adding libraries...')
libraries = find_libraries(libraries, executables)
for lib_path, real_path in libraries:
lib_name = os.path.basename(lib_path)
lib_info = archive.gettarinfo(real_path, os.path.join('lib', lib_name))
debug(' adding library: %s (%s)' % (lib_name, real_path))
archive.addfile(lib_info, open(real_path))
debug('adding data...')
for data_path, real_path in map(lambda s: s.split(':'), data):
add_data_to_archive(archive, os.path.normpath(data_path), os.path.normpath(real_path))
debug('adding environment scripts...')
for env_path, real_path in find_files(environments, 'PWD'):
env_name = os.path.basename(env_path)
env_info = archive.gettarinfo(real_path, os.path.join('env', env_name))
debug(' adding environment script: %s (%s)' % (env_name, real_path))
archive.addfile(env_info, open(real_path))
run_info = TarInfo('run.sh')
run_info_data = RUN_SH % command
run_info.mode = 0755
run_info.mtime = time()
run_info.size = len(run_info_data)
debug('adding run.sh...')
archive.addfile(run_info, StringIO(run_info_data))
archive.close()
if os.path.exists(sfx_path):
os.unlink(sfx_path)
debug('creating sfx...')
sfx_file = open(sfx_path, 'a+')
copyfileobj(StringIO(SFX_SH), sfx_file)
copyfileobj(open(arc_path, 'r'), sfx_file)
sfx_file.close()
debug('cleaning up...')
os.chmod(sfx_path, 0755)
os.unlink(arc_path)
def add_data_to_archive(archive, data_path, real_path):
if os.path.isdir(real_path):
for root, dirs, files in os.walk(real_path):
for n in files + dirs:
dp = os.path.join(root.replace(real_path, data_path), n)
rp = os.path.join(root, n)
add_data_to_archive(archive, dp, rp)
else:
data_info = archive.gettarinfo(os.path.realpath(real_path), data_path)
debug(' adding data: %s (%s)' % (data_path, real_path))
archive.addfile(data_info, open(real_path))
# Print utilities
def debug(s):
if STARCH_VERBOSE:
print '[D]', s
def warn(s):
print >>sys.stderr, '[W]', s
def error(s):
print >>sys.stderr, '[E]', s
sys.exit(1)
# Find file utilities
def find_files(files, env_var, default_paths = None):
if default_paths:
paths = default_paths
else:
paths = [os.curdir]
if env_var in os.environ:
paths.extend(os.environ[env_var].split(':'))
for file in files:
is_found = False
for path in paths:
file_path = os.path.join(path, file)
if os.path.exists(file_path):
is_found = True
yield file_path, os.path.realpath(file_path)
break
if not is_found:
error('could not find file: %s' % file)
raise StopIteration
def find_executables(executables):
exes = []
for ep, rp in find_files(executables, 'PATH'):
exes.append((ep, rp))
return exes
def find_libraries(libraries, executables):
libs = []
for lp, rp in find_files(libraries, 'LD_LIBRARY_PATH', ['/lib', '/lib64', '/usr/lib', '/usr/lib64']):
libs.append((lp, rp))
if STARCH_AUTODETECT:
for exe_path, real_path in executables:
if STARCH_PLATFORM == 'Darwin':
libs.extend(autodetect_libraries_darwin(exe_path))
else:
libs.extend(autodetect_libraries_linux(exe_path))
return libs
def autodetect_libraries_linux(executable):
libs = []
try:
p = Popen(['ldd', executable], stdout = PIPE)
for line in p.communicate()[0].split('\n'):
try:
lib_path = line.split('=>')[-1].strip().split()[0]
if os.path.exists(lib_path):
libs.append((lib_path, os.path.realpath(lib_path)))
except Exception, e:
pass
except Exception, e:
error('could not execute ldd on %s: %s' % (executable, str(e)))
return libs
def autodetect_libraries_darwin(executable):
libs = []
try:
p = Popen(['otool', '-L', executable], stdout = PIPE)
for line in p.communicate()[0].split('\n'):
try:
lib_path = line.split('(')[0].strip()
if os.path.exists(lib_path):
libs.append((lib_path, os.path.realpath(lib_path)))
except Exception, e:
pass
except Exception, e:
error('could not execute otool on %s: %s' % (executable, str(e)))
return libs
# Configuration Parser
class StarchConfigParser(ConfigParser):
def get(self, section, name, default = None):
try:
return ConfigParser.get(self, section, name)
except (NoSectionError, NoOptionError), e:
return default
# Parse commandline options
def parse_command_line_options():
global STARCH_VERBOSE
global STARCH_AUTODETECT
global STARCH_PLATFORM
parser = OptionParser('%prog [options] <sfx_path>')
parser.add_option('-A', dest = 'autodetect', action = 'store_false',
help = 'Do not automatically detect library dependencies (default: enabled).', default=True)
parser.add_option('-C', dest = 'config', action = 'store',
help = 'Use configuration file.', metavar = 'cfg', default = None)
parser.add_option('-c', dest = 'command', action = 'store',
help = 'Specify command to execute.', metavar = 'cmd', default = '')
parser.add_option('-d', dest = 'data', action = 'append',
help = 'Add data (new path:old path).', metavar = 'npath:opath', default = [])
parser.add_option('-e', dest = 'environments', action = 'append',
help = 'Add environment script.', metavar = 'env', default = [])
parser.add_option('-l', dest = 'libraries', action = 'append',
help = 'Add library.', metavar = 'lib', default = [])
parser.add_option('-x', dest = 'executables', action = 'append',
help = 'Add executable.', metavar = 'exe', default = [])
parser.add_option('-v', dest = 'verbose', action = 'store_true',
help = 'Display verbose messages.', default = False)
(options, args) = parser.parse_args()
if len(args) != 1:
parser.print_help()
sys.exit(1)
STARCH_VERBOSE = options.verbose
STARCH_AUTODETECT = options.autodetect
STARCH_PLATFORM = os.uname()[0]
if options.config:
if not os.path.exists(options.config):
error('config file \'%s\' does not exist' % options.config)
config = StarchConfigParser()
config.read(options.config)
options.executables.extend(config.get('starch', 'executables', '').split())
options.libraries.extend(config.get('starch', 'libraries', '').split())
options.data.extend(config.get('starch', 'data', '').split())
options.environments.extend(config.get('starch', 'environments', '').split())
if not options.command:
options.command = config.get('starch', 'command', '')
if not options.executables:
error('no executables specified')
if not options.command:
options.command = os.path.basename(options.executables[0]) + ' $@'
warn('no command specified, so using: %s' % options.command)
debug('command ... ' + options.command)
return args[0], options.executables, options.libraries, \
options.data, options.environments, options.command
# Main execution
if __name__ == '__main__':
create_sfx(*parse_command_line_options())
# vim: set sts=4 sw=4 ts=8 expandtab ft=python:
|