Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > hpw to convert a linux python script ?

Reply
Thread Tools

hpw to convert a linux python script ?

 
 
Stef Mientki
Guest
Posts: n/a
 
      02-11-2009
hello,

I've a python script, written for some Linux version,
now I want to run it under windows.
It complains of not finding files in
/usr/share/tinybldLin/ ....
where .... is the directory where the script is located and started from.

As there are a whole lot of these lines, in a whole lot of files,
I wonder if there's a simple trick to point
/usr/share/tinybldLin/
to my directory ?


thanks,
Stef
 
Reply With Quote
 
 
 
 
Alec Schueler
Guest
Posts: n/a
 
      02-11-2009
On Feb 11, 7:58*pm, Stef Mientki <(E-Mail Removed)> wrote:
> As there are a whole lot of these lines, in a whole lot of files,
> I wonder if there's a simple trick to point
> * /usr/share/tinybldLin/
> to my directory ?
>
> thanks,
> Stef


Find and replace?
 
Reply With Quote
 
 
 
 
Stef Mientki
Guest
Posts: n/a
 
      02-11-2009
Alec Schueler wrote:
> On Feb 11, 7:58 pm, Stef Mientki <(E-Mail Removed)> wrote:
>
>> As there are a whole lot of these lines, in a whole lot of files,
>> I wonder if there's a simple trick to point
>> /usr/share/tinybldLin/
>> to my directory ?
>>
>> thanks,
>> Stef
>>

>
> Find and replace?
>

well I was thinking of a more elegant way,
so the scripts remains, or better become multi-platform.

anyway thanks,
Stef
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
rdmurray@bitdance.com
Guest
Posts: n/a
 
      02-11-2009
Stef Mientki <(E-Mail Removed)> wrote:
> Alec Schueler wrote:
> > On Feb 11, 7:58 pm, Stef Mientki <(E-Mail Removed)> wrote:
> >
> >> As there are a whole lot of these lines, in a whole lot of files,
> >> I wonder if there's a simple trick to point
> >> /usr/share/tinybldLin/
> >> to my directory ?
> >>
> >> thanks,
> >> Stef
> >>

> >
> > Find and replace?
> >

> well I was thinking of a more elegant way,
> so the scripts remains, or better become multi-platform.
>
> anyway thanks,


Well, you'll have to rewrite the script so that it doesn't make
assumptions about path names, and we can't help you with that without
seeing some of the script code. There _might_ be easy fixes, but if the
path is hardcoded in many places in the script...then you will just have
to go through and un-hard-code it.

--RDM

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      02-11-2009
On Feb 12, 7:48*am, Stef Mientki <(E-Mail Removed)> wrote:
> Alec Schueler wrote:
> > On Feb 11, 7:58 pm, Stef Mientki <(E-Mail Removed)> wrote:

>
> >> As there are a whole lot of these lines, in a whole lot of files,
> >> I wonder if there's a simple trick to point
> >> * /usr/share/tinybldLin/
> >> to my directory ?

>
> >> thanks,
> >> Stef

>
> > Find and replace?

>
> well I was thinking of a more elegant way,
> so the scripts remains, or better become multi-platform.


Your current setup is not even single-platform portable ... consider
the scenario where some Linux admin wants your gear to live in some
directory other than /usr/share/tinybldLin/

The standard technique for ensuring that kind of portability involves:
1. Find the directory in which your script lives. Inspect sys.argv[0],
use some os.path.some_functionality() to extract the directory, name
it (say) home_base.
2. When you want to open a data file in the script's directory, do
this:
the_path = os.path.join(home_base, 'stef1.dat')
f = open(the_path, .....)

NOTE: Use os.path.join() instead of some DIY technique; it knows
better than you what the path separator is on the OS it's being run
on. In fact, read the whole of the os.path manual carefully before you
jump in to changing your code.

If you prefer to have the data files in (say) .../script_locn/data
instead of .../script_locn, the adjustment should be obvious.

Unfortunately, you are still going to need use find and replace to fix
your script. Code in haste, repent at leisure

HTH,
John



 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      02-12-2009
John Machin wrote:
> On Feb 12, 7:48 am, Stef Mientki <(E-Mail Removed)> wrote:
>> Alec Schueler wrote:
>>> On Feb 11, 7:58 pm, Stef Mientki <(E-Mail Removed)> wrote:
>>>> As there are a whole lot of these lines, in a whole lot of files,
>>>> I wonder if there's a simple trick to point
>>>> /usr/share/tinybldLin/
>>>> to my directory ?
>>>> thanks,
>>>> Stef
>>> Find and replace?

>> well I was thinking of a more elegant way,
>> so the scripts remains, or better become multi-platform.

