www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.process.system and friends

reply "Gor Gyolchanyan" <gor.f.gyolchanyan gmail.com> writes:
Good day,

I'm trying to write a script in D for building D projects. I want 
to set the environment variable LIB for optlink to be able to 
pick up library files from where I put them after compiling 
dependencies.

The problem is, that the std.process.setenv is only available 
under Linux and std.process.system("set LIB=target") does not do 
anything. I guess std.process.system creates a separate process 
every time, sets the local environment variable and exists, 
erasing the local environment variable.
I tried supplying multiple commands at once by specifying a 
multi-line argument to std.process.system, but it didn't work at 
all.

How can I set my local envoronment variables so that further 
calls to std.process.system can pick  it up?

Regards,
Gor Gyolchanyan.
Feb 02 2013
next sibling parent Rainer Schuetze <r.sagitario gmx.de> writes:
On 02.02.2013 12:41, Gor Gyolchanyan wrote:
 Good day,

 I'm trying to write a script in D for building D projects. I want to set
 the environment variable LIB for optlink to be able to pick up library
 files from where I put them after compiling dependencies.

 The problem is, that the std.process.setenv is only available under
 Linux and std.process.system("set LIB=target") does not do anything. I
 guess std.process.system creates a separate process every time, sets the
 local environment variable and exists, erasing the local environment
 variable.
 I tried supplying multiple commands at once by specifying a multi-line
 argument to std.process.system, but it didn't work at all.

 How can I set my local envoronment variables so that further calls to
 std.process.system can pick  it up?
Have you tried putenv? It is only declared in core.sys.posix.stdlib, but the dmc-runtime-library must have it as dmd is also using it.
Feb 02 2013
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/02/2013 03:41 AM, Gor Gyolchanyan wrote:

 How can I set my local envoronment variables so that further calls to
 std.process.system can pick it up?
std.process has the nice AA-like 'environment': import std.process; void main() { environment["MY_ENV_VAR"] = "42"; system("echo $MY_ENV_VAR"); // <-- prints "42" assert(shell("echo $MY_ENV_VAR") == "42\n"); } There is also 'setenv' in that module but I find 'environment' more convenient. (Also, 'setenv' is evil because it has a bool parameter. ;)) Ali
Feb 03 2013
parent reply "Peter Sommerfeld" <noreply rubrica.at> writes:
    pathSeparator ~ path  ~ pathToMyDir   Ali =C7ehreli wrote:

 import std.process;

 void main()
 {
      environment["MY_ENV_VAR"] =3D "42";

      system("echo $MY_ENV_VAR");  // <-- prints "42"
      assert(shell("echo $MY_ENV_VAR") =3D=3D "42\n");
 }

 There is also 'setenv' in that module but I find 'environment' more  =
 convenient. (Also, 'setenv' is evil because it has a bool parameter. ;=
)) Win7, dmd 2.061: environment seems to be read only. string path =3D environment["PATH"]; // OK environment("PATH"] =3D pathSeparator ~ path ~ pathToMyDir ; // failed, unidentified identifier setenv("PATH", pathSeparator ~ path ~ pathToMyDir, true); // failed, undefined identifier Do you know a way to set it via system/shell calls, notably under window= s? Peter
Feb 03 2013
next sibling parent reply "HeiHon" <heiko.honrath web.de> writes:
On Sunday, 3 February 2013 at 19:05:01 UTC, Peter Sommerfeld 
wrote:
 Do you know a way to set it via system/shell calls, notably 
 under windows?

 Peter
