Skip to content

Commit

Permalink
Making the mass grep coloful
Browse files Browse the repository at this point in the history
  • Loading branch information
palikar committed Jun 17, 2020
1 parent 2539fd5 commit d0687b6
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 25 deletions.
5 changes: 2 additions & 3 deletions code_manager/commands/commit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import subprocess
import sys

import code_manager.utils.logger

class CommitCommand:

Expand All @@ -14,7 +13,7 @@ def execute(self, args, path):
if not os.path.exists(os.path.join(path, '.git')):
return 0
ret = subprocess.run(['git', 'commit', *args], stdout=subprocess.STDOUT, cwd=path)
return 0
return 0


ExportedClass = CommitCommand
30 changes: 24 additions & 6 deletions code_manager/commands/grep.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import subprocess
import sys

import code_manager.utils.logger
from code_manager.core.configuration import ConfigurationAware
from code_manager.utils.logger import RED
from code_manager.utils.logger import RESET

class GrepCommand:

class GrepCommand(ConfigurationAware):

name = 'grep'

def __init__(self):
self.color = False if self.opt.get('Commands', 'grep-colors', fallback=True) == 'false' else True

def execute(self, args, path):
grep_command = ['grep', '-r']

if self.color and not args.no_color:
color = True
grep_command.append('--color=always')
elif args.no_color:
color = False
grep_command.append('--color=never')

ret = subprocess.run(['grep', '-r', *args], stdout=subprocess.PIPE, cwd=path)
grep_command.extend(args.rest)
ret = subprocess.run(grep_command, stdout=subprocess.PIPE, cwd=path)

for line in ret.stdout.splitlines():
sys.stdout.buffer.write(bytes(self.pack + ':', 'utf-8') + line + b'\n')
sys.stdout.buffer.flush()
if color:
sys.stdout.buffer.write(bytes(RED + self.pack + RESET + ':', 'utf-8') + line + b'\n')
else:
sys.stdout.buffer.write(bytes(self.pack + ':', 'utf-8') + line + b'\n')
sys.stdout.buffer.flush()

return 0
return 0


ExportedClass = GrepCommand
5 changes: 2 additions & 3 deletions code_manager/commands/pull.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import subprocess
import sys

import code_manager.utils.logger

class PullCommand:

Expand All @@ -14,7 +13,7 @@ def execute(self, args, path):
if not os.path.exists(os.path.join(path, '.git')):
return 0
ret = subprocess.run(['git', 'pull', *args], stdout=subprocess.STDOUT, cwd=path)
return 0
return 0


ExportedClass = PullCommand
8 changes: 3 additions & 5 deletions code_manager/commands/push.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import subprocess
import os
import sys
import subprocess

import code_manager.utils.logger

class PushCommand:

name = 'push'

def execute(self, args, path):
if not os.path.exists(os.path.join(path, '.git')):
return 0
ret = subprocess.run(['git', 'push', *args], stdout=subprocess.STDOUT, cwd=path)
return 0
return 0


ExportedClass = PushCommand
6 changes: 2 additions & 4 deletions code_manager/commands/sed.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import subprocess
import sys

import code_manager.utils.logger

class SedCommand:

name = 'sed'

def execute(self, args, path):
ret = subprocess.run(['sed', *args], stdout=subprocess.PIPE, cwd=path)
return 0
ret = subprocess.run(['sed', *args], stdout=subprocess.PIPE, cwd=path)
return 0


ExportedClass = SedCommand
12 changes: 10 additions & 2 deletions code_manager/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,17 @@ def rebuild_cache(self, asume_installed=True):
logging.debug('Rebuilding cache for package \'%s\'', pack)
self.cache.rebuild(pack, pack_root)

def run_command(self, command, args):
def run_command(self, command, args, thing=None, prim_args=None):
self.commands.load_commands()
for pack, _ in self.packages.items():

if thing in self.packages.keys():
queue = [thing]
elif thing in self.packages_list.keys():
queue = self.config['packages_list'][thing]
else:
queue = self.packages.keys()

for pack in queue:

if not self.cache.is_fetched(pack):
continue
Expand Down
12 changes: 10 additions & 2 deletions code_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,15 @@ def get_arg_parser():
)

grep_parser = subparsers.add_parser('grep', help='Distributed grep over the fetched pakcages')
grep_parser.add_argument('rest', nargs=argparse.REMAINDER)
grep_parser.add_argument(
'-t', '--thing', action='store', default=None, dest='thing',
help='Group or package to execute the grep for.',
)
grep_parser.add_argument(
'-n', '--no-color', action='store_true', default=None, dest='no_color',
help='Supress any color in the output',
)
grep_parser.add_argument('rest', nargs=argparse.REMAINDER, help='Arguments passed to the grep command')

sed_parser = subparsers.add_parser('sed', help='Distributed sed over the fetched pakcages')
sed_parser.add_argument('rest', nargs=argparse.REMAINDER)
Expand Down Expand Up @@ -479,7 +487,7 @@ def rebuild_cache(args, core):

@command('grep')
def grep(args, core):
core.run_command('grep', args.rest)
core.run_command('grep', args, thing=args.thing)


@command('sed')
Expand Down

0 comments on commit d0687b6

Please sign in to comment.