@@ -97,14 +97,17 @@ def _nixpkgs_package_impl(repository_ctx):
97
97
# as we can do.
98
98
timeout = 1073741824
99
99
100
- res = repository_ctx .execute (nix_build , quiet = False , timeout = timeout ,
101
- environment = dict (NIX_PATH = nix_path ))
102
- if res .return_code == 0 :
103
- output_path = res .stdout .splitlines ()[- 1 ]
104
- else :
105
- _execute_error (res , "Cannot build Nix attribute `{}`"
106
- .format (repository_ctx .attr .attribute_path ))
107
-
100
+ exec_result = _execute_or_fail (
101
+ repository_ctx ,
102
+ nix_build ,
103
+ failure_message = "Cannot build Nix attribute '{}'." .format (
104
+ repository_ctx .attr .attribute_path
105
+ ),
106
+ quiet = False ,
107
+ timeout = timeout ,
108
+ environment = dict (NIX_PATH = nix_path ),
109
+ )
110
+ output_path = exec_result .stdout .splitlines ()[- 1 ]
108
111
# Build a forest of symlinks (like new_local_package() does) to the
109
112
# Nix store.
110
113
_symlink_children (repository_ctx , output_path )
@@ -142,6 +145,26 @@ def nixpkgs_package(*args, **kwargs):
142
145
else :
143
146
_nixpkgs_package (* args , ** kwargs )
144
147
148
+ def _execute_or_fail (repository_ctx , arguments , failure_message = "" , * args , ** kwargs ):
149
+ """Call repository_ctx.execute() and fail if non-zero return code."""
150
+ result = repository_ctx .execute (arguments , * args , ** kwargs )
151
+ if result .return_code :
152
+ outputs = dict (
153
+ failure_message = failure_message ,
154
+ arguments = arguments ,
155
+ return_code = result .return_code ,
156
+ stderr = result .stderr ,
157
+ )
158
+ fail ("""
159
+ {failure_message}
160
+ Command: {arguments}
161
+ Return code: {return_code}
162
+ Error output:
163
+ {stderr}
164
+ """ ).format (** outputs )
165
+ return result
166
+
167
+
145
168
def _symlink_children (repository_ctx , target_dir ):
146
169
"""Create a symlink to all children of `target_dir` in the current
147
170
build directory."""
@@ -154,13 +177,10 @@ def _symlink_children(repository_ctx, target_dir):
154
177
# filenames can contain \n
155
178
"-print0" ,
156
179
]
157
- find_res = repository_ctx .execute (find_args )
158
- if find_res .return_code == 0 :
159
- for target in find_res .stdout .rstrip ("\0 " ).split ("\0 " ):
160
- basename = target .rpartition ("/" )[- 1 ]
161
- repository_ctx .symlink (target , basename )
162
- else :
163
- _execute_error (find_res )
180
+ exec_result = _execute_or_fail (repository_ctx , find_args )
181
+ for target in exec_result .stdout .rstrip ("\0 " ).split ("\0 " ):
182
+ basename = target .rpartition ("/" )[- 1 ]
183
+ repository_ctx .symlink (target , basename )
164
184
165
185
166
186
def _executable_path (repository_ctx , exe_name , extra_msg = "" ):
@@ -170,19 +190,3 @@ def _executable_path(repository_ctx, exe_name, extra_msg=""):
170
190
fail ("Could not find the `{}` executable in PATH.{}\n "
171
191
.format (exe_name , " " + extra_msg if extra_msg else "" ))
172
192
return path
173
-
174
-
175
- def _execute_error (exec_result , msg ):
176
- """Print a nice error message for a failed `execute`."""
177
- fail ("""
178
- execute() error: {msg}
179
- status code: {code}
180
- stdout:
181
- {stdout}
182
- stderr:
183
- {stderr}
184
- """ .format (
185
- msg = msg ,
186
- code = exec_result .return_code ,
187
- stdout = exec_result .stdout ,
188
- stderr = exec_result .stderr ))
0 commit comments