Created
November 18, 2020 17:26
-
-
Save Algomorph/fbf63c816855ba7db0c37fccf562ecfc to your computer and use it in GitHub Desktop.
This nifty little script allows for TAs of CMSC131/CMSC132 at UMD to convert student project submissions (distributed from submit server) into actual Eclipse projects, that can be then imported into an Eclipse Workspace without any trouble.
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/python3 | |
import sys | |
import os | |
import re | |
import shutil | |
PROJECT_TEMPLATE = ( | |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
"<projectDescription>\n" + | |
" <name>INSERT_NAME_HERE</name>\n" + | |
" <comment></comment>\n" + | |
" <projects>\n" + | |
" </projects>\n" + | |
" <buildSpec>\n" + | |
" <buildCommand>\n" + | |
" <name>org.eclipse.jdt.core.javabuilder</name>\n" + | |
" <arguments>\n" + | |
" </arguments>\n" + | |
" </buildCommand>\n" + | |
" </buildSpec>\n" + | |
" <natures>\n" + | |
" <nature>org.eclipse.jdt.core.javanature</nature>\n" + | |
" </natures>\n" + | |
"</projectDescription>\n" | |
) | |
CLASSPATH_TEMPLATE = ( | |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
"<classpath>\n" + | |
" <classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13\">\n" + | |
" <attributes>\n" + | |
" <attribute name=\"module\" value=\"true\"/>\n" + | |
" </attributes>\n" + | |
" </classpathentry>\n" + | |
" <classpathentry kind=\"src\" path=\"src\"/>\n" + | |
" <classpathentry kind=\"output\" path=\"bin\"/>\n" + | |
"</classpath>\n" | |
) | |
def main(): | |
listing = os.listdir(".") | |
for entry in listing: | |
if os.path.isdir(entry) and entry != ".idea" and entry != ".metadata" and entry != "GradingTest" \ | |
and not entry.startswith("OnlineTest"): | |
directory_id = re.search(r"^\w+", entry).group(0) | |
eclipse_project_folder = "OnlineTest_" + directory_id | |
if os.path.exists(eclipse_project_folder): | |
shutil.rmtree(eclipse_project_folder) | |
os.mkdir(eclipse_project_folder) | |
src_folder = os.path.join(eclipse_project_folder, "src") | |
os.mkdir(src_folder) | |
os.mkdir(os.path.join(eclipse_project_folder, "bin")) | |
for directory, _, filenames in os.walk(entry): | |
for filename in filenames: | |
file_path = os.path.join(directory, filename) | |
contents = open(file_path).readlines() | |
package = None | |
for line in contents: | |
if line.startswith("package"): | |
match = re.search(r"^\s*package\s*(\w+(?:\.\w+)*);", line) | |
if match is not None: | |
package = match.group(1) | |
break | |
if package is not None: | |
subdirectories = package.split(".") | |
directory_in_project = os.path.join(src_folder, os.path.join(*subdirectories)) | |
if not os.path.exists(directory_in_project): | |
os.makedirs(directory_in_project) | |
shutil.copy(file_path, os.path.join(directory_in_project, filename)) | |
project_file = open(os.path.join(eclipse_project_folder, ".project"), 'w') | |
project_file_contents = PROJECT_TEMPLATE.replace("INSERT_NAME_HERE", eclipse_project_folder) | |
project_file.write(project_file_contents) | |
project_file.close() | |
classpath_file = open(os.path.join(eclipse_project_folder, ".classpath"), 'w') | |
classpath_file.write(CLASSPATH_TEMPLATE) | |
classpath_file.close() | |
print("Generated Eclipse project " + eclipse_project_folder + " from folder " + entry) | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment