Skip to content

Conversation

Jxpro
Copy link

@Jxpro Jxpro commented Mar 15, 2024

Description

The StarPerf suite's XML and TLE Constellations Testing modules encounter a FileNotFoundError when attempting to create or access .h5 files within the data/XML_constellation/ and data/TLE_constellation/ directories. This issue predominantly arises after a git clone operation, as Git does not track empty directories, resulting in the absence of these critical paths in the cloned repository structure.

The primary cause of this issue is the nature of git clone operations where directories without files are not created in the cloned repository. As a result, the expected directory structure needed for file operations does not exist, causing runtime errors during constellation data generation.

Log

Starting StarPerf...
    Starting XML Constellations Testing...
        Test(01/16) : constellation generation
Traceback (most recent call last):
...
FileNotFoundError: [Errno 2] Unable to synchronously create file (unable to open file: name = 'data/XML_constellation/Starlink.h5', errno = 2, error message = 'No such file or directory', flags = 13, o_flags = 302)

Solution

Modified the constellation_configuration function to check and create the data/XML_constellation/ and data/TLE_constellation/ directories if it does not exist. This is achieved using the os.makedirs() function with the exist_ok=True parameter, ensuring idempotency.

Testing

✅ Ensured existing unit tests pass with the proposed changes
✅ Manual testing performed post-git clone to validate the fix

@Jxpro Jxpro changed the title Fix FileNotFoundError in XML Constellations Testing by Ensuring Parent Directories Exist Fix FileNotFoundError in Constellations Generation Testing by Ensuring Parent Directories Exist Mar 15, 2024
@root-hbx
Copy link
Member

Good catch! @Jxpro But I believe there's a better way to refactor this issue:

We can define a helper function globally that automatically gets called whenever with open() is used. This way, we won't have to worry about creating files or directories in the future.

Here's an example for your reference:

Func Def

def safe_open(file_path, mode='r', *args, **kwargs):
    """Safely open a file, automatically creating parent directories if needed.

    Args
        param file_path: Path to the file.
        param mode: File open mode, e.g., 'r', 'w', 'a'.
    """
    if 'w' in mode or 'a' in mode or 'x' in mode:
        os.makedirs(os.path.dirname(file_path), exist_ok=True)
    return open(file_path, mode, *args, **kwargs)

Func Calling

with safe_open('data/TLE_constellation/sat_data.txt', 'w') as f:
    f.write('satellite info')

Highlight

Let's go further, there are some similar problems to be solved:

  • Ensuring a file exists before attempting to open it.
  • Confirming a folder exists when trying to access it.
  • Verifying the parent directory of a file exists before file operations.
  • Checking if the parent directory of a folder exists prior to folder operations.

It would be great if these common scenarios could all be encapsulated within a single, versatile function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants