Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
parse an environment file
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  19 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jason Friedman  
View profile  
 More options Sep 30 2012, 8:11 pm
Newsgroups: comp.lang.python
From: Jason Friedman <ja...@powerpull.net>
Date: Sun, 30 Sep 2012 18:11:09 -0600
Local: Sun, Sep 30 2012 8:11 pm
Subject: parse an environment file
$ crontab -l
* * * * * env

This produces mail with the following contents:

HOME=/home/spjsf
LOGNAME=spjsf
PATH=/usr/bin:/bin
PWD=/home/spjsf
SHELL=/bin/sh
SHLVL=1
USER=spjsf
_=/usr/bin/env

On the other hand

$ env

produces about 100 entries, most of which are provided by my .bashrc;
cron provides only a limited number of environment variables.

I want my python 3.2.2 script, called via cron, to know what those
additional variables are.  How?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Sep 30 2012, 8:58 pm
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Sun, 30 Sep 2012 20:58:15 -0400
Local: Sun, Sep 30 2012 8:58 pm
Subject: Re: parse an environment file
On 09/30/2012 08:11 PM, Jason Friedman wrote:

First comment:  I don't know anything about an "environment file."  An
environment is an attribute of a process, and it's inherited by
subprocesses that process launches.  This is not a Python thing, it's an
OS thing, for whatever OS you're running.

> $ crontab -l
> * * * * * env

> This produces mail with the following contents:

> HOME=/home/spjsf
> LOGNAME=spjsf
> PATH=/usr/bin:/bin
> PWD=/home/spjsf
> SHELL=/bin/sh
> SHLVL=1
> USER=spjsf
> _=/usr/bin/env

In other words, these are the environment variables that cron has, and
that it gives to any children it launches.

> On the other hand

> $ env

> produces about 100 entries, most of which are provided by my .bashrc;
> cron provides only a limited number of environment variables.

Gee, those are variables specific to a particular (bash) shell, so how
would a program launched by cron know about them?

> I want my python 3.2.2 script, called via cron, to know what those
> additional variables are.  How?

You'd have to change the environment that cron sees (before login), or
add to the crontab, or /etc/default/cron, or else run some shell from
cron that defines those variables and then runs the application.  The
way to do this is specific to whatever OS you're running, and is
irrelevant to Python.

Try googling.  For some examples, none of which use Python:

http://www.perlmonks.org/?node_id=624540
http://www.unix.com/solaris/164483-environment-variables-cron.html
http://www.computing.net/answers/unix/how-to-set-environment-variable...
http://linuxshellaccount.blogspot.com/2007/10/crontab-and-your-enviro...

--

DaveA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Sep 30 2012, 9:17 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 01 Oct 2012 01:17:15 GMT
Local: Sun, Sep 30 2012 9:17 pm
Subject: Re: parse an environment file

On Sun, 30 Sep 2012 18:11:09 -0600, Jason Friedman wrote:
> $ crontab -l
> * * * * * env

> This produces mail with the following contents:

[snip]

Yes, env returns the environment variables of the current environment.

> On the other hand

> $ env

> produces about 100 entries, most of which are provided by my .bashrc;
> cron provides only a limited number of environment variables.

That's because it's a different environment.

> I want my python 3.2.2 script, called via cron, to know what those
> additional variables are.  How?

In general, you can't, because they may not even exist when your script
runs. There's no guarantee that "your environment" (which one? you might
have many, or none) exists at the time, and env certainly cannot guess
which one that might be.

But specifically, if you know the ID of a process, you may be able to see
that process' environment variables by reading the /proc/<PID>/environ
virtual file, which is a NULL-delimited list of environment variables.

As usual, permissions apply. In general, you can read your own processes,
but not those of other users.

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 1 2012, 12:20 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Mon, 1 Oct 2012 14:20:55 +1000
Local: Mon, Oct 1 2012 12:20 am
Subject: Re: parse an environment file

On Mon, Oct 1, 2012 at 10:11 AM, Jason Friedman <ja...@powerpull.net> wrote:
> $ env

> produces about 100 entries, most of which are provided by my .bashrc;
> cron provides only a limited number of environment variables.

> I want my python 3.2.2 script, called via cron, to know what those
> additional variables are.  How?

Looking into my crystal ball, I'm wondering if perhaps what you're
asking is for your cron job to have environment variables that aren't
set by cron, ones that you can see in your bash environment. This is a
common issue (usually with $PATH), and by no means Python-specific. A
quick web search will bring up some results, for instance:

http://www.google.com/search?q=cron+environment+variables
http://duckduckgo.com/?q=cron+environment+variables
http://www.bing.com/search?q=cron+environment+variables

If this isn't what you're asking about, please consider clarifying
your question :)

Chris Angelico


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ulrich Eckhardt  
View profile  
 More options Oct 1 2012, 6:15 am
Newsgroups: comp.lang.python
From: Ulrich Eckhardt <ulrich.eckha...@dominolaser.com>
Date: Mon, 01 Oct 2012 12:00:04 +0200
Local: Mon, Oct 1 2012 6:00 am
Subject: Re: parse an environment file
Am 01.10.2012 02:11, schrieb Jason Friedman:
> $ crontab -l
> * * * * * env

> This produces mail with the following contents:

[...]
> SHELL=/bin/sh

         ^^^^^^^
[...]

> On the other hand

> $ env

> produces about 100 entries, most of which are provided by my .bashrc;

bash != sh

