From 3f4a3cae7a6a858fd7f12a027058ee73372fcb48 Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Mon, 27 Apr 2015 14:31:25 -0700 Subject: [PATCH] Ant build.xml creator first cut --- Ant-Common.xml | 19 +- access/{build.xml => nobuild.xml} | 0 annotations/{build.xml => nobuild.xml} | 0 arrays/{build.xml => no.xml} | 0 build.xml | 13 +- concurrency/{build.xml => nobuild.xml} | 0 containers/{build.xml => nobuild.xml} | 0 control/{build.xml => nobuild.xml} | 0 enumerated/{build.xml => nobuild.xml} | 0 exceptions/{build.xml => nobuild.xml} | 0 generics/{build.xml => nobuild.xml} | 0 holding/{build.xml => nobuild.xml} | 0 initialization/{build.xml => nobuild.xml} | 0 innerclasses/{build.xml => nobuild.xml} | 0 interfaces/{build.xml => nobuild.xml} | 0 io/{build.xml => nobuild.xml} | 0 net/{build.xml => nobuild.xml} | 0 object/{build.xml => nobuild.xml} | 0 operators/{build.xml => nobuild.xml} | 0 patterns/{build.xml => notbuild.xml} | 0 polymorphism/{build.xml => nobuild.xml} | 0 references/{build.xml => nobuild.xml} | 0 reusing/{build.xml => nobuild.xml} | 0 strings/{build.xml => nobuild.xml} | 0 DEclipse.py => tools/DEclipse.py | 0 Eclipse.py => tools/Eclipse.py | 0 tools/Examples.py | 239 ++++++++++++++++++ .../FindBugsExcluder.py | 0 .../FindBugsFilter.xml | 0 JavaLint.py => tools/JavaLint.py | 0 .../RedundantImportDetector.py | 0 chapterOrder.xml => tools/chapterOrder.xml | 0 typeinfo/{build.xml => nobuild.xml} | 0 xml/{build.xml => nobuild.xml} | 0 34 files changed, 268 insertions(+), 3 deletions(-) rename access/{build.xml => nobuild.xml} (100%) rename annotations/{build.xml => nobuild.xml} (100%) rename arrays/{build.xml => no.xml} (100%) rename concurrency/{build.xml => nobuild.xml} (100%) rename containers/{build.xml => nobuild.xml} (100%) rename control/{build.xml => nobuild.xml} (100%) rename enumerated/{build.xml => nobuild.xml} (100%) rename exceptions/{build.xml => nobuild.xml} (100%) rename generics/{build.xml => nobuild.xml} (100%) rename holding/{build.xml => nobuild.xml} (100%) rename initialization/{build.xml => nobuild.xml} (100%) rename innerclasses/{build.xml => nobuild.xml} (100%) rename interfaces/{build.xml => nobuild.xml} (100%) rename io/{build.xml => nobuild.xml} (100%) rename net/{build.xml => nobuild.xml} (100%) rename object/{build.xml => nobuild.xml} (100%) rename operators/{build.xml => nobuild.xml} (100%) rename patterns/{build.xml => notbuild.xml} (100%) rename polymorphism/{build.xml => nobuild.xml} (100%) rename references/{build.xml => nobuild.xml} (100%) rename reusing/{build.xml => nobuild.xml} (100%) rename strings/{build.xml => nobuild.xml} (100%) rename DEclipse.py => tools/DEclipse.py (100%) rename Eclipse.py => tools/Eclipse.py (100%) create mode 100644 tools/Examples.py rename FindBugsExcluder.py => tools/FindBugsExcluder.py (100%) rename FindBugsFilter.xml => tools/FindBugsFilter.xml (100%) rename JavaLint.py => tools/JavaLint.py (100%) rename RedundantImportDetector.py => tools/RedundantImportDetector.py (100%) rename chapterOrder.xml => tools/chapterOrder.xml (100%) rename typeinfo/{build.xml => nobuild.xml} (100%) rename xml/{build.xml => nobuild.xml} (100%) diff --git a/Ant-Common.xml b/Ant-Common.xml index e6ee4b40..974129e4 100644 --- a/Ant-Common.xml +++ b/Ant-Common.xml @@ -1,4 +1,4 @@ - + @@ -24,6 +24,22 @@ + + + + + Running: @{cls} + + Finished: @{cls} + -------------------------------- + + + @@ -46,6 +62,7 @@ diff --git a/access/build.xml b/access/nobuild.xml similarity index 100% rename from access/build.xml rename to access/nobuild.xml diff --git a/annotations/build.xml b/annotations/nobuild.xml similarity index 100% rename from annotations/build.xml rename to annotations/nobuild.xml diff --git a/arrays/build.xml b/arrays/no.xml similarity index 100% rename from arrays/build.xml rename to arrays/no.xml diff --git a/build.xml b/build.xml index d77d861f..e803f088 100644 --- a/build.xml +++ b/build.xml @@ -1,10 +1,10 @@ - + Main build.xml for the source code for - Thinking in Java, 4th Edition (Refreshed) by Bruce Eckel + Thinking in Java Director's Cut by Bruce Eckel Code available at http://www.MindView.net See installation instructions in README.md See copyright notice in CopyRight.txt @@ -43,6 +43,15 @@ concurrency/build.xml gui/build.xml swt/build.xml + patterns/build.xml + references/build.xml + assertions/build.xml + database/build.xml + unittesting/build.xml + debugging/build.xml + logging/build.xml + network/build.xml + remote/build.xml "/> diff --git a/concurrency/build.xml b/concurrency/nobuild.xml similarity index 100% rename from concurrency/build.xml rename to concurrency/nobuild.xml diff --git a/containers/build.xml b/containers/nobuild.xml similarity index 100% rename from containers/build.xml rename to containers/nobuild.xml diff --git a/control/build.xml b/control/nobuild.xml similarity index 100% rename from control/build.xml rename to control/nobuild.xml diff --git a/enumerated/build.xml b/enumerated/nobuild.xml similarity index 100% rename from enumerated/build.xml rename to enumerated/nobuild.xml diff --git a/exceptions/build.xml b/exceptions/nobuild.xml similarity index 100% rename from exceptions/build.xml rename to exceptions/nobuild.xml diff --git a/generics/build.xml b/generics/nobuild.xml similarity index 100% rename from generics/build.xml rename to generics/nobuild.xml diff --git a/holding/build.xml b/holding/nobuild.xml similarity index 100% rename from holding/build.xml rename to holding/nobuild.xml diff --git a/initialization/build.xml b/initialization/nobuild.xml similarity index 100% rename from initialization/build.xml rename to initialization/nobuild.xml diff --git a/innerclasses/build.xml b/innerclasses/nobuild.xml similarity index 100% rename from innerclasses/build.xml rename to innerclasses/nobuild.xml diff --git a/interfaces/build.xml b/interfaces/nobuild.xml similarity index 100% rename from interfaces/build.xml rename to interfaces/nobuild.xml diff --git a/io/build.xml b/io/nobuild.xml similarity index 100% rename from io/build.xml rename to io/nobuild.xml diff --git a/net/build.xml b/net/nobuild.xml similarity index 100% rename from net/build.xml rename to net/nobuild.xml diff --git a/object/build.xml b/object/nobuild.xml similarity index 100% rename from object/build.xml rename to object/nobuild.xml diff --git a/operators/build.xml b/operators/nobuild.xml similarity index 100% rename from operators/build.xml rename to operators/nobuild.xml diff --git a/patterns/build.xml b/patterns/notbuild.xml similarity index 100% rename from patterns/build.xml rename to patterns/notbuild.xml diff --git a/polymorphism/build.xml b/polymorphism/nobuild.xml similarity index 100% rename from polymorphism/build.xml rename to polymorphism/nobuild.xml diff --git a/references/build.xml b/references/nobuild.xml similarity index 100% rename from references/build.xml rename to references/nobuild.xml diff --git a/reusing/build.xml b/reusing/nobuild.xml similarity index 100% rename from reusing/build.xml rename to reusing/nobuild.xml diff --git a/strings/build.xml b/strings/nobuild.xml similarity index 100% rename from strings/build.xml rename to strings/nobuild.xml diff --git a/DEclipse.py b/tools/DEclipse.py similarity index 100% rename from DEclipse.py rename to tools/DEclipse.py diff --git a/Eclipse.py b/tools/Eclipse.py similarity index 100% rename from Eclipse.py rename to tools/Eclipse.py diff --git a/tools/Examples.py b/tools/Examples.py new file mode 100644 index 00000000..57bfc087 --- /dev/null +++ b/tools/Examples.py @@ -0,0 +1,239 @@ +#! py -3 +""" +Extract code examples from TIJ4 Refreshed. Extracts from plain text file +""" +from pathlib import Path +import sys, os +import re +import argparse +import shutil +import pprint +destination = Path('.') / "ExtractedExamples" +sourceText = Path('.') / "TIJDirectorsCut.txt" +github = Path(r'C:\Users\Bruce\Documents\GitHub\TIJ-Directors-Cut') + +startBuild = """\ + + + + + + + + + +""" + +endBuild = """\ + + + + +""" + +parser = argparse.ArgumentParser() +parser.add_argument("-e", "--extract", action='store_true', + help="Extract examples from TIJDirectorsCut.txt") +parser.add_argument("-x", "--clean", action='store_true', + help="Remove ExtractedExamples directory") +parser.add_argument("-c", "--compare", action='store_true', + help="Compare files from Github repository to extracted examples") +parser.add_argument("-a", "--ant", action='store_true', + help="Copy ant build files from Github repository to extracted examples") +parser.add_argument("-m", "--makeant", action='store_true', + help="Make ant files that don't exist") + + +def extractExamples(): + if not destination.exists(): + destination.mkdir() + if not sourceText.exists(): + print("Cannot find", sourceText) + sys.exit() + with sourceText.open("rb") as book: + text = book.read().decode("utf-8", "ignore") + for listing in re.findall("^//:.*?///:~", text, re.DOTALL | re.MULTILINE): + title = listing.splitlines()[0] + if "//: as a special marker" in title: + continue + title = title.split()[1] + print(title) + target = destination / Path(title) + if not target.parent.exists(): + target.parent.mkdir(parents=True) + with target.open("w", newline='') as codeListing: + codeListing.writelines(listing) + + +def clean(): + print("clean") + if destination.exists(): + shutil.rmtree(str(destination)) + + +def compareWithGithub(): + leader = len(str(github)) + 1 + githubfiles = [str(file)[leader:] for file in github.glob("**/*")] + githubfiles = [ghf for ghf in githubfiles if not ghf.startswith(".git")] + duplicates = { ghf for ghf in githubfiles if githubfiles.count(ghf) > 1 } + print("duplicates = ", duplicates) + + leader2 = len(str(destination)) + 1 + destfiles = [str(file)[leader2:] for file in destination.glob("**/*")] + duplicates = { ghf for ghf in destfiles if destfiles.count(ghf) > 1 } + print("duplicates = ", duplicates) + + githubfiles = set(githubfiles) + destfiles = set(destfiles) + + print("in githubfiles but not destfiles:") + for f in githubfiles.difference(destfiles): + print("\t", f) + + print("#" * 80) + + print("in destfiles but not githubfiles:") + for f in destfiles.difference(githubfiles): + print("\t", f) + + +def githubDirs(): + leader = len(str(github)) + 1 + buildfiles = [str(file)[leader:] for file in github.glob("**/build.xml")] + return {str((github / f).parent)[leader:] for f in buildfiles} + + +def destDirs(pattern="**"): + leader = len(str(destination)) + 1 + return {str(file)[leader:] for file in destination.glob(pattern)} + + +def copyAntBuildFiles(): + for common in githubDirs().intersection(destDirs()): + print("->", common) + build = github / common / "build.xml" + # print (str(build), build.exists()) + target = destination / common + # print (str(target), target.exists()) + shutil.copy(str(build), str(target)) + shutil.copy(str(github / "Ant-Common.xml"), str(destination)) + + +class CodeFile: + def __init__(self, javaFile): + with javaFile.open() as j: + self.code = j.read() + self.lines = self.code.splitlines() + self.main = None + if "public static void main" in self.code: + self.main = True + self.package = None + if "package " in self.code: + for line in self.lines: + if line.startswith("package ") and line.strip().endswith(";"): + self.package = line + break + self.tagLine = self.lines[0][4:] + self.relpath = '../' + '/'.join(self.tagLine.split('/')[:-1]) + self.name = javaFile.name.split('.')[0] + + def __repr__(self): + result = self.tagLine + if self.package: + result += "\n" + self.package + result += "\n" + return result + # return "\n".join(self.lines) + + def packageName(self): + return self.package.split()[1][:-1] + + def checkPackage(self): + if not self.package: + return True + path = '.'.join(self.tagLine.split('/')[:-1]) + packagePath = self.packageName() + return path == packagePath + + +class Chapter: + def __init__(self, dir): + self.dir = dir + self.code_files = [CodeFile(javaFile) for javaFile in dir.glob("**/*.java")] + self.excludes = [cf.name + ".java" for cf in self.code_files if "{CompileTimeError}" in cf.code] + + def __repr__(self): + result = "-" * 80 + result += "\n" + str(self.dir) + "\n" + result += "-" * 80 + result += "\n" + for cf in self.code_files: + result += str(cf.name) + "\n" + return result + + def checkPackages(self): + for cf in self.code_files: + if not cf.checkPackage(): + print("BAD PACKAGE") + print("\t", cf.tagLine) + print("\t", cf.package) + print("\n".join(cf.lines)) + + def makeBuildFile(self): + buildFile = startBuild % (self.dir.name, " ".join(self.excludes)) + for cf in self.code_files: + if cf.name + ".java" in self.excludes: + continue + print(cf.name) + if cf.main: + if not cf.package: + buildFile += ' \n' % cf.name + else: + buildFile += ' \n' % (cf.packageName() + '.' + cf.name, cf.relpath) + buildFile += endBuild + # print(buildFile) + with (self.dir / "build.xml").open("w") as buildxml: + buildxml.write(buildFile) + + +def createAntFiles(): + chapters = [Chapter(fd) for fd in destination.glob("*") if fd.is_dir() if not (fd / "build.xml").exists()] + # chapters = [Chapter(fd) for fd in destination.glob("*") if fd.is_dir()] + for chapter in chapters: + print(chapter) + chapter.checkPackages() + chapter.makeBuildFile() + + +def default(): + clean() + extractExamples() + copyAntBuildFiles() + createAntFiles() + os.chdir("ExtractedExamples") + + +if __name__ == '__main__': + args = parser.parse_args() + + if not any(vars(args).values()): default() + + if args.extract: + extractExamples() + copyAntBuildFiles() + createAntFiles() + + if args.compare: + compareWithGithub() + + if args.ant: + copyAntBuildFiles() + + if args.makeant: + createAntFiles() + + if args.clean: + clean() diff --git a/FindBugsExcluder.py b/tools/FindBugsExcluder.py similarity index 100% rename from FindBugsExcluder.py rename to tools/FindBugsExcluder.py diff --git a/FindBugsFilter.xml b/tools/FindBugsFilter.xml similarity index 100% rename from FindBugsFilter.xml rename to tools/FindBugsFilter.xml diff --git a/JavaLint.py b/tools/JavaLint.py similarity index 100% rename from JavaLint.py rename to tools/JavaLint.py diff --git a/RedundantImportDetector.py b/tools/RedundantImportDetector.py similarity index 100% rename from RedundantImportDetector.py rename to tools/RedundantImportDetector.py diff --git a/chapterOrder.xml b/tools/chapterOrder.xml similarity index 100% rename from chapterOrder.xml rename to tools/chapterOrder.xml diff --git a/typeinfo/build.xml b/typeinfo/nobuild.xml similarity index 100% rename from typeinfo/build.xml rename to typeinfo/nobuild.xml diff --git a/xml/build.xml b/xml/nobuild.xml similarity index 100% rename from xml/build.xml rename to xml/nobuild.xml