How to force os.stat re-read file stats by same path

285 Views Asked by At

I have a code that is architecturally close to posted below (unfortunately i can't post full version cause it's proprietary). I have an self-updating executable and i'm trying to test this feature. We assume that full path to this file will be in A.some_path after executing input. My problem is that assertion failed, because on second call os.stat still returning the previous file stats (i suppose it thinks that nothing could changed so it's unnecessary). I have tried to launch this manually and self-updating works completely fine and the file is really removing and recreating with stats changing. Is there any guaranteed way to force os.stat re-read file stats by the same path, or alternative option to make it works (except recreating an A object)?

from pathlib import Path
import unittest
import os

class A:
    some_path = Path()

    def __init__(self, _some_path):
        self.some_path = Path(_some_path)
        
    def get_path(self):
        return self.some_path

class TestKit(unittest.TestCase):
    def setUp(self):
        pass

    def check_body(self, a):
        some_path = a.get_path()
        modification_time = os.stat(some_path).st_mtime
        # Launching self-updating executable
        self.assertTrue(modification_time < os.stat(some_path).st_mtime)

    def check(self):
        a = A(input('Enter the file path\n'))
        self.check_body(a)

def Tests():
    suite = unittest.TestSuite()
    suite.addTest(TestKit('check'))
    return suite

def main():
    tests_suite = Tests()
    unittest.TextTestRunner().run(tests_suite)

if __name__ == "__main__":
    main()
1

There are 1 best solutions below

0
k0shinus On

I have found the origins of the problem: i've tried to launch self-updating via os.system which wait till the process done. But first: during the self-updating we launch several detached proccesses and actually should wait unitl all them have ended, and the second: even the signal that the proccess ends doesn't mean that OS really completely realease the file, and looks like on assertTrue we are not yet done with all our routines. For my task i simply used sleep, but normal solution should analyze the existing proccesses in the system and wait for them to finish, or at least there should be several attempts with awaiting.