/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
88 by Tim Marston
python3ification
23
from .command import Command
28 by Tim Marston
added revert 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 ):
88 by Tim Marston
python3ification
38
		print("Usage: " + the.program.name + " revert [--repo=REPO] [--all] [FILE]...")
39
		print()
28 by Tim Marston
added revert command
40
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
88 by Tim Marston
python3ification
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")
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.")
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:")
53
		print("      --all        confirm that you want to revert all files")
54
		print("      --quiet      do not report changes to the home directory")
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")
28 by Tim Marston
added revert command
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
63 by Tim Marston
determine and instantiate repo vcs dynamically; for new repos, added default vcs
75
			elif opt in [ '-v', '--verbose' ]:
76
				the.verbose += 1
28 by Tim Marston
added revert command
77
			elif opt == "--help":
78
				self.print_help()
55 by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because
79
28 by Tim Marston
added revert command
80
		# discard first argument (the command)
81
		args.pop( 0 )
82
83
		# remaining arguments
84
		self.files = args
85
34 by Tim Marston
made revert require --all to act on all files
86
		# check that either files or --all was given
87
		if len( self.files ) == 0 and not self.all:
88
			raise the.program.UsageError(
89
				"no files were specified and --all was not given" )
90
		if len( self.files ) > 0 and self.all:
91
			raise the.program.UsageError(
92
				"files specified, which conflicts with using --all" )
93
28 by Tim Marston
added revert command
94
95
	def run( self ):
96
97
		# set up repo and check it exists
98
		the.repo.check_dir_exists()
99
100
		# determine files
101
		files = self.expand_files( self.files )
102
103
		# initialise deployment and ensure it's not ongoing
104
		deployment = Deployment()
105
		deployment.check_ongoing( False )
106
107
		# check for local changes
108
		if the.repo.vcs.has_changes():
109
			raise the.program.FatalError(
110
				'repo has local changes: %s\n'
55 by Tim Marston
moved handling of --verbose to main program; manuall parse config file (because
111
				'Hint: see "%s stage-revert --help"' %
28 by Tim Marston
added revert command
112
				( the.repo.name, the.program.name ) )
113
114
		# check status
82 by Tim Marston
added general reporting to CopyBase and configured it via copy-in and copy-out
115
		walker = CopyOutWalker( files if files else None, not self.quiet )
28 by Tim Marston
added revert command
116
		walker.walk()