/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
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
82 by Tim Marston
added general reporting to CopyBase and configured it via copy-in and copy-out
32
	def expand_files( files, recurse = True ):
37 by Tim Marston
fixed bugs in command.expand_files, where results could be not unique, nor
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
82 by Tim Marston
added general reporting to CopyBase and configured it via copy-in and copy-out
37
		directory).  All files must exist in the repository.  Directories are
38
		recursed in to as required.
39
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
40
		"""
41
82 by Tim Marston
added general reporting to CopyBase and configured it via copy-in and copy-out
42
		ret = set()
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
43
		home_dir_prefix = os.path.realpath( the.full_home_dir ) + os.sep
44
45
		# iterate through file list
46
		for file in files:
37 by Tim Marston
fixed bugs in command.expand_files, where results could be not unique, nor
47
			parts = os.path.split( file )
48
			abs_file = os.path.join(
49
				os.path.realpath( parts[ 0 ] ), parts[ 1 ] )
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
50
51
			# check the file is in the home directory
52
			if abs_file[ : len( home_dir_prefix ) ] != home_dir_prefix:
53
				raise the.program.FatalError(
48 by Tim Marston
during add, ensure parent directories are added
54
					'not under home directory: %s' % abs_file )
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
55
56
			# relative file
57
			rel_file = abs_file[ len( home_dir_prefix ) : ]
58
59
			# check if file exists in repository
60
			repo_file = os.path.join( the.repo.full_dir, rel_file )
61
			if not os.path.lexists( repo_file ):
62
				raise the.program.FatalError(
63
					'not managed by stdhome: %s' % rel_file )
64
65
			# append the file or directory tree
82 by Tim Marston
added general reporting to CopyBase and configured it via copy-in and copy-out
66
			ret.update( Walker.generate_walk_list(
67
				the.repo.full_dir, rel_file, recurse ) )
8 by Tim Marston
added diff command; moved all command to commands subdir; made stage-revert
68
37 by Tim Marston
fixed bugs in command.expand_files, where results could be not unique, nor
69
		return sorted( set ( ret ) )