Running a msys script from a Windows prompt

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Running a msys script from a Windows prompt

Paul Moore
I want to write a Python script in Windows that fires off a shell script to run under msys. A simple test case is easy - I just use Python's subprocess module to run

C:\Msys\bin\sh C:\MyScripts\script.sh

However, this does not seem to set up the msys shell environment correctly - gcc is not in the PATH. I can't use --login, as that changes directory to my msys home directory and I want the script to run in my current directory under Windows. The problem is basically that the msys /etc/profile does some essential things (setting PATH) but also some unnecessary ones (changing directory).

I can't easily just use --login and change directory in my script, as to do that I'd need to convert the current directory name from Windows form to msys form (there's no equivalent of cygpath under msys - is that right?)

The best solution I have been able to find so far is to edit my build script to start with the lines

x=$(pwd)
. /etc/profile
cd $x

That's a bit ugly (as it requires changing my script in a way that isn't consistent with how I'd run it on msys directly) but appears to work.

Is that a reasonable approach, or is there a better one I could be using?

Thanks,
Paul.

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Running a msys script from a Windows prompt

lrn-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 24.05.2013 17:32, Paul Moore wrote:
> I can't easily just use --login and change directory in my script, as to do
> that I'd need to convert the current directory name from Windows form to
> msys form (there's no equivalent of cygpath under msys - is that right?)
MSys understands DOSish paths just fine - as long as you use '/' as the
directory separator (so, do path_variable.replace ('\\', '/') and you're
good).

>
> The best solution I have been able to find so far is to edit my build
> script to start with the lines
>
> x=$(pwd)
> . /etc/profile
> cd $x
>
> That's a bit ugly (as it requires changing my script in a way that isn't
> consistent with how I'd run it on msys directly) but appears to work.
>
> Is that a reasonable approach?
I do this:

argv = os.environ['BUILDSLAVE_SHELL'].split()
# Create a temporary script file that changes current directory
# and runs the command we want
tf, tf_name = tempfile.mkstemp ()
f = os.fdopen (tf, 'wb')
fcontents = '#!/bin/sh\ncd {}\n{}'.format (
    re.sub(r'(?<!\\) ','\\ ', workdir.replace('\\','/')),
    ' '.join (command))
f.write (fcontents)
f.close ()
argv += [tf_name.replace('\\','/')]

p = subprocess.Popen (argv)

and once it finishes:

try:
  os.remove (tf_name)
except:
  pass


(BUILDSLAVE_SHELL is set to "c:\....\msys\bin\sh.exe --login -c")
("command" is the command you want to run, as a list)
("workdir" is the directory to run the command in)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)

iQEcBAEBAgAGBQJRn2+gAAoJEOs4Jb6SI2CwsgQIALEWT372PVYs8Vh7knaiUgj8
mvyjMUJdG4VLztPfaJ9OnN6GXwOx5aV6EUaBaWXJuXwCJziB/y+plaDF0IlPtqRn
2br7ZSL6eYCQWkQ6867jF8H6d6N8mIav/nMKT4FW8AtBAajp6/tBE7JnCNZdFpBg
fAw0sBr8reowwnEU+zkMD2dm+TdD4fIltFXLx+kGczyy+ixa2vmXYV813UAqfmV6
D8C7T1ThbWBOn/xAbAPMbbENO22W42kc1eLSKKnk9KwCGc4L0xDcn6bdkO7c2p5x
wqEpOYTNlgmlaGG2B4mwvyQgRNBVrG3wtyE50i6vcjUFxXlSE7D2XIpmF8QYkPc=
=yyE+
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Running a msys script from a Windows prompt

Paul Moore
On 24 May 2013 14:48, LRN <[hidden email]> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On <a href="tel:24.05.2013%2017" value="+12405201317">24.05.2013 17:32, Paul Moore wrote:
> I can't easily just use --login and change directory in my script, as to do
> that I'd need to convert the current directory name from Windows form to
> msys form (there's no equivalent of cygpath under msys - is that right?)
MSys understands DOSish paths just fine - as long as you use '/' as the
directory separator (so, do path_variable.replace ('\\', '/') and you're
good).

Ah, OK. I was using standard backslashes - I hadn't realised that I could use / and not bother about converting drive letters to /c/.
 
> The best solution I have been able to find so far is to edit my build
> script to start with the lines
>
> x=$(pwd)
> . /etc/profile
> cd $x
>
> That's a bit ugly (as it requires changing my script in a way that isn't
> consistent with how I'd run it on msys directly) but appears to work.
>
> Is that a reasonable approach?
I do this:

argv = os.environ['BUILDSLAVE_SHELL'].split()
# Create a temporary script file that changes current directory
# and runs the command we want
tf, tf_name = tempfile.mkstemp ()
f = os.fdopen (tf, 'wb')
fcontents = '#!/bin/sh\ncd {}\n{}'.format (
    re.sub(r'(?<!\\) ','\\ ', workdir.replace('\\','/')),
    ' '.join (command))
f.write (fcontents)
f.close ()
argv += [tf_name.replace('\\','/')]

p = subprocess.Popen (argv)

and once it finishes:

try:
  os.remove (tf_name)
except:
  pass


(BUILDSLAVE_SHELL is set to "c:\....\msys\bin\sh.exe --login -c")
("command" is the command you want to run, as a list)
("workdir" is the directory to run the command in)

Cool, that's similar to what I'm doing, then, but including --login and undoing the unwanted directory change afterwards. Sounds like it's about as good as I can expect, then.

Paul

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe