1 54 package org.python.util; 56 57 import org.apache.tools.ant.taskdefs.MatchingTask; 58 import org.apache.tools.ant.BuildException; 59 import org.apache.tools.ant.taskdefs.Java; 60 import org.apache.tools.ant.types.Path; 61 import org.apache.tools.ant.DirectoryScanner; 62 63 import java.io.File ; 64 65 79 public class JythoncAntTask extends MatchingTask { 80 81 protected static final String JYTHONC_PY = "Tools/jythonc/jythonc.py"; 82 protected static final String JYTHON_CLASS = "org.python.util.jython"; 83 84 87 protected Path classpath; 88 89 92 protected String packageName; 93 94 98 protected File jarFile; 99 100 103 protected File jythoncpy; 104 105 109 protected boolean deep; 110 111 116 protected boolean core; 117 118 122 protected boolean all; 123 124 128 protected String addpackages; 129 130 133 protected File jarFileBean; 134 135 139 protected String skipModule; 140 141 144 protected String compiler; 145 146 152 protected String compileropts; 153 154 158 protected String falsenames; 159 160 163 protected File jythonHome; 164 165 168 protected File destDir; 169 170 173 protected File srcDir; 174 175 179 protected File workdir; 180 181 184 protected String extraArgs; 185 186 189 public JythoncAntTask() { 190 setIncludes("**/*.py"); 191 } 192 193 198 public Path createClasspath() { 199 if(classpath == null) { 200 classpath = new Path(this.getProject()); 201 } 202 return classpath.createPath(); 203 } 204 205 209 public void setClasspath(Path aClasspath) { 210 classpath = aClasspath; 211 } 212 213 217 public void setPackage(String aString) { 218 packageName = aString; 219 } 220 221 225 public void setJar(File aJarFile) { 226 jarFile = aJarFile; 227 deep = true; 228 } 229 230 235 public void setCore(boolean aValue) { 236 core = aValue; 237 deep = true; 238 } 239 240 244 public void setAll(boolean aValue) { 245 all = aValue; 246 deep = true; 247 } 248 249 252 public void setBean(File aJarFileBean) { 253 jarFileBean = aJarFileBean; 254 } 255 256 260 public void setSkip(String aValue) { 261 skipModule = aValue; 262 } 263 264 268 public void setDeep(boolean aValue) { 269 deep = aValue; 270 } 271 272 273 277 public void setAddpackages(String aValue) { 278 addpackages = aValue; 279 } 280 281 285 public void setWorkdir(File aValue) { 286 if( aValue.exists() ) { 287 if( ! aValue.isDirectory() ) { 288 throw new BuildException( "Workdir ("+ aValue + ") is not a directory" ); 289 } 290 } else { 291 aValue.mkdirs(); 292 } 293 workdir = aValue; 294 } 295 296 299 public void setCompiler(String aCompiler) { 300 compiler = aCompiler; 301 } 302 303 307 public void setCompileropts(String aValue) { 308 compileropts = aValue; 309 } 310 311 315 public void setFalsenames(String aValue) { 316 falsenames = aValue; 317 } 318 319 322 public void setHome(File aFile) { 323 jythonHome = aFile; 324 } 325 326 329 public void setSrcdir(File aFile) { 330 srcDir = aFile; 331 } 332 333 336 public void setDestdir(File aFile) { 337 destDir = aFile; 338 } 339 340 343 public void setJythoncpy(File aValue) { 344 jythoncpy = aValue; 345 } 346 347 350 public void setArgs(String aValue) { 351 extraArgs = aValue; 352 } 353 354 357 public String getCompilerOptions() { 358 StringBuffer aStringBuffer = new StringBuffer (); 359 if( destDir != null ) { 360 aStringBuffer.append("-d \""); 361 aStringBuffer.append( destDir ); 362 aStringBuffer.append("\""); 363 364 createClasspath().setLocation(destDir); 365 destDir.mkdirs(); 366 } 367 if( compileropts != null ) { 368 aStringBuffer.append(compileropts); 369 } 370 if( aStringBuffer.length() == 0 ) { 371 return null; 372 } else { 373 return aStringBuffer.toString(); 374 } 375 } 376 377 380 public File getPythonHome() { 381 if(jythonHome == null ) { 382 String aPythonHome = getProject().getProperty("python.home"); 383 if(aPythonHome == null ) { 384 throw new BuildException("No python.home or home specified"); 385 } 386 jythonHome = new File (aPythonHome); 387 } 388 return jythonHome; 389 } 390 391 394 public File getJythoncPY() { 395 if(jythoncpy == null ) { 396 return new File (getPythonHome(),JYTHONC_PY); 397 } 398 return jythoncpy; 399 } 400 401 402 405 public void execute() { 406 try { 407 Java javaTask = null; 408 409 javaTask = (Java)getProject().createTask("java"); 410 javaTask.setTaskName("jythonc"); 411 412 javaTask.setClassname( JYTHON_CLASS ); 413 414 javaTask.createJvmarg().setValue( "-Dpython.home=" + getPythonHome() ); 415 416 File aJythonJarFile = new File (getPythonHome(), "jython.jar" ); 418 createClasspath().setLocation(aJythonJarFile); 419 420 javaTask.setClasspath(classpath); 421 422 javaTask.createArg().setFile( getJythoncPY() ); 424 425 if( packageName != null ) { 426 javaTask.createArg().setValue("--package"); 427 javaTask.createArg().setValue(packageName); 428 } 429 430 if( jarFile != null ) { 431 javaTask.createArg().setValue( "--jar" ); 432 javaTask.createArg().setFile( jarFile ); 433 } 434 435 if(deep) { 436 javaTask.createArg().setValue( "--deep" ); 437 } 438 439 if(core) { 440 javaTask.createArg().setValue( "--core" ); 441 } 442 443 if(all) { 444 javaTask.createArg().setValue( "--all" ); 445 } 446 447 if( jarFileBean != null ) { 448 javaTask.createArg().setValue( "--bean" ); 449 javaTask.createArg().setFile( jarFileBean ); 450 } 451 452 if( addpackages != null ) { 453 javaTask.createArg().setValue( "--addpackages " ); 454 javaTask.createArg().setValue( addpackages ); 455 } 456 457 if( workdir != null ) { 458 javaTask.createArg().setValue( "--workdir " ); 459 javaTask.createArg().setFile( workdir ); 460 } 461 462 if( skipModule != null ) { 463 javaTask.createArg().setValue("--skip"); 464 javaTask.createArg().setValue(skipModule); 465 } 466 467 if( compiler == null ) { 469 String buildCompiler = getProject().getProperty("build.compiler"); 472 if (buildCompiler != null && buildCompiler.equals("jikes")) { 473 javaTask.createArg().setValue("--compiler"); 474 javaTask.createArg().setValue("jikes"); 475 } 476 } else { 477 javaTask.createArg().setValue("--compiler"); 478 javaTask.createArg().setValue(compiler); 479 } 480 481 String aCompilerOpts = getCompilerOptions(); 482 if( aCompilerOpts != null ) { 483 javaTask.createArg().setValue("--compileropts"); 484 javaTask.createArg().setValue(aCompilerOpts); 485 } 486 487 if( falsenames != null ) { 488 javaTask.createArg().setValue("--falsenames"); 489 javaTask.createArg().setValue(falsenames); 490 } 491 492 if( extraArgs != null ) { 493 javaTask.createArg().setLine(extraArgs); 494 } 495 496 if( srcDir == null ) { 498 srcDir = project.resolveFile("."); 499 } 500 DirectoryScanner scanner = super.getDirectoryScanner(srcDir); 501 String [] dependencies = scanner.getIncludedFiles(); 502 log("compiling " + dependencies.length + " file" + 503 ((dependencies.length == 1)?"":"s")); 504 String baseDir = scanner.getBasedir().toString() + File.separator; 505 for (int i = 0; i < dependencies.length; i++) { 507 String targetFile = dependencies[i]; 508 javaTask.createArg().setValue(baseDir + targetFile); 509 } 510 511 javaTask.setDir(srcDir); 513 javaTask.setFork(true); 514 if (javaTask.executeJava() != 0) { 515 throw new BuildException("jythonc reported an error"); 516 } 517 } catch (Exception e) { 518 String msg = "Exception while calling " + JYTHON_CLASS + ". Details: " + e.toString(); 520 throw new BuildException(msg, e); 521 } 522 } 523 } 524 525 | Popular Tags |