Run update-copyright.py.
[hooke.git] / hooke / plugin / system.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
17
18 """The `system` module provides :class:`SystemPlugin` and several
19 associated :class:`hooke.command.Command`\s for interacting with the
20 operating system and execution environment.
21 """
22
23 import os
24 import os.path
25 import subprocess
26 import sys
27
28 from ..command import Command, Argument
29 from . import Builtin
30
31
32 class SystemPlugin (Builtin):
33     def __init__(self):
34         super(SystemPlugin, self).__init__(name='system')
35         self._commands = [
36             ListDirectoryCommand(self), GetWorkingDirectoryCommand(self),
37             ChangeDirectoryCommand(self), SystemCommand(self)]
38
39
40 class ListDirectoryCommand (Command):
41     """List the files in a directory.
42     """
43     def __init__(self, plugin):
44         super(ListDirectoryCommand, self).__init__(
45             name='ls', aliases=['dir'],
46             arguments=[
47                 Argument(
48     name='path', type='path', default='.',
49     help="""
50 Path to the directory whose contents get listed.  Defaults to the
51 current working directory.
52 """.strip()),
53 ],
54             help=self.__doc__, plugin=plugin)
55
56     def _run(self, hooke, inqueue, outqueue, params):
57         outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
58
59 class GetWorkingDirectoryCommand (Command):
60     """Get the current working directory.
61     """
62     def __init__(self, plugin):
63         super(GetWorkingDirectoryCommand, self).__init__(
64             name='cwd', aliases=['pwd'], help=self.__doc__, plugin=plugin)
65
66     def _run(self, hooke, inqueue, outqueue, params):
67         outqueue.put(os.getcwd())
68
69 class ChangeDirectoryCommand (Command):
70     """Change the current working directory.
71     """
72     def __init__(self, plugin):
73         super(ChangeDirectoryCommand, self).__init__(
74             name='cd',
75             arguments=[
76                 Argument(
77     name='path', type='path', default='~',
78     help="""
79 Path of the directory to change into.  Default to the user's home
80 directory.
81 """.strip()),
82 ],
83             help=self.__doc__, plugin=plugin)
84
85     def _run(self, hooke, inqueue, outqueue, params):
86         os.chdir(os.path.expanduser(params['path']))
87
88 class SystemCommand (Command):
89     """Execute a system command and report the output.
90     """
91     def __init__(self, plugin):
92         super(SystemCommand, self).__init__(
93             name='system',
94             arguments=[
95                 Argument(
96     name='command', type='string', optional=False, count=-1,
97     help="""
98 Command line to execute.
99 """.strip()),
100 ],
101             help=self.__doc__, plugin=plugin)
102
103     def _run(self, hooke, inqueue, outqueue, params):
104         p = subprocess.Popen(
105             params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
106         stdout,stderr = p.communicate()
107         returncode = p.wait()
108         outqueue.put(stdout)
109         outqueue.put(stderr)