/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: 2021-09-01 12:28:59 UTC
  • Revision ID: tim@ed.am-20210901122859-tkvlmry7hef8ahwc
moved bzr -> brz

Show diffs side-by-side

added added

removed removed

 
1
# command.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 os
 
23
import stdhome.the as the
 
24
from stdhome.walker.walker import Walker
 
25
 
 
26
 
 
27
class Command:
 
28
        """Base class for command classes.
 
29
        """
 
30
 
 
31
        @staticmethod
 
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 ):
 
67
                """Returns a unique, sorted list of relative files, calculated from the list
 
68
                provided, which is made up from individual files and directories
 
69
                relative to the CWD (and which must be contained within the home
 
70
                directory, although the files need not actually exist in the home
 
71
                directory).  All files must exist in the repository.  Directories are
 
72
                recursed in to as required.
 
73
 
 
74
                """
 
75
 
 
76
                ret = set()
 
77
 
 
78
                # iterate through file list
 
79
                for file in files:
 
80
                        ( rel_file, abs_file ) = self.resolve_homedir_file( file )
 
81
 
 
82
                        # check that file exists in repository
 
83
                        repo_file = os.path.join( the.repo.full_dir, rel_file )
 
84
                        if not os.path.lexists( repo_file ):
 
85
                                raise the.program.FatalError(
 
86
                                        'not managed by stdhome: %s' % rel_file )
 
87
 
 
88
                        # append the file or directory tree
 
89
                        ret.update( Walker.generate_walk_list(
 
90
                                the.repo.full_dir, rel_file, recurse ) )
 
91
 
 
92
                return sorted( set ( ret ) )
 
93
 
 
94
 
 
95
        @staticmethod
 
96
        def print_stage_commands_notice():
 
97
                #      01234567890123456789012345678901234567890123456789012345678901234567890123456789
 
98
                print("In addition to using the primary commands (such as add and remove) to modify a")
 
99
                print("remote repository, you can also set up changes in the local repository and")
 
100
                print("manually commit them in one go to a remote repository when you're ready.  This")
 
101
                print("is done with the staging commands.  Note, some primary commands will not work")
 
102
                print("when there are staged changes.  You can revert all staged changes with the")
 
103
                print("stage-revert command.")
 
104