예제 #1
0
파일: shell.py 프로젝트: yuhangwang/craftr
  def __init__(self, command, input_=None, encoding=sys.getdefaultencoding(),
      pipe=True, merge=False, shell=False, cwd=None):

    super().__init__()

    if shell and isinstance(command, (list, tuple)):
      command = ' '.join(quote(x) for x in autoexpand(command))
    elif not shell:
      command = autoexpand(command)

    if pipe:
      stdout = subprocess.PIPE
      if merge:
        stderr = subprocess.STDOUT
      else:
        stderr = subprocess.PIPE
    else:
      stdout = stderr = None

    self.command = command
    self.program = command[0] if not shell else shlex.split(command)[0]
    self.popen = subprocess.Popen(command, shell=shell, stdout=stdout,
      stderr=stderr, cwd=cwd)
    self.stdout, self.stderr = self.popen.communicate(input_)

    if encoding is not None:
      if self.stdout is not None:
        self.stdout = self.stdout.decode(encoding)
      if self.stderr is not None:
        self.stderr = self.stderr.decode(encoding)

    if self.returncode != 0:
      raise Process.ExitCodeError(self)
예제 #2
0
  def __init__(self, module, name, inputs, outputs, requires=(),
      foreach=False, description=None, **kwargs):
    from craftr.utils.lists import autoexpand

    inputs = autoexpand(inputs)
    outputs = autoexpand(outputs)
    requires = autoexpand(requires)

    if not isinstance(module, Module):
      raise TypeError('<module> must be a Module object', type(module))
    if not isinstance(name, str):
      raise TypeError('<name> must be a string', type(name))
    if not utils.ident.validate_var(name):
      raise ValueError('invalid target name', name)

    super().__init__()
    self.module = module
    self.name = name
    self.inputs = inputs
    self.outputs = outputs
    self.requires = requires
    self.foreach = foreach
    self.description = description
    self.commands = []
    self.meta = {}

    for key, value in tuple(kwargs.items()):
      if key.startswith('meta_'):
        name = key[5:]
        if name:
          self.meta[name] = value
          kwargs.pop(key)

    for key, value in sorted(kwargs.items(), key=lambda x: x[0]):
      if not re.match('command\d?$', key):
        raise TypeError('unexpected keyword argument ' + key)
      if not isinstance(value, list):
        raise TypeError('<' + key + '> must be a list', type(value))
      self.commands.append(autoexpand(value))