From 0ed605a4af61a8c3f810947389d27919f81dd98a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferit=20Yi=C4=9Fit=20BALABAN?= Date: Sat, 19 Mar 2022 19:54:27 +0300 Subject: [PATCH] Implement proc() to call processes --- dotman.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/dotman.py b/dotman.py index eae7958..fa459f3 100755 --- a/dotman.py +++ b/dotman.py @@ -3,7 +3,7 @@ # Ferit Yiğit BALABAN , 2022 # import os.path -import subprocess as sp +from subprocess import PIPE, run import sys from datetime import datetime as dt from termcolor import colored @@ -38,21 +38,27 @@ def remove_from_left_until_slash(text): 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): cprint(text, color, end='') def grab_dotfiles(): os.mkdir(remove_from_left_until_slash(DOTFILES_REPOSITORY)) - p = sp.Popen(['git', 'clone https://github.com/fybalaban/dotfiles'], cwd=DOTFILES_REPOSITORY) - _ = p.communicate()[0] - return p.returncode == 0, p.returncode + code, output = proc(f'git clone {REMOTE_REPOSITORY}', DOTFILES_REPOSITORY) + return code == 0, code def copy(source, dest, method='subprocess'): if method == 'subprocess': - p = sp.Popen(['cp', '-av', source, dest]) - _ = p.communicate()[0] + proc(f'cp -av {source} {dest}') def special_copy(source, dest): @@ -83,19 +89,18 @@ def special_copy(source, dest): def git_commit(): - p1 = sp.Popen(['/usr/bin/git', 'add', '.'], cwd=DOTFILES_REPOSITORY) - _ = p1.communicate()[0] + flag_commit = True + 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') - commit_name = f'"dotman {date}"' - p2 = sp.Popen(['/usr/bin/git', 'commit', '-m', commit_name], cwd=DOTFILES_REPOSITORY) - _ = p2.communicate()[0] + if flag_commit: + date = dt.now().strftime('%d.%m.%Y %H.%M') + proc(f'/usr/bin/git commit -m "dotman {date}"', DOTFILES_REPOSITORY) def push_remote(): - p = sp.Popen(['/usr/bin/git', 'push'], cwd=DOTFILES_REPOSITORY) - _ = p.communicate()[0] - return p.returncode == 0, p.returncode + code, output = proc('/usr/bin/git push', DOTFILES_REPOSITORY) + return code == 0, code def main():