4 from optparse import OptionParser
7 from BeautifulSoup import *
13 update_repo="file:///home/nashif/system-updates/repo"
14 update_cache="/tmp/updates"
18 class FakeSecHead(object):
19 def __init__(self, fp):
21 self.sechead = '[os-release]\n'
24 try: return self.sechead
25 finally: self.sechead = None
26 else: return self.fp.readline()
28 def get_current_version():
29 config = ConfigParser.SafeConfigParser()
30 config.readfp(FakeSecHead(open('/etc/os-release')))
31 return dict(config.items('os-release'))
34 def checksum(fileName, checksum_type="sha256", excludeLine="", includeLine=""):
35 """Compute sha256 hash of the specified file"""
37 if checksum_type == "md5":
41 fd = open(fileName,"rb")
43 print "Unable to open the file in readmode:", filename
45 content = fd.readlines()
47 for eachLine in content:
48 if excludeLine and eachLine.startswith(excludeLine):
56 print "Checking for new updates..."
57 response = urllib2.urlopen("%s/data/updatemd.xml" % update_repo )
58 updatemd = response.read()
59 if not os.path.exists("%s/data" %update_cache):
60 os.mkdir("%s/data" %update_cache)
62 fp = open("%s/data/updatemd.xml" % update_cache , "w")
66 updatemd_local = open("%s/data/updatemd.xml" % update_cache )
67 root = etree.XML(updatemd_local.read())
68 data = root.xpath("//data[@type='update']")[0]
69 loc = data.xpath("location")[0]
70 href = loc.attrib['href']
71 chksum = data.xpath("checksum")[0]
72 chksum_type = chksum.attrib['type']
74 if os.path.exists("%s/data/updates.xml" % update_cache):
75 cur_sum = checksum("%s/data/updates.xml" % update_cache, checksum_type=chksum_type)
76 if cur_sum == chksum.text:
77 print "Using file from cache, no new updates on server."
79 print "Fetching new updates..."
80 get_new_update_list(href)
82 get_new_update_list(href)
86 def get_new_update_list(location):
87 up = urllib2.urlopen("%s/%s" % (update_repo, location) )
88 update_raw = up.read()
89 fp = open("%s/data/updates.xml" % update_cache , "w")
95 fp = open("%s/data/updates.xml" % update_cache , "r")
96 updates_root = etree.XML(fp.read())
97 updates = updates_root.xpath("//update")
98 for update in updates:
100 print " %s:" %attr['id']
101 print " %s" %update.xpath("title")[0].text
106 parser = OptionParser()
107 parser.add_option("-V", "--os-version", action="store_true", dest="osver", default=False,
108 help="Current OS Version")
109 parser.add_option("-l", "--list-updates", action="store_true", dest="listupdates", default=False,
111 parser.add_option("-d", "--download-only", action="store_true", dest="downloadonly", default=False,
112 help="Download only")
113 parser.add_option("-i", "--install", dest="install", metavar="LABEL",
114 help="Install update")
115 parser.add_option("-r", "--recommended", dest="recommended", action="store_true", default=False,
116 help="Install recommended updates only")
117 parser.add_option("-q", "--quiet",
118 action="store_false", dest="verbose", default=True,
119 help="don't print status messages to stdout")
121 (options, args) = parser.parse_args()
124 os_release = get_current_version()
125 print os_release['version_id'].strip('"')
127 if options.listupdates: