Return-Path: Mailing-List: contact ant-dev-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list ant-dev@jakarta.apache.org Received: (qmail 28802 invoked from network); 29 Nov 2000 07:01:04 -0000 Received: from postbox.viquity.com (HELO dcsrv0.ecom2ecom.com) (63.198.126.137) by locus.apache.org with SMTP; 29 Nov 2000 07:01:04 -0000 Received: by dcsrv0 with Internet Mail Service (5.5.2650.21) id ; Tue, 28 Nov 2000 22:59:42 -0800 Message-ID: <635802DA64D4D31190D500508B9B04104E0FC4@dcsrv0> From: Jose Alberto Fernandez To: "Ant Project List (E-mail)" Subject: [PATCH] Obtaining the environement variables and propagating them Date: Tue, 28 Nov 2000 22:59:41 -0800 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2650.21) Content-Type: multipart/mixed; boundary="----_=_NextPart_000_01C059D1.F26E0F70" X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_000_01C059D1.F26E0F70 Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C059D1.F26E0F70" ------_=_NextPart_001_01C059D1.F26E0F70 Content-Type: text/plain; charset="iso-8859-1" Hi, As promissed, here is a first shot at geting the environment and propagating it properly during the operations. The patch modifies Execute.java which is the lowest level. Added a new attribute to ExecTask "newenvironment='true'" if you do not want propagation. Added attribute "environment='prefix'" to to indicate to load the properties from the environment: I have tested which calls the methods on Execute to do the job, so most should be tested at this point. But I only have NT, someone needs to verify other environments and in particular set the correct values for the command on the MAC if there are any. Hope is useful.... Jose Alberto ------_=_NextPart_001_01C059D1.F26E0F70 Content-Type: text/html; charset="iso-8859-1"
Hi,
 
As promissed, here is a first shot at geting the environment and propagating it properly
during the <exec*> operations.
 
The patch modifies Execute.java which is the lowest level. Added a new attribute to ExecTask
"newenvironment='true'" if you do not want propagation.
 
Added attribute "environment='prefix'" to <property/> to indicate to load the properties from the
environment:
 
   <property environment="ant.env" />
 
I have tested <property> which calls the methods on Execute to do the job, so most should be
tested at this point. But I only have NT, someone needs to verify other environments and in particular
set the correct values for the command on the MAC if there are any.
 
Hope is useful....
 
Jose Alberto

 
------_=_NextPart_001_01C059D1.F26E0F70-- ------_=_NextPart_000_01C059D1.F26E0F70 Content-Type: application/octet-stream; name="Jose Alberto Fernandez.vcf" Content-Disposition: attachment; filename="Jose Alberto Fernandez.vcf" BEGIN:VCARD VERSION:2.1 N:Fernandez;Jose FN:Jose Alberto Fernandez ORG:Viquity;Engineering TITLE:Architect Software Development TEL;WORK;VOICE:408-548-9755 ADR;WORK:;Sunnyvale;1161 N Fair Oaks Ave;Sunnyvale;CA;94089;USA LABEL;WORK;ENCODING=QUOTED-PRINTABLE:Sunnyvale=0D=0A1161 N Fair Oaks Ave=0D=0ASunnyvale, CA 94089=0D=0AUSA EMAIL;PREF;INTERNET:JFernandez@viquity.com REV:20000629T183010Z END:VCARD ------_=_NextPart_000_01C059D1.F26E0F70 Content-Type: text/plain; name="env_patch.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="env_patch.txt" Index: docs/index.html =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /home/cvspublic/jakarta-ant/docs/index.html,v retrieving revision 1.154 diff -u -r1.154 index.html --- docs/index.html 2000/11/27 07:54:34 1.154 +++ docs/index.html 2000/11/29 06:48:57 @@ -1701,6 +1701,12 @@ returncode other than 0. No + + newenvironment + Do not propagate old environment when new = environment + variables are specified. + No, default is = false +

Examples

@@ -1820,6 +1826,12 @@ No + newenvironment + Do not propagate old environment when new = environment + variables are specified. + No, default is = false + + parallel Run the command only once, appending all files = as arguments. Defaults to true. If false, command will be executed @@ -3456,7 +3468,7 @@ property cannot be set, and will be ignored. This means that = properties set outside the current project always override the properties of the = current project.

