text
stringlengths
1
93.6k
def decompile_jar(jar_path: str, output_dir: str) -> bool:
"""
Decompiles a JAR file using the CFR tool.
Args:
jar_path (str): Path to the JAR file to be decompiled.
output_dir (str): Directory where the decompiled source code will be saved.
Returns:
bool: True if decompilation is successful, False otherwise.
"""
CFR_JAR_PATH = os.path.join("lib", "cfr-0.152.jar")
# Remove the output directory if it already exists
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
os.makedirs(output_dir)
# Run CFR to decompile the JAR file
try:
print("Starting JAR decompilation...")
command = [
"java", "-jar", CFR_JAR_PATH, jar_path, "--outputdir", output_dir
]
subprocess.run(command, check=True)
print(f"Decompilation complete. Source code saved to {output_dir}")
return True
except subprocess.CalledProcessError as e:
print(f"Decompilation failed: {e}")
return False
except Exception as e:
print(f"An error occurred: {e}")
return False
def code_to_text(directory: str) -> str:
"""
Converts the code in a directory to text.
Args:
directory (str): The directory containing the code files.
Returns:
str: The text representation of the code.
Return Structure:
file1_path:
```
1 code
2 code
...
```
file2_path:
Cannot load non-text file
...
"""
def is_text_file(file_path):
txt_extensions = [
".txt",
".java",
".py",
".md",
".json",
".yaml",
".yml",
".xml",
".toml",
".ini",
".js",
".groovy",
".log",
".properties",
".cfg",
".conf",
".bat",
".sh",
"README",
]
return any(file_path.endswith(ext) for ext in txt_extensions)
text = ""
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, directory)
if is_text_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Add line numbers to content
numbered_lines = [f"{i+1:<3} {line}" for i, line in enumerate(content.splitlines())]
numbered_content = '\n'.join(numbered_lines)
text += f"{relative_path}:\n```\n{numbered_content}\n```\n"
except Exception as e:
text += f"{relative_path}: Cannot load non-text file\n"
else:
text += f"{relative_path}: Cannot load non-text file\n"