Skip to content

Commit

Permalink
Starch small fixes (cooperative-computing-lab#2780)
Browse files Browse the repository at this point in the history
* print help and quit when executable option is missing

* adds starch configuration file example

* Adds SFX_EXEC to change what command to execute in the package.

The use case is for when several executables are added to the package.
E.g.:

starch -x date -x uname -c date example.sfx

./example.sfx
Mon Jan 10 11:12:02 EST 2022

SFX_EXEC=uname ./example.sfx
Linux

This also useful to run a shell with all the PATH and LD_LIBRARY_PATH
variables set correctly to run a set of commands:

SFX_EXEC=/bin/sh ./example.sfx
sh$ which date
/home/myuser/example.sfx.27441.dir/bin/date

* Adds --sfx-help to show help message for sfx packages.

E.g:
starch -x date -c date example.sfx
./example.sfx --sfx-help
Executing the sfx file by itself executes the command date

environment variables to set packages options:
SFX_EXEC=CMD Execute CMD instead of the default.
SFX_EXTRACT_ONLY=1 Extract package, but do not run a command.
SFX_KEEP=1 Do not remove extracted package when command finishes.
SFX_DIR=DIR Extract to DIR, instead of a temporary directory. Implies
SFX_KEEP=1

* Correctly quote SFX_EXEC

* cleanup TR_starch_extract_and_remove.sh test

* make SFX_DIR available for commands
  • Loading branch information
btovar authored Jan 18, 2022
1 parent d1c4da5 commit 56829da
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
27 changes: 27 additions & 0 deletions makeflow/example/starch-example.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Example of a starch command configuration.
# Create a self-contained package to run the command `date` using the timezone
# specified in starch-example.env with:
#
# starch -C starch-example.config date.sfx
# ./date.sfx
#

[starch]
# command to execute when executing the resulting starch package
command = date

# comma-separated list of executables to include in the package. If not an
# absolute path, then they musth be found in PATH
executables = /bin/date

# comma-separated list of dynamically linked libraries (e.g. *.so files) to
# include in the package
libraries =

# comma-separated list of sh files to be sourced before the command's
# execution. Useful to export necessary environment variables.
environments = starch-example.env

# comma-separated list of mappings of PACKAGE_PATH:HOST_PATH of files to include
# in the package.
data = my_data/motd:/etc/motd
1 change: 1 addition & 0 deletions makeflow/example/starch-example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export TZ=Australia/Melbourne
36 changes: 29 additions & 7 deletions makeflow/src/starch
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ extract() {
}
run() {
# make the extraction directory easily available in run.sh
export SFX_DIR
$SFX_DIR/run.sh "$@"
SFX_EXIT_STATUS=$?
}
Expand Down Expand Up @@ -153,7 +155,27 @@ if [ -d "$SFX_DIR/lib" ]; then
fi
fi
fi
%s "$@"
if [ "$1" = "--sfx-help" ]
then
echo starch package options:
echo executing the sfx file by itself executes the command:
echo "{cmd}"
echo
echo environment variables to set packages options:
echo SFX_EXEC=CMD Execute CMD instead of the default.
echo SFX_EXTRACT_ONLY=1 Extract package, but do not run a command.
echo SFX_KEEP=1 Do not remove extracted package when command finishes.
echo SFX_DIR=DIR Extract to DIR, instead of a temporary directory. Implies SFX_KEEP=1
exit 0
fi
if [ -n "$SFX_EXEC" ]
then
$SFX_EXEC "$@"
else
{cmd} "$@"
fi
'''

# Create SFX
Expand Down Expand Up @@ -206,7 +228,7 @@ def create_sfx(sfx_path, executables, libraries, data, environments, command):
archive.addfile(env_info, open(real_path, 'rb'))

run_info = TarInfo('run.sh')
run_info_data = RUN_SH % command
run_info_data = RUN_SH.format(cmd=command)
run_info.mode = int('755', 8)
run_info.mtime = time()
run_info.size = len(run_info_data)
Expand Down Expand Up @@ -329,7 +351,6 @@ def autodetect_libraries_darwin(executable):
return libs

# Configuration Parser

class StarchConfigParser(ConfigParser):
def safe_get(self, section, name, default = None):
try:
Expand All @@ -338,7 +359,6 @@ class StarchConfigParser(ConfigParser):
return default

# Parse commandline options

def parse_command_line_options():
global STARCH_AUTODETECT
global STARCH_PLATFORM
Expand Down Expand Up @@ -391,19 +411,21 @@ def parse_command_line_options():
options.command = config.safe_get('starch', 'command', '')

if not options.executables:
logging.error('no executables specified')
logging.error('At least one executable should be specified with -x')
parser.print_help()
sys.exit(1)


if not options.command:
options.command = os.path.basename(options.executables[0])
logging.warn('no command specified, so using: %s' % options.command)
logging.warn('no command specified with -c, using: %s' % options.command)

logging.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())

Expand Down
4 changes: 2 additions & 2 deletions makeflow/test/TR_starch_extract_and_remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ tarfile=starch.tar.gz

prepare()
{
cd ..; tar czvf $tarfile src; cd -; mv ../$tarfile .
(cd .. && tar czvf $tarfile src) && mv ../$tarfile .
exit 0
}

run()
{
../src/starch -v -x tar -x rm -c 'tar_test() { for f in $@; do if ! tar xvf $f; then exit 1; fi ; done; rm $@; }; tar_test' $sfxfile
../src/starch -v -x tar -x rm -c 'tar_test() { for f in "$@"; do if ! tar xvf $f; then exit 1; fi ; done; }; tar_test' $sfxfile
exec ./$sfxfile $tarfile
}

Expand Down

0 comments on commit 56829da

Please sign in to comment.