Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
Release 0.1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
ShixiangWang committed Nov 1, 2019
1 parent cb83800 commit 1a9d4ca
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

Version 0.1.17
=============

- Added `batch` command

Version 0.1.16
=============

Expand Down
3 changes: 2 additions & 1 deletion formatting.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
yapf -ir src/loon/__init__.py -vv
yapf -ir src/loon/skeleton.py -vv
yapf -ir src/loon/classes.py -vv
yapf -ir src/loon/utils.py -vv
yapf -ir src/loon/utils.py -vv
yapf -ir src/loon/tool.py -vv
2 changes: 1 addition & 1 deletion src/loon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import os

__version__ = "0.1.16"
__version__ = "0.1.17"
__author__ = "ShixiangWang"
__copyright__ = "ShixiangWang"
__license__ = "mit"
Expand Down
38 changes: 38 additions & 0 deletions src/loon/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
if __package__ == '' or __package__ is None: # Use for test
from __init__ import __version__, __author__, __license__
from classes import Host, PBS
from tool import batch
else:
from loon import __version__, __author__, __license__
from loon.classes import Host, PBS
from loon.tool import batch

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -189,6 +191,34 @@ def parse_args(args):
help="Use rsync instead of scp",
action='store_true')

# Create the parser for the "batch" command
parser_batch = subparsers.add_parser(
'batch',
help="Batch process commands with placeholders",
parents=[verbose_parser])
parser_batch.add_argument(
'-f',
'--file',
help=
r'A structed file like CSV, TSV etc. Each column is placeholder target, i.e. {0} targets the first column, {1} targets the second column, etc.',
type=str,
required=True)
parser_batch.add_argument(
'-s',
'--sep',
help=r"File separator, ',' for CSV (default) and '\t' for TSV",
default=',',
required=False)
parser_batch.add_argument('--header',
help="Set it if input file contains header",
action='store_true')
parser_batch.add_argument('--dry',
help="Dry run the commands",
action='store_true')
parser_batch.add_argument('cmds',
type=str,
help="A sample command with placeholders")

# Create the parser for the "pbstemp" command
parser_pbstemp = subparsers.add_parser('pbstemp',
help='Generate a PBS template file',
Expand Down Expand Up @@ -387,6 +417,14 @@ def main(args):
args.destination,
_logger=_logger,
use_rsync=use_rsync)
elif args.subparsers_name == 'batch':
_logger.info("Batch command is detected.")
batch(args.file,
args.cmds,
sep=args.sep,
header=args.header,
dry_run=args.dry,
_logger=_logger)
elif args.subparsers_name == 'pbstemp':
_logger.info("pbstemp command is detected.")
pbs.gen_template(args.input, args.output)
Expand Down
34 changes: 34 additions & 0 deletions src/loon/tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""
Tool functions
"""

import sys
from subprocess import run, PIPE
from loon.utils import isfile, isdir, read_csv


def batch(input, cmds, sep=',', header=False, dry_run=False, _logger=None):
"""Batch process commands according to mappings from file"""
if not isfile(input):
print("Error: file %s does not exist" % input)
sys.exit(1)

data = read_csv(input, sep=sep, rm_comment=True)
if not header:
# Remove header
_ = data.pop(0)

cmd_list = []
for row in data:
cmd_list.append(cmds.format(*row))
for cmd in cmd_list:
print("=> Running %s" % cmd)
if not dry_run:
run_res = run(cmd, shell=True)
_logger.info("Status code: " + str(run_res.returncode))
if run_res.returncode != 0:
print("Error: some error occurred, please check the info!")
sys.exit(run_res.returncode)

return

0 comments on commit 1a9d4ca

Please sign in to comment.