Skip to content

Commit

Permalink
Merge 6f7429e into 2f2713f
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyLothar committed Apr 9, 2015
2 parents 2f2713f + 6f7429e commit d0ab900
Show file tree
Hide file tree
Showing 18 changed files with 587 additions and 274 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: python

python:
- "3.4"

install:
- pip install -r tests/requirements.txt
- pip install nose

script: nosetests --with-coverage --cover-package=shcmd

after_success:
- pip install coveralls
- coveralls
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.rst LICENSE tests/requirements.txt
38 changes: 0 additions & 38 deletions README.md

This file was deleted.

35 changes: 35 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
SHCMD
-----

Note: Work in Progress.

This lib aims to provide a Human friendly interface for subprocess.

If you need piped subprocesses, give envoy_ a try.

.. image:: https://travis-ci.org/SkyLothar/shcmd.svg?branch=master
:target: https://travis-ci.org/SkyLothar/shcmd
.. image:: https://travis-ci.org/SkyLothar/shcmd.svg?branch=master
:target: https://travis-ci.org/SkyLothar/shcmd
.. image:: https://img.shields.io/coveralls/SkyLothar/shcmd/master.svg
:target: https://coveralls.io/r/SkyLothar/shcmd

Usage
^^^^^^

.. code-block:: python
import shcmd
with shcmd.cd("/tmp"):
# get result directly
assert shcmd.run("pwd") == "/tmp"
# get streamed result packed in a generator
streamed = shcmd.run("ls", stream=True)
for filename in streamed.iter_lines():
print(filename)
# get full stdout/stderr
print(streamed.stdout)
print(streamed.stderr)
.. _`envoy`: https://github.com/kennethreitz/envoy
28 changes: 13 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# -*- coding: utf-8 -*-

__version__ = ""
__author__ = ""
__email__ = ""
__url__ = ""


import os
import sys

from codecs import open

__version__ = "0.1.0"
__author__ = "SkyLothar"
__email__ = "allothar@gmail.com"
__url__ = "https://github.com/skylothar/shcmd"


try:
import setuptools
except ImportError:
Expand All @@ -25,7 +25,7 @@
packages = ["shcmd"]


with open("README.md", "r", "utf-8") as f:
with open("README.rst", "r", "utf-8") as f:
readme = f.read()

with open("tests/requirements.txt", "r", "utf-8") as f:
Expand All @@ -35,33 +35,31 @@
setuptools.setup(
name="shcmd",
version=__version__,
description="",
description="simple command-line wrapper",
long_description=readme,
author=__author__,
author_email=__email__,
url=__url__,
packages=packages,
package_data={
"": ["LICENSE", "NOTICE"]
"": ["LICENSE"]
},
package_dir={
"shcmd": "shcmd"
},
include_package_data=True,
install_requires=[],
license="Apache 2.0",
zip_safe=False,
classifiers=(
"Development Status :: 5 - Production/Stable",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Natural Language :: English",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4"

),
],
setup_requires=["nose >= 1.0"],
tests_require=tests_require,
test_suite="nose.collector"
Expand Down
29 changes: 29 additions & 0 deletions shcmd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
__version__ = "0.1.2"
__author__ = "SkyLothar"
__email__ = "allothar@gmail.com"
__url__ = "https://github.com/skylothar/shcmd"

import os

__all__ = ["cd", "cd_to", "run", "tar_generator"]

from .cmd import cd, cd_to
from .proc import Proc
from .tar import tar_generator
from .utils import expand_args


DEFAULT_TIMEOUT = 60


def run(cmd, cwd=None, env=None, timeout=None, stream=False):
proc = Proc(
expand_args(cmd),
os.path.realpath(cwd or os.getcwd()),
env=env or {},
timeout=timeout or DEFAULT_TIMEOUT
)

if not stream:
proc.block()
return proc
62 changes: 26 additions & 36 deletions shcmd/cmd.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
# -*- coding: utf8 -*-

import contextlib
import functools
import os
import shlex

from . import compat

@contextlib.contextmanager
def cd(cd_path):
"""cd to target dir when running in this block
def split_args(cmd_args):
"""Split command args to args list
Returns a list of args
:param cd_path: dir to cd into
:param cmd_args: command args, can be tuple, list or str
"""
if isinstance(cmd_args, (tuple, list)):
args_list = list(cmd_args)
elif compat.is_py2 and isinstance(cmd_args, compat.str):
args_list = shlex.split(cmd_args.encode("utf8"))
elif compat.is_py3 and isinstance(cmd_args, compat.bytes):
args_list = shlex.split(cmd_args.decode("utf8"))
else:
args_list = shlex.split(cmd_args)
return args_list
Usage::

@contextlib.contextmanager
def cd(cd_path):
>>> with cd("/tmp"):
... print("we are in /tmp now")
"""
oricwd = os.getcwd()
try:
os.chdir(cd_path)
Expand All @@ -34,23 +24,23 @@ def cd(cd_path):
os.chdir(oricwd)


class CmdRequest(object):
def __init__(self, cmd, cwd=None):
self._raw = cmd
self._cmd = split_args(cmd)
self._cwd = os.path.realpath(cwd or os.getcwd())
def cd_to(path):
"""make a generator like cd, but use it for function
def __str__(self):
return "<CmdRequest ({0})@{1}>".format(self._raw, self.cwd)
Usage::
@property
def raw(self):
return self._raw
>>> @cd_to("/")
... def say_where():
... print(os.getcwd())
...
>>> say_where()
/
@property
def cmd(self):
return self._cmd[:]

@property
def cwd(self):
return self._cwd
"""
def cd_to_decorator(func):
@functools.wraps(func)
def _cd_and_exec(*args, **kwargs):
with cd(path):
return func(*args, **kwargs)
return _cd_and_exec
return cd_to_decorator
98 changes: 0 additions & 98 deletions shcmd/compat.py

This file was deleted.

2 changes: 1 addition & 1 deletion shcmd/errors.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
class CmdError(Exception):
class ShCmdError(Exception):
pass
Loading

0 comments on commit d0ab900

Please sign in to comment.