community-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1878445 - /comdev/reporter.apache.org/trunk/scripts/urlutils.py
Date Wed, 03 Jun 2020 16:06:49 GMT
Author: sebb
Date: Wed Jun  3 16:06:48 2020
New Revision: 1878445

URL: http://svn.apache.org/viewvc?rev=1878445&view=rev
Log:
Synch with recent changes from projects.a.o

Modified:
    comdev/reporter.apache.org/trunk/scripts/urlutils.py

Modified: comdev/reporter.apache.org/trunk/scripts/urlutils.py
URL: http://svn.apache.org/viewvc/comdev/reporter.apache.org/trunk/scripts/urlutils.py?rev=1878445&r1=1878444&r2=1878445&view=diff
==============================================================================
--- comdev/reporter.apache.org/trunk/scripts/urlutils.py (original)
+++ comdev/reporter.apache.org/trunk/scripts/urlutils.py Wed Jun  3 16:06:48 2020
@@ -55,14 +55,18 @@ def mod_date(t):
         return None
     return time.strftime(_HTTP_TIME_FORMAT, time.gmtime(t))
 
-def getIfNewer(url, sinceTime, encoding=None, errors=None, silent=False, debug=False):
+def getIfNewer(url, sinceTime=None, encoding=None, errors=None, silent=False, debug=False,
method='GET'):
     """
-        Get a URL if it is not newer
+        Get a URL if it is newer
     
         @param url: the url to fetch (required)
-        @param sinceTime: the most recent Last-Modified string (required, format as per mod_date())
+        @param sinceTime: the most recent Last-Modified string (format as per mod_date())
         @param encoding: the encoding to use (default 'None')
         @param errors: If encoding is provided, this specifies the on-error action (e.g.
'ignore')
+        @param silent: whether to print the url and headers (default True)
+        @param debug: whether to print additional info (default False)
+        @param method: the HTTP method to use (default GET)
+
         @return: (lastMod, response)
         - lastMod: the Last-Modified string (from sinceTime if the URL is not later) may
be None
         - response: the HTTPResponse (encoding == None) or TextIOBase object.
@@ -76,7 +80,7 @@ def getIfNewer(url, sinceTime, encoding=
     response = None
     try:
         if not silent: print("%s %s" % (url, headers))
-        req = Request(url, headers=headers)
+        req = Request(url, headers=headers, method=method)
         resp = urlopen(req, timeout=URL_TIMEOUT)
         # Debug - detect why json sometimes returned as HTML but no error code
         if debug and not silent: print("STATUS %s" % resp.getcode()) # Works for Py2/3
@@ -98,6 +102,24 @@ def getIfNewer(url, sinceTime, encoding=
             raise
     return lastMod, response
 
+def URLopen(url, sinceTime=None, encoding=None, errors=None, silent=True, debug=False, method='GET'):
+    """
+    Get the URL response as for getIfNewer, but default to silent=True and omit lastMod from
reply
+    """
+    _, response = getIfNewer(url, sinceTime=sinceTime, encoding=encoding, errors=errors,
silent=silent, debug=debug, method=method)
+    return response
+
+def URLexists(url):
+    """
+    Does the URL exist?
+    Uses HEAD to check
+    """
+    try:
+        getIfNewer(url, method='HEAD', silent=True)
+        return True
+    except:
+        return False
+
 def findRelPath(relpath):
     for d in ['./','../','../../']: # we may located at same level or 1 or 2 below
         dir = join(d,relpath)



Mime
View raw message