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/>.
 
 
23
import stdhome.the as the
 
 
24
from stdhome.walker.walker import Walker
 
 
28
        """Base class for command classes.
 
 
32
        def expand_files( files ):
 
 
33
                """Returns a unique, sorted list of relative files, calculated from the list
 
 
34
                provided, which is made up from individual files and directories
 
 
35
                relative to the CWD (and which must be contained within the home
 
 
36
                directory, although the files need not actually exist in the home
 
 
37
                directory).  All files must exist in the repository.  Any directories
 
 
38
                are recursed in to so that their content is also returned.
 
 
42
                home_dir_prefix = os.path.realpath( the.full_home_dir ) + os.sep
 
 
44
                # iterate through file list
 
 
46
                        parts = os.path.split( file )
 
 
47
                        abs_file = os.path.join(
 
 
48
                                os.path.realpath( parts[ 0 ] ), parts[ 1 ] )
 
 
50
                        # check the file is in the home directory
 
 
51
                        if abs_file[ : len( home_dir_prefix ) ] != home_dir_prefix:
 
 
52
                                raise the.program.FatalError(
 
 
53
                                        'not under home directory: %s' % abs_file )
 
 
56
                        rel_file = abs_file[ len( home_dir_prefix ) : ]
 
 
58
                        # check if file exists in repository
 
 
59
                        repo_file = os.path.join( the.repo.full_dir, rel_file )
 
 
60
                        if not os.path.lexists( repo_file ):
 
 
61
                                raise the.program.FatalError(
 
 
62
                                        'not managed by stdhome: %s' % rel_file )
 
 
64
                        # append the file or directory tree
 
 
65
                        ret.extend( Walker.generate_walk_list( rel_file ) )
 
 
67
                return sorted( set ( ret ) )