/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/program.py

  • Committer: Tim Marston
  • Date: 2014-04-12 13:03:11 UTC
  • Revision ID: tim@ed.am-20140412130311-kwednshmt07i1a26
remove ambiguous argument parsing (the first argument should be the command,
because we can't reliably parse-away options, which may have argument, e.g.,
"stdhome -r test status")

Show diffs side-by-side

added added

removed removed

1
 
# stdhome.py
 
1
# program.py
2
2
#
3
3
# Copyright (C) 2013 Tim Marston <tim@edm.am>
4
4
#
19
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
 
21
21
 
22
 
import sys, os
23
 
import getopt
24
 
 
25
 
 
26
 
class Stdhome:
 
22
import os, sys, getopt, ConfigParser
 
23
import the
 
24
from vcs.vcs import Vcs
 
25
 
 
26
 
 
27
class Program:
27
28
 
28
29
 
29
30
        def __init__( self, version ):
 
31
                self.name = os.path.basename( sys.argv[ 0 ] )
30
32
                self.version = version
31
 
                self.program = os.path.basename( sys.argv[ 0 ] )
 
33
 
 
34
 
 
35
        def die( self, error_message ):
 
36
                prefix = self.name + ( ' ' + the.command if the.command else '' )
 
37
                print >> sys.stderr, '%s: %s' % ( prefix, error_message )
 
38
                exit( 1 )
32
39
 
33
40
 
34
41
        def print_usage( self, error_message ):
35
 
                print >> sys.stderr, self.program + ": " + error_message
36
 
                print >> sys.stderr, "Try '" + self.program + " --help' for more information."
37
 
                exit( 1 )
 
42
                command = ' ' + the.command if the.command else ''
 
43
                self.die( error_message + \
 
44
                                  "\nTry '%s%s --help' for more information." % \
 
45
                                  ( self.name, command ) )
38
46
 
39
47
 
40
48
        def print_help( self ):
41
 
                print "Usage: " + self.program + " COMMAND [OPTION]..."
 
49
                print "Usage: " + self.name + " COMMAND [OPTION]..."
42
50
                print
43
51
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
44
 
                print "Manage your home directories, across multiple computers, similar to how you"
45
 
                print "would software under version control."
 
52
                print "Tool to manage a set of files in your home directory and distribute them across"
 
53
                print "multiple computers, merging local changes (in the same way as you would manage"
 
54
                print "source code under version control)."
46
55
                print
47
56
                print "Global options (for all commands):"
48
57
                print "     --help     display help and exit"
52
61
                print "  init          initialise a local copy of your repositories"
53
62
                print "  update        update files in your home directory"
54
63
                print "  resolve       try to finish an update (that had conflicts)"
55
 
                print "  add           add local files/changes to the repository"
56
 
                print "  remove        remove a local file from the repository"
57
 
                print "  revert        undo changes made to a local file"
58
 
                print "  stage-add     stage local files/changes"
59
 
                print "  stage-remove  stage the removal of files"
60
 
                print "  stage-revert  revert staged changes"
61
 
                print "  stage-status  show status of staging area"
62
 
                print "  stage-commit  commit staged changes to repository"
 
64
#               print "  add           add local files/changes to the repository"
 
65
#               print "  remove        remove a local file from the repository"
 
66
                print "  status        list files that have changed locally"
 
67
                print "  diff          shows changes made to local files"
 
68
                print "  revert        undo changes made to local files"
 
69
#               print "  stage-add     add (but don't commit) files/changes to local repository"
 
70
#               print "  stage-remove  delete *but don't comit) files from the local repository"
 
71
                print "  stage-revert  revert changes in the local repository"
 
72
#               print "  stage-status  show status of local repository"
 
73
#               print "  stage-diff    shows changes in local repository"
 
74
#               print "  stage-commit  commit changes in the local repository"
63
75
                print
64
76
                print "For help about a particular command (including the additional options that the"
65
77
                print "command accepts) try typing:"
66
 
                print "  $ " + self.program + " COMMAND --help"
 
78
                print "  $ " + self.name + " COMMAND --help"
67
79
                exit( 0 )
68
80
 
69
81
 
70
82
        def print_version( self ):
71
83
                print "stdhome " + self.version
72
84
                print
73
 
                print "Copyright (C) 2013 Tim Marston"
 
85
                print "Copyright (C) 2013 to 2014 Tim Marston"
74
86
                print
75
87
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
76
88
                print "This program is free software, and you may use, modify and redistribute it"
92
104
                - `command`: the given command
93
105
                """
94
106
                # commands
95
 
                if [ 'init', 'update', 'resolve', 'add', 'remove', 'revert',
96
 
                         'stage-add', 'stage-remove', 'stage-revert', 'stage-status',
97
 
                         'stage-commit' ].count( command ) == 1:
 
107
                if [ 'init', 'update', 'resolve', 'add', 'remove', 'revert', 'status',
 
108
                         'diff', 'stage-add', 'stage-remove', 'stage-revert',
 
109
                         'stage-status', 'stage-diff', 'stage-commit'
 
110
                ].count( command ) == 1:
98
111
                        return command
99
112
 
100
 
                # aliases
101
 
                elif command == 'up':
102
 
                        return 'update'
103
 
                elif command == 'rm':
104
 
                        return 'remove'
 
113
                # resolve aliases
 
114
                alias = {
 
115
                        'up': 'update',
 
116
                        'rm': 'remove',
 
117
                        'st': 'status',
 
118
                        'co': 'init',
 
119
                }.get( command, False )
 
120
                if alias: return alias
105
121
 
106
122
                # invalid
107
 
                else:
108
 
                        return None
109
 
 
110
 
 
111
 
        def get_command_argument( self, args ):
112
 
                """
113
 
                Find the first program argument what isn't an option argument.
114
 
 
115
 
        Arguments:
116
 
        - `args`: the program arguments
117
 
        """
118
 
                while args:
119
 
                        if args[ 0 ] == '--':
120
 
                                return args[ 1 ] if len( args ) > 1 else None
121
 
                        if args[ 0 ][ 0 : 1 ] != '-':
122
 
                                return args[ 0 ]
123
 
                        args = args[ 1 : ]
124
123
                return None
125
124
 
 
125
 
126
126
        def run( self ):
127
127
                # make an initial attempt to parse the command line, looking only for
128
128
                # --help and --version, so that they have the chance to run without a
132
132
                                sys.argv[ 1: ], "",
133
133
                                [ "help", "version" ] )
134
134
 
135
 
                        for opt, optargs in opts:
 
135
                        for opt, optarg in opts:
136
136
                                # we only show help if there are no non-option arguments (e.g.,
137
137
                                # a command) specified.  If a command has been specified it will
138
138
                                # have to be parsed and --help will be handled by it, instead)
142
142
                                        self.print_version()
143
143
 
144
144
                except getopt.GetoptError as e:
145
 
                        # ignore any errors -- we aren't parsing the command line properly
 
145
                        # ignore errors -- we aren't parsing the command line properly yet
146
146
                        pass
147
147
 
148
 
                # find the first non-option argument (the command)
149
 
                self.command = self.get_command_argument( sys.argv[ 1: ] )
150
 
                if self.command == None:
 
148
                # read program configuration
 
149
                self.read_config()
 
150
 
 
151
                # the first argument should be the command
 
152
                the.command = sys.argv[ 1 ] if len( sys.argv ) > 1 else None
 
153
                if the.command == None:
151
154
                        self.print_usage( "missing command" )
152
155
 
153
156
                # check command is valid
154
 
                self.command = self.check_command( self.command )
155
 
                if self.command == None:
 
157
                the.command = self.check_command( the.command )
 
158
                if the.command == None:
156
159
                        self.print_usage( "bad command" )
157
160
 
158
 
                # calculate class name
159
 
                bits = self.command.split( '-' )
160
 
                class_name = 'Command'
 
161
                # calculate module and class name
 
162
                class_name = module_name = ''
 
163
                bits = the.command.split( '-' )
161
164
                for bit in bits:
162
165
                        class_name += bit[ 0 ].upper() + bit[ 1 : ]
163
 
 
164
 
                # instantiate and run the command class
165
 
                module = __import__( 'stdhome.command_' + self.command )
166
 
                the_class = getattr( module, class_name )
167
 
                instance = the_class()
168
 
                instance.run( self )
 
166
                        if module_name: module_name += '_'
 
167
                        module_name += bit
 
168
                class_name += 'Command'
 
169
 
 
170
                # import module and instantiate the class
 
171
                module = __import__( 'stdhome.command.' + module_name,
 
172
                                                         fromlist = [ class_name ] )
 
173
                instance = getattr( module, class_name )()
 
174
 
 
175
                # fully parse the command line, as per the command
 
176
                try:
 
177
                        instance.parse_command_line()
 
178
                except( getopt.GetoptError, self.UsageError ) as e:
 
179
                        self.print_usage( e.msg )
 
180
                except self.FatalError as e:
 
181
                        self.die( e.msg )
 
182
 
 
183
                # do late initialisation
 
184
                the.late_init()
 
185
 
 
186
                # run the command
 
187
                try:
 
188
                        instance.run()
 
189
                except Vcs.VcsError as e:
 
190
                        message = e.msg.rstrip()
 
191
                        if the.verbose:
 
192
                                message += '\n\nOUTPUT:\n' + e.output.rstrip()
 
193
                        self.die( message )
 
194
                except self.FatalError as e:
 
195
                        self.die( e.msg )
 
196
 
 
197
 
 
198
        def read_config( self ):
 
199
                config = ConfigParser.SafeConfigParser( allow_no_value = True )
 
200
                config.read( the.config_file )
 
201
                if config.has_option( 'stdhome', 'home-dir' ):
 
202
                        the.home_dir = config.get( 'stdhome', 'home-dir' )
 
203
 
 
204
 
 
205
        class UsageError( Exception ):
 
206
 
 
207
                def __init__( self, error_message ):
 
208
                        self.msg = error_message
 
209
 
 
210
 
 
211
        class FatalError( Exception ):
 
212
 
 
213
                def __init__( self, message ):
 
214
                        self.msg = message