7f490366bb9a0abe0093de350e8d1cb9254530ce
[hooke.git] / hooke / plugin / system.py
1 # Copyright
2
3 """The `system` module provides :class:`SystemPlugin` and several
4 associated :class:`hooke.command.Command`\s for interacting with the
5 operating system and execution environment.
6 """
7
8 import os
9 import os.path
10 import subprocess
11 import sys
12
13 from ..command import Command, Argument
14 from ..plugin import Builtin
15
16
17 class SystemPlugin (Builtin):
18     def __init__(self):
19         super(SystemPlugin, self).__init__(name='system')
20
21     def commands(self):
22         return [ListDirectoryCommand(), GetWorkingDirectoryCommand(),
23                 ChangeDirectoryCommand(), SystemCommand()]
24
25
26 class ListDirectoryCommand (Command):
27     """List the files in a directory.
28     """
29     def __init__(self):
30         super(ListDirectoryCommand, self).__init__(
31             name='ls', aliases=['dir'],
32             arguments=[
33                 Argument(
34     name='path', type='path', default='.',
35     help="""
36 Path to the directory whose contents get listed.  Defaults to the
37 current working directory.
38 """.strip()),
39 ],
40             help=self.__doc__)
41
42     def _run(self, hooke, inqueue, outqueue, params):
43         outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
44
45 class GetWorkingDirectoryCommand (Command):
46     """Get the current working directory.
47     """
48     def __init__(self):
49         super(GetWorkingDirectoryCommand, self).__init__(
50             name='cwd', aliases=['pwd'], help=self.__doc__)
51
52     def _run(self, hooke, inqueue, outqueue, params):
53         outqueue.put(os.getcwd())
54
55 class ChangeDirectoryCommand (Command):
56     """Change the current working directory.
57     """
58     def __init__(self):
59         super(ChangeDirectoryCommand, self).__init__(
60             name='cd',
61             arguments=[
62                 Argument(
63     name='path', type='path', default='~',
64     help="""
65 Path of the directory to change into.  Default to the user's home
66 directory.
67 """.strip()),
68 ],
69             help=self.__doc__)
70
71     def _run(self, hooke, inqueue, outqueue, params):
72         os.chdir(os.path.expanduser(params['path']))
73
74 class SystemCommand (Command):
75     """Execute a system command and report the output.
76     """
77     def __init__(self):
78         super(SystemCommand, self).__init__(
79             name='system',
80             arguments=[
81                 Argument(
82     name='command', type='string', optional=False, count=-1,
83     help="""
84 Command line to execute.
85 """.strip()),
86 ],
87             help=self.__doc__)
88
89     def _run(self, hooke, inqueue, outqueue, params):
90         p = subprocess.Popen(
91             params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
92         stdout,stderr = p.communicate()
93         returncode = p.wait()
94         outqueue.put(stdout)
95         outqueue.put(stderr)