You might want to use Tango: module env; // 2013-01-08 // dmd2.061 + Siegelord Tango-D2-d2port import tango.sys.Environment; import tango.io.Stdout; void main(string[] args) { string VAR = "TESTENVVAR"; string VAL1 = "VAL1"; string VAL2 = "VAL2"; assert(Environment.get(VAR) is null); Environment.set(VAR, VAL1); assert(Environment.get(VAR) == VAL1); Environment.set(VAR, ""); assert(Environment.get(VAR) is null); Environment.set(VAR, VAL2); assert(Environment.get(VAR) == VAL2); Environment.set(VAR, null); assert(Environment.get(VAR) is null); Environment.set(VAR, VAL1); foreach (key, value; Environment.get) Stdout.formatln("'{}' = '{}'", key, value); foreach(i, arg; args) { auto p = Environment.exePath (cast (char[]) arg); Stdout.formatln("exePath(arg[{}] '{}') = '{}'", i, arg, p); } }
Feb 06 2013
parent reply "Peter Sommerfeld" <noreply rubrica.at> writes:
HeiHon <heiko.honrath web.de> schrieb:

 On Sunday, 3 February 2013 at 19:05:01 UTC, Peter Sommerfeld wrote:
 Do you know a way to set it via system/shell calls, notably under  
 windows?

 Peter
You might want to use Tango: module env; // 2013-01-08 // dmd2.061 + Siegelord Tango-D2-d2port
 import tango.sys.Environment;
Hmmm, AFAIK it is outdated, isn't it ? I also hesitate to introduce major dependencies for this small point. Can you point me to the sources of this Tango version please. May be I can reuse a small part of it. Thanks, Peter
Feb 06 2013
parent reply "HeiHon" <heiko.honrath web.de> writes:
 Hmmm, AFAIK it is outdated, isn't it ? I also hesitate to 
 introduce
 major dependencies for this small point.
It was originally for D1, but SiegeLord ported almost all of it to D2.
 Can you point me to the sources of this Tango version please.
 May be I can reuse a small part of it.
https://github.com/SiegeLord/Tango-D2 There are still some very useful things in Tango that you don't find in Phobos (e.g. logging) and it plays nicely together with Phobos.
Feb 06 2013
parent "Peter Sommerfeld" <noreply rubrica.at> writes:
Am 06.02.2013, 14:51 Uhr, schrieb HeiHon <heiko.honrath web.de>:
 https://github.com/SiegeLord/Tango-D2

 There are still some very useful things in Tango that you don't find in  
 Phobos (e.g. logging) and it plays nicely together with Phobos.
Thanks, interesting read. Will have a deeper look in it later. Peter
Feb 06 2013
prev sibling parent reply notna <notna.remove.this ist-einmalig.de> writes:
Hi Peter.


This works for me on Win7 with DMD2.061

    http://dpaste.dzfl.pl/64529e76


in this example here you are mixing (] parentheses :O Don't think this 
is your real code, is it :O

              |      |
              V      V
 environment("PATH"] = pathSeparator ~ path ~ pathToMyDir ;
 // failed, unidentified identifier
Feb 07 2013
parent reply "Peter Sommerfeld" <noreply rubrica.at> writes:
notna wrote:
 This works for me on Win7 with DMD2.061

     http://dpaste.dzfl.pl/64529e76
Yes that works, but that was not quite the question. The point is you cannot *set* the path variable. But meanwhile I think it isn't a good idea anyway. The PATH belongs to the user/system, not to programs. Otherwise that may introduce some harm...
 environment("PATH"] = pathSeparator ~ path ~ pathToMyDir ;
 // failed, unidentified identifier
Yep, typo! Peter
Feb 07 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/07/2013 02:58 PM, Peter Sommerfeld wrote:

 The point is you cannot *set* the path variable.
 But meanwhile I think it isn't a good idea anyway.
 The PATH belongs to the user/system, not to programs.
 Otherwise that may introduce some harm...
The environment is private to the running process (unless some of the variables are "exported" to child processes of that process.) There is no danger of affecting the system. Ali
Feb 07 2013
parent reply "Peter Sommerfeld" <noreply rubrica.at> writes:
Ali =C7ehreli wrote:

 On 02/07/2013 02:58 PM, Peter Sommerfeld wrote:

  > The point is you cannot *set* the path variable.
  > But meanwhile I think it isn't a good idea anyway.
  > The PATH belongs to the user/system, not to programs.
  > Otherwise that may introduce some harm...

 The environment is private to the running process (unless some of the =
=
 variables are "exported" to child processes of that process.) There is=
=
 no danger of affecting the system.
