From: Yanick D. <ydu...@ne...> - 2002-07-22 14:44:47
|
Hi there, I posted the following on the user list last week but finally thought it might be better to post it on the dev list: I am currently working on a jythonc Ant task. It works and looks like t= he following: <project name=3D"jythonc" default=3D"main" basedir=3D"."> <taskdef name=3D"jythonc" classname=3D"jython.ant.JythoncTask"/> <target name=3D"main"> <jythonc package=3D"Test" compiler=3D"c:/jdk1.4/bin/javac" jar=3D"${basedir}/hello.jar" workdir=3D"${basedir}" jythonHome=3D"d:/java/jython-2.1" deep=3D"false" basedir=3D"d:/java/jython-2.1/mymodules" includes=3D"**/*.py" /> </target> </project> I would like to be able to pass the compilation classpath and the python.path "dynamically" to jythonc (it looks in the jython registry b= y default), so that jythonc compilation can fully be integrated in our bu= ild process; is that possible? Thanks, I'm almost there... (BTW, below is t= he code for the task... ). public class JythoncTask extends MatchingTask{ static final String PACKAGE =3D "--package"; static final String JAR =3D "--jar"; static final String WORKDIR =3D "--workdir"; static final String COMPILER =3D "--compiler"; static final String COMPILEROPTS =3D "--compileropts"; static final String SKIP =3D "--skip"; static final String DEEP =3D "--deep"; static final String CORE =3D "--core"; static final String ALL =3D "--all"; public String _package; public String _jar; public File _workdir; public File _jythonHome; public File _compiler; public File _basedir; public boolean _deep; public boolean _core; // implies the deep option public boolean _all; // implies the deep option public void setPackage(String packg){ _package =3D packg; } public void setJar(String jar){ _jar =3D jar; } public void setWorkdir(File work){ _workdir =3D work; } public void setCompiler(File comp){ _compiler =3D comp; } public void setJythonHome(File home){ _jythonHome =3D home; } public void setCore(boolean core){ _core =3D core; } public void setDeep(boolean deep){ _deep =3D deep; } public void setBasedir(File basedir){ _basedir =3D basedir; } public void execute() throws BuildException{ assertDir(_workdir, "workdir"); assertDir(_basedir, "basedir"); assertDir(_jythonHome, "jythonHome"); DirectoryScanner ds =3D getDirectoryScanner(_basedir); String[] modules =3D ds.getIncludedFiles(); String[] skip =3D ds.getExcludedFiles(); String[] args =3D getCommandArgs(modules, skip); Process p; try{ p =3D Runtime.getRuntime().exec(args, null, _basedir); }catch(IOException e){ throw new BuildException(e); } BufferedReader reader =3D new BufferedReader(new InputStreamReader(p.getInputStream())); String line; try{ while((line =3D reader.readLine()) !=3D null){ System.out.println(line); } }catch(IOException e){ throw new BuildException(e); }finally{ if(reader !=3D null){ try{ reader.close(); }catch(IOException e){ //noop } } } int exitValue; try{ exitValue =3D p.waitFor(); }catch(InterruptedException e){ throw new BuildException(e); }finally{ if(reader !=3D null){ try{ reader.close(); }catch(IOException e){ //noop } } } if(exitValue !=3D 0){ throw new BuildException("jythonc compilation failed"); } } private String[] getCommandArgs(String[] modules, String[] skip){ ArrayList args =3D new ArrayList(100); args.add(_jythonHome.getAbsolutePath() + File.separator + "jythonc.bat"); args.add(COMPILER); args.add(getCompilerPath()); if(_deep){ args.add("" + DEEP); } if(_deep && _core){ args.add("" + CORE); } if(_deep && _all){ args.add("" + ALL); } args.add(WORKDIR); args.add(_workdir.getAbsolutePath()); args.add(JAR); args.add(_jar); if(skip.length > 0){ args.add(SKIP); StringBuffer toSkip =3D new StringBuffer(); for(int i =3D 0; i < skip.length; i++){ toSkip.append(_basedir + File.separator + skip[i]); if(i < skip.length - 1){ toSkip.append(","); } } args.add(toSkip.toString()); } for(int i =3D 0; i < modules.length; i++){ args.add(_basedir + File.separator + modules[i]); } return (String[])args.toArray(new String[args.size()]); } private String getCompilerPath(){ if(_compiler !=3D null){ return _compiler.getAbsolutePath(); } else{ return System.getProperty("java.home") + File.separator + "bin" + File.separator + "javac"; } } private File assertDir(File dir, String attributeName) throws BuildException{ if(dir =3D=3D null){ throw new BuildException(attributeName + "not set"); } if(!dir.isDirectory()){ throw new BuildException(dir + " is not a directory"); } return dir; } } Yanick Duchesne Software Architect Architecte d'applications Newtrade Technologies Inc. ydu...@ne... tel +1 514 286 8180 x 226 -----------------------------------------------------------------------= ----- ---- If you have received this message in error, please notify the sender immediately and delete the original without making a copy, disclosing i= ts contents or taking any action based thereon. Si vous avez re=E7u ce message par erreur, veuillez en aviser imm=E9dia= tement le signataire et effacer l'original, sans en tirer de copie, en d=E9voiler= le contenu ni prendre quelque mesure fond=E9e sur celui-ci. |