Exemplo n.º 1
0
 def install(self, templater, src, dst, actions=[], noempty=False):
     """install the src to dst using a template"""
     self.action_executed = False
     src = os.path.join(self.base, os.path.expanduser(src))
     if not os.path.exists(src):
         self.log.err('source dotfile does not exist: {}'.format(src))
         return []
     dst = os.path.expanduser(dst)
     if self.totemp:
         dst = self._pivot_path(dst, self.totemp)
     if utils.samefile(src, dst):
         # symlink loop
         self.log.err('dotfile points to itself: {}'.format(dst))
         return []
     if self.debug:
         self.log.dbg('install {} to {}'.format(src, dst))
     if os.path.isdir(src):
         return self._handle_dir(templater,
                                 src,
                                 dst,
                                 actions=actions,
                                 noempty=noempty)
     return self._handle_file(templater,
                              src,
                              dst,
                              actions=actions,
                              noempty=noempty)
Exemplo n.º 2
0
 def _handle_file(self, templater, profile, src, dst):
     """install src to dst when is a file"""
     self.log.dbg('generate template for {}'.format(src))
     if utils.samefile(src, dst):
         # symlink loop
         self.log.err('dotfile points to itself: {}'.format(dst))
         return []
     content = templater.generate(src, profile)
     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(dst, content, st.st_mode)
     if ret < 0:
         self.log.err('installing \"{}\" to \"{}\"'.format(src, dst))
         return []
     if ret > 0:
         self.log.dbg('ignoring \"{}\", same content'.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 []
Exemplo n.º 3
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 []
Exemplo n.º 4
0
    def install(self,
                templater,
                src,
                dst,
                actionexec=None,
                noempty=False,
                ignore=[]):
        """
        install src to dst using a template
        @templater: the templater object
        @src: dotfile source path in dotpath
        @dst: dotfile destination path in the FS
        @actionexec: action executor callback
        @noempty: render empty template flag
        @ignore: pattern to ignore when installing

        return
        - True, None        : success
        - False, error_msg  : error
        - False, None       : ignored
        """
        if self.debug:
            self.log.dbg('installing \"{}\" to \"{}\"'.format(src, dst))
        if not dst or not src:
            if self.debug:
                self.log.dbg('empty dst for {}'.format(src))
            return self._log_install(True, None)
        self.action_executed = False
        src = os.path.join(self.base, os.path.expanduser(src))
        if not os.path.exists(src):
            err = 'source dotfile does not exist: {}'.format(src)
            return self._log_install(False, err)
        dst = os.path.expanduser(dst)
        if self.totemp:
            dst = self._pivot_path(dst, self.totemp)
        if utils.samefile(src, dst):
            # symlink loop
            err = 'dotfile points to itself: {}'.format(dst)
            return self._log_install(False, err)
        isdir = os.path.isdir(src)
        if self.debug:
            self.log.dbg('install {} to {}'.format(src, dst))
            self.log.dbg('is a directory \"{}\": {}'.format(src, isdir))
        if isdir:
            b, e = self._handle_dir(templater,
                                    src,
                                    dst,
                                    actionexec=actionexec,
                                    noempty=noempty,
                                    ignore=ignore)
            return self._log_install(b, e)
        b, e = self._handle_file(templater,
                                 src,
                                 dst,
                                 actionexec=actionexec,
                                 noempty=noempty,
                                 ignore=ignore)
        return self._log_install(b, e)
Exemplo n.º 5
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
Exemplo n.º 6
0
 def install(self, templater, profile, src, dst):
     """install the src to dst using a template"""
     src = os.path.join(self.base, os.path.expanduser(src))
     dst = os.path.join(self.base, os.path.expanduser(dst))
     if utils.samefile(src, dst):
         # symlink loop
         self.log.err('dotfile points to itself: {}'.format(dst))
         return []
     self.log.dbg('install {} to {}'.format(src, dst))
     if os.path.isdir(src):
         return self._handle_dir(templater, profile, src, dst)
     return self._handle_file(templater, profile, src, dst)
Exemplo n.º 7
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
Exemplo n.º 8
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