>
> Your current setup is not even single-platform portable ... consider
> the scenario where some Linux admin wants your gear to live in some
> directory other than /usr/share/tinybldLin/
>
> The standard technique for ensuring that kind of portability involves:
> 1. Find the directory in which your script lives. Inspect sys.argv[0],
> use some os.path.some_functionality() to extract the directory, name
> it (say) home_base.
> 2. When you want to open a data file in the script's directory, do
> this:
> the_path = os.path.join(home_base, 'stef1.dat')
> f = open(the_path, .....)
>
> NOTE: Use os.path.join() instead of some DIY technique; it knows
> better than you what the path separator is on the OS it's being run
> on. In fact, read the whole of the os.path manual carefully before you
> jump in to changing your code.
>
> If you prefer to have the data files in (say) .../script_locn/data
> instead of .../script_locn, the adjustment should be obvious.
>
> Unfortunately, you are still going to need use find and replace to fix
> your script. Code in haste, repent at leisure
>

Of course the real issue here is that the same hard-coded directory name
(or possibly multiple directory names all rooted in the same directory)
are scattered throughout the program. Your (the OP's) first task should
be to store that "root" directory in a variable and then use the
variable in place of all current uses of the root directory.

For example, I have a Django project that I like to run under both
Windows and Cygwin. So in my settings.py file I have:

# Django settings for PyTeach project.
import sys
if sys.platform == 'cygwin':
fsroot = "/home/sholden"
elif sys.platform == 'win32':
fsroot = "C:/Users/sholden/Documents"
else:
sys.exit("Only configured for Windows and Cygwin")

Than further down I have

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = fsroot+'/Projects/PytDj/images/'

Note that most Windows APIs allow you to use the forward slash as a
delimiter. It's mostly the command line and Windows Explorer that are
snotty about insisting on the backslash.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
Stephen Hansen
Guest
Posts: n/a
 
      02-13-2009
> # Absolute path to the directory that holds media.
> # Example: "/home/media/media.lawrence.com/"
> MEDIA_ROOT = fsroot+'/Projects/PytDj/images/'
>
> Note that most Windows APIs allow you to use the forward slash as a
> delimiter. It's mostly the command line and Windows Explorer that are
> snotty about insisting on the backslash.


Idle curiosity, why do you prefer that to:

MEDIA_ROOT = os.path.join(fsroot, 'Projects', 'PytDj', 'images')

Not arguing that one is actually better then the other, just curious.

In my own environment we have grown a whole big filesystem abstraction
layer because we have to be able to address the same resources on
network volumes in the same client on multiple OS's (mainly windows
and mac presently, but a linux client has long been a
consideration),... and on the mac having to sometimes use mac paths
(":this:thatther") and sometimes posix paths ("/this/that/other")
depending on what API layer I'm poking at in a given point of view...
so personally I just end up being mildly curious how everyone else
handles paths and such.

Cuz it was quite a bit of a pain early on.

--S
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      02-15-2009
Stephen Hansen wrote:
>> # Absolute path to the directory that holds media.
>> # Example: "/home/media/media.lawrence.com/"
>> MEDIA_ROOT = fsroot+'/Projects/PytDj/images/'
>>
>> Note that most Windows APIs allow you to use the forward slash as a
>> delimiter. It's mostly the command line and Windows Explorer that are
>> snotty about insisting on the backslash.

>
> Idle curiosity, why do you prefer that to:
>
> MEDIA_ROOT = os.path.join(fsroot, 'Projects', 'PytDj', 'images')
>
> Not arguing that one is actually better then the other, just curious.
>

Going for "good enough", I suppose. It's more readable, at least, not
that that makes a *huge* difference.

> In my own environment we have grown a whole big filesystem abstraction
> layer because we have to be able to address the same resources on
> network volumes in the same client on multiple OS's (mainly windows
> and mac presently, but a linux client has long been a
> consideration),... and on the mac having to sometimes use mac paths
> (":this:thatther") and sometimes posix paths ("/this/that/other")
> depending on what API layer I'm poking at in a given point of view...
> so personally I just end up being mildly curious how everyone else
> handles paths and such.
>
> Cuz it was quite a bit of a pain early on.


The old Mac paths would indeed require your solution, which is strictly
speaking the best.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Terminate a python script from linux shell / bash script Gros Bedo Python 0 07-13-2008 06:59 PM
Re: Terminate a python script from linux shell / bash script norseman Python 0 07-10-2008 09:44 PM
RE: Terminate a python script from linux shell / bash script Gros Bedo Python 1 07-10-2008 08:50 PM
Terminate a python script from linux shell / bash script Gros Bedo Python 2 07-10-2008 08:42 AM
Hpw make lists that are easy to sort. Anton Vredegoor Python 10 03-31-2007 07:56 AM



Advertisments