Esempio n. 1
0
    def _make_file_info(self,
                        remote_path,
                        local_path=None,
                        delim_start=None,
                        delim_end=None,
                        load_contents=1):
        if not local_path:
            # Safe enough to assume local path is the same as the remote one
            local_path = remote_path

        try:
            file_stat = os.lstat(local_path)
        except OSError, e:
            raise cfg_exceptions.RepositoryLocalFileError(
                "Error lstat()-ing local file: %s" %
                e), None, sys.exc_info()[2]
Esempio n. 2
0
class Repository:
    _uid_cache = {}
    _gid_cache = {}
    _local_config = local_config

    def __init__(self):
        self.default_delimiters = None
        self.maximum_file_size = None

    # Helpers

    # Unless overridden in a subclass, per-file delimiters are the same as the
    # global delimiters
    def get_file_delimiters(self, file):
        "returns the default delimiters for this file"
        return self.get_default_delimiters()

    def get_default_delimiters(self):
        "returns the default delimiters"
        if self.default_delimiters is None:
            self.default_delimiters = self._get_default_delimiters()
        return self.default_delimiters

    def _get_default_delimiters(self):
        raise NotImplementedError

    def get_maximum_file_size(self):
        "returns the maximum file size"
        if self.maximum_file_size is None:
            self.maximum_file_size = self._get_maximum_file_size()
        return self.maximum_file_size

    def _get_maximum_file_size(self):
        "To be overwritten in subclasses"
        return 1024

    def make_stat_info(self, path, file_stat):
        # Returns the stat information as required by the API
        ret = {}
        fields = {
            'mode': stat.ST_MODE,
            'user': stat.ST_UID,
            'group': stat.ST_GID,
            'size': stat.ST_SIZE,
            'mtime': stat.ST_MTIME,
            'ctime': stat.ST_CTIME,
        }
        for label, st in fields.items():
            ret[label] = file_stat[st]

        # server expects things like 644, 700, etc.
        ret['mode'] = deci_to_octal(ret['mode'] & 07777)

        #print ret['size']
        #if ret['size'] > self.get_maximum_file_size():
        #    die(4, "File %s exceeds the maximum file size (%s)" %
        #        (path, ret['size']))

        uid = ret['user']
        gid = ret['group']

        pw_name = self._uid_cache.get(uid)
        if not pw_name:
            try:
                pw_name = pwd.getpwuid(uid)[0]
            except KeyError:
                print "Error looking up user id %s" % (uid, )

        if pw_name:
            ret['user'] = pw_name
            self._uid_cache[uid] = pw_name

        gr_name = self._gid_cache.get(gid)
        if not gr_name:
            try:
                gr_name = grp.getgrgid(gid)[0]
            except KeyError:
                print "Error looking up group id %s" % (gid, )

        if gr_name:
            ret['group'] = gr_name
            self._gid_cache[gid] = gr_name

        # if selinux is disabled or on RHEL4 we do not send the selinux_ctx
        # flag at all - see bug 644985 - SELinux context cleared from
        # RHEL4 rhncfg-client
        try:
            selinux_ctx = lgetfilecon(path)[1]
        except OSError:
            selinux_ctx = ''
        if selinux_ctx == None:
            selinux_ctx = ''

        ret['selinux_ctx'] = selinux_ctx

        return ret

    def _make_file_info(self,
                        remote_path,
                        local_path=None,
                        delim_start=None,
                        delim_end=None,
                        load_contents=1):
        if not local_path:
            # Safe enough to assume local path is the same as the remote one
            local_path = remote_path

        try:
            file_stat = os.lstat(local_path)
        except OSError, e:
            raise cfg_exceptions.RepositoryLocalFileError(
                "Error lstat()-ing local file: %s" %
                e), None, sys.exc_info()[2]

        # Dlimiters
        if delim_start or delim_end:
            if not (delim_start and delim_end):
                # If only one delimiter is provided, assume the delimiters are
                # the same, whatever that is (or is nice)
                delim_start = delim_end = (delim_start or delim_end)
        else:
            # Use the default
            delim_start, delim_end = self.get_file_delimiters(remote_path)

        params = {
            'path': remote_path,
            'delim_start': delim_start,
            'delim_end': delim_end,
        }

        file_contents = None
        if os.path.islink(local_path):
            params['config_file_type_id'] = 3
            params['symlink'] = os.readlink(local_path)
            load_contents = 0
        elif os.path.isdir(local_path):
            params['config_file_type_id'] = 2
            load_contents = 0
        else:
            params['config_file_type_id'] = 1

        if load_contents:
            try:
                file_contents = open(local_path, "r").read()
            except IOError, e:
                raise cfg_exceptions.RepositoryLocalFileError(
                    "Error opening local file: %s" %
                    e), None, sys.exc_info()[2]

            self._add_content(file_contents, params)