Permalink
Browse files

Working on it

  • Loading branch information...
mortenvp committed May 11, 2018
1 parent 6cb0325 commit cd2fbbb7fd7ed20a4943e0e9bd6e70985fc5e174
Showing with 90 additions and 9 deletions.
  1. +70 −0 src/git_sphinx_build/commandline.py
  2. +12 −9 src/git_sphinx_build/virtualenv.py
  3. +8 −0 test/test_commandline.py
@@ -0,0 +1,70 @@
import os
import subprocess
import time
import logging
from . import compat
from . import check_output
from . import run_result
from . import run_error
class Prompt(object):
def __init__(self, cwd=None, env=None, stdout=None, stderr=None, log=None):
self.cwd = cwd if cwd else os.getcwd()
self.env = env if env else dict(os.environ)
self.stdout = stdout if stdout else subprocess.PIPE
self.stderr = stderr if stderr else subprocess.PIPE
self.log = log if log else logging.getLogger(__name__)
def run(self, command, **kwargs):
"""Runs the command
:param command: String or list of arguments
:param kwargs: Keyword arguments passed to Popen(...)
:return: A RunResult object representing the result of the command
"""
if isinstance(command, compat.string_type):
kwargs['shell'] = True
if 'env' not in kwargs:
kwargs['env'] = self.env
if 'stdout' not in kwargs:
kwargs['stdout'] = self.stdout
if 'stderr' not in kwargs:
kwargs['stderr'] = self.stderr
if 'cwd' not in kwargs:
kwargs['cwd'] = self.cwd
self.log.debug(command)
start_time = time.time()
popen = subprocess.Popen(
command,
# Need to decode the stdout and stderr with the correct
# character encoding (http://stackoverflow.com/a/28996987)
universal_newlines=True,
**kwargs)
stdout, stderr = popen.communicate()
end_time = time.time()
if isinstance(command, list):
command = ' '.join(command)
result = run_result.RunResult(
command=command, path=kwargs['cwd'],
stdout=stdout, stderr=stderr, returncode=popen.returncode,
time=end_time - start_time)
if popen.returncode != 0:
raise run_error.RunError(result)
return result
@@ -5,9 +5,11 @@
import sys
import hashlib
import shutil
import logging
from . import git_run
from . import command
from . import commandline
URL = 'https://github.com/pypa/virtualenv.git'
VERSION = '15.1.0'
@@ -25,19 +27,19 @@ class VirtualEnv(object):
with '../somefile.txt'.
"""
def __init__(self, process, process_factory, log):
self.process = process
self.process_factory = process_factory
self.log = log
def __init__(self, prompt=None, log=None):
def create(self, path):
self.prompt = prompt if prompt else commandline.Prompt()
self.log = log if log else logging.getLogger(__name__)
def create_environment(self, path):
if not os.path.isdir(path):
args = ['python', '-m', 'virtualenv', path,
'--no-site-packages']
self.process.run(command=args)
self.prompt.run(command=args)
# Create a new environment based on the new virtualenv
env = dict(os.environ)
@@ -50,8 +52,10 @@ def create(self, path):
env['PATH'] = os.path.pathsep.join([python_path, env['PATH']])
return env
def from_git(git, clone_path, process_factory, log):
def from_git(git, clone_path, prompt_class, log):
""" This method will construct the VirtualEvn with a command where
the virtualenv is in the path.
"""
@@ -72,8 +76,7 @@ def from_git(git, clone_path, process_factory, log):
env = dict(os.environ)
env.update({'PYTHONPATH': clone_path})
process = process_factory.build()
process.env = env
process = prompt_class(env=env)
return VirtualEnv(process=process, process_factory=process_factory,
log=log)
View
@@ -0,0 +1,8 @@
import git_sphinx_build
import git_sphinx_build.commandline as cli
def test_run(testdirectory):
prompt = cli.Prompt()
prompt.run('python --version', cwd=testdirectory.path())

0 comments on commit cd2fbbb

Please sign in to comment.