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