public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] portage r14718 - main/branches/2.1.7/bin
@ 2009-10-24  7:06 Zac Medico (zmedico)
  0 siblings, 0 replies; only message in thread
From: Zac Medico (zmedico) @ 2009-10-24  7:06 UTC (permalink / raw
  To: gentoo-commits

Author: zmedico
Date: 2009-10-24 07:06:28 +0000 (Sat, 24 Oct 2009)
New Revision: 14718

Modified:
   main/branches/2.1.7/bin/repoman
Log:
Use urllib.urlopen() instead of portage.fetch() for fetching metadata.dtd.
(trunk r14709)

Modified: main/branches/2.1.7/bin/repoman
===================================================================
--- main/branches/2.1.7/bin/repoman	2009-10-24 07:06:07 UTC (rev 14717)
+++ main/branches/2.1.7/bin/repoman	2009-10-24 07:06:28 UTC (rev 14718)
@@ -25,6 +25,7 @@
 import tempfile
 import time
 import platform
+import urllib
 
 from io import StringIO
 from itertools import chain
@@ -440,6 +441,10 @@
 	"x11-misc/imake",
 ])
 
+metadata_dtd_uri = 'http://www.gentoo.org/dtd/metadata.dtd'
+# force refetch if the local copy creation time is older than this
+metadata_dtd_ctime_interval = 60 * 60 * 24 * 7 # 7 days
+
 # file.executable
 no_exec = frozenset(["Manifest","ChangeLog","metadata.xml"])
 
@@ -765,59 +770,80 @@
 for x in qacats:
 	stats[x]=0
 	fails[x]=[]
+
 xmllint_capable = False
 metadata_dtd = os.path.join(repoman_settings["DISTDIR"], 'metadata.dtd')
-if options.mode == "manifest":
-	pass
-elif not find_binary('xmllint'):
-	print(red("!!! xmllint not found. Can't check metadata.xml.\n"))
-	if options.xml_parse or repolevel==3:
-		print(red("!!!")+" sorry, xmllint is needed.  failing\n")
-		sys.exit(1)
-else:
-	#hardcoded paths/urls suck. :-/
-	must_fetch=1
-	backup_exists=0
+
+def fetch_metadata_dtd():
+	"""
+	Fetch metadata.dtd if it doesn't exist or the ctime is older than
+	metadata_dtd_ctime_interval.
+	@rtype: bool
+	@returns: True if successful, otherwise False
+	"""
+
+	must_fetch = True
+	metadata_dtd_st = None
+	current_time = int(time.time())
 	try:
-		# if it's been over a week since fetching (or the system clock is fscked), grab an updated copy of metadata.dtd 
-		# clock is fscked or it's been a week. time to grab a new one.
-		ct = os.stat(metadata_dtd)[ST_CTIME]
-		if abs(time.time() - ct) > (60*60*24*7):
-			# don't trap the exception, we're watching for errno 2 (file not found), anything else is a bug.
-			backup_exists=1
-		else:
-			must_fetch=0
+		metadata_dtd_st = os.stat(metadata_dtd)
+	except EnvironmentError as e:
+		if e.errno not in (errno.ENOENT, errno.ESTALE):
+			raise
+		del e
+	else:
+		# Trigger fetch if metadata.dtd mtime is old or clock is wrong.
+		if abs(current_time - metadata_dtd_st.st_ctime) \
+			< metadata_dtd_ctime_interval:
+			must_fetch = False
 
-	except (OSError,IOError) as e:
-		if e.errno != 2:
-			print(red("!!!")+" caught exception '%s' for %s/metadata.dtd, bailing" % (str(e), portage.CACHE_PATH))
-			sys.exit(1)
-
 	if must_fetch:
-		print() 
-		print(green("***")+" the local copy of metadata.dtd needs to be refetched, doing that now")
 		print()
-		val = 0
+		print(green("***") + " the local copy of metadata.dtd " + \
+			"needs to be refetched, doing that now")
+		print()
 		try:
+			url_f = urllib.urlopen(metadata_dtd_uri)
+			last_modified = url_f.info().getdate('last-modified')
+			if last_modified is not None:
+				last_modified = time.mktime(last_modified)
+
+			metadata_dtd_tmp = "%s.%s" % (metadata_dtd, os.getpid())
 			try:
-				os.unlink(metadata_dtd)
-			except OSError as e:
-				if e.errno != errno.ENOENT:
-					raise
-				del e
-			val=portage.fetch(['http://www.gentoo.org/dtd/metadata.dtd'],repoman_settings,fetchonly=0, \
-				try_mirrors=0)
+				local_f = open(metadata_dtd_tmp, mode='wb')
+				local_f.write(url_f.read())
+				local_f.close()
+				if last_modified is not None:
+					os.utime(metadata_dtd_tmp,
+						(int(last_modified), int(last_modified)))
+				os.rename(metadata_dtd_tmp, metadata_dtd)
+			finally:
+				try:
+					os.unlink(metadata_dtd_tmp)
+				except OSError:
+					pass
 
-		except SystemExit as e:
-			raise  # Need to propogate this
-		except Exception as e:
+			url_f.close()
+
+		except EnvironmentError as e:
 			print()
-			print(red("!!!")+" attempting to fetch 'http://www.gentoo.org/dtd/metadata.dtd', caught")
-			print(red("!!!")+" exception '%s' though." % str(e))
-			val=0
-		if not val:
+			print(red("!!!")+" attempting to fetch '%s', caught" % metadata_dtd_uri)
+			print(red("!!!")+" exception '%s' though." % (e,))
 			print(red("!!!")+" fetching new metadata.dtd failed, aborting")
-			sys.exit(1)
+			return False
+
+	return True
+
+if options.mode == "manifest":
+	pass
+elif not find_binary('xmllint'):
+	print(red("!!! xmllint not found. Can't check metadata.xml.\n"))
+	if options.xml_parse or repolevel==3:
+		print(red("!!!")+" sorry, xmllint is needed.  failing\n")
+		sys.exit(1)
+else:
+	if not fetch_metadata_dtd():
+		sys.exit(1)
 	#this can be problematic if xmllint changes their output
 	xmllint_capable=True
 




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-10-24  7:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-10-24  7:06 [gentoo-commits] portage r14718 - main/branches/2.1.7/bin Zac Medico (zmedico)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox