|
15 | 15 |
|
16 | 16 | """Image V1 Action Implementations""" |
17 | 17 |
|
| 18 | +import io |
18 | 19 | import logging |
19 | 20 | import os |
20 | 21 | import six |
@@ -214,40 +215,46 @@ def take_action(self, parsed_args): |
214 | 215 | elif parsed_args.file: |
215 | 216 | # Send an open file handle to glanceclient so it will |
216 | 217 | # do a chunked transfer |
217 | | - kwargs["data"] = open(parsed_args.file, "rb") |
| 218 | + kwargs["data"] = io.open(parsed_args.file, "rb") |
218 | 219 | else: |
219 | 220 | # Read file from stdin |
220 | | - kwargs["data"] = None |
221 | 221 | if sys.stdin.isatty() is not True: |
222 | 222 | if msvcrt: |
223 | 223 | msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) |
224 | 224 | # Send an open file handle to glanceclient so it will |
225 | 225 | # do a chunked transfer |
226 | 226 | kwargs["data"] = sys.stdin |
227 | 227 |
|
| 228 | + # Wrap the call to catch exceptions in order to close files |
228 | 229 | try: |
229 | | - image = utils.find_resource( |
230 | | - image_client.images, |
231 | | - parsed_args.name, |
232 | | - ) |
233 | | - |
234 | | - # Preserve previous properties if any are being set now |
235 | | - if image.properties: |
236 | | - if parsed_args.properties: |
237 | | - image.properties.update(kwargs['properties']) |
238 | | - kwargs['properties'] = image.properties |
239 | | - |
240 | | - except exceptions.CommandError: |
241 | | - if not parsed_args.volume: |
242 | | - # This is normal for a create or reserve (create w/o an image) |
243 | | - # But skip for create from volume |
244 | | - image = image_client.images.create(**kwargs) |
245 | | - else: |
246 | | - # Update an existing reservation |
| 230 | + try: |
| 231 | + image = utils.find_resource( |
| 232 | + image_client.images, |
| 233 | + parsed_args.name, |
| 234 | + ) |
247 | 235 |
|
248 | | - # If an image is specified via --file, --location or |
249 | | - # --copy-from let the API handle it |
250 | | - image = image_client.images.update(image.id, **kwargs) |
| 236 | + # Preserve previous properties if any are being set now |
| 237 | + if image.properties: |
| 238 | + if parsed_args.properties: |
| 239 | + image.properties.update(kwargs['properties']) |
| 240 | + kwargs['properties'] = image.properties |
| 241 | + |
| 242 | + except exceptions.CommandError: |
| 243 | + if not parsed_args.volume: |
| 244 | + # This is normal for a create or reserve (create w/o |
| 245 | + # an image), but skip for create from volume |
| 246 | + image = image_client.images.create(**kwargs) |
| 247 | + else: |
| 248 | + # Update an existing reservation |
| 249 | + |
| 250 | + # If an image is specified via --file, --location or |
| 251 | + # --copy-from let the API handle it |
| 252 | + image = image_client.images.update(image.id, **kwargs) |
| 253 | + finally: |
| 254 | + # Clean up open files - make sure data isn't a string |
| 255 | + if ('data' in kwargs and hasattr(kwargs['data'], 'close') and |
| 256 | + kwargs['data'] != sys.stdin): |
| 257 | + kwargs['data'].close() |
251 | 258 |
|
252 | 259 | info = {} |
253 | 260 | info.update(image._info) |
|
0 commit comments