Hello Sheldon,
On Wed, 2014-02-19 at 14:14 +0800, Sheldon wrote:
comments below
On 02/19/2014 09:08 AM, Paulo Vital wrote:
> +
> + def _write2disk(self, repo={}):
> + """
> + Write repository info into disk.
> + """
> + # Get a list with all reposdir configured in system's YUM.
> + conf_dir = self._conf.reposdir
> + if not conf_dir:
> + raise NotFoundError("KCHREPOS0015E")
> +
> + if len(repo) == 0:
> + raise InvalidParameter("KCHREPOS0016E")
> +
> + # Generate the content to be wrote.
> + repo_content = '[%s]\n' % repo['repo_id']
> + repo_content = repo_content + 'name=%s\n' %
repo['repo_name']
> +
> + if isinstance(repo['baseurl'], list):
> + link = repo['baseurl'][0]
> + else:
> + link = repo['baseurl']
> +
> + if repo['is_mirror']:
> + repo_content = repo_content + 'mirrorlist=%s\n' % link
> + else:
> + repo_content = repo_content + 'baseurl=%s\n' % link
> +
> + if repo['enabled']:
> + repo_content = repo_content + 'enabled=1\n'
> + else:
> + repo_content = repo_content + 'enabled=0\n'
> +
> + if repo['gpgcheck']:
> + repo_content = repo_content + 'gpgcheck=1\n'
> + else:
> + repo_content = repo_content + 'gpgcheck=0\n'
> +
> + if repo['gpgkey']:
> + if isinstance(repo['gpgkey'], list):
> + link = repo['gpgkey'][0]
> + else:
> + link = repo['gpgkey']
> + repo_content = repo_content + 'gpgckey=%s\n' % link
> +
> + # Scan for the confdirs and write the file in the first available
> + # directory in the system. YUM will scan each confdir for repo files
> + # and load it contents, so we can write in the first available dir.
I just wonder can we get the repo_file by:
In [19]: repo.repofile
Out[19]: '///etc/yum.repos.d/fedora.repo'
You're right, but this is true only if the repo information came from a
file that already exists in system. This method is used to write the
file of new repositories that are only on YumBase() data structure. That
was the behavior I could see in my tests.
For APT it's different. If you pass a file path to the method
responsible to add a new repository, the module's method will create the
file.
> + for dir in conf_dir:
> + if os.path.isdir(dir):
> + repo_file = dir + '/%s.repo' % repo['repo_id']
> + if os.path.isfile(repo_file):
> + os.remove(repo_file)
> +
> + try:
> + with open(repo_file, 'w') as fd:
> + fd.write(repo_content)
> + fd.close()
> + except:
> + raise OperationFailed("KCHREPOS0017E",
> + {'repo_file': repo_file})
> + break
> + return True
> +
> + def _removefromdisk(self, repo_id):
> + """
> + Delete the repo file from disk of a given repository
> + """
> + conf_dir = self._conf.reposdir
> + if not conf_dir:
> + raise NotFoundError("KCHREPOS0015E")
> +
> + for dir in conf_dir:
> + if os.path.isdir(dir):
> + repo_file = dir + '/%s.repo' % repo_id
> + if os.path.isfile(repo_file):
> + os.remove(repo_file)
> +
> + return True
> +
> +