Skip to content

Commit

Permalink
Add all bunch of distributed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
palikar committed Jun 17, 2020
1 parent d0687b6 commit 96ec9ea
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ repos:
- id: double-quote-string-fixer
- id: name-tests-test
- id: requirements-txt-fixer
- id: check-executables-have-shebangs
- id: check-json
- id: double-quote-string-fixer
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.0
hooks:
Expand Down
13 changes: 12 additions & 1 deletion code_manager/commands/commit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import subprocess

Expand All @@ -12,7 +13,17 @@ def __init__(self):
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)

msg = args.message
msg += '\n\n\nCreated by code-manager'
msg = f'"{msg}"'

push_command = ['git', 'commit', '-m', msg]
push_command.extend(args.rest)

logging.debug('Running command: [%s] in %s', ' '.join(push_command), path)
subprocess.run(push_command, stdout=subprocess.STDOUT, cwd=path)

return 0


Expand Down
50 changes: 50 additions & 0 deletions code_manager/commands/find.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import logging
import sys
import os
import subprocess


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


class FindCommand(ConfigurationAware):

name = 'find'

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

def execute(self, args, path):

if self.color and not args.no_color:
color = True
elif args.no_color:
color = False

if not os.path.exists(os.path.join(path, '.git')):
find_command = ['find', '-name', args.files]
logging.debug('Running command: [%s] in %s', ' '.join(find_command), path)
ret = subprocess.run(find_command, stdout=subprocess.PIPE, cwd=path, check=False)
else:
git_command = ['git', 'ls-files', args.files]
logging.debug('Running command: [%s] in %s', ' '.join(git_command), path)
ret = subprocess.run(git_command, stdout=subprocess.PIPE, cwd=path, check=False)

files = ret.stdout.splitlines()

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

sys.stdout.buffer.flush()


return 0


ExportedClass = FindCommand
5 changes: 4 additions & 1 deletion code_manager/commands/grep.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import subprocess
import sys

Expand All @@ -24,7 +25,9 @@ def execute(self, args, path):
grep_command.append('--color=never')

grep_command.extend(args.rest)
ret = subprocess.run(grep_command, stdout=subprocess.PIPE, cwd=path)

logging.debug('Running command: [%s] in %s', ' '.join(grep_command), path)
ret = subprocess.run(grep_command, stdout=subprocess.PIPE, cwd=path, check=False)

for line in ret.stdout.splitlines():
if color:
Expand Down
5 changes: 4 additions & 1 deletion code_manager/commands/pull.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import subprocess

Expand All @@ -12,7 +13,9 @@ def __init__(self):
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)
push_command = ['git', 'pull', *args.rest]
logging.debug('Running command: [%s] in %s', ' '.join(push_command), path)
subprocess.run(push_command, stdout=subprocess.STDOUT, cwd=path)
return 0


Expand Down
5 changes: 4 additions & 1 deletion code_manager/commands/push.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import subprocess

Expand All @@ -9,7 +10,9 @@ class PushCommand:
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)
push_command = ['git', 'push', *args.rest]
logging.debug('Running command: [%s] in %s', ' '.join(push_command), path)
subprocess.run(push_command, stdout=subprocess.STDOUT, cwd=path)
return 0


Expand Down
49 changes: 47 additions & 2 deletions code_manager/commands/sed.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
import logging
import os
import subprocess
import sys

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

class SedCommand:

class SedCommand(ConfigurationAware):

name = 'sed'

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

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

if self.color and not args.no_color:
color = True
elif args.no_color:
color = False

if not os.path.exists(os.path.join(path, '.git')):
find_command = ['find', '-name', args.files]
logging.debug('Running command: [%s] in %s', ' '.join(find_command), path)
ret = subprocess.run(find_command, stdout=subprocess.PIPE, cwd=path, check=False)
else:
git_command = ['git', 'ls-files', args.files]
logging.debug('Running command: [%s] in %s', ' '.join(git_command), path)
ret = subprocess.run(git_command, stdout=subprocess.PIPE, cwd=path, check=False)

