I started a serious attempt to learn Python as my first programming language with some basic knowledge of algorithms. Since everyone recommends that the best way to get started is to find something useful, I decided to make a small script to manage my repositories.
Key things: - Enable / disable YUM repositories - Change priority on current YUM repositories - Add / remove repositories
While parsing a file and replacing / adding / deleting data is quite simple, I am afraid (mainly due to lack of knowledge) with one thing with "optparse" ... I want to add to the option (- l), which lists the current available repositories ... I created a simple function that does this job (not very complicated), but I cannot "connect" it to "-l" on optparse. Can anyone provide examples / suggestions on how to do this?
The current script looks something like this:
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import optparse import ConfigParse repo_file = "/home/nmarques/my_repos.repo" parser = optparse.OptionParser() parser.add_option("-e", dest="repository", help="Enable YUM repository") parser.add_option("-d", dest="repository", help="Disable YUM repository") parser.add_option("-l", dest="list", help="Display list of repositories", action="store_true") (options, args) = parser.parse_args() def list_my_repos() # check if repository file exists, read repositories, print and exit if os.path.exists(repo_file): config = ConfigParser.RawConfigParser() config.read(repo_file) print "Found the following YUM repositories on " + os.path.basename(repo_file) + ":" for i in config.sections(): print i sys.exit(0) # otherwise exit with code 4 else: print "Main repository configuration (" + repo_file +") file not found!" sys.exit(4) list_my_repos()
Any suggestions for improvement (documents, examples) are welcome. The main goal once again is that when executing script.py -l
it can run list_my_repos()
.
source share