42e9cbe3a11bd0db7e382f032aab99930eee9b5c
[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
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General 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
37     def commands(self):
38         return [ListDirectoryCommand(), GetWorkingDirectoryCommand(),
39                 ChangeDirectoryCommand(), SystemCommand()]
40
41
42 class ListDirectoryCommand (Command):
43     """List the files in a directory.
44     """
45     def __init__(self):
46         super(ListDirectoryCommand, self).__init__(
47             name='ls', aliases=['dir'],
48             arguments=[
49                 Argument(
50     name='path', type='path', default='.',
51     help="""
52 Path to the directory whose contents get listed.  Defaults to the
53 current working directory.
54 """.strip()),
55 ],
56             help=self.__doc__)
57
58     def _run(self, hooke, inqueue, outqueue, params):
59         outqueue.put('\n'.join(sorted(os.listdir(params['path']))))
60
61 class GetWorkingDirectoryCommand (Command):
62     """Get the current working directory.
63     """
64     def __init__(self):
65         super(GetWorkingDirectoryCommand, self).__init__(
66             name='cwd', aliases=['pwd'], help=self.__doc__)
67
68     def _run(self, hooke, inqueue, outqueue, params):
69         outqueue.put(os.getcwd())
70
71 class ChangeDirectoryCommand (Command):
72     """Change the current working directory.
73     """
74     def __init__(self):
75         super(ChangeDirectoryCommand, self).__init__(
76             name='cd',
77             arguments=[
78                 Argument(
79     name='path', type='path', default='~',
80     help="""
81 Path of the directory to change into.  Default to the user's home
82 directory.
83 """.strip()),
84 ],
85             help=self.__doc__)
86
87     def _run(self, hooke, inqueue, outqueue, params):
88         os.chdir(os.path.expanduser(params['path']))
89
90 class SystemCommand (Command):
91     """Execute a system command and report the output.
92     """
93     def __init__(self):
94         super(SystemCommand, self).__init__(
95             name='system',
96             arguments=[
97                 Argument(
98     name='command', type='string', optional=False, count=-1,
99     help="""
100 Command line to execute.
101 """.strip()),
102 ],
103             help=self.__doc__)
104
105     def _run(self, hooke, inqueue, outqueue, params):
106         p = subprocess.Popen(
107             params['command'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
108         stdout,stderr = p.communicate()
109         returncode = p.wait()
110         outqueue.put(stdout)
111         outqueue.put(stderr)