files = ret.stdout.splitlines()

for file_name in files:
file_name = file_name.decode('utf-8')
sed_command = ['sed']
if args.inplace:
sed_command.append('-i')
sed_command.extend(args.sed_args)
sed_command.extend(['-e', args.expression, file_name])

logging.debug('Running command: [%s] in ', ' '.join(sed_command), path)
ret = subprocess.run(sed_command, stdout=subprocess.PIPE, cwd=path, check=False)

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

return 0


Expand Down
71 changes: 65 additions & 6 deletions code_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ def get_arg_parser():
)

groups_parser = subparsers.add_parser('list-groups', help='List the avaialble groups or the packeges in them')

groups_parser.add_argument(
'groups',
action='store',
Expand All @@ -334,7 +333,7 @@ def get_arg_parser():
grep_parser = subparsers.add_parser('grep', help='Distributed grep over the fetched pakcages')
grep_parser.add_argument(
'-t', '--thing', action='store', default=None, dest='thing',
help='Group or package to execute the grep for.',
help='Group or package to execute the command for.',
)
grep_parser.add_argument(
'-n', '--no-color', action='store_true', default=None, dest='no_color',
Expand All @@ -343,17 +342,72 @@ def get_arg_parser():
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(
'-t', '--thing', action='store', default=None, dest='thing',
help='Group or package to execute the command for.',
)
sed_parser.add_argument(
'-s', '--sed-args', action='append', default=[], dest='sed_args',
help='Extra argument for the sed command.',
)
sed_parser.add_argument(
'-i', '--in-place', action='store_true', default=False, dest='inplace',
help='Run the sed command with the \'-i\' flag.',
)
sed_parser.add_argument(
'-n', '--no-color', action='store_true', default=None, dest='no_color',
help='Supress any color in the output',
)
sed_parser.add_argument(
'expression', action='store',
help='Sed expression to use.',
)
sed_parser.add_argument(
'files', action='store',
help='A glob for filenames.',
)
sed_parser.add_argument('rest', nargs=argparse.REMAINDER)

push_parser = subparsers.add_parser('push', help='Push every pacakge with git repo')
push_parser.add_argument(
'-t', '--thing', action='store', default=None, dest='thing',
help='Group or package to execute the command for.',
)
push_parser.add_argument('rest', nargs=argparse.REMAINDER)

pull_parser = subparsers.add_parser('pull', help='Pull every pacakge with git repo')
pull_parser.add_argument(
'-t', '--thing', action='store', default=None, dest='thing',
help='Group or package to execute the command for.',
)
pull_parser.add_argument('rest', nargs=argparse.REMAINDER)

commit_parser = subparsers.add_parser('commit', help='Make commit for every pacakge with git repo')
commit_parser.add_argument(
'-t', '--thing', action='store', default=None, dest='thing',
help='Group or package to execute the command for.',
)
commit_parser.add_argument(
'message', action='store',
help='Message for the newly created commit.',
)
commit_parser.add_argument('rest', nargs=argparse.REMAINDER)

find_parser = subparsers.add_parser('find', help='Run distributed find command for every package.')
find_parser.add_argument(
'-t', '--thing', action='store', default=None, dest='thing',
help='Group or package to execute the command for.',
)
find_parser.add_argument(
'-n', '--no-color', action='store_true', default=None, dest='no_color',
help='Supress any color in the output',
)
find_parser.add_argument(
'files', action='store',
help='A glob for filenames.',
)
find_parser.add_argument('rest', nargs=argparse.REMAINDER)

return parser


Expand Down Expand Up @@ -492,22 +546,27 @@ def grep(args, core):

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


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


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


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


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


@command('list-fetchers')
Expand Down

0 comments on commit 96ec9ea

Please sign in to comment.