コード例 #1
0
    def _sync(self, gitcmd=None, logger=None):  # pylint: disable=R0915
        changes = 0

        def debug(msg):
            if logger:
                logger.debug(msg)

        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 self.pattern.match(oldf[slen:]):
                    debug('filter out %s' % oldf)
                    continue

                newf = oldf.replace(self.src, self.dest)
                if not os.path.lexists(newf):
                    debug('remove file %s' % oldf)
                    changes += 1
                    if gitcmd:
                        gitcmd.rm(oldf)
                    else:
                        os.unlink(oldf)

            for dname in dirs:
                oldd = os.path.join(root, dname)
                if self.sccsp.match_dir(oldd[slen:]):
                    continue
                elif self.pattern.match_dir(oldd[slen:]):
                    debug('filter out %s' % oldd)
                    continue

                newd = oldd.replace(self.src, self.dest)
                if not os.path.lexists(newd):
                    debug('remove directory %s' % oldd)
                    changes += 1
                    if gitcmd:
                        gitcmd.rm('-r', oldd)
                    else:
                        shutil.rmtree(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.pattern.match(newd[dlen:]):
                    debug('filter out %s' % oldd)
                elif not os.path.lexists(os.path.dirname(oldd)):
                    debug('ignored %s without dir' % oldd)
                elif not os.path.lexists(oldd):
                    debug('makedir %s' % oldd)
                    os.makedirs(oldd)
                elif not os.path.isdir(oldd):
                    debug('type changed %s' % oldd)
                    os.unlink(oldd)
                    os.makedirs(oldd)
                else:
                    debug('no change %s' % oldd)

            for name in files:
                newf = os.path.join(root, name)
                timest = os.lstat(newf)
                if timest.st_mtime > self._timestamp:
                    self._timestamp = timest.st_mtime

                oldf = newf.replace(self.dest, self.src)
                if self.pattern.match(newf[dlen:]):
                    debug('filter out %s' % oldf)
                elif not os.path.lexists(os.path.dirname(oldf)):
                    debug('ignored %s without dir' % oldf)
                elif os.path.islink(newf):
                    if not self._equal_link(oldf, newf):
                        debug('copy the link file %s' % oldf)
                        FileUtils.copy_file(newf, oldf)
                        if gitcmd:
                            gitcmd.add(oldf)
                        changes += 1
                elif not os.path.lexists(oldf):
                    debug('add file %s' % newf)
                    dirn = os.path.dirname(oldf)
                    if not os.path.lexists(dirn):
                        os.makedirs(dirn)

                    FileUtils.copy_file(newf, oldf)
                    if gitcmd:
                        gitcmd.add(oldf)
                    changes += 1
                else:
                    if os.path.islink(oldf):
                        debug('link file %s' % newf)
                        FileUtils.copy_file(newf, oldf)
                        if gitcmd:
                            gitcmd.add(oldf)
                        changes += 1
                    elif not filecmp.cmp(newf, oldf):
                        debug('change file %s' % newf)
                        FileUtils.copy_file(newf, oldf)
                        if gitcmd:
                            gitcmd.add(oldf)
                        changes += 1
                    else:
                        debug('no change %s' % newf)

        return changes
コード例 #2
0
ファイル: file_diff.py プロジェクト: krep2/krep
    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