-

There are four ways to set properties:

+

There are five ways to set properties:

  • By supplying both the name and value = attribute.
  • By supplying both the name and refid = attribute.
  • @@ -3466,6 +3478,9 @@
  • By setting the resource attribute with the resource name = of the property file to load. This property file has the format as = defined by the file used in the class java.util.Properties.
  • +
  • By setting the environment attribute with a prefix to = use. + Properties will be defined for every environment variable by + prefixing the suplied name and a period to the name of the = variable.

Although combinations of the three ways are possible, only one = should be used at a time. Problems might occur with the order in which properties are = set, for Index: src/main/org/apache/tools/ant/taskdefs/ExecTask.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecT= ask.java,v retrieving revision 1.9 diff -u -r1.9 ExecTask.java --- src/main/org/apache/tools/ant/taskdefs/ExecTask.java 2000/10/31 = 11:12:04 1.9 +++ src/main/org/apache/tools/ant/taskdefs/ExecTask.java 2000/11/29 = 06:49:02 @@ -74,6 +74,7 @@ private File out; private File dir; protected boolean failOnError =3D false; + protected boolean newEnvironment =3D false; private Integer timeout =3D null; private Environment env =3D new Environment(); protected Commandline cmdl =3D new Commandline(); @@ -132,6 +133,13 @@ } =20 /** + * Use a completely new environment + */ + public void setNewenvironment(boolean newenv) { + newEnvironment =3D newenv; + } + + /** * Add a nested env element - an environment variable. */ public void addEnv(Environment.Variable var) { @@ -198,6 +206,7 @@ Project.MSG_VERBOSE); } } + exe.setNewenvironment(newEnvironment); exe.setEnvironment(environment); return exe; } Index: src/main/org/apache/tools/ant/taskdefs/Execute.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Execu= te.java,v retrieving revision 1.9 diff -u -r1.9 Execute.java --- src/main/org/apache/tools/ant/taskdefs/Execute.java 2000/11/25 = 02:38:36 1.9 +++ src/main/org/apache/tools/ant/taskdefs/Execute.java 2000/11/29 = 06:49:02 @@ -63,8 +63,12 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.BufferedReader; +import java.io.StringReader; +import java.io.ByteArrayOutputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Vector; =20 =20 /** @@ -84,9 +88,11 @@ private ExecuteWatchdog watchdog; private File workingDirectory =3D null; private Project project =3D null; + private boolean newEnvironment =3D false; =20 private static String antWorkingDirectory =3D = System.getProperty("user.dir"); private static CommandLauncher launcher =3D = createCommandLauncher(); + private static Vector procEnvironment =3D null; =20 /**=20 * Builds a command launcher for the OS and JVM we are running = under @@ -139,6 +145,71 @@ } =20 /** + * Find the list of environment variables for this process. + */ + public static synchronized Vector getProcEnvironment() { + if (procEnvironment !=3D null) return procEnvironment; + + procEnvironment =3D new Vector(); + try { + ByteArrayOutputStream out =3D new ByteArrayOutputStream(); + Execute exe =3D new Execute(new PumpStreamHandler(out)); + exe.setCommandline(getProcEnvCommand()); + // Make sure we do not recurse forever + exe.setNewenvironment(true); + int retval =3D exe.execute(); + if ( retval !=3D 0 ) { + // Just try to use what we got + } + BufferedReader in =3D=20 + new BufferedReader(new StringReader(out.toString())); + String line; + while ((line =3D in.readLine()) !=3D null) { + procEnvironment.addElement(line); + } + }=20 + catch (java.io.IOException exc) { + // Just try to see how much we got + } + return procEnvironment; + } + + private static String[] getProcEnvCommand() { + String osname =3D System.getProperty("os.name").toLowerCase(); + if ( osname.indexOf("mac os") >=3D 0 ) { + // Mac + // TODO: I have no idea how to get it, someone must fix it + String[] cmd =3D null; + return cmd; + } + else if ( osname.indexOf("os/2") >=3D 0 ) { + // OS/2 - use same mechanism as Windows 2000 + // Not sure + String[] cmd =3D {"cmd", "/c", "set" }; + return cmd; + } + else if ( osname.indexOf("windows") >=3D 0 ) { + // Determine if we're running under 2000/NT or 98/95 + if ( osname.indexOf("nt") >=3D 0 || osname.indexOf("2000") = >=3D 0 ) { + // Windows 2000/NT + String[] cmd =3D {"cmd", "/c", "set" }; + return cmd; + } + else { + // Windows 98/95 - need to use an auxiliary script + String[] cmd =3D {"command", "/c", "set" }; + return cmd; + } + } + else { + // Generic UNIX + // Alternatively one could use: /bin/sh -c env + String[] cmd =3D {"/usr/bin/env"}; + return cmd; + } + } + + /** * Creates a new execute object using = PumpStreamHandler for * stream handling. */ @@ -191,12 +262,22 @@ } =20 /** - * Returns the commandline used to create a subprocess. + * Set whether to propagate the default environment or not. * - * @return the commandline used to create a subprocess + * @param newenv whether to propagate the process environment. */ + public void setNewenvironment(boolean newenv) { + newEnvironment =3D newenv; + } + + /** + * Returns the environment used to create a subprocess. + * + * @return the environment used to create a subprocess + */ public String[] getEnvironment() { - return env; + if (env =3D=3D null || newEnvironment) return env; + return patchEnvironment(); } =20 =20 @@ -275,6 +356,30 @@ =20 protected int getExitValue() { return exitValue; + } + + /** + * Patch the current environment with the new values from the = user. + * @return the patched environment + */ + private String[] patchEnvironment() { + Vector osEnv =3D (Vector) getProcEnvironment().clone(); + for (int i =3D 0; i < env.length; i++) { + int pos =3D env[i].indexOf('=3D'); + // Get key including "=3D" + String key =3D env[i].substring(0, pos+1); + int size =3D osEnv.size(); + for (int j =3D 0; j < size; j++) { + if (((String)osEnv.elementAt(j)).startsWith(key)) { + osEnv.removeElementAt(j); + break; + } + } + osEnv.addElement(env[i]); + } + String[] result =3D new String[osEnv.size()]; + osEnv.copyInto(result); + return result; } =20 /** Index: src/main/org/apache/tools/ant/taskdefs/Property.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: = /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Prope= rty.java,v retrieving revision 1.19 diff -u -r1.19 Property.java --- src/main/org/apache/tools/ant/taskdefs/Property.java 2000/10/13 = 09:14:37 1.19 +++ src/main/org/apache/tools/ant/taskdefs/Property.java 2000/11/29 = 06:49:02 @@ -73,6 +73,7 @@ protected String value; protected File file; protected String resource; + protected String env; protected Reference ref =3D null; =20 protected boolean userProperty=3Dfalse; // set read-only = properties @@ -121,6 +122,14 @@ return resource; } =20 + public void setEnvironment(String env) { + this.env =3D env; + } + + public String getEnvironment() { + return env; + } + public void setUserProperty(boolean userProperty) { this.userProperty =3D userProperty; } @@ -139,6 +148,8 @@ =20 if (resource !=3D null) loadResource(resource); =20 + if (env !=3D null) loadEnvironment(env); + if ((name !=3D null) && (ref !=3D null)) { Object obj =3D ref.getReferencedObject(getProject()); if (obj !=3D null) { @@ -183,6 +194,24 @@ props.load(is); addProperties(props); } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + protected void loadEnvironment( String prefix ) { + Properties props =3D new Properties(); + if (!prefix.endsWith(".")) prefix +=3D "."; + log("Loading Environment " + prefix, Project.MSG_VERBOSE); + try { + Vector osEnv =3D Execute.getProcEnvironment(); + for (Enumeration e =3D osEnv.elements(); = e.hasMoreElements(); ) { + String entry =3D (String)e.nextElement(); + int pos =3D entry.indexOf('=3D'); + props.put(prefix + entry.substring(0, pos),=20 + entry.substring(pos + 1)); + } + addProperties(props); } catch (Exception ex) { ex.printStackTrace(); } ------_=_NextPart_000_01C059D1.F26E0F70--