Alexander Shestak
Alexander Shestak

Reputation: 15

Why code stop working after os.system command?

I have this code:

import os
import threading

def push_repository():
    try:
       os.system('git add *')
       os.system('git commit -m "Automated commit"')
       os.system('git push')
    except Exception as e:
       print(e)

def commit_and_push_git_folders(path):

 for root, dirs, files in os.walk(path):
     for dir in dirs:
         if dir == '.git':
             print(os.path.join(root, dir))
             os.chdir(os.path.join(root, dir, '..'))
             t = threading.Thread(target=push_repository)
             t.start()

commit_and_push_git_folders('.')

Everything work properly, except the fact, that it goes only in first folder in given path and than stop working.
If I remove os.system's command in push_repository function and change it to print(os.path.join(root, dir)) everything is work cool and it goes in every single dir.

I already try to use threads, don't use threads, use subproccess and it's still goes off when first repositories was pushed

The current output is something like:
.\ANN-visualizer\.git
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
Everything up-to-date

But I have a few more folders with .git folder that need to be pushed.

Upvotes: 1

Views: 110

Answers (1)

Frank Yellin
Frank Yellin

Reputation: 11330

As I noted above, I suspect that the "current directory" is a global variable rather than per thread.

You could try:

def push_repository(directory):
    try:
       os.system(f'git -C {directory} add *')
       os.system(f'git -C {directory} commit -m "Automated commit"')
       os.system(f'git -C {directory} push')
    except Exception as e:
       print(e)

def commit_and_push_git_folders(path):

 for root, dirs, files in os.walk(path):
     for dir in dirs:
         if dir == '.git':
             print(os.path.join(root, dir))
             directory = os.path.join(root, dir, '..'))
             t = threading.Thread(target=push_repository, args=(directory,))
             t.start()

Upvotes: 1

Related Questions