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 |
|
39 |
# 01234567890123456789012345678901234567890123456789012345678901234567890123456789 |
|
40 |
print "Revert any local changes to a file in the home directory." |
|
41 |
||
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 |
|
48 |
print "To see a list files that can be reverted, type:" |
|
49 |
print " " + the.program.name + " status" |
|
50 |
||
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 in [ '--verbose', '-v' ]: |
|
72 |
the.verbose += 1 |
|
73 |
elif opt == "--help": |
|
74 |
self.print_help() |
|
75 |
||
76 |
# discard first argument (the command) |
|
77 |
args.pop( 0 ) |
|
78 |
||
79 |
# remaining arguments |
|
80 |
self.files = args |
|
81 |
||
34
by Tim Marston
made revert require --all to act on all files |
82 |
# check that either files or --all was given |
83 |
if len( self.files ) == 0 and not self.all: |
|
84 |
raise the.program.UsageError( |
|
85 |
"no files were specified and --all was not given" ) |
|
86 |
if len( self.files ) > 0 and self.all: |
|
87 |
raise the.program.UsageError( |
|
88 |
"files specified, which conflicts with using --all" ) |
|
89 |
||
28
by Tim Marston
added revert command |
90 |
|
91 |
def run( self ): |
|
92 |
||
93 |
# set up repo and check it exists |
|
94 |
the.repo.check_dir_exists() |
|
95 |
||
96 |
# determine files |
|
97 |
files = self.expand_files( self.files ) |
|
98 |
||
99 |
# initialise deployment and ensure it's not ongoing |
|
100 |
deployment = Deployment() |
|
101 |
deployment.check_ongoing( False ) |
|
102 |
||
103 |
# check for local changes |
|
104 |
if the.repo.vcs.has_changes(): |
|
105 |
raise the.program.FatalError( |
|
106 |
'repo has local changes: %s\n' |
|
107 |
'Hint: see "%s stage-revert --help"' % |
|
108 |
( the.repo.name, the.program.name ) ) |
|
109 |
||
110 |
# check status |
|
111 |
walker = CopyOutWalker( files if files else None ) |
|
112 |
walker.walk() |