def sync(self, logger=None, quickcopy=False, symlinks=True, scmtool=None): if not quickcopy: ret = self._sync(logger=logger, symlinks=symlinks, scmtool=scmtool) else: self._timestamp = FileUtils.last_modified(self.src) FileUtils.rmtree(self.dest, ignore_list=self.sccsp.get_patterns(), scmtool=scmtool) ret = FileUtils.copy_files(self.src, self.dest, symlinks=symlinks, scmtool=scmtool) return ret
def _sync(self, logger=None, symlinks=False, scmtool=None): # pylint: disable=R0915 changes = 0 def debug(msg): if logger: logger.debug(msg) def unlink(filename): if scmtool: scmtool.rm('-rf', filename) elif os.path.islink(filename): os.unlink(filename) elif os.path.isdir(filename): shutil.rmtree(filename) else: os.unlink(filename) slen = len(self.src) + 1 dlen = len(self.dest) + 1 # remove files for root, dirs, files in os.walk(self.src): for name in files: oldf = os.path.join(root, name) if self.sccsp.match(oldf[slen:]): continue elif not self.pattern.match(oldf[slen:]): debug('ignore %s with file pattern' % oldf) continue newf = oldf.replace(self.src, self.dest) if not os.path.lexists(newf): debug('remove %s' % oldf) changes += 1 unlink(oldf) if self.pattern.has_dir_rule(): for dname in dirs: oldd = os.path.join(root, dname) if self.sccsp.match_dir(oldd[slen:]): continue elif not self.pattern.match_dir(oldd[slen:]): debug('ignore %s with dir pattern' % oldd) continue newd = oldd.replace(self.src, self.dest) if not os.path.lexists(newd): debug('remove %s' % oldd) changes += 1 unlink(oldd) for root, dirs, files in os.walk(self.dest): for dname in dirs: newd = os.path.join(root, dname) oldd = newd.replace(self.dest, self.src) if self.sccsp.match_dir(newd[dlen:]): continue elif not self.pattern.match_dir(newd[dlen:]): debug('ignore %s with file pattern' % oldd) elif os.path.islink(newd): if not self._equal_link(oldd, newd): debug('mkdir %s' % newd) FileUtils.copy_file(newd, oldd, symlinks=symlinks) changes += 1 elif os.path.exists(oldd) and not os.path.isdir(oldd): debug('type changed %s' % oldd) unlink(oldd) for name in files: newf = os.path.join(root, name) timest = FileUtils.last_modified(newf) if timest > self._timestamp: self._timestamp = timest oldf = newf.replace(self.dest, self.src) if self.sccsp.match(newf[dlen:]): continue elif not self.pattern.match(newf[dlen:]): debug('ignore %s with file pattern' % oldf) elif os.path.islink(newf): if not self._equal_link(oldf, newf): debug('copy %s' % newf) FileUtils.copy_file(newf, oldf, symlinks=symlinks, scmtool=scmtool) changes += 1 elif not os.path.lexists(oldf): debug('add %s' % newf) dirn = os.path.dirname(oldf) if not os.path.lexists(dirn): os.makedirs(dirn) FileUtils.copy_file(newf, oldf, symlinks=symlinks, scmtool=scmtool) changes += 1 else: if os.path.islink(oldf): debug('link %s' % newf) FileUtils.copy_file(newf, oldf, symlinks=symlinks, scmtool=scmtool) changes += 1 elif not filecmp.cmp(newf, oldf): debug('change %s' % newf) FileUtils.copy_file(newf, oldf, symlinks=symlinks, scmtool=scmtool) changes += 1 else: debug('nochange %s' % oldf) return changes