Exemple #1
0
 def _handle_file(self, templater, src, dst, actions=[], noempty=False):
     """install src to dst when is a file"""
     if self.debug:
         self.log.dbg('generate template for {}'.format(src))
         self.log.dbg('ignore empty: {}'.format(noempty))
     if utils.samefile(src, dst):
         # symlink loop
         self.log.err('dotfile points to itself: {}'.format(dst))
         return []
     content = templater.generate(src)
     if noempty and utils.content_empty(content):
         self.log.warn('ignoring empty template: {}'.format(src))
         return []
     if content is None:
         self.log.err('generate from template {}'.format(src))
         return []
     if not os.path.exists(src):
         self.log.err('source dotfile does not exist: {}'.format(src))
         return []
     st = os.stat(src)
     ret = self._write(src, dst, content, st.st_mode, actions=actions)
     if ret < 0:
         self.log.err('installing {} to {}'.format(src, dst))
         return []
     if ret > 0:
         if self.debug:
             self.log.dbg('ignoring {}'.format(dst))
         return []
     if ret == 0:
         if not self.dry and not self.comparing:
             self.log.sub('copied {} to {}'.format(src, dst))
         return [(src, dst)]
     return []
Exemple #2
0
    def _handle_file(self,
                     templater,
                     src,
                     dst,
                     actionexec=None,
                     noempty=False,
                     ignore=[]):
        """install src to dst when is a file"""
        if self.debug:
            self.log.dbg('generate template for {}'.format(src))
            self.log.dbg('ignore empty: {}'.format(noempty))
            self.log.dbg('ignore pattern: {}'.format(ignore))

        if utils.must_ignore([src, dst], ignore, debug=self.debug):
            if self.debug:
                self.log.dbg('ignoring install of {} to {}'.format(src, dst))
            return False, None

        if utils.samefile(src, dst):
            # symlink loop
            err = 'dotfile points to itself: {}'.format(dst)
            return False, err
        saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst))
        try:
            content = templater.generate(src)
        except UndefinedException as e:
            return False, str(e)
        finally:
            templater.restore_vars(saved)
        if noempty and utils.content_empty(content):
            if self.debug:
                self.log.dbg('ignoring empty template: {}'.format(src))
            return False, None
        if content is None:
            err = 'empty template {}'.format(src)
            return False, err
        if not os.path.exists(src):
            err = 'source dotfile does not exist: {}'.format(src)
            return False, err
        st = os.stat(src)
        ret, err = self._write(src,
                               dst,
                               content,
                               st.st_mode,
                               actionexec=actionexec)
        if ret < 0:
            return False, err
        if ret > 0:
            if self.debug:
                self.log.dbg('ignoring {}'.format(dst))
            return False, None
        if ret == 0:
            if not self.dry and not self.comparing:
                self.log.sub('copied {} to {}'.format(src, dst))
            return True, None
        err = 'installing {} to {}'.format(src, dst)
        return False, err
Exemple #3
0
    def _install_file(self, templater, src, dst,
                      actionexec=None, noempty=False,
                      ignore=[], template=True):
        """install src to dst when is a file"""
        if self.debug:
            self.log.dbg('deploy file: {}'.format(src))
            self.log.dbg('ignore empty: {}'.format(noempty))
            self.log.dbg('ignore pattern: {}'.format(ignore))
            self.log.dbg('template: {}'.format(template))
            self.log.dbg('no empty: {}'.format(noempty))

        if utils.must_ignore([src, dst], ignore, debug=self.debug):
            if self.debug:
                self.log.dbg('ignoring install of {} to {}'.format(src, dst))
            return False, None

        if utils.samefile(src, dst):
            # symlink loop
            err = 'dotfile points to itself: {}'.format(dst)
            return False, err

        if not os.path.exists(src):
            err = 'source dotfile does not exist: {}'.format(src)
            return False, err

        # handle the file
        content = None
        if template:
            # template the file
            saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst))
            try:
                content = templater.generate(src)
            except UndefinedException as e:
                return False, str(e)
            finally:
                templater.restore_vars(saved)
            if noempty and utils.content_empty(content):
                if self.debug:
                    self.log.dbg('ignoring empty template: {}'.format(src))
                return False, None
            if content is None:
                err = 'empty template {}'.format(src)
                return False, err
        ret, err = self._write(src, dst,
                               content=content,
                               actionexec=actionexec,
                               template=template)

        # build return values
        if ret < 0:
            # error
            return False, err
        if ret > 0:
            # already exists
            if self.debug:
                self.log.dbg('ignoring {}'.format(dst))
            return False, None
        if ret == 0:
            # success
            if not self.dry and not self.comparing:
                self.log.sub('copied {} to {}'.format(src, dst))
            return True, None
        # error
        err = 'installing {} to {}'.format(src, dst)
        return False, err
Exemple #4
0
    def _copy_file(self,
                   templater,
                   src,
                   dst,
                   actionexec=None,
                   noempty=False,
                   ignore=[],
                   is_template=True,
                   chmod=None):
        """
        install src to dst when is a file

        return
        - True, None        : success
        - False, error_msg  : error
        - False, None       : ignored
        - False, 'aborted'    : user aborted
        """
        if self.debug:
            self.log.dbg('deploy file: {}'.format(src))
            self.log.dbg('ignore empty: {}'.format(noempty))
            self.log.dbg('ignore pattern: {}'.format(ignore))
            self.log.dbg('is_template: {}'.format(is_template))
            self.log.dbg('no empty: {}'.format(noempty))

        # check no loop
        if utils.samefile(src, dst):
            err = 'dotfile points to itself: {}'.format(dst)
            return False, err

        if utils.must_ignore([src, dst], ignore, debug=self.debug):
            if self.debug:
                self.log.dbg('ignoring install of {} to {}'.format(src, dst))
            return False, None

        if utils.samefile(src, dst):
            # loop
            err = 'dotfile points to itself: {}'.format(dst)
            return False, err

        if not os.path.exists(src):
            err = 'source dotfile does not exist: {}'.format(src)
            return False, err

        # handle the file
        content = None
        if is_template:
            # template the file
            saved = templater.add_tmp_vars(self._get_tmp_file_vars(src, dst))
            try:
                content = templater.generate(src)
            except UndefinedException as e:
                return False, str(e)
            finally:
                templater.restore_vars(saved)
            # test is empty
            if noempty and utils.content_empty(content):
                if self.debug:
                    self.log.dbg('ignoring empty template: {}'.format(src))
                return False, None
            if content is None:
                err = 'empty template {}'.format(src)
                return False, err

        # write the file
        ret, err = self._write(src,
                               dst,
                               content=content,
                               actionexec=actionexec,
                               chmod=chmod)
        if ret and not err:
            if not self.dry and not self.comparing:
                self.log.sub('install {} to {}'.format(src, dst))
        return ret, err