0

Some context

I need to emulate the behaviour of the shell command source but in Python. Specifically sourcing tcsh scripts, but it would be nice for it to be as generic as possible.

I've seen this answer where it makes the shell run env to print all environment variables.

My question

env or printenv only prints environment variables but not local variables. However the tcsh scripts I want to source use commands like

set FOO=bar

So is there a way in tcsh (and other shells, just for completeness) to print all local variables?

Edit: Yes I know I'd have to run TCSH as a subprocess for it to parse the script, and yes python can't read into TCSH's memory which is why I want a way to print all local variables within TCSH so Python can read them.

  • 2
    Emulating `source` is essentially emulating the shell itself, since `source` simply means to execute each command in a file in the current shell. – chepner Jun 09 '22 at 19:00
  • It occurs to me perhaps to alias `set` to `setenv` before sourcing it... Idk if that would work, would have to try – Abraham Murciano Benzadon Jun 09 '22 at 19:01
  • 1
    Environment variables are language-agnostic, but most shells provide access to the environment using their own variables as the interface. – chepner Jun 09 '22 at 19:01
  • @chepner yes I am aware of what source does, I just can't find any reference on how to access TCSH's local variables – Abraham Murciano Benzadon Jun 09 '22 at 19:02
  • Unless you are actually running an instance of `tcsh`, you can't, short of parsing the file yourself. – chepner Jun 09 '22 at 19:03
  • I wouldn't assume tcsh to _have_ such an interface. csh's history re: design for programmatic use is... [not](https://www.grymoire.com/unix/CshTop10.txt) [good](https://web.mit.edu/ghudson/info/csh.whynot). – Charles Duffy Jun 09 '22 at 19:04
  • @chepner yes, I'd be running an instance of TCSH, as described in the answer I linked – Abraham Murciano Benzadon Jun 09 '22 at 19:04
  • ("and other shells, just for completeness" arguably makes this too broad to be on-topic; but common POSIX-family shells absolutely do have mechanisms to query defined variables which can be used to build an export interface). – Charles Duffy Jun 09 '22 at 19:04
  • Then you need some form of interprocess communication; a Python script can't simply reach into a `tcsh` process and grabs parts of its memory (at least, not on any operating system I would trust using). – chepner Jun 09 '22 at 19:05
  • @CharlesDuffy if it was up to me I wouldn't have my company set TCSH as everyone's default shell... – Abraham Murciano Benzadon Jun 09 '22 at 19:05
  • @chepner as shown in the answer I linked, I plan on launching tcsh as a subprocess which sources the script in question and then prints all its local variables in an easily parseable format from within the tcsh process, then pipe its stdout to python so I can read that – Abraham Murciano Benzadon Jun 09 '22 at 19:07
  • 2
    The `set` command with no arguments may be what you want. – chepner Jun 09 '22 at 19:18
  • What do you mean by _sourcing tcsh code from Python_? This sounds to me a bit like a request of evaluating arbitrary Java code in Python. You would basically have to implement a full tcsh interpreter inside Python. This is no impossible, but certainly a project which gives work to a programming team for several years. – user1934428 Jun 10 '22 at 07:24

2 Answers2

0

The solution is quite simple. Just like the environment wich can be printed with setenv, you can print the local variables with set. Hope it was useful !

0

For the record, I solved this a while ago thanks to @chepner's comment

The set command with no arguments may be what you want.

Here's the resulting python library which accomplishes reading local and environment variables set by scripts for most major shells out of the box, with some customization available to support any shell.