Regarding the PATH win7 seems not to thinks so. May be on *nix? Peter
Feb 07 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/07/2013 04:37 PM, Peter Sommerfeld wrote:
 Ali Çehreli wrote:

 On 02/07/2013 02:58 PM, Peter Sommerfeld wrote:

 The point is you cannot *set* the path variable.
 But meanwhile I think it isn't a good idea anyway.
 The PATH belongs to the user/system, not to programs.
 Otherwise that may introduce some harm...
The environment is private to the running process (unless some of the variables are "exported" to child processes of that process.) There is no danger of affecting the system.
Regarding the PATH win7 seems not to thinks so. May be on *nix? Peter
We are talking about changing the PATH environment variable from inside a program right? I don't use win7 so I have to take your word for it but I am very surprised. I would understand if we are talking about changing the PATH variable that gets copied to the environment of each process but setting the variable in a process should stay with that process. Ali
Feb 07 2013
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 07 Feb 2013 20:45:49 -0500, Ali =C3=87ehreli <acehreli yahoo.com=
 wrote:
 On 02/07/2013 04:37 PM, Peter Sommerfeld wrote:
  > Ali =C3=87ehreli wrote:
  >
  >> On 02/07/2013 02:58 PM, Peter Sommerfeld wrote:
  >>
  >> > The point is you cannot *set* the path variable.
  >> > But meanwhile I think it isn't a good idea anyway.
  >> > The PATH belongs to the user/system, not to programs.
  >> > Otherwise that may introduce some harm...
  >>
  >> The environment is private to the running process (unless some of =
the
  >> variables are "exported" to child processes of that process.) Ther=
e =
 is
  >> no danger of affecting the system.
  >
  > Regarding the PATH win7 seems not to thinks so. May be on *nix?
  >
  > Peter

 We are talking about changing the PATH environment variable from insid=
e =
 a program right? I don't use win7 so I have to take your word for it b=
ut =
 I am very surprised.
"exporting" is a feature of the shell in unix. Exporting means that the= variable will be copied to child processes, and does NOT affect parent processes or any other processes. It is private to the process, and is "passed on" to children (via one of the exec functions). If the parent changes the variable after passing to the child, the child's copy does n= ot change. Windows is actually EXACTLY the same, except you don't use getenv and = putenv, you use SetEnvironmentVariable and GetEnvironmentVariable. You = = can use the former if you want to use the C compatibility layer, but = that's only if you use all compatibility layer functions for everything = = (executing processes etc.) See here = http://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=3Dvs.= 85).aspx Likely the reason why the call is failing is because of an incorrect = implementation in phobos. -Steve
Feb 07 2013
parent reply "Peter Sommerfeld" <noreply rubrica.at> writes:
Steven Schveighoffer schrieb:
 Windows is actually EXACTLY the same, except you don't use getenv and  
 putenv, you use SetEnvironmentVariable and GetEnvironmentVariable.  You  
 can use the former if you want to use the C compatibility layer, but  
 that's only if you use all compatibility layer functions for everything  
 (executing processes etc.)
How is the latter done ? A Version switch to posix? Peter
Feb 08 2013
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 08 Feb 2013 04:17:02 -0500, Peter Sommerfeld <noreply rubrica.at>  
wrote:

 Steven Schveighoffer schrieb:
 Windows is actually EXACTLY the same, except you don't use getenv and  
 putenv, you use SetEnvironmentVariable and GetEnvironmentVariable.  You  
 can use the former if you want to use the C compatibility layer, but  
 that's only if you use all compatibility layer functions for everything  
 (executing processes etc.)
How is the latter done ? A Version switch to posix?
Actually, D on Windows uses DMC as its C runtime, so you cannot use any MSVC C runtime functions, such as _putenv. So this is not an option for you. If DMC has equivalent functions, you will have to look them up. -Steve
Feb 08 2013
prev sibling parent "bioinfornatics" <bioinfornatics gmail.com> writes:
 I'm trying to write a script in D for building D projects.
They are dbuilder who do this . you can fork it https://github.com/dbuilder-developers/dbuilder
Feb 09 2013