Implement proc() to call processes

This commit is contained in:
Ferit Yiğit BALABAN 2022-03-19 19:54:27 +03:00
parent 1d67926fa9
commit 0ed605a4af
No known key found for this signature in database
GPG Key ID: BB8494BA65A90842

View File

@ -3,7 +3,7 @@
# Ferit Yiğit BALABAN <fyb@duck.com>, 2022 # Ferit Yiğit BALABAN <fyb@duck.com>, 2022
# #
import os.path import os.path
import subprocess as sp from subprocess import PIPE, run
import sys import sys
from datetime import datetime as dt from datetime import datetime as dt
from termcolor import colored from termcolor import colored
@ -38,21 +38,27 @@ def remove_from_left_until_slash(text):
return text.removesuffix(buffer[::-1]) return text.removesuffix(buffer[::-1])
def proc(command, cwd=''):
if cwd == '':
r = run(command, shell=True, capture_output=True, stdout=PIPE, text=True)
else:
r = run(command, shell=True, capture_output=True, stdout=PIPE, text=True, cwd=cwd)
return r.returncode, r.stdout
def prclr(text, color): def prclr(text, color):
cprint(text, color, end='') cprint(text, color, end='')
def grab_dotfiles(): def grab_dotfiles():
os.mkdir(remove_from_left_until_slash(DOTFILES_REPOSITORY)) os.mkdir(remove_from_left_until_slash(DOTFILES_REPOSITORY))
p = sp.Popen(['git', 'clone https://github.com/fybalaban/dotfiles'], cwd=DOTFILES_REPOSITORY) code, output = proc(f'git clone {REMOTE_REPOSITORY}', DOTFILES_REPOSITORY)
_ = p.communicate()[0] return code == 0, code
return p.returncode == 0, p.returncode
def copy(source, dest, method='subprocess'): def copy(source, dest, method='subprocess'):
if method == 'subprocess': if method == 'subprocess':
p = sp.Popen(['cp', '-av', source, dest]) proc(f'cp -av {source} {dest}')
_ = p.communicate()[0]
def special_copy(source, dest): def special_copy(source, dest):
@ -83,19 +89,18 @@ def special_copy(source, dest):
def git_commit(): def git_commit():
p1 = sp.Popen(['/usr/bin/git', 'add', '.'], cwd=DOTFILES_REPOSITORY) flag_commit = True
_ = p1.communicate()[0] code, output = proc('/usr/bin/git add .', DOTFILES_REPOSITORY)
flag_commit = flag_commit or 'nothing to commit' in output
date = dt.now().strftime('%d.%m.%Y %H.%M') if flag_commit:
commit_name = f'"dotman {date}"' date = dt.now().strftime('%d.%m.%Y %H.%M')
p2 = sp.Popen(['/usr/bin/git', 'commit', '-m', commit_name], cwd=DOTFILES_REPOSITORY) proc(f'/usr/bin/git commit -m "dotman {date}"', DOTFILES_REPOSITORY)
_ = p2.communicate()[0]
def push_remote(): def push_remote():
p = sp.Popen(['/usr/bin/git', 'push'], cwd=DOTFILES_REPOSITORY) code, output = proc('/usr/bin/git push', DOTFILES_REPOSITORY)
_ = p.communicate()[0] return code == 0, code
return p.returncode == 0, p.returncode
def main(): def main():