/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/command/revert.py

  • Committer: Tim Marston
  • Date: 2013-12-08 19:28:11 UTC
  • Revision ID: tim@ed.am-20131208192811-r20qj7cgmn4duw11
initial commit; basic app startup and initial command-line processing

Show diffs side-by-side

added added

removed removed

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
 
 
32
 
        def __init__( self ):
33
 
                self.all = False
34
 
                self.quiet = False
35
 
 
36
 
 
37
 
        def print_help( self ):
38
 
                print "Usage: " + the.program.name + " revert [--repo=REPO] [--all] [FILE]..."
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"
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"
58
 
                exit( 0 )
59
 
 
60
 
 
61
 
        def parse_command_line( self ):
62
 
                opts, args = getopt.gnu_getopt(
63
 
                        sys.argv[ 1: ], "r:v",
64
 
                        [ "all", "quiet", "repo=", "verbose", "help" ] )
65
 
                for opt, optarg in opts:
66
 
                        if opt == "--all":
67
 
                                self.all = True
68
 
                        elif opt == "--quiet":
69
 
                                self.quiet = True
70
 
                        elif opt in [ '--repo', '-r' ]:
71
 
                                if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
72
 
                                        raise the.program.FatalError(
73
 
                                                'invalid repository name: ' + optarg )
74
 
                                the.repo = optarg
75
 
                        elif opt in [ '-v', '--verbose' ]:
76
 
                                the.verbose += 1
77
 
                        elif opt == "--help":
78
 
                                self.print_help()
79
 
 
80
 
                # discard first argument (the command)
81
 
                args.pop( 0 )
82
 
 
83
 
                # remaining arguments
84
 
                self.files = args
85
 
 
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
 
 
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'
111
 
                                'Hint: see "%s stage-revert --help"' %
112
 
                                ( the.repo.name, the.program.name ) )
113
 
 
114
 
                # check status
115
 
                walker = CopyOutWalker( files if files else None, not self.quiet )
116
 
                walker.walk()