aboutsummaryrefslogtreecommitdiffstats
path: root/fpga/usrp3/tools/scripts/shared-ip-loc-manage.sh
blob: 447f087e02af88db05b87f82f2ec2e94a56ad10a (plain)
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
#!/bin/bash

function help {
    cat <<EOHELP

Usage: shared-ip-loc-manage.sh [--help|-h] [--timeout=<TIMEOUT>] --path=<PATH> <ACTION>

--path      : Path to IP location
--timeout   : Timeout in seconds for the operation to complete [Optional]
              (Default: 1200)
--force     : Force operation
--help -h   : Shows this message.

ACTION      : Choose from reserve, release

EOHELP
}

function wait_for_lock {
    if [ -d "$ip_dir" ]; then
        remaining=$(($timeout))
	    trap 'echo"";echo "BUILDER: Waiting for concurrent IP build to finish... (skipped)";break;' SIGINT; \
        while [ -f "$ip_dir/.build_lock" ]; do
            if [ $remaining -gt 0 ]; then 
                echo -ne "Waiting for concurrent IP build to finish... (${remaining}s [Ctrl-C to proceed])\033[0K\r"
                sleep 1
                : $((remaining--))
            else
                break
            fi
        done
	    trap - SIGINT; \
        if [ $remaining -eq 0 ]; then
            echo "BUILDER: Waiting for concurrent IP build to finish... (timeout)"
        fi
    fi
}

function lock {
    if [ -d "$ip_dir" ]; then
        touch $ip_dir/.build_lock
    fi
}

function unlock {
    rm -f $ip_dir/.build_lock
}

function reserve {
    if [ -d "$ip_dir" ]; then
        wait_for_lock
        if [ $remaining -eq 0 ]; then
            echo "Force creating new IP location: $ip_dir"
            unlock
            rm -rf $ip_dir
            mkdir -p $ip_dir
        fi
    fi    
    if [ ! -d "$ip_dir" ]; then
        mkdir -p $ip_dir
    fi
    echo "BUILDER: Reserving IP location: $ip_dir"
    lock
}

function release {
    echo "BUILDER: Releasing IP location: $ip_dir"
    unlock
}

# Parse options
ip_dir=""
action=""
timeout=1800
remaining=0
force=0

for arg in "$@"; do
    if [[ $arg == "--help" ]]; then
        help
        exit 0
    elif [[ $arg == "--force" ]]; then
        force=1
    elif [[ $arg =~ "--path="(.+) ]]; then
        ip_dir=`readlink -m ${BASH_REMATCH[1]}`
    elif [[ $arg =~ "--timeout="(.+) ]]; then
        timeout=${BASH_REMATCH[1]}
    else
        action=$arg
        break
    fi
done

# Validate inputs
if [ -z $ip_dir ]; then
    echo "ERROR: Please specify a valid path using the --path option."
    exit 1
fi

case $action in
    reserve)
        if [ $force -eq 1 ]; then
            echo "Force creating new IP location: $ip_dir"
            rm -rf $ip_dir
            mkdir -p $ip_dir
            lock
        else
            reserve
        fi
        ;;
    release)
        release
        ;;
    *)
        echo "ERROR: Please specify a valid action (reserve, release)"
        exit 1
        ;;
esac