~yerinalexey/dotfiles

1a5c7c9ed5324283bc2d7c322082ebdcd870a5c9 — Alexey Yerin 1 year, 8 months ago e743a02
scripts/jimport: cache results for faster lookups
1 files changed, 23 insertions(+), 10 deletions(-)

M scripts/jimport
M scripts/jimport => scripts/jimport +23 -10
@@ 3,6 3,7 @@
# Generates imports for Java stuff

from glob import iglob
import json
import os
import sys
import zipfile


@@ 10,7 11,6 @@ import zipfile
class_map = {}

def list_classes(jar):
	global class_map
	zfile = zipfile.ZipFile(jar)
	for member in zfile.namelist():
		# Don't care about nested classes or non-classes


@@ 28,19 28,32 @@ _, class_name = sys.argv

home = os.getenv('HOME')
root = os.getcwd()
if root == home:
	print("Do not run this in the home directory", file=sys.stderr)
	exit(1)
while not os.access(os.path.join(root, ".gradle"), os.F_OK | os.R_OK):
	if root == '/' or root == home:
		print("Could not find '.gradle'", file=sys.stderr)
		exit(1)
	root, _ = os.path.split(root)
if root == home:
	print("Could not find '.gradle'", file=sys.stderr)
	exit(1)

for jar in iglob(os.path.join(root, ".gradle/**/*.jar"), recursive=True):
	list_classes(jar)
cache_path = os.path.join(root, ".gradle", "jimport-cache.json")

if class_name not in class_map:
	print(f"No such class '{class_name}'", file=sys.stderr)
	exit(1)
print('\n'.join(class_map[class_name]))
def prepare_cache():
	for jar in iglob(os.path.join(root, ".gradle/**/*.jar"), recursive=True):
		list_classes(jar)
	with open(cache_path, "w+") as cache:
		json.dump(class_map, cache)

if class_name == "-r":
	prepare_cache()
	exit()

if os.access(cache_path, os.F_OK | os.R_OK):
	with open(cache_path) as cache:
		class_map = json.load(cache)
else:
	prepare_cache()

if class_name in class_map:
    print('\n'.join(class_map[class_name]))