Home > Enterprise >  Trying to clone a list of GitLab Repo's with subprocess module
Trying to clone a list of GitLab Repo's with subprocess module

Time:07-12

I'm trying to clone a big list of GitLab repositories from my GitLab Server. However, I'm having trouble getting the subprocess module to work. I've tried the answer to this question but it won't work for me. Open Git Bash shell using a python script and then run a shell script in git bash shell

Here is my code:

repo_list = [list_with_all_repository_names]

    for repo_name in repo_list:
        repo_url = f'https://url_to_my_gitlab_server/{repo_name}.git'
        p = subprocess.run(["C:/Users/my_user_name/AppData/Local/Programs/Git/git-bash.exe",
                            "C:/Users/my_user_name/Documents/Gitlab Repos/git-clone-shell.sh",
                            f"git clone {repo_name}"],
                         bufsize=-1,
                         executable=None,
                         stdin=None,
                         stdout=None,
                         stderr=None,
                         preexec_fn=None,
                         close_fds=False,
                         shell=False,
                         cwd="C:/Users/my_user_name/Documents/Gitlab Repos")
        time.sleep(20)

My shell script is simply a placeholder for the f"git clone {repo_name}" argument in the subprocess.run call, and therefore it's contents are no more than $1

Git bash opens but immediatly crashes and I don't know why. I've also tried to run the command directly in bash without a shell script, but this simply opens git bash and does nothing:

p = subprocess.run(
    [f'git clone {repo_url}'],
    bufsize=-1,
    executable='C:/Users/my_user_name/AppData/Local/Programs/Git/git-bash.exe',
    capture_output=True,
    cwd="C:/Users/my_user_name/Documents/Gitlab Repos"
)

I have the feeling that I'm close to a solution but I don't understand where it's going wrong.

CodePudding user response:

I've solved the problem by running the script in a WSL-environment, with the following subprocess setting:

for repo_name in repo_list:
    repo_url = f'url_to_gitlab_server/{repo_name}.git'
    p = subprocess.run(["git", "clone", f"{repo_url}"],
                     bufsize=-1,
                     stdin=None,
                     stdout=None,
                     stderr=None,
                     shell=False)
    print(p)
  • Related