19
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
import os, re, shutil, filecmp, json
24
from walker.copy_in import CopyInWalker
25
from walker.conflict import ConflictWalker
26
from walker.copy_out import CopyOutWalker
29
32
def __init__( self ):
31
# initialise the file list
33
if not os.path.exists( the.repo.expanded_dir ):
34
raise RuntimeError( 'logic error: Deployment initialised when '
36
self.load_deployment_state()
37
self.conflicts_checked = False
40
def load_deployment_state( self ):
41
"""Load any deployment state. If one is found then a deployment will be
42
considered to be ongoing.
45
# list of files that were copied-in (or at least given the opportunity
46
# to be) and updated through the vcs update. This means that, while
47
# there may have been conflicts during the update (which the user will
48
# have to have dealt with in the repo), any conflicts arising with these
49
# files in the home directory are no longer important and can be
50
# ignored. In short, this is a list of files that can safely be
51
# deployed, regardless of the state of the home directory.
52
self.imported_files = None
54
# list of files that were affected by a recent vcs update (so only these
55
# files need to be checked for deployment conflicts or copied-out)
56
self.affected_files = None
58
# the revno that the repo was as prior to a recent update
59
self.initial_revno = None
62
if not os.path.exists( the.repo.full_dir ): return
64
# if no file list exists, we're done
65
file = os.path.join( the.full_mddir, "deploy.%s" % the.repo.name )
66
if not os.path.isfile( file ):
70
if the.verbose >= 1: print "deployment state found; loading"
72
state = json.loads( f.read() )
74
# unpack deployment state
75
self.imported_files = state['imported_files'];
76
self.initial_revno = state['initial_revno'];
77
self.affected_files = state['affected_files'];
80
def save_deployment_state( self ):
81
"""Save the current deployment state (so there will be a deployment ongoing).
84
if the.verbose >= 1: print "saving deployment state"
86
# create metadata directory, as necessary
87
if not os.path.isdir( the.full_mddir ):
88
os.mkdir( the.full_mddir )
90
# pack deployment state
92
'imported_files': self.imported_files,
93
'initial_revno': self.initial_revno,
94
'affected_files': self.affected_files,
98
file = os.path.join( the.full_mddir, "deploy.%s" % the.repo.name )
100
f.write( json.dumps( state ) );
103
def remove_deployment_state( self ):
104
"""Remove the current deployment state (so no deployment will be ongoing).
108
file = os.path.join( the.full_mddir, "deploy.%s" % the.repo.name )
109
if( os.path.isfile( file ) ):
112
if the.verbose >= 1: print "removing deployment state"
116
def is_ongoing( self ):
117
"""Is there a deployment currently ongoing?
120
return False if self.imported_files is None else True
123
def check_ongoing( self, ongoing = True ):
124
"""Check that a deployment either is or is not ongoing and raise an error if
129
if self.imported_files is None:
130
raise self.DeploymentOngoing( False )
38
assert False # not implemented
41
def load_file_list( self ):
43
# if no file list, don't load one
132
if self.imported_files is not None:
133
raise self.DeploymentOngoing( True )
136
def get_initial_revno( self ):
137
"""Get the initial revision identifier from the deployment state.
140
return self.initial_revno
143
def copy_in( self, initial_revno = None ):
144
"""Copy-in changes from the home directory to the repository. When finished,
145
the state of deployment is saved, meaning that a deployment is ongoing.
52
148
# check we don't already have a file list
53
if self.file_list is not None:
54
raise the.FatalError( 'deployment in progress; ' + \
55
'see "%s resolve --help" for information' % the.program.name )
57
assert False # not implemented
149
self.check_ongoing( False )
151
# if the repo doesn't exist, we have an empty file list
152
if not os.path.exists( the.repo.full_dir ):
153
self.imported_files = list()
156
if the.verbose >= 1: print "importing files..."
157
walker = CopyInWalker()
159
self.imported_files = walker.walk_list
161
# obtain initial revno
162
self.initial_revno = the.repo.vcs.get_revno()
165
self.save_deployment_state()
168
def get_conflicts( self, affected_files = None ):
169
"""Check to see if there are any deployment conflicts. If a list of affected
170
files is supplied, then only those files are checked (and they are also
171
saved with the deployment state). Otherwise, all files in the
172
repository are checked.
175
# check we have a file list
176
self.check_ongoing( True )
179
if affected_files is not None:
180
self.affected_files = affected_files
181
self.save_deployment_state()
183
# check for deployment conflicts
184
walker = ConflictWalker( self.imported_files, self.affected_files )
187
self.conflicts_checked = True
188
return walker.changed + walker.obstructed
60
191
def copy_out( self ):
61
if self.pre_copy_out_list is None:
64
print "deploying files"
67
for ( repodir, directories, files ) in os.walk( the.repo.expanded_dir ):
68
assert path[ : len( the.repo.expanded_dir ) ] == \
70
relative_path = path[ len( the.repo.expanded_dir ) : ]
71
fsdir = the.expanded_fsdir + relative_path
76
for dir in directories:
77
if os.path.exists( fsdir + dir ) and \
78
not os.path.isdir( fsdir + dir ):
82
# if exists in repo as directory and also in fs as non-directory
83
if os.path.isdir( the.repo.expnaded_dir + relative_path +
192
"""Copy-out changed files from the repository to the home directory. If the
193
deployment state includes a list of affected files, then only those
194
files are copied-out.
197
# check we have a file list
198
self.check_ongoing( True )
200
# check that deployment conflicts have been checked-for
201
if not self.conflicts_checked:
202
raise RuntimeError('logic error: deployment conflicts unchecked' )
205
if the.verbose >= 1: print "exporting files..."
206
walker = CopyOutWalker( self.affected_files )
210
self.remove_deployment_state()
213
class DeploymentOngoing( the.program.FatalError ):
215
def __init__( self, ongoing ):
217
self.msg = "there is an ongoing deployment"
219
self.msg = "there is no ongoing deployment"
222
class CopyInConflicts( the.program.FatalError ):
224
def __init__( self, conflicts ):
225
self.conflicts = conflicts