Commits

anatoly techtonik  committed 6ad0790

Add patch detection
+ bootstrap code for creating file and module cache .locally
+ download patch.py
* improve reporting for each file (cached/non-cached)

Open issues with patch keyword: 1655

Files attached: 3441
Patches: 2935

Finished in 0:12:20.095000

  • Participants
  • Parent commits dabc07b

Comments (0)

Files changed (1)

+#!/usr/bin/env python
 #---
 #
 # Get statistics for modules from standard Python library
 
 """
 
-from os import makedirs
-from os.path import abspath, exists, dirname
-from sys import version_info
 from datetime import datetime
+from sys import version_info, stdout
 
 START = datetime.now()
 
   import xmlrpclib
   input = raw_input
 
+# -- 0. Bootstrap locally --
+
+from os import makedirs
+from os.path import abspath, exists, dirname
+import sys
+
+PY3K = sys.version_info >= (3, 0)
+if PY3K:
+  import urllib.request as urllib
+else:
+  import urllib
+
 ROOT = abspath(dirname(__file__))
-CACHE = ROOT + '/.filecache/'
+CACHE = ROOT + '/.locally/files/'
+LIB = ROOT + '/.locally/lib/'
 if not exists(CACHE):
   print("Creating cache directory")
   makedirs(CACHE)
 
+# 1. create LIB dir to download required modules locally
+#    and add it to Python sys.path to make them importable
+
+if not exists(LIB):
+  print("Creating directory for additional Python modules")
+  makedirs(LIB)
+sys.path += [LIB]
+
+# 2. download required Python modules
+
+required_files = [
+  ('patch.py', 'http://python-patch.googlecode.com/svn/trunk/patch.py'),
+]
+for f, url in required_files:
+  if exists(LIB + f):
+    print("Skipping " + f + " download")
+  else:
+    print("Downloading %s into %s" % (f, LIB))
+    urllib.urlretrieve(url, LIB + f)
+
+# 3. import downloaded stuff
+
+import patch
+
+# -- /bootstrap --
+
 # -- 1. Get list of all open issues with patches --
 #
 # Using http://roundup.sourceforge.net/docs/xmlrpc.html
 # 
 # [ ] patch keyword filtering may not be reliable
 #   [ ] get list of all issues with attachments
-#   [ ] filter patches
+#   [x] filter patches
 #
 
 bpo = xmlrpclib.ServerProxy('http://bugs.python.org', allow_none=True)
 print('Open issues with patch keyword: %s' % len(open_issues))
 
 
-# -- 2. read patch files for each issue and get paths per issue
+# -- 2. Read patch files for each issue and get paths per issue
 
 files_attached = 0
-files_patches = []
+files_patches = 0
 
 # [x] implement caching for issue files
 #  [ ] check what urlretrieve does for incomplete files
 def fileget(number):
+  """ Download and cache specified file from Roundup
+      tracker. Return tuple (filepath, cached).
+  """
+  filename = CACHE + number
+  cached = False
   if not exists(CACHE + number):
-    print('  ' + number)
     try:
-      urllib.urlretrieve('http://bugs.python.org/file' + number + '/', CACHE + number)
+      urllib.urlretrieve('http://bugs.python.org/file' + number + '/', filename)
     except:
       from traceback import print_exc
       print_exc()
       print("\nException. Press Enter to continue")
       input()
   else:
-    print('  ' + number + ' (cached)')
-      
+    cached = True
+  return (filename, cached)
 
 issue_patches = {}  # 'number' => []
 for issue in open_issues:
   issue_patches[issue] = bpo.display('issue'+issue, 'files')['files']
   for filen in issue_patches[issue]:
     files_attached += 1
-    fileget(filen)
+    stdout.write('  ' + filen)
+    # get file
+    path, cached = fileget(filen)
+    if cached:
+      stdout.write(' (cached)')
+    # check if it is a patch
+    if patch.fromfile(path):
+      files_patches += 1
+    else:
+      stdout.write(' (not a patch)')
+    stdout.write('\n')
+
 
 print('')
 print('Open issues with patch keyword: %s' % len(open_issues))
-print('Number of files attached: %s' % files_attached)
 print('')
-print('Finished in %s' % (datetime.now() - START))
+print('Files attached: %s' % files_attached)
+print('Patches: %s' % files_patches)
+print('')
+print('Finished in %s' % (datetime.now() - START)[:-4])