From cdedfa951ecef20871c09f98ee206add6108566c Mon Sep 17 00:00:00 2001 From: PurpleBabar Date: Mon, 27 Nov 2023 15:10:57 +0100 Subject: [PATCH 1/2] feat(python3 compat & recursion depth): recursive option and python3 I added the recursive option (with the level of depth) and adapted the script for python3 --- show_status | 82 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/show_status b/show_status index 1747808..128c092 100755 --- a/show_status +++ b/show_status @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # @desc Tired of having to go into each sub dir to find out whether or # not you did a git commit? Tire no more, just use this! @@ -9,8 +9,8 @@ # Grab some libraries import sys import os -import glob -import commands +import pathlib +import subprocess from optparse import OptionParser # Setup some stuff @@ -19,20 +19,20 @@ gitted = False mini = True class bcolors: - HEADER = '\033[95m' - OKBLUE = '\033[94m' - OKGREEN = '\033[92m' - WARNING = '\033[93m' - FAIL = '\033[91m' - ENDC = '\033[0m' - - def disable(self): - self.HEADER = '' - self.OKBLUE = '' - self.OKGREEN = '' - self.WARNING = '' - self.FAIL = '' - self.ENDC = '' + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + + def disable(self): + self.HEADER = '' + self.OKBLUE = '' + self.OKGREEN = '' + self.WARNING = '' + self.FAIL = '' + self.ENDC = '' parser = OptionParser(description="\ Show Status is awesome. If you tell it a directory to look in, it'll scan \ @@ -62,6 +62,14 @@ parser.add_option("-r", "--remote", help = "Set the remote name (remotename:branchname)" ) +parser.add_option("-R", "--recursive", + action = "store", + dest = "recursive", + default = 1, + help = "Depth of recursion", + type = int + ) + parser.add_option("--push", action = "store_true", dest = "push", @@ -99,17 +107,22 @@ if __name__ == "__main__": sys.stdout.write('Scanning sub directories of %s\n' %options.dirname) # See whats here - for infile in glob.glob( os.path.join(options.dirname, '*') ): + mai_dir = pathlib.Path(options.dirname) + globRegex = '' + for recursion in range(options.recursive): + globRegex += '*/' + + for infile in pathlib.Path(options.dirname).glob(globRegex): #is there a .git file - if os.path.exists( os.path.join(infile, ".git") ): + if os.path.isdir(infile) and os.path.exists( os.path.join(infile, ".git") ): #Yay, we found one! gitted = True # OK, contains a .git file. Let's descend into it # and ask git for a status - out = commands.getoutput('cd '+ infile + '; git status') + out = subprocess.getoutput('cd '+ str(infile) + '; git status') # Mini? if False == options.verbose: @@ -119,18 +132,18 @@ if __name__ == "__main__": branch = out[j+10:k]; branchColor = bcolors.WARNING; - if branch == 'master': - branchColor = bcolors.OKGREEN + if branch == 'master': + branchColor = bcolors.OKGREEN - branch = "[ " + branchColor + branch.ljust(15) + bcolors.ENDC + " ]" + branch = "[ " + branchColor + branch.ljust(15) + bcolors.ENDC + " ]" - if -1 != out.find('nothing'): + if -1 != out.find('nothing'): result = bcolors.OKGREEN + "No Changes" + bcolors.ENDC # Pull from the remote if False != options.pull: - push = commands.getoutput( - 'cd '+ infile + + push = subprocess.getoutput( + 'cd '+ str(infile) + '; git pull '+ ' '.join(options.remote.split(":")) ) @@ -138,27 +151,27 @@ if __name__ == "__main__": # Push to the remote if False != options.push: - push = commands.getoutput( - 'cd '+ infile + + push = subprocess.getoutput( + 'cd '+ str(infile) + '; git push '+ ' '.join(options.remote.split(":")) ) result = result + " (Pushed) \n" + push - else: - result = bcolors.FAIL + "Changes" + bcolors.ENDC + else: + result = bcolors.FAIL + "Changes" + bcolors.ENDC # Write to screen - sys.stdout.write("-- " + bcolors.OKBLUE + infile.ljust(55) + bcolors.ENDC + branch + " : " + result +"\n") + sys.stdout.write("-- " + bcolors.OKBLUE + str(infile).ljust(55) + bcolors.ENDC + branch + " : " + result +"\n") else: #Print some repo details - sys.stdout.write("\n---------------- "+ infile +" -----------------\n") + sys.stdout.write("\n---------------- "+ str(infile) +" -----------------\n") sys.stdout.write(out) - sys.stdout.write("\n---------------- "+ infile +" -----------------\n") + sys.stdout.write("\n---------------- "+ str(infile) +" -----------------\n") # Come out of the dir and into the next - commands.getoutput('cd ../') + subprocess.getoutput('cd ../') @@ -167,4 +180,3 @@ if __name__ == "__main__": show_error("Error: None of those sub directories had a .git file.\n") sys.stdout.write("Done\n") - From 3a92d71ecc3fe21602e0762f7f71bf08d338446d Mon Sep 17 00:00:00 2001 From: PurpleBabar Date: Mon, 27 Nov 2023 15:13:11 +0100 Subject: [PATCH 2/2] fix(typo) --- show_status | 1 - 1 file changed, 1 deletion(-) diff --git a/show_status b/show_status index 128c092..486dfa0 100755 --- a/show_status +++ b/show_status @@ -107,7 +107,6 @@ if __name__ == "__main__": sys.stdout.write('Scanning sub directories of %s\n' %options.dirname) # See whats here - mai_dir = pathlib.Path(options.dirname) globRegex = '' for recursion in range(options.recursive): globRegex += '*/'