Instead of running a script in default POSIX shell, you might be able to
run it in bash, which will then read your ~/.bashrc (verify that from
the docs, I'm not 100% sure). Maybe it is as easy as changing the first
line to '#!/bin/bash'.

> I want my python 3.2.2 script, called via cron, to know what those
> additional variables are.

To be honest, I would reconsider the approach. You could patch the cron
invokation, but that still won't fix any other invokations like starting
it from a non-bash shell, filemanager, atd etc. You could instead set
these variables in a different place that is considered by more
applications. I wonder if maybe ~/.profile would be such a place.

Alternatively, assuming these environment variables are just for your
Python program, you could store these settings in a separate
configuration file instead. Environment variables are always a bit like
using globals instead of function parameters.

Good luck!

Uli


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alain Ketterlin  
View profile  
 More options Oct 1 2012, 6:16 am
Newsgroups: comp.lang.python
From: Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
Date: Mon, 01 Oct 2012 12:16:00 +0200
Subject: Re: parse an environment file

Jason Friedman <ja...@powerpull.net> writes:

[...]

> I want my python 3.2.2 script, called via cron, to know what those
> additional variables are.  How?

This is not a python question. Have a look at the crontab(5) man page,
it's all explained there.

-- Alain.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jason Friedman  
View profile  
 More options Oct 1 2012, 10:13 am
Newsgroups: comp.lang.python
From: Jason Friedman <ja...@powerpull.net>
Date: Mon, 1 Oct 2012 08:12:50 -0600
Local: Mon, Oct 1 2012 10:12 am
Subject: Re: parse an environment file

> I want my python 3.2.2 script, called via cron, to know what those
> additional variables are.  How?

Thank you for the feedback.  A crontab line of

* * * * * . /path/to/export_file && /path/to/script.py

does indeed work, but for various reasons this approach will not
always be available to me.

Let me restate my question.  I have a file that looks like this:
export VAR1=foo
export VAR2=bar
# Comment
export VAR3=${VAR1}${VAR2}

I want this:
my_dict = {'VAR1': 'foo', 'VAR2': 'bar', 'VAR3': 'foobar'}

I can roll my own, but I'm thinking there is a module or existing code
that does this.  I looked at the os and sys and configparse modules
but did not see it.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 1 2012, 10:21 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Tue, 2 Oct 2012 00:21:45 +1000
Local: Mon, Oct 1 2012 10:21 am
Subject: Re: parse an environment file

On Tue, Oct 2, 2012 at 12:12 AM, Jason Friedman <ja...@powerpull.net> wrote:
> Let me restate my question.  I have a file that looks like this:
> export VAR1=foo
> export VAR2=bar
> # Comment
> export VAR3=${VAR1}${VAR2}

> I want this:
> my_dict = {'VAR1': 'foo', 'VAR2': 'bar', 'VAR3': 'foobar'}

> I can roll my own, but I'm thinking there is a module or existing code
> that does this.  I looked at the os and sys and configparse modules
> but did not see it.

Is there a reason to use that format, rather than using Python
notation? I've at times made config files that simply get imported.
Instead of a dictionary, you'd have a module object:

# config.py
VAR1='foo'
VAR2='bar'
VAR3=VAR1+VAR2

# main file
import config as my_dict

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 1 2012, 10:42 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Tue, 2 Oct 2012 00:41:58 +1000
Local: Mon, Oct 1 2012 10:41 am
Subject: Re: parse an environment file

On Tue, Oct 2, 2012 at 12:37 AM, Jason Friedman <ja...@powerpull.net> wrote:
>> Is there a reason to use that format, rather than using Python
>> notation? I've at times made config files that simply get imported.
>> Instead of a dictionary, you'd have a module object:

>> # config.py
>> VAR1='foo'
>> VAR2='bar'
>> VAR3=VAR1+VAR2

> There is a reason:  /path/to/export_file exists for Bash scripts, too,
> and I do not think I could get Bash to read config.py in the format
> stated above.  I want to maintain only one file.

(Responding on-list and hoping it was merely oversight that had that
email come to me personally)

Ah, fair enough. Well, since you're using the full range of bash
functionality, the only viable way to parse it is with bash itself.
I'd recommend going with the version you have above:

> * * * * * . /path/to/export_file && /path/to/script.py

Under what circumstances is this not an option? That'd be the next
thing to consider.

Alternatively, you may want to consider making your own config file
format. If you consciously restrict yourself to a severe subset of
bash functionality, you could easily parse it in Python - for
instance, always look for "export %s=%s" with simple strings for the
variable name and value.

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Oct 1 2012, 11:29 am
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Mon, 1 Oct 2012 08:29:22 -0700 (PDT)
Local: Mon, Oct 1 2012 11:29 am
Subject: Re: parse an environment file

I think one can ues some decorators to wrap OS  or platform
dependent functions.

I am sure someone did that long time ago as the iron python
wrapped dot-net.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Oct 1 2012, 11:29 am
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Mon, 1 Oct 2012 08:29:22 -0700 (PDT)
Local: Mon, Oct 1 2012 11:29 am
Subject: Re: parse an environment file

I think one can ues some decorators to wrap OS  or platform
dependent functions.

I am sure someone did that long time ago as the iron python
wrapped dot-net.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans Mulder  
View profile  
 More options Oct 1 2012, 11:35 am
Newsgroups: comp.lang.python
From: Hans Mulder <han...@xs4all.nl>
Date: Mon, 01 Oct 2012 17:35:04 +0200
Local: Mon, Oct 1 2012 11:35 am
Subject: Re: parse an environment file
On 1/10/12 16:12:50, Jason Friedman wrote:

One tactic is to write a wrapper script in shellese that sets the
variables and then runs your script.  Something like:

#/bin/bash
export VAR1=foo
export VAR2=bar
# Comment
export VAR3=${VAR1}${VAR2}

# Read some more settings from a file
. /path/to/file/with/more/exports

# Drum roll .....
/path/to/your/script.py

This allows you to copy-and-paste all sorts of weird and wonderful
shell syntax into your wrapper script.

AFAIK, there is no Python module that can read shell syntax.
You could translate all that shell syntax manually to Python,
but that may not be worth the effort.

Hope this helps,

-- HansM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
xDog Walker  
View profile  
 More options Oct 2 2012, 12:04 pm
Newsgroups: comp.lang.python
From: xDog Walker <thud...@gmail.com>
Date: Tue, 2 Oct 2012 09:03:47 -0700
Local: Tues, Oct 2 2012 12:03 pm
Subject: Re: parse an environment file
On Monday 2012 October 01 08:35, Hans Mulder wrote:

> AFAIK, there is no Python module that can read shell syntax.

The stdlib's shlex might be that module.

--
Yonder nor sorghum stenches shut ladle gulls stopper torque wet
strainers.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Oct 2 2012, 12:16 pm
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Tue, 2 Oct 2012 09:16:09 -0700 (PDT)
Local: Tues, Oct 2 2012 12:16 pm
Subject: Re: parse an environment file

shlex can only split shell code into tokens.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ramchandra Apte  
View profile  
 More options Oct 2 2012, 12:16 pm
Newsgroups: comp.lang.python
From: Ramchandra Apte <maniandra...@gmail.com>
Date: Tue, 2 Oct 2012 09:16:09 -0700 (PDT)
Local: Tues, Oct 2 2012 12:16 pm
Subject: Re: parse an environment file

shlex can only split shell code into tokens.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jason Friedman  
View profile  
 More options Oct 2 2012, 11:49 pm
Newsgroups: comp.lang.python
From: Jason Friedman <ja...@powerpull.net>
Date: Tue, 2 Oct 2012 21:49:21 -0600
Local: Tues, Oct 2 2012 11:49 pm
Subject: Re: parse an environment file
> Ah, fair enough. Well, since you're using the full range of bash
> functionality, the only viable way to parse it is with bash itself.
> I'd recommend going with the version you have above:

>> * * * * * . /path/to/export_file && /path/to/script.py

> Under what circumstances is this not an option? That'd be the next
> thing to consider.

> Alternatively, you may want to consider making your own config file
> format. If you consciously restrict yourself to a severe subset of
> bash functionality, you could easily parse it in Python - for
> instance, always look for "export %s=%s" with simple strings for the
> variable name and value.

Thank you, Chris, off-list post unintentional.  It may be the case
that I do not maintain /path/to/export_file; I might just be allowed
to read it.
Based on your responses and everyone's responses I'm guessing that
what I am doing is sufficiently novel that there is no canned
solution.  I looked at shlex but did not see how that would be
helpful.
Thank you all for your thoughts.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 3 2012, 12:04 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Wed, 3 Oct 2012 14:03:59 +1000
Local: Wed, Oct 3 2012 12:03 am
Subject: Re: parse an environment file

On Wed, Oct 3, 2012 at 1:49 PM, Jason Friedman <ja...@powerpull.net> wrote:
> Based on your responses and everyone's responses I'm guessing that
> what I am doing is sufficiently novel that there is no canned
> solution.  I looked at shlex but did not see how that would be
> helpful.

The only canned solution for parsing a bash script is bash. Think
about it the other way around: If you wanted to have a Python variable
made available to a bash script, the obvious thing to do is to invoke
Python. It's the same thing.

I recommend going with Hans Mulder's suggestion of a wrapper script;
that seems to be the cleanest option available.

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jason Friedman  
View profile  
 More options Oct 6 2012, 1:14 pm
Newsgroups: comp.lang.python
From: Jason Friedman <ja...@powerpull.net>
Date: Sat, 6 Oct 2012 11:14:47 -0600
Local: Sat, Oct 6 2012 1:14 pm
Subject: Re: parse an environment file

> The only canned solution for parsing a bash script is bash. Think
> about it the other way around: If you wanted to have a Python variable
> made available to a bash script, the obvious thing to do is to invoke
> Python. It's the same thing.

I scratched my own itch:
http://code.activestate.com/recipes/578280-parse-profile/.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Oct 6 2012, 1:33 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sun, 7 Oct 2012 04:33:16 +1100
Local: Sat, Oct 6 2012 1:33 pm
Subject: Re: parse an environment file

On Sun, Oct 7, 2012 at 4:14 AM, Jason Friedman <ja...@powerpull.net> wrote:
>> The only canned solution for parsing a bash script is bash. Think
>> about it the other way around: If you wanted to have a Python variable
>> made available to a bash script, the obvious thing to do is to invoke
>> Python. It's the same thing.

> I scratched my own itch:
> http://code.activestate.com/recipes/578280-parse-profile/.

And there you have it! A subset of bash syntax and a Python parser. Job done!

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »