/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/stage_revert.py

  • Committer: Tim Marston
  • Date: 2016-04-10 20:20:56 UTC
  • Revision ID: tim@ed.am-20160410202056-x0mo1ktjbjgqe2pr
added symlink to executable

Show diffs side-by-side

added added

removed removed

1
 
# command_stage_revert.py
 
1
# stage_revert.py
2
2
#
3
3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
4
4
#
20
20
 
21
21
 
22
22
import sys, re, getopt
23
 
import the
24
 
from deployment import Deployment
25
 
 
26
 
 
27
 
class CommandStageRevert:
 
23
from command import Command
 
24
import stdhome.the as the
 
25
from stdhome.deployment import Deployment
 
26
 
 
27
 
 
28
class StageRevertCommand( Command ):
28
29
 
29
30
 
30
31
        def __init__( self ):
31
 
                self.repo = None
 
32
                self.force = False
32
33
 
33
34
 
34
35
        def print_help( self ):
35
 
                print "Usage: " + the.program.name + " update [--repo=REPO]"
 
36
                print "Usage: " + the.program.name + " stage-revert [--repo=REPO]"
36
37
                print
37
38
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
38
39
                print "Revert any modifications made to a local repository, losing those changes."
48
49
                print "repository."
49
50
                print
50
51
                print "Options:"
 
52
                print "      --force      force revert of stage (when there is a problem)"
51
53
                print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
52
54
                print "  -v, --verbose    display information about what is being done"
53
55
                print "      --help       display help and exit"
57
59
        def parse_command_line( self ):
58
60
                opts, args = getopt.gnu_getopt(
59
61
                        sys.argv[ 1: ], "r:v",
60
 
                        [ "repo=", "verbose", "help" ] )
 
62
                        [ "force", "repo=", "verbose", "help" ] )
61
63
                for opt, optarg in opts:
62
 
                        if opt in [ '--repo', '-r' ]:
63
 
                                if not re.match( '^[-a-zA-z0-9.]+$', optarg ):
 
64
                        if opt == "--force":
 
65
                                self.force = True
 
66
                        elif opt in [ '--repo', '-r' ]:
 
67
                                if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
64
68
                                        raise the.program.FatalError(
65
69
                                                'invalid repository name: ' + optarg )
66
 
                                self.repo = optarg
 
70
                                the.repo = optarg
67
71
                        elif opt in [ '--verbose', '-v' ]:
68
 
                                the.verbose = True
 
72
                                the.verbose += 1
69
73
                        elif opt == "--help":
70
74
                                self.print_help()
71
 
                
 
75
 
72
76
                # discard first argument (the command)
73
77
                args.pop( 0 )
74
78
 
80
84
        def run( self ):
81
85
 
82
86
                # set up repo and check it exists
83
 
                the.set_repo( self.repo )
84
87
                the.repo.check_dir_exists()
85
88
 
86
 
                # check there isn't a deployment in progress
 
89
                # initialise deployment
87
90
                deployment = Deployment()
88
 
                deployment.check_ongoing( False )
 
91
                if deployment.is_ongoing() and deployment.get_initial_revno() is None \
 
92
                   and not self.force:
 
93
                        raise RuntimeError(
 
94
                                "can't revert ongoing deployment: initial revno is missing!\n"
 
95
                                "use --force to override, but an update may miss changes!" )
89
96
 
90
97
                # check for local changes
91
 
                if the.verbose: print "reverting %s" % the.repo.dir
92
 
                the.repo.vcs.revert()
 
98
                if the.verbose >= 1: print "reverting %s" % the.repo.dir
 
99
                the.repo.vcs.revert( deployment.get_initial_revno() )
 
100
 
 
101
                # remove deployment state
 
102
                deployment.remove_deployment_state()