Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaming files: Linux support #11

Open
theodumont opened this issue Aug 12, 2020 · 1 comment
Open

Renaming files: Linux support #11

theodumont opened this issue Aug 12, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@theodumont
Copy link
Owner

In functions.py, rename_files uses the command

start /WAIT $file

executed by a subprocess to run a file and being able to know whether it is still open or not.
This command doesn't exist on Linux, and it is a bid sad because it is the only line of code that causes Linux non being supported by the tool 😢

@theodumont theodumont added the bug Something isn't working label Aug 12, 2020
@roadelou
Copy link
Contributor

Maybe look for this StackOverflow post, even though I guess you have already seen it. I talks about about using psutils to check if a file is opened somewhere else 🔍

I am not sure about the problem you are trying to solve here, i.e. why are you looking if the file is opened somewhere else? What if it is?


Also, running files blindly may not be the best way to check that ❗ First of all because most files are not executable (so the subprocess will crash, but why not), and second of all because you have little way to control what the program will do once it has started ⚠️

What if I am trying to rename WannaCry.exe 💣 to NoTouchVirus.exe for instance? Should the default behavior be to that the tool runs the WannaCry.exe file 💥 just to check if someone else is writing to it? That seems like a risky move to me 😆


For reference purposes, the equivalent of

start /WAIT $file

On Linux would probably be

sh $file
# or, but less portable
bash $file
# or, without the new subprocess
./$file
# which is equivalent to (I think)
exec $file

Once again, the child sub-process will crash if the file is not executable 💥


As a side note, Popen in Python (or subprocess.run) already creates a child process, so using start within Popen creates a subprocess within the subprocess, which may or may not be what you want. Also, the shell=True argument of Popen tells it to run the command within a new terminal (or shell in the Linux world). Usually this makes it more convenient to use because you can give the shell a string for its command, whereas a direct call to the OS would expect a list of strings instead.

Put graphically, Popen("start /WAIT $file", shell=True) amounts to:

The python process
|-- The Popen process
     |-- A new terminal (because of shell=True)
          |-- A new terminal (because of start)
               |-- The process running $file

If you don't want the additional processes (but maybe you do?), one way to run the file would be to use:

from subprocess import run
# You can adapt the following line with Popen.
subprocess.run([str(file_name)])
# For instance to run cli.py with Python
subprocess.run(["python3", "cli.py"])

This should schedule the process directly, without the intermediary shells 🐚

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants