-
Notifications
You must be signed in to change notification settings - Fork 2
/
zfind
executable file
·50 lines (38 loc) · 1.56 KB
/
zfind
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
''' Recursively search for files contained in jar/zip files.
This isn't for searching _inside_ the files, like zgrep, but
for matching the file names themselves. For instance:
$ zfind org.apache.http.client.params.HttpClientParams ~/.m2
will search Maven's local cache for any jars containing the
HttpClientParams class. It turns out the dots in the package name
conveniently match slashes in the java class's name:
/home/dnorth/.m2/repository/org/apache/httpcomponents/httpclient/4.0.1/httpclient-4.0.1.jar: org/apache/http/client/params/HttpClientParams.class
Ah, there it is! '''
__AUTHOR__ = 'Dan North <[email protected]>'
import os, sys
from contextlib import closing
import re
import zipfile
def scan_file(pattern, path):
''' Search a zip file's contents for matching file names '''
if zipfile.is_zipfile(path):
with closing(zipfile.ZipFile(path)) as zf:
for name in zf.namelist():
if re.search(pattern, name):
print "%s: %s" % (path, name)
def scan_zip_files(pattern, path):
''' Use os.walk to recurse down a directory hierarchy looking for zip files '''
if os.path.isfile(path):
scan_file(pattern, path)
else:
for here, dirs, files in os.walk(path):
for f in (os.path.join(here, f) for f in files):
scan_file(pattern, f)
if __name__ == '__main__':
if len(sys.argv) < 3:
print >>sys.stderr, "Usage: %s pattern path..." % sys.argv[0]
sys.exit(1)
pattern = sys.argv[1]
scanned, total = 0, 0
for path in sys.argv[2:]:
scan_zip_files(pattern, path)