4931d1179a141ffce19236afc6184ef2c0e4054e
[zope-bootstrap.git] / Makefile.master
1 # -*- makefile -*-
2
3 #
4 # The master makefile sets up the follosing customizable (double-colon) targtets.
5 #
6 # debdepends::
7 #     This optional target may be used by the user to install the necessary 
8 #     system requirements via 'aptitude'. Must be called as root
9 #
10 # setup::
11 #     Called for basic setup. Executed before installing the basic eggs
12 #
13 # eggs::
14 #     Called to install eggs
15 #
16 # buildout::
17 #     Called to install the buildout (e.g. call paster create)
18 #
19 # init-hook::
20 #     Called after the buildout is installed but BEFORE bootstraping
21 #
22 # update-hook::
23 #     Called BEFORE updating the buildout (e.g. before calling bin/buildout)
24 #
25 # bootstrap::
26 #     Called to bootstrap the buildout. Here you can add actions to be
27 #     performed AFTER bootstraping the project whenever a bootstrap is performed
28 #
29 # update::
30 #     Called to update the buildout (calls bin/buildout). Here you can add
31 #     commands to call AFTER bin/buildout returns.
32 #
33 # .gitignore::
34 #     Called to populate the .gitignore file. Use '@$(gitignore) <pattern>' to
35 #     add a pattern
36 #
37 # .env::
38 #     Called to add environment variables to be set when executing a shell
39 #     Use @$(env) <var> <value> to add a variable settings. <var> may be
40 #         PATH         Add <value> to PATH
41 #         PYTHONPATH   Add <value> to PYTHONPATH
42 #         <other>      Set variable to <value>
43
44 # clean::
45 #     Called to clean up all generated files
46 #
47
48 ###########################################################################
49
50 BASEDIR=$(shell pwd)
51
52 DLCACHE ?= dlcache
53
54 default: update-nonet
55 .PHONY: default
56
57 # Usage:
58 #     @$(httpget) <url> [<target>]
59 #
60 # Download <url> to <target>. If <target> is missing, <target> defaults to
61 # the last component of the <url>.
62 #
63 # If the file exists in $(DLCACHE), it is retrieved from there instead.
64 define httpget
65     _httpget() {                                                        \
66         name="$${1##*/}";                                               \
67         target="$$2";                                                   \
68         [ -n "$$target" ] || target="$$name";                           \
69         cache="$(DLCACHE)/$$name";                                      \
70         if [ -r "$$cache" ]; then                                       \
71             echo "Fetching '$$name' from cache.";                       \
72         else                                                            \
73             [ -z "$(NONET)" ] || ( echo "Missing '$$1'."; exit 2 );     \
74             echo "Downloading '$$1'.";                                  \
75             wget -O "$$cache" "$$1";                                    \
76         fi;                                                             \
77         cp "$$cache" "$$target";                                        \
78     };                                                                  \
79     _httpget
80 endef
81
82 # Usage:
83 #     @$(dlegg) <package>
84 #
85 # Downloads egg to '$(DLCACHE)/<package>-*.egg' unless a file with that name
86 # already exists.
87 #
88 # THIS DOES NOT WORK WITH ALL PACKAGES !!! USE WITH CARE
89 define _dlegg
90     _dlegg() {                                                          \
91         if [ -r $(DLCACHE)/$$1-*.egg ]; then                            \
92             name="`cd $(DLCACHE); echo $$1-*.egg`";                     \
93             echo "Fetching '$$name' from cache.";                       \
94         else                                                            \
95             [ -z "$(NONET)" ] || ( echo "Missing '$$1'."; exit 2 );     \
96             $(EASY_INSTALL) -zmaxd $(DLCACHE) $$1;                      \
97             name="`cd $(DLCACHE); echo $$1-*.egg`";                     \
98             if [ -d $(DLCACHE)/$$name ]; then                           \
99                 cd $(DLCACHE)/$$name;                                   \
100                 zip -r ../$$name.zip .;                                 \
101                 cd ..;                                                  \
102                 rm -r $$name;                                           \
103                 mv $$name.zip $$name;                                   \
104             fi;                                                         \
105         fi;                                                             \
106     }
107 endef
108 define dlegg
109     $(_dlegg); _dlegg
110 endef
111
112 # Usage:
113 #     @$(install) <package>
114 #
115 # Downloads egg to '$(DLCACHE)/<package>-*.egg' and installs it unless a file
116 # with that name already exists.
117 define install
118     _install() { \
119         echo $(PIP_CACHE_FILES); \
120         echo "$(PIP) install --download-cache="$(DLCACHE)/pip" $(PIP_OPTS) $$1";        \
121         $(PIP) install --download-cache="$(DLCACHE)/pip" $(PIP_OPTS) "$$1";             \
122     };                                                                                  \
123     _install
124 endef
125
126 ###########################################################################
127 # Customization targets
128
129 debdepends::
130 .PHONY: debdepends
131
132 setup::
133         mkdir -p $(DLCACHE) $(DLCACHE)/pip
134 .PHONY: setup
135
136 eggs::
137 .PHONY: eggs
138
139 buildout::
140 .PHONY: buildout
141
142 init-hook::
143 .PHONY: init-hook
144
145 update-hook::
146 .PHONY: update-hook
147
148 bootstrap::
149         sed -i -e '1s/^#!.*\/python/#!$(subst /,\/,$(PYTHON))/' $(PYTHON_DIR)/bin/*
150         $(PYTHON) -c 'from zc.buildout.buildout import main; main(["bootstrap"])'
151
152 update:: update-hook
153         bin/buildout $(BUILDOUT_OPTS)
154
155 define gitignore
156     _gitignore () {                                             \
157         [ -r .gitignore ] || touch .gitignore;                  \
158         grep -qxF "$$1" .gitignore || echo "$$1" >>.gitignore;  \
159     };                                                          \
160     _gitignore
161 endef
162 .gitignore::
163         @echo "Updating .gitignore."
164         @$(gitignore) "*.pyc"
165         @$(gitignore) "*.egg-info/"
166         @$(gitignore) "/.env"
167         @$(gitignore) "/$(DLCACHE)/"
168
169 define env
170     _env () {                                                   \
171         [ -r .env ] || touch .env;                              \
172         case "$$1" in                                           \
173         PATH|PYTHONPATH) line="export $$1=\"$$2:\$$$$1\"" ;;    \
174         *) line="export $$1=\"$$2\"" ;;                         \
175         esac;                                                   \
176         grep -qxF "$$line" .env || echo "$$line" >>.env;        \
177     };                                                          \
178     _env
179 endef
180 .env::
181         @echo "Updating .env."
182
183 clean::
184         rm -f .gitignore .env
185 .PHONY: clean
186
187 ###########################################################################
188 # internal targets
189
190 prepare-pipcache:
191         @(                                                              \
192             cd $(DLCACHE)/pip;                                          \
193             for file in *.tar.gz; do                                    \
194                 name="$${file##*%2F}";                                  \
195                 if [ "$$name" != "$$file" -a ! -r "$$name" ]; then      \
196                     ln -s $$file $$name || exit 1;                      \
197                 fi;                                                     \
198             done                                                        \
199         )
200
201 PIP_CACHE_FILES := $(shell cd $(DLCACHE)/pip; ls *.tar.gz | grep -vF '%2F')
202
203 do-init-nonet: PIP_OPTS  = --no-index $(patsubst %,--find-link=file://$(BASEDIR)/$(DLCACHE)/pip/%,$(PIP_CACHE_FILES))
204 do-init-nonet: BUILDOUT_OPTS = -N
205 do-init-nonet: NONET=1
206 do-init-nonet: init
207
208 ###########################################################################
209 # user targets
210
211 init: setup eggs buildout init-hook update-hook bootstrap update .gitignore .env
212
213 init-nonet: prepare-pipcache
214         @$(MAKE) do-init-nonet
215
216 versions:
217         @echo "# Add the following lines to [versions] in buildout.cfg to pin all packages"
218         @bin/buildout -vvvvv | sed -ne 's/^Picked: //p' | sort | uniq 
219
220 shell:
221         @eval "`cat .env`"; $$SHELL
222
223 update-nonet: BUILDOUT_OPTS=-No
224 update-nonet: update