|
62 | 62 |
|
63 | 63 | from easybuild.base import fancylogger |
64 | 64 | # import build_log must stay, to use of EasyBuildLog |
65 | | -from easybuild.tools.build_log import EasyBuildError, dry_run_msg, print_msg, print_warning |
| 65 | +from easybuild.tools.build_log import EasyBuildError, CWD_NOTFOUND_ERROR, dry_run_msg, print_msg, print_warning |
66 | 66 | from easybuild.tools.config import ERROR, GENERIC_EASYBLOCK_PKG, IGNORE, WARN, build_option, install_path |
67 | 67 | from easybuild.tools.output import PROGRESS_BAR_DOWNLOAD_ONE, start_progress_bar, stop_progress_bar, update_progress_bar |
68 | 68 | from easybuild.tools.hooks import load_source |
@@ -407,26 +407,42 @@ def remove(paths): |
407 | 407 | raise EasyBuildError("Specified path to remove is not an existing file or directory: %s", path) |
408 | 408 |
|
409 | 409 |
|
| 410 | +def get_cwd(must_exist=True): |
| 411 | + """ |
| 412 | + Retrieve current working directory |
| 413 | + """ |
| 414 | + try: |
| 415 | + cwd = os.getcwd() |
| 416 | + except FileNotFoundError as err: |
| 417 | + if must_exist is True: |
| 418 | + raise EasyBuildError(CWD_NOTFOUND_ERROR) |
| 419 | + |
| 420 | + _log.debug("Failed to determine current working directory, but proceeding anyway: %s", err) |
| 421 | + cwd = None |
| 422 | + |
| 423 | + return cwd |
| 424 | + |
| 425 | + |
410 | 426 | def change_dir(path): |
411 | 427 | """ |
412 | 428 | Change to directory at specified location. |
413 | 429 |
|
414 | 430 | :param path: location to change to |
415 | 431 | :return: previous location we were in |
416 | 432 | """ |
417 | | - # determining the current working directory can fail if we're in a non-existing directory |
418 | | - try: |
419 | | - cwd = os.getcwd() |
420 | | - except OSError as err: |
421 | | - _log.debug("Failed to determine current working directory (but proceeding anyway: %s", err) |
422 | | - cwd = None |
| 433 | + # determine origin working directory: can fail if non-existent |
| 434 | + prev_dir = get_cwd(must_exist=False) |
423 | 435 |
|
424 | 436 | try: |
425 | 437 | os.chdir(path) |
426 | 438 | except OSError as err: |
427 | | - raise EasyBuildError("Failed to change from %s to %s: %s", cwd, path, err) |
| 439 | + raise EasyBuildError("Failed to change from %s to %s: %s", prev_dir, path, err) |
428 | 440 |
|
429 | | - return cwd |
| 441 | + # determine final working directory: must exist |
| 442 | + # stoplight meant to catch filesystems in a faulty state |
| 443 | + get_cwd() |
| 444 | + |
| 445 | + return prev_dir |
430 | 446 |
|
431 | 447 |
|
432 | 448 | def extract_file(fn, dest, cmd=None, extra_options=None, overwrite=False, forced=False, change_into_dir=False, |
@@ -671,9 +687,9 @@ def parse_http_header_fields_urlpat(arg, urlpat=None, header=None, urlpat_header |
671 | 687 | if argline == '' or '#' in argline[0]: |
672 | 688 | continue # permit comment lines: ignore them |
673 | 689 |
|
674 | | - if os.path.isfile(os.path.join(os.getcwd(), argline)): |
| 690 | + if os.path.isfile(os.path.join(get_cwd(), argline)): |
675 | 691 | # expand existing relative path to absolute |
676 | | - argline = os.path.join(os.path.join(os.getcwd(), argline)) |
| 692 | + argline = os.path.join(os.path.join(get_cwd(), argline)) |
677 | 693 | if os.path.isfile(argline): |
678 | 694 | # argline is a file path, so read that instead |
679 | 695 | _log.debug('File included in parse_http_header_fields_urlpat: %s' % argline) |
@@ -1332,14 +1348,14 @@ def get_local_dirs_purged(): |
1332 | 1348 | # and hidden directories |
1333 | 1349 | ignoredirs = ["easybuild"] |
1334 | 1350 |
|
1335 | | - lst = os.listdir(os.getcwd()) |
| 1351 | + lst = os.listdir(get_cwd()) |
1336 | 1352 | lst = [d for d in lst if not d.startswith('.') and d not in ignoredirs] |
1337 | 1353 | return lst |
1338 | 1354 |
|
1339 | 1355 | lst = get_local_dirs_purged() |
1340 | | - new_dir = os.getcwd() |
| 1356 | + new_dir = get_cwd() |
1341 | 1357 | while len(lst) == 1: |
1342 | | - new_dir = os.path.join(os.getcwd(), lst[0]) |
| 1358 | + new_dir = os.path.join(get_cwd(), lst[0]) |
1343 | 1359 | if not os.path.isdir(new_dir): |
1344 | 1360 | break |
1345 | 1361 |
|
|
0 commit comments