Untitled

From Bystroushaak, 5 Years ago, written in Plain Text, viewed 352 times.
URL http://paste.security-portal.cz/view/7a667800 Embed
Download Paste or View Raw
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import os.path
  6. import urllib2
  7.  
  8.  
  9.  
  10. def processConf(s):
  11.         "Remove #comments from string."
  12.         s = map(lambda x: x.split("#")[0], s.splitlines())
  13.         return filter(lambda x: x.strip() != "", s)
  14.  
  15. def createIfNotExists(path):
  16.         if not os.path.exists(path):
  17.                 os.makedirs(path)
  18.  
  19.  
  20.  
  21. PORTAGE_FILE = "snapshots/portage-latest.tar.bz2"
  22. PORTAGE_URL  = "http://distfiles.gentoo.org/" + PORTAGE_FILE
  23. PORTAGE_LASTMOD_FILE = "lastmod.txt"
  24.  
  25. STAGE3_FILE = "http://distfiles.gentoo.org/releases/amd64/autobuilds/latest-stage3-amd64-hardened.txt"
  26.  
  27. # process stage3 file
  28. filename = processConf(urllib2.urlopen(STAGE3_FILE).read())[0]
  29. if not os.path.exists(filename):
  30.         createIfNotExists(os.path.dirname(filename))
  31.  
  32.         fh = open(filename, "wb")
  33.  
  34.         hostname = os.path.dirname(STAGE3_FILE)
  35.         response = urllib2.urlopen(hostname + "/" + filename)
  36.         fh.write(response.read())
  37.  
  38.         fh.close()
  39.  
  40.  
  41. # process portagefile
  42. lastmod  = ""
  43. if os.path.exists(PORTAGE_LASTMOD_FILE):
  44.         fh = open(PORTAGE_LASTMOD_FILE)
  45.         lastmod = fg.read()
  46.         fh.close()
  47.  
  48. response = urllib2.urlopen(PORTAGE_URL)
  49. response_headers = response.info().dict
  50. server_lastmod = response_headers["last-modified"]
  51.  
  52. if server_lastmod != lastmod:
  53.         createIfNotExists(os.path.dirname(PORTAGE_FILE))
  54.  
  55.         fh = open(PORTAGE_FILE, "wb")
  56.         fh.write(response.read())
  57.         fh.close()
  58.  
  59.         fh = open(PORTAGE_LASTMOD_FILE, "wt")
  60.         fh.write(server_lastmod)
  61.         fh.close()
  62.  
  63.  

Reply to "Untitled"

Here you can reply to the paste above