/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: 2016-04-10 20:17:32 UTC
  • Revision ID: tim@ed.am-20160410201732-w6358gtt2df1y5e9
added 'ci' as an alias of add; fixed issue with lack of output from vcs when
there are merge conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
import os, sys, getopt
23
23
import the
 
24
from config import Config
24
25
from vcs.vcs import Vcs
25
26
 
26
27
 
62
63
                print "  update        update files in your home directory"
63
64
                print "  resolve       try to finish an update (that had conflicts)"
64
65
                print "  add           add local files/changes to the repository"
65
 
                print "  remove        remove a local file from the repository"
 
66
#               print "  remove        remove a local file from the repository"
66
67
                print "  status        list files that have changed locally"
67
68
                print "  diff          shows changes made to local files"
68
 
                print "  revert        undo changes made to a local file"
69
 
                print "  stage-add     stage local files/changes"
70
 
                print "  stage-remove  stage the removal of files"
71
 
                print "  stage-revert  revert staged changes"
72
 
                print "  stage-status  show status of staging area"
73
 
                print "  stage-diff    shows staged changes"
74
 
                print "  stage-commit  commit staged changes to repository"
 
69
                print "  revert        undo changes made to local files"
 
70
#               print "  stage-add     add (but don't commit) files/changes to local repository"
 
71
#               print "  stage-remove  delete *but don't comit) files from the local repository"
 
72
                print "  stage-revert  revert changes in the local repository"
 
73
#               print "  stage-status  show status of local repository"
 
74
#               print "  stage-diff    shows changes in local repository"
 
75
#               print "  stage-commit  commit changes in the local repository"
75
76
                print
76
77
                print "For help about a particular command (including the additional options that the"
77
78
                print "command accepts) try typing:"
82
83
        def print_version( self ):
83
84
                print "stdhome " + self.version
84
85
                print
85
 
                print "Copyright (C) 2013 Tim Marston"
 
86
                print "Copyright (C) 2013 to 2014 Tim Marston"
86
87
                print
87
88
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
88
89
                print "This program is free software, and you may use, modify and redistribute it"
97
98
 
98
99
 
99
100
        def check_command( self, command ):
100
 
                """
101
 
                Check that the given command is valid and return the full name of the command.
102
 
        
103
 
                Arguments:
104
 
                - `command`: the given command
105
 
                """
 
101
                """Check that the given command is valid and return the full name of the
 
102
                command.
 
103
 
 
104
                @param command the given command
 
105
                """
 
106
 
106
107
                # commands
107
108
                if [ 'init', 'update', 'resolve', 'add', 'remove', 'revert', 'status',
108
109
                         'diff', 'stage-add', 'stage-remove', 'stage-revert',
111
112
                        return command
112
113
 
113
114
                # resolve aliases
114
 
                elif command == 'up':
115
 
                        return 'update'
116
 
                elif command == 'rm':
117
 
                        return 'remove'
 
115
                alias = {
 
116
                        'up': 'update',
 
117
                        'rm': 'remove',
 
118
                        'st': 'status',
 
119
                        'co': 'init',
 
120
                        'ci': 'add',
 
121
                }.get( command, False )
 
122
                if alias: return alias
118
123
 
119
124
                # invalid
120
 
                else:
121
 
                        return None
 
125
                return None
122
126
 
123
127
 
124
128
        def get_command_argument( self, args ):
 
129
                """Find the first program argument what isn't an option argument.
 
130
 
 
131
                @param args the program arguments
125
132
                """
126
 
                Find the first program argument what isn't an option argument.
127
133
 
128
 
        Arguments:
129
 
        - `args`: the program arguments
130
 
        """
131
134
                while args:
132
135
                        if args[ 0 ] == '--':
133
136
                                return args[ 1 ] if len( args ) > 1 else None
139
142
 
140
143
        def run( self ):
141
144
                # make an initial attempt to parse the command line, looking only for
142
 
                # --help and --version, so that they have the chance to run without a
 
145
                # --help and --version so that they have the chance to run without a
143
146
                # command being specified
144
147
                try:
145
148
                        opts, args = getopt.gnu_getopt(
159
162
                        # ignore errors -- we aren't parsing the command line properly yet
160
163
                        pass
161
164
 
162
 
                # find the first non-option argument (the command)
 
165
                # read program configuration
 
166
                the.config = Config()
 
167
 
 
168
                # find the command by grabbing the first program argument that doesn't
 
169
                # look like an option (note: the first argument that doesn't look like
 
170
                # an option may actually be an argument to an option, so this is far
 
171
                # from perfect, but, provided the user is mindful, this is preferable to
 
172
                # forcing the user to always specify options after the command)
163
173
                the.command = self.get_command_argument( sys.argv[ 1: ] )
164
174
                if the.command == None:
165
175
                        self.print_usage( "missing command" )
170
180
                        self.print_usage( "bad command" )
171
181
 
172
182
                # calculate module and class name
 
183
                class_name = module_name = ''
173
184
                bits = the.command.split( '-' )
174
 
                class_name = 'Command'
175
 
                module_name = 'command'
176
185
                for bit in bits:
177
186
                        class_name += bit[ 0 ].upper() + bit[ 1 : ]
178
 
                        module_name += '_' + bit
 
187
                        if module_name: module_name += '_'
 
188
                        module_name += bit
 
189
                class_name += 'Command'
179
190
 
180
191
                # import module and instantiate the class
181
 
                module = __import__( 'stdhome.' + module_name,
 
192
                module = __import__( 'stdhome.command.' + module_name,
182
193
                                                         fromlist = [ class_name ] )
183
194
                instance = getattr( module, class_name )()
184
195
 
185
 
                # run the command
 
196
                # fully parse the command line, as per the command
186
197
                try:
187
198
                        instance.parse_command_line()
188
 
                        instance.run()
189
199
                except( getopt.GetoptError, self.UsageError ) as e:
190
200
                        self.print_usage( e.msg )
 
201
                except self.FatalError as e:
 
202
                        self.die( e.msg )
 
203
 
 
204
                # do late initialisation
 
205
                the.late_init()
 
206
 
 
207
                # run the command
 
208
                try:
 
209
                        instance.run()
191
210
                except Vcs.VcsError as e:
192
 
                        message = e.msg.rstrip()
193
 
                        if the.verbose and hasattr( e, 'output' ) and e.output:
194
 
                                message += '\n\nOUTPUT:\n' + e.output.rstrip()
 
211
                        message = e.msg.rstrip() + '\n\nVCS OUTPUT:\n' + e.output.rstrip()
195
212
                        self.die( message )
196
213
                except self.FatalError as e:
197
214
                        self.die( e.msg )