/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
28 by Tim Marston
added revert command
1
# revert.py
2
#
3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
4
#
5
# This file is part of stdhome (hereafter referred to as "this program").
6
# See http://ed.am/dev/stdhome for more information.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21
22
import sys, re, getopt
23
from command import Command
24
import stdhome.the as the
25
from stdhome.deployment import Deployment
26
from stdhome.walker.copy_out import CopyOutWalker
27
28
29
class RevertCommand( Command ):
30
31
34 by Tim Marston
made revert require --all to act on all files
32
	def __init__( self ):
33
		self.all = False
34
35
28 by Tim Marston
added revert command
36
	def print_help( self ):
34 by Tim Marston
made revert require --all to act on all files
37
		print "Usage: " + the.program.name + " revert [--repo=REPO] [--all] [FILE]..."
28 by Tim Marston
added revert command
38
		print
39
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
40
		print "Revert any local changes to a file in the home directory."
41
		print
42
		print "This reverts the content of the named files, in the local filesystem, specified"
34 by Tim Marston
made revert require --all to act on all files
43
		print "relative to the home directory, to its state in the repo.  Specifying individual"
44
		print "files and directories will revert them (recursively, in the case of"
45
		print "directories).  Or you can revert all files in your home directory by specifying"
46
		print "no files and explicitly using the --all option."
28 by Tim Marston
added revert command
47
		print
48
		print "To see a list files that can be reverted, type:"
49
		print "    " + the.program.name + " status"
50
		print
51
		print "Options:"
34 by Tim Marston
made revert require --all to act on all files
52
		print "      --all        confirm that you want to revert all files"
28 by Tim Marston
added revert command
53
		print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
54
		print "  -v, --verbose    display information about what is being done"
55
		print "      --help       display help and exit"
56
		exit( 0 )
57
58
59
	def parse_command_line( self ):
60
		opts, args = getopt.gnu_getopt(
61
			sys.argv[ 1: ], "r:v",
34 by Tim Marston
made revert require --all to act on all files
62
			[ "all", "repo=", "verbose", "help" ] )
28 by Tim Marston
added revert command
63
		for opt, optarg in opts:
34 by Tim Marston
made revert require --all to act on all files
64
			if opt == "--all":
65
				self.all = True
66
			elif opt in [ '--repo', '-r' ]:
28 by Tim Marston
added revert command
67
				if not re.match( '^[-a-zA-z0-9.]+$', optarg ):
68
					raise the.program.FatalError(
69
						'invalid repository name: ' + optarg )
70
				the.repo = optarg
71
			elif opt == "--help":
72
				self.print_help()
55 by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because
73
28 by Tim Marston
added revert command
74
		# discard first argument (the command)
75
		args.pop( 0 )
76
77
		# remaining arguments
78
		self.files = args
79
34 by Tim Marston
made revert require --all to act on all files
80
		# check that either files or --all was given
81
		if len( self.files ) == 0 and not self.all:
82
			raise the.program.UsageError(
83
				"no files were specified and --all was not given" )
84
		if len( self.files ) > 0 and self.all:
85
			raise the.program.UsageError(
86
				"files specified, which conflicts with using --all" )
87
28 by Tim Marston
added revert command
88
89
	def run( self ):
90
91
		# set up repo and check it exists
92
		the.repo.check_dir_exists()
93
94
		# determine files
95
		files = self.expand_files( self.files )
96
97
		# initialise deployment and ensure it's not ongoing
98
		deployment = Deployment()
99
		deployment.check_ongoing( False )
100
101
		# check for local changes
102
		if the.repo.vcs.has_changes():
103
			raise the.program.FatalError(
104
				'repo has local changes: %s\n'
55 by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because
105
				'Hint: see "%s stage-revert --help"' %
28 by Tim Marston
added revert command
106
				( the.repo.name, the.program.name ) )
107
108
		# check status
109
		walker = CopyOutWalker( files if files else None )
110
		walker.walk()