aboutsummaryrefslogtreecommitdiff
path: root/NvCloth/scripts/packman/packman.sh
blob: 95bb1619155db89a4bcfbc34432e369a13a4a653 (plain) (blame)
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
#!/bin/bash

PM_PACKMAN_VERSION=5.4.2

# Specify where packman command exists
export PM_INSTALL_PATH=$(dirname ${BASH_SOURCE})

add_packages_root_to_file()
{
	FILE_PATH=$1
	if [ -f "$FILE_PATH" ]; then
		if ! grep -Fq "PM_PACKAGES_ROOT" $FILE_PATH ; then
			echo "Adjusting $FILE_PATH"
			echo -e "export PM_PACKAGES_ROOT=\$HOME/packman-repo\n" >> $FILE_PATH
		fi
	fi
}

# The packages root may already be configured by the user
if [ -z "$PM_PACKAGES_ROOT" ]; then
	# Set variable permanently using .profile for this user (if exists)
	add_packages_root_to_file ~/.profile
	add_packages_root_to_file ~/.bashrc
	# Set variable temporarily in this process so that the following execution will work 
	export PM_PACKAGES_ROOT="${HOME}/packman-repo"
fi

# Ensure the packages root path exists:
if [ ! -d "$PM_PACKAGES_ROOT" ]; then
	echo "Creating packman packages repository at $PM_PACKAGES_ROOT"
	mkdir -p "$PM_PACKAGES_ROOT"
fi

# The packman module may be externally configured
if [ -z "$PM_MODULE_EXT" ]; then
	PM_MODULE_DIR="$PM_PACKAGES_ROOT/packman-common/$PM_PACKMAN_VERSION"
	export PM_MODULE="$PM_MODULE_DIR/packman.py"
else
	export PM_MODULE="$PM_MODULE_EXT"
fi

fetch_file_from_s3() 
{
	SOURCE=$1
	SOURCE_URL=http://packman-bootstrap.s3.amazonaws.com/$SOURCE
	TARGET=$2
	echo "Fetching $SOURCE from S3 ..."
	if command -v wget >/dev/null 2>&1; then
		wget --quiet -O$TARGET $SOURCE_URL
	else
		curl -o $TARGET $SOURCE_URL -s -S
	fi		
}

# Ensure the packman package exists:
if [ ! -f "$PM_MODULE" ]; then
	PM_MODULE_PACKAGE="packman-common@$PM_PACKMAN_VERSION.zip"
	TARGET="/tmp/$PM_MODULE_PACKAGE"
	# We always fetch packman from S3:
	fetch_file_from_s3 $PM_MODULE_PACKAGE $TARGET
	if [ "$?" -eq "0" ]; then
		echo "Unpacking ..."
		mkdir -p "$PM_MODULE_DIR"
		unzip -q $TARGET -d "$PM_MODULE_DIR"
		rm $TARGET
	else
		echo "Failure while fetching packman module from S3!"
		exit 1
	fi
fi

# For now assume python is installed on the box and we just need to find it
if command -v python2.7 >/dev/null 2>&1; then
	export PM_PYTHON=python2.7
elif command -v python2 >/dev/null 2>&1; then
    export PM_PYTHON=python2
else
	export PM_PYTHON=python
fi

# Ensure 7za package exists:
PM_7za_VERSION=16.02.2
export PM_7za_PATH="$PM_PACKAGES_ROOT/7za/$PM_7za_VERSION"
if [ ! -d "$PM_7za_PATH" ]; then
    export PM_7za_PATH="$PM_PACKAGES_ROOT/chk/7za/$PM_7za_VERSION"
    if [ ! -d "$PM_7za_PATH" ]; then
        $PM_PYTHON "$PM_MODULE" install 7za $PM_7za_VERSION -r packman:cloudfront
        if [ "$?" -ne 0 ]; then
           echo "Failure while installing required 7za package"
           exit 1
        fi
    fi
fi

# Generate temporary file name for environment variables:
PM_VAR_PATH=`mktemp -u -t tmp.$$.pmvars.XXXXXX`

$PM_PYTHON -u "$PM_MODULE" $* --var-path="$PM_VAR_PATH"
exit_code=$?
# Export the variables if the file was used and remove the file:
if [ -f "$PM_VAR_PATH" ]; then
	while read -r line
	do
        if [ ${#line} -gt 0 ]; then
    		export "$line"
        fi
	done < "$PM_VAR_PATH"
    rm -f "$PM_VAR_PATH"
fi

# Return the exit code from python
if [ "$exit_code" != 0 ]; then
    exit "$exit_code"
fi