Created
October 3, 2014 18:25
-
-
Save amiraliakbari/f26ec6631a912401b095 to your computer and use it in GitHub Desktop.
Find JAR files containing a specific class/package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import glob | |
import shutil | |
def main(): | |
class_name = raw_input('Which class do you want to import? ') | |
dir_path = raw_input('Enter the path for directory containing JAR files: [.] ') or os.getcwd() | |
found_jars = [] | |
for f in glob.glob(os.path.join(dir_path, '*.jar')): | |
files_list = os.popen('unzip -l {}'.format(f)).read().split('\n') | |
matches = 0 | |
n = len(files_list) | |
for i, l in enumerate(files_list): | |
if i in [0, 1, n - 1, n - 2]: | |
continue | |
a = l.strip().replace(' ', ' ').replace(' ', ' ').split(' ', 3) | |
fn = a[3] | |
if not fn.endswith('.class'): | |
continue | |
fqn = fn.replace('/', '.')[:-6] | |
if class_name in fqn: | |
matches += 1 | |
if matches > 0: | |
print("{} {} ({})".format(len(found_jars) + 1, f, matches)) | |
found_jars.append(f) | |
if len(found_jars): | |
if (raw_input('Copy JAR file? [Y/n] ').lower() or 'y') == 'y': | |
sel = 0 | |
if len(found_jars) > 1: | |
sel = int(raw_input('Select the file to copy: [1-{}] '.format(len(found_jars)))) - 1 | |
dst = '' | |
while not os.path.isdir(dst): | |
dst = raw_input('Copy destination? ') | |
shutil.copy2(found_jars[sel], dst) | |
if __name__ == '__main__': | |
try: | |
main() | |
except KeyboardInterrupt: | |
print('terminated.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment