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

  • Committer: Tim Marston
  • Date: 2016-12-13 21:40:34 UTC
  • Revision ID: tim@ed.am-20161213214034-nd5t7ztnlrjd627i
fix add command and generic filename expansion/resolution to expend to
homedir-relative filename and absolute filename based on original filename, as
specified, rather than a fully, symlink-resolved filename.  So, e.g., if ~/bob
was a symlink to ~/fred, then ~/bob/a would resolve to the relative filename
bob/a, becuase it is inside the homedir (it would resolve to fred/a otherwise)

Show diffs side-by-side

added added

removed removed

29
29
        """
30
30
 
31
31
        @staticmethod
32
 
        def expand_files( files ):
 
32
        def resolve_homedir_file( file ):
 
33
                """Given a filename, which could be absolute or relative to the CWD, this
 
34
                returns a pair of 'resolved' filenames: the name of the file relative to
 
35
                the homedir and the full, absolute filename.  Neither of the returned
 
36
                filenames are guaranteed nor required to exist.  But the supplied
 
37
                filename will cause an error if it is not iteself or when resolved (if
 
38
                it is a symlink) under the homedirectory.
 
39
 
 
40
                """
 
41
                home_dir_prefix = os.path.realpath( the.full_home_dir ) + os.sep
 
42
 
 
43
                # obtain absolute filename
 
44
                abs_file = os.path.abspath( file )
 
45
 
 
46
                # if absolute filename is not under home directory, attempt to
 
47
                # resolve symlinks
 
48
                if abs_file[ : len( home_dir_prefix ) ] != home_dir_prefix:
 
49
                        parts = os.path.split( file )
 
50
                        if os.path.exists( parts[ 0 ] ):
 
51
                                abs_file = os.path.join(
 
52
                                        os.path.realpath( parts[ 0 ] ), parts[ 1 ] )
 
53
 
 
54
                # absolute file must now be under home directory and exist
 
55
                if abs_file[ : len( home_dir_prefix ) ] != home_dir_prefix:
 
56
                        raise the.program.FatalError(
 
57
                                        'not under home directory: %s' % file )
 
58
 
 
59
                # relative file
 
60
                rel_file = abs_file[ len( home_dir_prefix ) : ]
 
61
 
 
62
                return ( rel_file, abs_file )
 
63
 
 
64
 
 
65
        @staticmethod
 
66
        def expand_files( files, recurse = True ):
33
67
                """Returns a unique, sorted list of relative files, calculated from the list
34
68
                provided, which is made up from individual files and directories
35
69
                relative to the CWD (and which must be contained within the home
36
70
                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.
 
71
                directory).  All files must exist in the repository.  Directories are
 
72
                recursed in to as required.
 
73
 
39
74
                """
40
75
 
41
 
                ret = list()
42
 
                home_dir_prefix = os.path.realpath( the.full_home_dir ) + os.sep
 
76
                ret = set()
43
77
 
44
78
                # iterate through file list
45
79
                for file in files:
46
 
                        parts = os.path.split( file )
47
 
                        abs_file = os.path.join(
48
 
                                os.path.realpath( parts[ 0 ] ), parts[ 1 ] )
49
 
 
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 )
54
 
 
55
 
                        # relative file
56
 
                        rel_file = abs_file[ len( home_dir_prefix ) : ]
57
 
 
58
 
                        # check if file exists in repository
 
80
                        ( rel_file, abs_file ) = self.resolve_homedir_file( file )
 
81
 
 
82
                        # check that file exists in repository
59
83
                        repo_file = os.path.join( the.repo.full_dir, rel_file )
60
84
                        if not os.path.lexists( repo_file ):
61
85
                                raise the.program.FatalError(
62
86
                                        'not managed by stdhome: %s' % rel_file )
63
87
 
64
88
                        # append the file or directory tree
65
 
                        ret.extend( Walker.generate_walk_list( rel_file ) )
 
89
                        ret.update( Walker.generate_walk_list(
 
90
                                the.repo.full_dir, rel_file, recurse ) )
66
91
 
67
92
                return sorted( set ( ret ) )