1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# program.py
#
# Copyright (C) 2013 Tim Marston <tim@edm.am>
#
# This file is part of stdhome (hereafter referred to as "this program").
# See http://ed.am/dev/stdhome for more information.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, sys, getopt
import the
from config import Config
from vcs.vcs import Vcs
class Program:
def __init__( self, version ):
self.name = os.path.basename( sys.argv[ 0 ] )
self.version = version
def die( self, error_message ):
prefix = self.name + ( ' ' + the.command if the.command else '' )
print >> sys.stderr, '%s: %s' % ( prefix, error_message )
exit( 1 )
def print_usage( self, error_message ):
command = ' ' + the.command if the.command else ''
self.die( error_message + \
"\nTry '%s%s --help' for more information." % \
( self.name, command ) )
def print_help( self ):
print "Usage: " + self.name + " COMMAND [OPTION]..."
print
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789
print "Tool to manage a set of files in your home directory and distribute them across"
print "multiple computers, merging local changes (in the same way as you would manage"
print "source code under version control)."
print
print "Global options (for all commands):"
print " --help display help and exit"
print " --version output version information and exit"
print
print "Commands:"
print " init initialise a local copy of your repositories"
print " update update files in your home directory"
print " resolve try to finish an update (that had conflicts)"
# print " add add local files/changes to the repository"
# print " remove remove a local file from the repository"
print " status list files that have changed locally"
print " diff shows changes made to local files"
print " revert undo changes made to local files"
# print " stage-add add (but don't commit) files/changes to local repository"
# print " stage-remove delete *but don't comit) files from the local repository"
print " stage-revert revert changes in the local repository"
# print " stage-status show status of local repository"
# print " stage-diff shows changes in local repository"
# print " stage-commit commit changes in the local repository"
print
print "For help about a particular command (including the additional options that the"
print "command accepts) try typing:"
print " $ " + self.name + " COMMAND --help"
exit( 0 )
def print_version( self ):
print "stdhome " + self.version
print
print "Copyright (C) 2013 to 2014 Tim Marston"
print
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789
print "This program is free software, and you may use, modify and redistribute it"
print "under the terms of the GNU General Public License version 3 or later. This"
print "program comes with ABSOLUTELY NO WARRANTY, to the extent permitted by law."
print
print "For more information, including documentation, please see the project website"
print "at http://ed.am/dev/stdhome."
print
print "Please report bugs to <tim@ed.am>."
exit( 0 )
def check_command( self, command ):
"""
Check that the given command is valid and return the full name of the command.
Arguments:
- `command`: the given command
"""
# commands
if [ 'init', 'update', 'resolve', 'add', 'remove', 'revert', 'status',
'diff', 'stage-add', 'stage-remove', 'stage-revert',
'stage-status', 'stage-diff', 'stage-commit'
].count( command ) == 1:
return command
# resolve aliases
alias = {
'up': 'update',
'rm': 'remove',
'st': 'status',
'co': 'init',
}.get( command, False )
if alias: return alias
# invalid
return None
def run( self ):
# make an initial attempt to parse the command line, looking only for
# --help and --version, so that they have the chance to run without a
# command being specified
try:
opts, args = getopt.gnu_getopt(
sys.argv[ 1: ], "",
[ "help", "version" ] )
for opt, optarg in opts:
# we only show help if there are no non-option arguments (e.g.,
# a command) specified. If a command has been specified it will
# have to be parsed and --help will be handled by it, instead)
if opt == "--help" and len( args ) == 0:
self.print_help()
elif opt == "--version":
self.print_version()
except getopt.GetoptError as e:
# ignore errors -- we aren't parsing the command line properly yet
pass
# read program configuration
the.config = Config()
# the first argument should be the command
the.command = sys.argv[ 1 ] if len( sys.argv ) > 1 else None
if the.command == None:
self.print_usage( "missing command" )
# check command is valid
the.command = self.check_command( the.command )
if the.command == None:
self.print_usage( "bad command" )
# calculate module and class name
class_name = module_name = ''
bits = the.command.split( '-' )
for bit in bits:
class_name += bit[ 0 ].upper() + bit[ 1 : ]
if module_name: module_name += '_'
module_name += bit
class_name += 'Command'
# import module and instantiate the class
module = __import__( 'stdhome.command.' + module_name,
fromlist = [ class_name ] )
instance = getattr( module, class_name )()
# fully parse the command line, as per the command
try:
instance.parse_command_line()
except( getopt.GetoptError, self.UsageError ) as e:
self.print_usage( e.msg )
except self.FatalError as e:
self.die( e.msg )
# do late initialisation
the.late_init()
# run the command
try:
instance.run()
except Vcs.VcsError as e:
message = e.msg.rstrip()
if the.verbose >= 1:
message += '\n\nOUTPUT:\n' + e.output.rstrip()
self.die( message )
except self.FatalError as e:
self.die( e.msg )
class UsageError( Exception ):
def __init__( self, error_message ):
self.msg = error_message
class FatalError( Exception ):
def __init__( self, message ):
self.msg = message
|