/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
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
# stdhome.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 sys, os
import getopt


class Stdhome:


	def __init__( self, version ):
		self.version = version
		self.program = os.path.basename( sys.argv[ 0 ] )


	def print_usage( self, error_message ):
		print >> sys.stderr, self.program + ": " + error_message
		print >> sys.stderr, "Try '" + self.program + " --help' for more information."
		exit( 1 )


	def print_help( self ):
		print "Usage: " + self.program + " COMMAND [OPTION]..."
		print
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
		print "Manage your home directories, across multiple computers, similar to how you"
		print "would software 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 "  revert        undo changes made to a local file"
		print "  stage-add     stage local files/changes"
		print "  stage-remove  stage the removal of files"
		print "  stage-revert  revert staged changes"
		print "  stage-status  show status of staging area"
		print "  stage-commit  commit staged changes to repository"
		print
		print "For help about a particular command (including the additional options that the"
		print "command accepts) try typing:"
		print "  $ " + self.program + " COMMAND --help"
		exit( 0 )


	def print_version( self ):
		print "stdhome " + self.version
		print
		print "Copyright (C) 2013 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',
			 'stage-add', 'stage-remove', 'stage-revert', 'stage-status',
			 'stage-commit' ].count( command ) == 1:
			return command

		# aliases
		elif command == 'up':
			return 'update'
		elif command == 'rm':
			return 'remove'

		# invalid
		else:
			return None


	def get_command_argument( self, args ):
		"""
		Find the first program argument what isn't an option argument.

        Arguments:
        - `args`: the program arguments
        """
		while args:
			if args[ 0 ] == '--':
				return args[ 1 ] if len( args ) > 1 else None
			if args[ 0 ][ 0 : 1 ] != '-':
				return args[ 0 ]
			args = args[ 1 : ]
		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, optargs 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 any errors -- we aren't parsing the command line properly
			pass

		# find the first non-option argument (the command)
		self.command = self.get_command_argument( sys.argv[ 1: ] )
		if self.command == None:
			self.print_usage( "missing command" )

		# check command is valid
		self.command = self.check_command( self.command )
		if self.command == None:
			self.print_usage( "bad command" )

		# calculate class name
		bits = self.command.split( '-' )
		class_name = 'Command'
		for bit in bits:
			class_name += bit[ 0 ].upper() + bit[ 1 : ]

		# instantiate and run the command class
		module = __import__( 'stdhome.command_' + self.command )
		the_class = getattr( module, class_name )
		instance = the_class()
		instance.run( self )