Skip to content

Instantly share code, notes, and snippets.

@dguerri
Last active May 2, 2016 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dguerri/b2cf2ad7524e8f43a127327fc7b78642 to your computer and use it in GitHub Desktop.
Save dguerri/b2cf2ad7524e8f43a127327fc7b78642 to your computer and use it in GitHub Desktop.
Copy applications and their dependencies (e.g. for chroot envs)
#!/bin/bash
#
# Copy applications and their dependencies (e.g. for chroot envs)
#
#
# Copyright Davide Guerri <davide.guerri@gmail.com>
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
set -ue
if [ $# -lt 3 ]; then
echo "Usage: $0 <src root dir> <src app> <dst dir>"
exit 1
fi
src_dir="$1"
app="$2"
dst_dir="$3"
app_path=$(cd "$src_dir" && find -name "$app" | head -1)
echo "Source $app path: $src_dir/$app_path"
copy_deps() {
local dep="$1"
local level="${2-1}"
printf "[%s] Analyzing dependencies of '%s'\n" "$level" "$dep"
for i in $(readelf -d "$dep" | awk '/NEEDED.+ Shared library/ {print $5}' | \
tr -d '[]'); do
lib=$(basename "$i")
file=$(cd "$src_dir" && find -name "$lib" | head -1)
if [ -z "$file" ]; then
echo "'$lib' not found, skipping..."
continue
fi
dir=$(dirname "$file")
target_dir="$dst_dir/$dir"
src_file="$src_dir/$file"
mkdir -p "$target_dir"
if find "$target_dir" -name "$(basename "$file")" | grep . >/dev/null; then
printf "[%s] Deps '%s' is already present in '%s', skipping...\n" \
"$level" "$(basename "$file")" "$dst_dir"
else
printf "[%s] Copying '%s' to '%s'\n" "$level" "$src_file" "$target_dir"
cp "$src_file" "$target_dir"
fi
copy_deps "$src_file" "$(expr $level + 1)"
done
}
copy_deps "$src_dir/$app_path"
echo "Copying application"
dir=$(dirname "$app_path")
echo "Dest $app path: $dst_dir/$app_path"
mkdir -p "$dst_dir/$dir"
cp "$src_dir/$app_path" "$dst_dir/$app_path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment