/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
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
96 by Tim Marston
fix broken use of self in @staticmethod
23
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
24
import stdhome.the as the
25
from stdhome.walker.walker import Walker
26
27
28
class Command:
29
	"""Base class for command classes.
30
	"""
31
32
	@staticmethod
86 by Tim Marston
fix add command and generic filename expansion/resolution to expend to
33
	def resolve_homedir_file( file ):
34
		"""Given a filename, which could be absolute or relative to the CWD, this
35
		returns a pair of 'resolved' filenames: the name of the file relative to
36
		the homedir and the full, absolute filename.  Neither of the returned
37
		filenames are guaranteed nor required to exist.  But the supplied
38
		filename will cause an error if it is not iteself or when resolved (if
39
		it is a symlink) under the homedirectory.
40
41
		"""
42
		home_dir_prefix = os.path.realpath( the.full_home_dir ) + os.sep
43
44
		# obtain absolute filename
45
		abs_file = os.path.abspath( file )
46
47
		# if absolute filename is not under home directory, attempt to
48
		# resolve symlinks
49
		if abs_file[ : len( home_dir_prefix ) ] != home_dir_prefix:
50
			parts = os.path.split( file )
51
			if os.path.exists( parts[ 0 ] ):
52
				abs_file = os.path.join(
53
					os.path.realpath( parts[ 0 ] ), parts[ 1 ] )
54
55
		# absolute file must now be under home directory and exist
56
		if abs_file[ : len( home_dir_prefix ) ] != home_dir_prefix:
57
			raise the.program.FatalError(
58
					'not under home directory: %s' % file )
59
60
		# relative file
61
		rel_file = abs_file[ len( home_dir_prefix ) : ]
62
63
		return ( rel_file, abs_file )
64
65
96 by Tim Marston
fix broken use of self in @staticmethod
66
	@classmethod
67
	def expand_files( cls, files, recurse = True ):
37 by Tim Marston
fixed bugs in command.expand_files, where results could be not unique, nor
68
		"""Returns a unique, sorted list of relative files, calculated from the list
69
		provided, which is made up from individual files and directories
70
		relative to the CWD (and which must be contained within the home
71
		directory, although the files need not actually exist in the home
82 by Tim Marston
added general reporting to CopyBase and configured it via copy-in and copy-out
72
		directory).  All files must exist in the repository.  Directories are
73
		recursed in to as required.
74
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
75
		"""
76
82 by Tim Marston
added general reporting to CopyBase and configured it via copy-in and copy-out
77
		ret = set()
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
78
79
		# iterate through file list
80
		for file in files:
96 by Tim Marston
fix broken use of self in @staticmethod
81
			( rel_file, abs_file ) = cls.resolve_homedir_file( file )
86 by Tim Marston
fix add command and generic filename expansion/resolution to expend to
82
83
			# check that file exists in repository
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
84
			repo_file = os.path.join( the.repo.full_dir, rel_file )
85
			if not os.path.lexists( repo_file ):
86
				raise the.program.FatalError(
87
					'not managed by stdhome: %s' % rel_file )
88
89
			# append the file or directory tree
82 by Tim Marston
added general reporting to CopyBase and configured it via copy-in and copy-out
90
			ret.update( Walker.generate_walk_list(
91
				the.repo.full_dir, rel_file, recurse ) )
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
92
37 by Tim Marston
fixed bugs in command.expand_files, where results could be not unique, nor
93
		return sorted( set ( ret ) )
90 by Tim Marston
add stage-status command; add common stage notice to helps
94
95
96
	@staticmethod
97
	def print_stage_commands_notice():
98
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
99
		print("In addition to using the primary commands (such as add and remove) to modify a")
100
		print("remote repository, you can also set up changes in the local repository and")
101
		print("manually commit them in one go to a remote repository when you're ready.  This")
102
		print("is done with the staging commands.  Note, some primary commands will not work")
103
		print("when there are staged changes.  You can revert all staged changes with the")
104
		print("stage-revert command.")