Browse code

Merge "Split functions-common: python functions"

Jenkins authored on 2015/02/07 05:45:45
Showing 3 changed files
... ...
@@ -13,6 +13,7 @@
13 13
 # Include the common functions
14 14
 FUNC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd)
15 15
 source ${FUNC_DIR}/functions-common
16
+source ${FUNC_DIR}/inc/python
16 17
 
17 18
 # Save trace setting
18 19
 XTRACE=$(set +o | grep xtrace)
... ...
@@ -15,7 +15,6 @@
15 15
 # - OpenStack Functions
16 16
 # - Package Functions
17 17
 # - Process Functions
18
-# - Python Functions
19 18
 # - Service Functions
20 19
 # - System Functions
21 20
 #
... ...
@@ -1589,204 +1588,6 @@ function screen_stop {
1589 1589
 }
1590 1590
 
1591 1591
 
1592
-# Python Functions
1593
-# ================
1594
-
1595
-# Get the path to the pip command.
1596
-# get_pip_command
1597
-function get_pip_command {
1598
-    which pip || which pip-python
1599
-
1600
-    if [ $? -ne 0 ]; then
1601
-        die $LINENO "Unable to find pip; cannot continue"
1602
-    fi
1603
-}
1604
-
1605
-# Get the path to the direcotry where python executables are installed.
1606
-# get_python_exec_prefix
1607
-function get_python_exec_prefix {
1608
-    if is_fedora || is_suse; then
1609
-        echo "/usr/bin"
1610
-    else
1611
-        echo "/usr/local/bin"
1612
-    fi
1613
-}
1614
-
1615
-# Wrapper for ``pip install`` to set cache and proxy environment variables
1616
-# Uses globals ``OFFLINE``, ``TRACK_DEPENDS``, ``*_proxy``
1617
-# pip_install package [package ...]
1618
-function pip_install {
1619
-    local xtrace=$(set +o | grep xtrace)
1620
-    set +o xtrace
1621
-    local offline=${OFFLINE:-False}
1622
-    if [[ "$offline" == "True" || -z "$@" ]]; then
1623
-        $xtrace
1624
-        return
1625
-    fi
1626
-
1627
-    if [[ -z "$os_PACKAGE" ]]; then
1628
-        GetOSVersion
1629
-    fi
1630
-    if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
1631
-        # TRACK_DEPENDS=True installation creates a circular dependency when
1632
-        # we attempt to install virtualenv into a virualenv, so we must global
1633
-        # that installation.
1634
-        source $DEST/.venv/bin/activate
1635
-        local cmd_pip=$DEST/.venv/bin/pip
1636
-        local sudo_pip="env"
1637
-    else
1638
-        local cmd_pip=$(get_pip_command)
1639
-        local sudo_pip="sudo -H"
1640
-    fi
1641
-
1642
-    local pip_version=$(python -c "import pip; \
1643
-                        print(pip.__version__.strip('.')[0])")
1644
-    if (( pip_version<6 )); then
1645
-        die $LINENO "Currently installed pip version ${pip_version} does not" \
1646
-            "meet minimum requirements (>=6)."
1647
-    fi
1648
-
1649
-    $xtrace
1650
-    $sudo_pip \
1651
-        http_proxy=${http_proxy:-} \
1652
-        https_proxy=${https_proxy:-} \
1653
-        no_proxy=${no_proxy:-} \
1654
-        $cmd_pip install \
1655
-        $@
1656
-
1657
-    INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
1658
-    if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
1659
-        local test_req="$@/test-requirements.txt"
1660
-        if [[ -e "$test_req" ]]; then
1661
-            $sudo_pip \
1662
-                http_proxy=${http_proxy:-} \
1663
-                https_proxy=${https_proxy:-} \
1664
-                no_proxy=${no_proxy:-} \
1665
-                $cmd_pip install \
1666
-                -r $test_req
1667
-        fi
1668
-    fi
1669
-}
1670
-
1671
-# should we use this library from their git repo, or should we let it
1672
-# get pulled in via pip dependencies.
1673
-function use_library_from_git {
1674
-    local name=$1
1675
-    local enabled=1
1676
-    [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
1677
-    return $enabled
1678
-}
1679
-
1680
-# setup a library by name. If we are trying to use the library from
1681
-# git, we'll do a git based install, otherwise we'll punt and the
1682
-# library should be installed by a requirements pull from another
1683
-# project.
1684
-function setup_lib {
1685
-    local name=$1
1686
-    local dir=${GITDIR[$name]}
1687
-    setup_install $dir
1688
-}
1689
-
1690
-# setup a library by name in editiable mode. If we are trying to use
1691
-# the library from git, we'll do a git based install, otherwise we'll
1692
-# punt and the library should be installed by a requirements pull from
1693
-# another project.
1694
-#
1695
-# use this for non namespaced libraries
1696
-function setup_dev_lib {
1697
-    local name=$1
1698
-    local dir=${GITDIR[$name]}
1699
-    setup_develop $dir
1700
-}
1701
-
1702
-# this should be used if you want to install globally, all libraries should
1703
-# use this, especially *oslo* ones
1704
-function setup_install {
1705
-    local project_dir=$1
1706
-    setup_package_with_req_sync $project_dir
1707
-}
1708
-
1709
-# this should be used for projects which run services, like all services
1710
-function setup_develop {
1711
-    local project_dir=$1
1712
-    setup_package_with_req_sync $project_dir -e
1713
-}
1714
-
1715
-# determine if a project as specified by directory is in
1716
-# projects.txt. This will not be an exact match because we throw away
1717
-# the namespacing when we clone, but it should be good enough in all
1718
-# practical ways.
1719
-function is_in_projects_txt {
1720
-    local project_dir=$1
1721
-    local project_name=$(basename $project_dir)
1722
-    return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
1723
-}
1724
-
1725
-# ``pip install -e`` the package, which processes the dependencies
1726
-# using pip before running `setup.py develop`
1727
-#
1728
-# Updates the dependencies in project_dir from the
1729
-# openstack/requirements global list before installing anything.
1730
-#
1731
-# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
1732
-# setup_develop directory
1733
-function setup_package_with_req_sync {
1734
-    local project_dir=$1
1735
-    local flags=$2
1736
-
1737
-    # Don't update repo if local changes exist
1738
-    # Don't use buggy "git diff --quiet"
1739
-    # ``errexit`` requires us to trap the exit code when the repo is changed
1740
-    local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
1741
-
1742
-    if [[ $update_requirements != "changed" ]]; then
1743
-        if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
1744
-            if is_in_projects_txt $project_dir; then
1745
-                (cd $REQUIREMENTS_DIR; \
1746
-                    python update.py $project_dir)
1747
-            else
1748
-                # soft update projects not found in requirements project.txt
1749
-                (cd $REQUIREMENTS_DIR; \
1750
-                    python update.py -s $project_dir)
1751
-            fi
1752
-        else
1753
-            (cd $REQUIREMENTS_DIR; \
1754
-                python update.py $project_dir)
1755
-        fi
1756
-    fi
1757
-
1758
-    setup_package $project_dir $flags
1759
-
1760
-    # We've just gone and possibly modified the user's source tree in an
1761
-    # automated way, which is considered bad form if it's a development
1762
-    # tree because we've screwed up their next git checkin. So undo it.
1763
-    #
1764
-    # However... there are some circumstances, like running in the gate
1765
-    # where we really really want the overridden version to stick. So provide
1766
-    # a variable that tells us whether or not we should UNDO the requirements
1767
-    # changes (this will be set to False in the OpenStack ci gate)
1768
-    if [ $UNDO_REQUIREMENTS = "True" ]; then
1769
-        if [[ $update_requirements != "changed" ]]; then
1770
-            (cd $project_dir && git reset --hard)
1771
-        fi
1772
-    fi
1773
-}
1774
-
1775
-# ``pip install -e`` the package, which processes the dependencies
1776
-# using pip before running `setup.py develop`
1777
-# Uses globals ``STACK_USER``
1778
-# setup_develop_no_requirements_update directory
1779
-function setup_package {
1780
-    local project_dir=$1
1781
-    local flags=$2
1782
-
1783
-    pip_install $flags $project_dir
1784
-    # ensure that further actions can do things like setup.py sdist
1785
-    if [[ "$flags" == "-e" ]]; then
1786
-        safe_chown -R $STACK_USER $1/*.egg-info
1787
-    fi
1788
-}
1789
-
1790 1592
 # Plugin Functions
1791 1593
 # =================
1792 1594
 
1793 1595
new file mode 100644
... ...
@@ -0,0 +1,223 @@
0
+#!/bin/bash
1
+#
2
+# **inc/python** - Python-related functions
3
+#
4
+# Support for pip/setuptools interfaces and virtual environments
5
+#
6
+# External functions used:
7
+# - GetOSVersion
8
+# - is_fedora
9
+# - is_suse
10
+# - safe_chown
11
+
12
+# Save trace setting
13
+INC_PY_TRACE=$(set +o | grep xtrace)
14
+set +o xtrace
15
+
16
+
17
+# Python Functions
18
+# ================
19
+
20
+# Get the path to the pip command.
21
+# get_pip_command
22
+function get_pip_command {
23
+    which pip || which pip-python
24
+
25
+    if [ $? -ne 0 ]; then
26
+        die $LINENO "Unable to find pip; cannot continue"
27
+    fi
28
+}
29
+
30
+# Get the path to the direcotry where python executables are installed.
31
+# get_python_exec_prefix
32
+function get_python_exec_prefix {
33
+    if is_fedora || is_suse; then
34
+        echo "/usr/bin"
35
+    else
36
+        echo "/usr/local/bin"
37
+    fi
38
+}
39
+
40
+# Wrapper for ``pip install`` to set cache and proxy environment variables
41
+# Uses globals ``INSTALL_TESTONLY_PACKAGES``, ``OFFLINE``, ``TRACK_DEPENDS``,
42
+# ``*_proxy``
43
+# pip_install package [package ...]
44
+function pip_install {
45
+    local xtrace=$(set +o | grep xtrace)
46
+    set +o xtrace
47
+    local offline=${OFFLINE:-False}
48
+    if [[ "$offline" == "True" || -z "$@" ]]; then
49
+        $xtrace
50
+        return
51
+    fi
52
+
53
+    if [[ -z "$os_PACKAGE" ]]; then
54
+        GetOSVersion
55
+    fi
56
+    if [[ $TRACK_DEPENDS = True && ! "$@" =~ virtualenv ]]; then
57
+        # TRACK_DEPENDS=True installation creates a circular dependency when
58
+        # we attempt to install virtualenv into a virualenv, so we must global
59
+        # that installation.
60
+        source $DEST/.venv/bin/activate
61
+        local cmd_pip=$DEST/.venv/bin/pip
62
+        local sudo_pip="env"
63
+    else
64
+        local cmd_pip=$(get_pip_command)
65
+        local sudo_pip="sudo -H"
66
+    fi
67
+
68
+    local pip_version=$(python -c "import pip; \
69
+                        print(pip.__version__.strip('.')[0])")
70
+    if (( pip_version<6 )); then
71
+        die $LINENO "Currently installed pip version ${pip_version} does not" \
72
+            "meet minimum requirements (>=6)."
73
+    fi
74
+
75
+    $xtrace
76
+    $sudo_pip \
77
+        http_proxy=${http_proxy:-} \
78
+        https_proxy=${https_proxy:-} \
79
+        no_proxy=${no_proxy:-} \
80
+        $cmd_pip install \
81
+        $@
82
+
83
+    INSTALL_TESTONLY_PACKAGES=$(trueorfalse False INSTALL_TESTONLY_PACKAGES)
84
+    if [[ "$INSTALL_TESTONLY_PACKAGES" == "True" ]]; then
85
+        local test_req="$@/test-requirements.txt"
86
+        if [[ -e "$test_req" ]]; then
87
+            $sudo_pip \
88
+                http_proxy=${http_proxy:-} \
89
+                https_proxy=${https_proxy:-} \
90
+                no_proxy=${no_proxy:-} \
91
+                $cmd_pip install \
92
+                -r $test_req
93
+        fi
94
+    fi
95
+}
96
+
97
+# should we use this library from their git repo, or should we let it
98
+# get pulled in via pip dependencies.
99
+function use_library_from_git {
100
+    local name=$1
101
+    local enabled=1
102
+    [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
103
+    return $enabled
104
+}
105
+
106
+# setup a library by name. If we are trying to use the library from
107
+# git, we'll do a git based install, otherwise we'll punt and the
108
+# library should be installed by a requirements pull from another
109
+# project.
110
+function setup_lib {
111
+    local name=$1
112
+    local dir=${GITDIR[$name]}
113
+    setup_install $dir
114
+}
115
+
116
+# setup a library by name in editiable mode. If we are trying to use
117
+# the library from git, we'll do a git based install, otherwise we'll
118
+# punt and the library should be installed by a requirements pull from
119
+# another project.
120
+#
121
+# use this for non namespaced libraries
122
+function setup_dev_lib {
123
+    local name=$1
124
+    local dir=${GITDIR[$name]}
125
+    setup_develop $dir
126
+}
127
+
128
+# this should be used if you want to install globally, all libraries should
129
+# use this, especially *oslo* ones
130
+function setup_install {
131
+    local project_dir=$1
132
+    setup_package_with_req_sync $project_dir
133
+}
134
+
135
+# this should be used for projects which run services, like all services
136
+function setup_develop {
137
+    local project_dir=$1
138
+    setup_package_with_req_sync $project_dir -e
139
+}
140
+
141
+# determine if a project as specified by directory is in
142
+# projects.txt. This will not be an exact match because we throw away
143
+# the namespacing when we clone, but it should be good enough in all
144
+# practical ways.
145
+function is_in_projects_txt {
146
+    local project_dir=$1
147
+    local project_name=$(basename $project_dir)
148
+    return grep "/$project_name\$" $REQUIREMENTS_DIR/projects.txt >/dev/null
149
+}
150
+
151
+# ``pip install -e`` the package, which processes the dependencies
152
+# using pip before running `setup.py develop`
153
+#
154
+# Updates the dependencies in project_dir from the
155
+# openstack/requirements global list before installing anything.
156
+#
157
+# Uses globals ``TRACK_DEPENDS``, ``REQUIREMENTS_DIR``, ``UNDO_REQUIREMENTS``
158
+# setup_develop directory
159
+function setup_package_with_req_sync {
160
+    local project_dir=$1
161
+    local flags=$2
162
+
163
+    # Don't update repo if local changes exist
164
+    # Don't use buggy "git diff --quiet"
165
+    # ``errexit`` requires us to trap the exit code when the repo is changed
166
+    local update_requirements=$(cd $project_dir && git diff --exit-code >/dev/null || echo "changed")
167
+
168
+    if [[ $update_requirements != "changed" ]]; then
169
+        if [[ "$REQUIREMENTS_MODE" == "soft" ]]; then
170
+            if is_in_projects_txt $project_dir; then
171
+                (cd $REQUIREMENTS_DIR; \
172
+                    python update.py $project_dir)
173
+            else
174
+                # soft update projects not found in requirements project.txt
175
+                (cd $REQUIREMENTS_DIR; \
176
+                    python update.py -s $project_dir)
177
+            fi
178
+        else
179
+            (cd $REQUIREMENTS_DIR; \
180
+                python update.py $project_dir)
181
+        fi
182
+    fi
183
+
184
+    setup_package $project_dir $flags
185
+
186
+    # We've just gone and possibly modified the user's source tree in an
187
+    # automated way, which is considered bad form if it's a development
188
+    # tree because we've screwed up their next git checkin. So undo it.
189
+    #
190
+    # However... there are some circumstances, like running in the gate
191
+    # where we really really want the overridden version to stick. So provide
192
+    # a variable that tells us whether or not we should UNDO the requirements
193
+    # changes (this will be set to False in the OpenStack ci gate)
194
+    if [ $UNDO_REQUIREMENTS = "True" ]; then
195
+        if [[ $update_requirements != "changed" ]]; then
196
+            (cd $project_dir && git reset --hard)
197
+        fi
198
+    fi
199
+}
200
+
201
+# ``pip install -e`` the package, which processes the dependencies
202
+# using pip before running `setup.py develop`
203
+# Uses globals ``STACK_USER``
204
+# setup_develop_no_requirements_update directory
205
+function setup_package {
206
+    local project_dir=$1
207
+    local flags=$2
208
+
209
+    pip_install $flags $project_dir
210
+    # ensure that further actions can do things like setup.py sdist
211
+    if [[ "$flags" == "-e" ]]; then
212
+        safe_chown -R $STACK_USER $1/*.egg-info
213
+    fi
214
+}
215
+
216
+
217
+# Restore xtrace
218
+$INC_PY_TRACE
219
+
220
+# Local variables:
221
+# mode: shell-script
222
+# End: