Advertisement
Guest User

Untitled

a guest
Mar 9th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1.  
  2.  
  3. from __future__ import with_statement  # for Python < 2.6
  4.  
  5. import os
  6. import re
  7. import zipfile
  8.  
  9. # open a zip file
  10. DST_FILE = 'sources.zip'
  11. if os.path.exists(DST_FILE):
  12.   print DST_FILE, "already exists"
  13.   exit(1)
  14. zip = zipfile.ZipFile(DST_FILE, 'w', zipfile.ZIP_DEFLATED)
  15.  
  16. # some files are duplicated, copy them only once
  17. written = {}
  18.  
  19. # iterate over all Java files
  20. for dir, subdirs, files in os.walk('.'):
  21.   for file in files:
  22.     if file.endswith('.java'):
  23.       # search package name
  24.       path = os.path.join(dir, file)
  25.       with open(path) as f:
  26.         for line in f:
  27.           match = re.match(r'\s*package\s+([a-zA-Z0-9\._]+);', line)
  28.           if match:
  29.             # copy source into the zip file using the package as path
  30.             zippath = match.group(1).replace('.', '/') + '/' + file
  31.             if zippath not in written:
  32.               written[zippath] = 1
  33.               zip.write(path, zippath)
  34.             break;
  35.          
  36. zip.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement