150af88b1a8fdddb02dc33a34b68a3051cc69fb3
1 # Copyright 2007, Red Hat, Inc
4 # This software may be freely redistributed under the terms of the GNU
5 # general public license.
7 # You should have received a copy of the GNU General Public License
8 # along with this program; if not, write to the Free Software
9 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 class CopyFile(func_module
.FuncModule
):
24 description
= "Allows for smart copying of a file."
26 def _checksum_blob(self
, blob
):
29 return thissum
.hexdigest()
31 def checksum(self
, thing
):
35 if os
.path
.exists(thing
):
36 fo
= open(thing
, 'r', CHUNK
)
39 chunk
= fo
.read(CHUNK
)
44 # assuming it's a string of some kind
47 return thissum
.hexdigest()
50 def copyfile(self
, filepath
, filebuf
, mode
=0644, uid
=0, gid
=0, force
=None):
51 # -1 = problem file was not copied
53 # 0 = file was not copied b/c file is unchanged
56 # we should probably verify mode,uid,gid are valid as well
58 dirpath
= os
.path
.dirname(filepath
)
59 if not os
.path
.exists(dirpath
):
62 remote_sum
= self
._checksum
_blob
(filebuf
.data
)
64 if os
.path
.exists(filepath
):
65 local_sum
= self
.checksum(filepath
)
67 if remote_sum
!= local_sum
or force
is not None:
68 # back up the localone
69 if os
.path
.exists(filepath
):
70 if not self
._backuplocal
(filepath
):
75 fo
= open(filepath
, 'w')
76 fo
.write(filebuf
.data
)
79 except (IOError, OSError), e
:
80 # XXX logger output here
85 # hmm, need to figure out proper exceptions -akl
87 # we could intify the mode here if it's a string
88 os
.chmod(filepath
, mode
)
89 os
.chown(filepath
, uid
, gid
)
90 except (IOError, OSError), e
:
95 def _backuplocal(self
, fn
):
97 make a date-marked backup of the specified file,
98 return True or False on success or failure
100 # backups named basename-YYYY-MM-DD@HH:MM~
101 ext
= time
.strftime("%Y-%m-%d@%H:%M~", time
.localtime(time
.time()))
102 backupdest
= '%s.%s' % (fn
, ext
)
105 shutil
.copy2(fn
, backupdest
)
106 except shutil
.Error
, e
:
107 #XXX logger output here