1+ #!/usr/bin/python
2+ """
3+ Eclipse.py by Bruce Eckel, for Thinking in Java 4e
4+ Modify or insert package statments so that Eclipse is happy with the code tree.
5+ Run this with no arguments from the root of the code tree.
6+
7+ The Ant build will not work once you run this program!
8+
9+ You may also want to modify the dotproject and dotclasspath text below.
10+
11+ You must have Python 2.3 installed to run this program. See www.python.org.
12+ """
13+ import os
14+
15+ os .remove ("reusing/Lisa.java" );
16+
17+ for path , dirs , files in os .walk ('.' ):
18+ for file in files :
19+ if file .endswith (".java" ):
20+ filepath = path + os .sep + file
21+ firstLine = open (filepath ).readline ().strip ()
22+ tagPath = firstLine .split ()[1 ]
23+ tagPath = "." .join (tagPath .split ('/' )[:- 1 ])
24+ packageStatement = "package " + tagPath + ";"
25+ code = open (filepath ).readlines ()
26+ found = False
27+ for line in code :
28+ if line .startswith ("package " ):
29+ found = True
30+ if not found :
31+ code .insert (1 , packageStatement + " /* Added by Eclipse.py */\n " )
32+ open (filepath , 'w' ).writelines (code )
33+
34+ here = os .path .abspath ('.' ).replace ("\\ " , "/" )
35+ if here .startswith ("/cygdrive/" ): # If using cygwin
36+ here = here .replace ("/cygdrive/" , "" , 1 )
37+ here = here [0 ] + ":" + here [1 :]
38+ print "here" , here
39+ open (".classpath" , 'w' ).write (\
40+ """<?xml version="1.0" encoding="UTF-8"?>
41+ <classpath>
42+ <classpathentry excluding="polymorphism/|holding/|flow/|exceptions/|concurrency/|typeinfo/|innerclasses/|arrays/|interfaces/|reusing/|initialization/|cloning/|io/|containers/|generics/|xml/|hiding/|io/xfiles/|passing/|gui/|annotations/|enumerated/|discovering/|object/|strings/|swt/" kind="src" path=""/>
43+ <classpathentry kind="src" path="annotations"/>
44+ <classpathentry kind="src" path="arrays"/>
45+ <classpathentry kind="src" path="cloning"/>
46+ <classpathentry kind="src" path="concurrency"/>
47+ <classpathentry kind="src" path="containers"/>
48+ <classpathentry kind="src" path="discovering"/>
49+ <classpathentry kind="src" path="enumerated"/>
50+ <classpathentry kind="src" path="exceptions"/>
51+ <classpathentry kind="src" path="flow"/>
52+ <classpathentry kind="src" path="generics"/>
53+ <classpathentry kind="src" path="gui"/>
54+ <classpathentry kind="src" path="hiding"/>
55+ <classpathentry kind="src" path="holding"/>
56+ <classpathentry kind="src" path="initialization"/>
57+ <classpathentry kind="src" path="innerclasses"/>
58+ <classpathentry kind="src" path="interfaces"/>
59+ <classpathentry excluding="xfiles/" kind="src" path="io"/>
60+ <classpathentry kind="src" path="io/xfiles"/>
61+ <classpathentry kind="src" path="object"/>
62+ <classpathentry kind="src" path="passing"/>
63+ <classpathentry kind="src" path="polymorphism"/>
64+ <classpathentry kind="src" path="reusing"/>
65+ <classpathentry kind="src" path="strings"/>
66+ <classpathentry kind="src" path="swt"/>
67+ <classpathentry kind="src" path="typeinfo"/>
68+ <classpathentry kind="src" path="xml"/>
69+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.5.0_01"/>
70+ <classpathentry kind="output" path="bin"/>
71+ </classpath>
72+ """ ) # % (here, here))
73+
74+ open (".project" , 'w' ).write (\
75+ """<?xml version="1.0" encoding="UTF-8"?>
76+ <projectDescription>
77+ <name>TIJ4</name>
78+ <comment></comment>
79+ <projects>
80+ </projects>
81+ <buildSpec>
82+ <buildCommand>
83+ <name>org.eclipse.jdt.core.javabuilder</name>
84+ <arguments>
85+ </arguments>
86+ </buildCommand>
87+ </buildSpec>
88+ <natures>
89+ <nature>org.eclipse.jdt.core.javanature</nature>
90+ </natures>
91+ </projectDescription>
92+
93+ """ )
94+
95+ if not os .path .exists (".settings" ):
96+ os .mkdir (".settings" )
97+ os .chdir (".settings" )
98+ open ("org.eclipse.jdt.core.prefs" , 'w' ).write (\
99+ """#Fri Jan 14 11:03:37 MST 2005
100+ org.eclipse.jdt.core.compiler.debug.localVariable=generate
101+ org.eclipse.jdt.core.compiler.compliance=1.5
102+ org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
103+ org.eclipse.jdt.core.compiler.debug.sourceFile=generate
104+ org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
105+ org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
106+ eclipse.preferences.version=1
107+ org.eclipse.jdt.core.compiler.debug.lineNumber=generate
108+ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
109+ org.eclipse.jdt.core.compiler.source=1.5
110+ org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
111+ """ )
112+
113+ print """Project ready to be opened with Eclipse (see www.Eclipse.org)
114+ Use DEclipse.py if you want to go back to building with Ant."""
115+
0 commit comments