/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# add.py
#
# Copyright (C) 2014 Tim Marston <tim@edm.am>
#
# This file is part of stdhome (hereafter referred to as "this program").
# See http://ed.am/dev/stdhome for more information.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys, re, getopt, os
from command import Command
import stdhome.the as the
from stdhome.deployment import Deployment
from stdhome.walker.copy_in import CopyInWalker
from stdhome.walker.walker import Walker


class AddCommand( Command ):


	def __init__( self ):
		self.recursive = False


	def print_help( self ):
		print "Usage: " + the.program.name + " add [--repo=REPO] FILE..."
		print
		#      01234567890123456789012345678901234567890123456789012345678901234567890123456789
		print "Add (or update) a file in the repository."
		print
		print "Add a named file from the local filesystem, specified relative to the home"
		print "directory, to the repo, or update an existing file in the repo with your local"
		print "changes.  Directories can also be added, but note that they are not added"
		print "recursively (as is common with version control) unless this is specifically"
		print "requested on the commandline."
		print
		print "Options:"
		print "  -r, --repo=REPO  select the repo to check-out or create (defaults to 'home')"
		print "  -R, --recursive  recursively add directories"
		print "  -v, --verbose    display information about what is being done"
		print "      --help       display help and exit"
		exit( 0 )


	def parse_command_line( self ):
		opts, args = getopt.gnu_getopt(
			sys.argv[ 1: ], "r:Rv",
			[ "repo=", "recuirsive", "verbose", "help" ] )
		for opt, optarg in opts:
			if opt in [ '--repo', '-r' ]:
				if not re.search( '^[-a-zA-z0-9.]+$', optarg ):
					raise the.program.FatalError(
						'invalid repository name: ' + optarg )
				the.repo = optarg
			elif opt in [ '--recursive', '-R' ]:
				self.recursive = True
			elif opt in [ '--verbose', '-v' ]:
				the.verbose += 1
			elif opt == "--help":
				self.print_help()

		# discard first argument (the command)
		args.pop( 0 )

		# check remaining arguments
		if len( args ) < 1:
			raise the.program.UsageError( 'too few arguments' )

		# files arguments
		self.files = args


	def run( self ):

		# set up repo and check it exists
		the.repo.check_dir_exists()

		# determine files
		files = self.expand_home_files( self.files, self.recursive )

		# initialise deployment and ensure it's not ongoing
		deployment = Deployment()
		deployment.check_ongoing( False )

		# check for local changes
		if the.repo.vcs.has_changes():
			raise the.program.FatalError(
				'repo has local changes: %s\n'
				'Hint: see "%s stage-revert --help"' %
				( the.repo.name, the.program.name ) )

		# check status
		walker = CopyInWalker( files, True )
		walker.walk()

		# make sure all files are added to version control
		if the.verbose >= 1: print "adding files"
		the.repo.vcs.add( files )

		# commit repo
		if the.verbose >= 1: print "committing " + the.repo.dir
		the.repo.vcs.commit()



	@staticmethod
	def expand_home_files( files, recurse ):
		"""Returns a unique, sorted list of relative filenames, calculated from the list
		provided, which is made up from individual files and directories
		relative to the CWD.  As directories are found, they may optionally be
		recursed in to.  Leading path components of any items are alse returned.
		Specified filenames must exist in the home directory (although they do
		not have to exist in the repo).

		This is a modified version of command.expand_files().

		"""

		ret = set()
		home_dir_prefix = os.path.realpath( the.full_home_dir ) + os.sep

		# iterate through file list
		for file in files:
			( rel_file, abs_file ) = Command.resolve_homedir_file( file )

			# check that file exists in homedir
			if not os.path.exists( abs_file ):
				raise the.program.FatalError(
					'not found in home directory: %s' % file )

			# ensure parent path parts are included
			path = rel_file
			while True:
				path, dummy = os.path.split( path )
				if len( path ): ret.add( path )
				else: break

			# append the file or directory
			ret.update( Walker.generate_walk_list(
				the.full_home_dir, rel_file, recurse ) )

		return sorted( ret )