/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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# deployment.py
#
# Copyright (C) 2013 to 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 os, re, shutil, filecmp, json
import the, util
from walker.copy_in_walker import CopyInWalker
from walker.conflict_walker import ConflictWalker
from walker.copy_out_walker import CopyOutWalker

class Deployment:


	def __init__( self ):
		self.load_deployment_state()
		self.conflicts_checked = False


	def load_deployment_state( self ):

		# list of files that were copied-in (or at least given the opportunity
        # to be) and updated through the vcs update.  This means that, while
        # there may have been conflicts during the update (which will be dealt
        # with in the repo), any arising conflicts with the filesystem are now
        # assumed to be because of the vcs update and can be ignored (that is to
        # say, we no longer care about the file in the home directory).  In
        # short, this is a list of files that it is safe to deploy, regardless
        # of the state of the filesystem.
		self.deploy_files = None

		# list of files that were changed by a recent vcs update (so only these
		# need to be checked for deployment conflicts or copied-out)
		self.updated_files = None

		# do we have a repo?
		if not os.path.exists( the.repo.full_dir ): return

		# if no file list exists, we're done
		file = os.path.join( the.full_mddir, "deploy.%s" % the.repo.name )
		if not os.path.isfile( file ):
			return

		# read the file list
		if the.verbose: print "loading deployment state"
		f = open( file, 'r' )
		state = json.loads( f.read() )

		# unpack deployment state
		self.deploy_files = state['deploy_files'];
		self.updated_files = state['updated_files'];


	def save_deployment_state( self ):
		if the.verbose: print "saving deployment state"

		# create metadata directory, as necessary
		if not os.path.isdir( the.full_mddir ):
			os.mkdir( the.full_mddir )

		# pack deployment state
		state = {
			'deploy_files': self.deploy_files,
			'updated_files': self.updated_files,
		}

		# create file
		file = os.path.join( the.full_mddir, "deploy.%s" % the.repo.name )
		f = open( file, 'w' )
		f.write( json.dumps( state ) );


	def remove_deployment_state( self ):

		# check it exists
		file = os.path.join( the.full_mddir, "deploy.%s" % the.repo.name )
		if( os.path.isfile( file ) ):

			# delete it
			if the.verbose: print "removing deployment state"
			os.unlink( file )


	def is_ongoing( self ):
		return False if self.deploy_files is None else True


	def check_ongoing( self, ongoing = True ):
		if( ongoing ):
			if self.deploy_files is None:
				raise self.DeploymentOngoing( False )
		else:
			if self.deploy_files is not None:
				raise self.DeploymentOngoing( True )


	def copy_in( self ):

		# check we don't already have a file list
		self.check_ongoing( False )

		# if the repo doesn't exist, we have an empty file list
		if not os.path.exists( the.repo.full_dir ):
			self.deploy_files = list()
		else:
			# copy in
			if the.verbose: print "importing files..."
			walker = CopyInWalker()
			walker.walk()
			if( walker.changed ):
				raise self.CopyInConflicts( walker.changed )
			self.deploy_files = walker.walk_list

		# save state
		self.save_deployment_state()


	def get_conflicts( self, updated_files = None ):

		# check we have a file list
		self.check_ongoing( True )

		# set updated files
		if updated_files is not None:
			self.updated_files = updated_files
			self.save_deployment_state()

		# check for deployment conflicts
		walker = ConflictWalker( self.deploy_files, self.updated_files )
		walker.walk()

		self.conflicts_checked = True
		return walker.changed


	def copy_out( self ):

		# check we have a file list
		self.check_ongoing( True )

		# check that deployment conflicts have been checked-for
		if not self.conflicts_checked:
			raise RuntimeError('logic error: deployment conflicts unchecked' )

		# copy out
		if the.verbose: print "exporting files..."
		walker = CopyOutWalker( self.updated_files )
		walker.walk()

		# clear state
		self.remove_deployment_state()


	class DeploymentOngoing( the.program.FatalError ):

		def __init__( self, ongoing ):
			if( ongoing ):
				self.msg = "there is an ongoing deployment"
			else:
				self.msg = "there is no ongoing deployment"


	class CopyInConflicts( the.program.FatalError ):

		def __init__( self, conflicts ):
			self.conflicts = conflicts