pythoncygwinrsync

Making Windows paths work in RSync through Cygwin, when getting paths from Python script


I am trying to make a Python GUI app that interfaces with RSync through Cygwin. I have gotten the subprocess done but RSync errors out due to the paths not being Unix compatible.

I know that paths should be formatted as /cygdrive/<drive letter>/<path>/<to>/<file or directory>/ in order to work, and that is what's holding me back. I do not know a way to make a path fetched with filedialog formatted like that, and I couldn't find something online with my attempts to search this up.

I've tried to to append /cygdrive/ with os.path.join (perhaps not what the command is supposed to do) infront of the path string but that did nothing, when I expected it to format the strings as /cygdrive/<drive letter>/<path>/<to>/<file or directory>

I've also tried using os.path.relpath, os.path.normpath and passing those directly to the subprocess command as subprocess.run(["rsync", "-a" , os.path.relpath(<Source Path>)], os.path.relpath(<Destination Path>)) but also nothing (also probably wrong commands but that's what I had in mind)

Current code stands like this

import customtkinter
from tkinter import filedialog as fdiag
from tkinter import messagebox as mbox
import subprocess
import os

# Vars
global fdiagSrc, fdiagDest
fdiagDest = ''
fdiagSrc = ''

#Prepare Cygwin stuff
os.chdir(b"C:/cygwin64/bin/")
print(os.getcwd()) #Was checking if it was changing in the directory just in case

# Declare button stuff here first
def srcSel(): # Open a file dialog for Source directory selection, and assign that directory as a string  to a global var
    global fdiagSrc
    fdiagSrc = fdiag.askdirectory()
    while (len(fdiagSrc) == 0): # Show a warning in case user pressed "Cancel" or the close button on the dialog window and re-open the dialog
        mbox.showwarning("Warning!", "You did not select any directory for source!")
        fdiagSrc = fdiag.askdirectory()
    mbox.showinfo("Src", fdiagSrc)

def destSel(): # Open a file dialog for Destination directory selection, and assign that directory as a string  to a global var
    global fdiagDest
    fdiagDest = fdiag.askdirectory() # Show a warning in case user pressed "Cancel" or the close button on the dialog window and re-open the dialog
    while (len(fdiagDest) == 0):
        mbox.showwarning("Warning!", "You did not select any directory for destination!")
        fdiagDest = fdiag.askdirectory()
    mbox.showinfo("Dest", fdiagDest)

def GoToSendDir():
    if (len(fdiagDest) == 0) or (len(fdiagSrc) == 0): # Show a warning in case they didn't select any directories
        mbox.showwarning("Warning!", "You did not set any directories for Source and/or Destination! Please do that.")
    else: #Proceed to transfer/mirror/clone whatever it is called
        print(os.path.normpath(fdiagSrc)) # Checking the path if it is formatted properly..
        subprocess.run(["rsync", " -v -a ",os.path.normpath(fdiagSrc), os.path.normpath(fdiagDest)])

[.... misc GUI stuff not related to issue ....]

Solution

  • Sorry for taking a while to close this issue; kind of forgot about it. Anyways, the comment/answer provided by Michael Butscher worked pretty well for my case, calling str.replace() on the variable that I stored the path, and doing some other processes to make it work with rsync through WSL (I'd use Cygwin, but I've yet to get it to work with me) as described below:

    os.path.normcase(variableThatStoresPath.replace(":", "")) + "/" 
    variableThatStoresPath = variableThatStoresPath.replace("\\", "/")
    

    (opted for os.path.normcase() as that played well with Windows Sybsystem for Linux, and replaced the : that's most times present in Windows-like paths i.e. F:\some\path\ with , utilising str.replace(). To select the path I used tkinter's filedialog.askdirectory() method. The next line does some more replacing to make the path more appropriate utilising str.replace() once again.)

    I will be trying Doug Henderson's suggestion, to handle the paths with the Pathlib module and report back here.

    Thank you both for your ideas!