Integrate a file that is Open with another user

Hello guys
After a while i have a big problem here that need guide to solve it.

I have an integrator that will save my files from temp directory to my target path, it works fine until i open the target path, just open the directory in windows explorer not open the file, when i hit publish, integrator give error.
this is the image:

and this is the error message:


i want to know my code have problem or this is related to saving file on opened directory?

this is snippet of integrator:

        if not os.path.exists(final_model_dir):
            os.makedirs(final_model_dir)
        else:
            # shutil.rmtree(final_model_dir)
            files = []
            for path, names, filenames in os.walk(final_model_dir):
                files.append(filenames)
            if files != []:
                shutil.rmtree(final_model_dir)
            os.makedirs(final_model_dir)

Overwriting is a difficult problem.

You could try/except a number of times, and hope that one of the times latches on to the file when no one else is.

retries = 3
while retries:
  try:
    # cleanup code here
  except OSError:
    if retries == 0:
      raise Exception("Could not cleanup")
    else:
      retries -= 1
      os.sleep(1)
  else:
    break

But you would still run the risk of corrupt data, where someone somewhere ignores the “in use” status of this file and writes to it at the same time anyway, or if someone reads from it during being written to and breaks writing.

The safest option I can think of is to (1) version your files as opposed to overwriting them (myfile_v001.obj). Another option to reduce the risk of corruption might be to (2) use symlinks.

A symlink would be small enough to be “instantly” replaced and therefore run a smaller risk of corruption, but more importantly your users would not actually be reading from this file for the majority of time, but rather from the file it points to (which could be a version).

Overwriting the symlink then, while someone else is reading from the target file, would not affect those users and should be more safe (but probably still not entirely safe, I’m not sure).

Hope it helps!

hey man
thank

because i can’t use versioning on my final files and i have to overwrite on them this option does not work
but i have to find a way that inform publish-man as a validator that the target file is in use

how can i find that a file or directory is open or is in use ATM?
do you have any idea?

I’m not terribly confident about this, but I can give you my best guess.

Finding out whether a file is in use, and who’s using it, would be platform specific as I think only the OS has access to this type of information, unless you keep track of it yourself.

The situation gets harder if the file is on a different computer than where it is being used, such as on a server.

In that case, you will probably need to:

  1. Query the computer hosting the file whether
  2. If it is in use, then you probably have to query the file sharing service responsible for hosting this file, such as samba, for which of the currently connected computers are accessing this file.
  3. One you have the file and computer accessing this file, you should also be able to access user information about who is accessing it, as samba would use that to determine whether this user has permissions.

At that point, you should have all the information you need to inform said user that he’d need to close his file for you to publish.

hey man

i think i have a problem in my integrate code
that i changed it on making dir, and it solved.
just checking there is directory or not if yes, just use shutil.copytree(src, dst).

it overwrite it fine.

anyway thanks man for your idea.