2c95845c839d2fb34e97188da7f7e88856b66d85
[proj/portage.git] / pym / portage / _sets / shell.py
1 # Copyright 2007 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 import subprocess
5
6 from portage import os
7 from portage import _unicode_decode
8 from portage._sets.base import PackageSet
9 from portage._sets import SetConfigError
10
11 __all__ = ["CommandOutputSet"]
12
13 class CommandOutputSet(PackageSet):
14 """This class creates a PackageSet from the output of a shell command.
15 The shell command should produce one atom per line, that is:
16
17 >>> atom1
18 atom2
19 ...
20 atomN
21
22 Args:
23 name: A string that identifies the set.
24 command: A string or sequence identifying the command to run
25 (see the subprocess.Popen documentaion for the format)
26 """
27 _operations = ["merge", "unmerge"]
28
29 def __init__(self, command):
30 super(CommandOutputSet, self).__init__()
31 self._command = command
32 self.description = "Package set generated from output of '%s'" % self._command
33
34 def load(self):
35 pipe = subprocess.Popen(self._command, stdout=subprocess.PIPE, shell=True)
36 stdout, stderr = pipe.communicate()
37 if pipe.wait() == os.EX_OK:
38 self._setAtoms(_unicode_decode(stdout).splitlines())
39
40 def singleBuilder(self, options, settings, trees):
41 if not "command" in options:
42 raise SetConfigError("no command specified")
43 return CommandOutputSet(options["command"])
44 singleBuilder = classmethod(singleBuilder)