/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
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
34
		self.quiet = False
34 by Tim Marston
made revert require --all to act on all files
35
36
28 by Tim Marston
added revert command
37
	def print_help( self ):
34 by Tim Marston
made revert require --all to act on all files
38
		print "Usage: " + the.program.name + " revert [--repo=REPO] [--all] [FILE]..."
28 by Tim Marston
added revert command
39
		print
40
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
41
		print "Revert any local changes to a file in the home directory."
42
		print
43
		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
44
		print "relative to the home directory, to its state in the repo.  Specifying individual"
45
		print "files and directories will revert them (recursively, in the case of"
46
		print "directories).  Or you can revert all files in your home directory by specifying"
47
		print "no files and explicitly using the --all option."
28 by Tim Marston
added revert command
48
		print
49
		print "To see a list files that can be reverted, type:"
50
		print "    " + the.program.name + " status"
51
		print
52
		print "Options:"
34 by Tim Marston
made revert require --all to act on all files
53
		print "      --all        confirm that you want to revert all files"
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
54
		print "      --quiet      do not report changes to the home directory"
28 by Tim Marston
added revert command
55
		print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
56
		print "  -v, --verbose    display information about what is being done"
57
		print "      --help       display help and exit"
58
		exit( 0 )
59
60
61
	def parse_command_line( self ):
62
		opts, args = getopt.gnu_getopt(
63
			sys.argv[ 1: ], "r:v",
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
64
			[ "all", "quiet", "repo=", "verbose", "help" ] )
28 by Tim Marston
added revert command
65
		for opt, optarg in opts:
34 by Tim Marston
made revert require --all to act on all files
66
			if opt == "--all":
67
				self.all = True
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
68
			elif opt == "--quiet":
69
				self.quiet = True
34 by Tim Marston
made revert require --all to act on all files
70
			elif opt in [ '--repo', '-r' ]:
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
71
				if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
28 by Tim Marston
added revert command
72
					raise the.program.FatalError(
73
						'invalid repository name: ' + optarg )
74
				the.repo = optarg
75
			elif opt == "--help":
76
				self.print_help()
55 by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because
77
28 by Tim Marston
added revert command
78
		# discard first argument (the command)
79
		args.pop( 0 )
80
81
		# remaining arguments
82
		self.files = args
83
34 by Tim Marston
made revert require --all to act on all files
84
		# check that either files or --all was given
85
		if len( self.files ) == 0 and not self.all:
86
			raise the.program.UsageError(
87
				"no files were specified and --all was not given" )
88
		if len( self.files ) > 0 and self.all:
89
			raise the.program.UsageError(
90
				"files specified, which conflicts with using --all" )
91
28 by Tim Marston
added revert command
92
93
	def run( self ):
94
95
		# set up repo and check it exists
96
		the.repo.check_dir_exists()
97
98
		# determine files
99
		files = self.expand_files( self.files )
100
101
		# initialise deployment and ensure it's not ongoing
102
		deployment = Deployment()
103
		deployment.check_ongoing( False )
104
105
		# check for local changes
106
		if the.repo.vcs.has_changes():
107
			raise the.program.FatalError(
108
				'repo has local changes: %s\n'
55 by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because
109
				'Hint: see "%s stage-revert --help"' %
28 by Tim Marston
added revert command
110
				( the.repo.name, the.program.name ) )
111
112
		# check status
61 by Tim Marston
added home directory change reporting to CopyOutWalker; added --quiet option to
113
		walker = CopyOutWalker( files if files else None, self.quiet )
28 by Tim Marston
added revert command
114
		walker.walk()