3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
 
 
5
# This file is part of stdhome (hereafter referred to as "this program").
 
 
6
# See http://ed.am/dev/stdhome for more information.
 
 
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.
 
 
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.
 
 
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/>.
 
 
22
import sys, re, getopt, os
 
 
23
from command import Command
 
 
24
import stdhome.the as the
 
 
25
from stdhome.deployment import Deployment
 
 
26
from stdhome.walker.copy_in import CopyInWalker
 
 
29
class AddCommand( Command ):
 
 
32
        def print_help( self ):
 
 
33
                print "Usage: " + the.program.name + " add [--repo=REPO] FILE..."
 
 
35
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
 
 
36
                print "Add (or update) a file in the repository."
 
 
38
                print "Add a named file from the local filesystem, specified relative to the home"
 
 
39
                print "directory, to the repo, or update an existing file in the repo with your local"
 
 
40
                print "changes.  Directories can also be added, but note that they are not added"
 
 
41
                print "recursively (as is common with version control)."
 
 
44
                print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
 
 
45
                print "  -v, --verbose    display information about what is being done"
 
 
46
                print "      --help       display help and exit"
 
 
50
        def parse_command_line( self ):
 
 
51
                opts, args = getopt.gnu_getopt(
 
 
52
                        sys.argv[ 1: ], "r:v",
 
 
53
                        [ "repo=", "verbose", "help" ] )
 
 
54
                for opt, optarg in opts:
 
 
55
                        if opt in [ '--repo', '-r' ]:
 
 
56
                                if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
 
 
57
                                        raise the.program.FatalError(
 
 
58
                                                'invalid repository name: ' + optarg )
 
 
63
                # discard first argument (the command)
 
 
66
                # check remaining arguments
 
 
68
                        raise the.program.UsageError( 'too few arguments' )
 
 
76
                # set up repo and check it exists
 
 
77
                the.repo.check_dir_exists()
 
 
80
                files = self.expand_home_dir_files( self.files )
 
 
82
                # initialise deployment and ensure it's not ongoing
 
 
83
                deployment = Deployment()
 
 
84
                deployment.check_ongoing( False )
 
 
86
                # check for local changes
 
 
87
                if the.repo.vcs.has_changes():
 
 
88
                        raise the.program.FatalError(
 
 
89
                                'repo has local changes: %s\n'
 
 
90
                                'Hint: see "%s stage-revert --help"' %
 
 
91
                                ( the.repo.name, the.program.name ) )
 
 
94
                walker = CopyInWalker( files )
 
 
97
                # make sure all files are added to version control
 
 
98
                if the.verbose >= 1: print "adding files"
 
 
99
                the.repo.vcs.add( files )
 
 
102
                if the.verbose >= 1: print "committing " + the.repo.dir
 
 
103
                the.repo.vcs.commit()
 
 
107
        def expand_home_dir_files( files ):
 
 
108
                """Returns a unique, sorted list of relative filenames, calculated from the list
 
 
109
                provided, which is made up from individual files and directories
 
 
110
                relative to the CWD.  Directories and are not recursed in to, but
 
 
111
                leading path components are also returned.  Specified filenames must
 
 
112
                exist in the home directory (although they may not exist in the repo).
 
 
117
                home_dir_prefix = os.path.realpath( the.full_home_dir ) + os.sep
 
 
119
                # iterate through file list
 
 
121
                        parts = os.path.split( file )
 
 
122
                        abs_file = os.path.join(
 
 
123
                                os.path.realpath( parts[ 0 ] ), parts[ 1 ] )
 
 
125
                        # check the file is in the home directory
 
 
126
                        if abs_file[ : len( home_dir_prefix ) ] != home_dir_prefix:
 
 
127
                                raise the.program.FatalError(
 
 
128
                                        'not under home directory: %s' % abs_file )
 
 
131
                        rel_file = abs_file[ len( home_dir_prefix ) : ]
 
 
133
                        # check if file exists in home directory
 
 
134
                        if not os.path.lexists( abs_file ):
 
 
135
                                raise the.program.FatalError(
 
 
136
                                        'not found in home directory: %s' % rel_file )
 
 
141
                                path, dummy = os.path.split( path )
 
 
142
                                if len( path ): ret.add( path )
 
 
145
                        # append the file or directory