annotate python/readenv.py @ 924:fc88a12acf48 default tip

add lfs filter
author Jeff Hammel <k0scist@gmail.com>
date Wed, 04 Dec 2024 15:50:53 -0800
parents 4ae9b16a1483
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
634
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 parse environment variables
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import argparse
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import os
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import subprocess
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import sys
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 def parse_env(output):
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 return dict([line.split('=',1) for line in output.splitlines()])
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def read_env(script):
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 """read environment following source script"""
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 command = 'bash -c "(. {} &> /dev/null) && /usr/bin/env"'.format(script)
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 command = "(. {} &> /dev/null) && /usr/bin/env".format(script)
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 output = subprocess.check_output(command, shell=True, env={})
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 return parse_env(output)
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def diff_env(script):
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 before = parse_env(subprocess.check_output('/usr/bin/env', shell=True))
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 after = read_env(script)
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 if __name__ == '__main__':
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 """test"""
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 import tempfile
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 from pprint import pprint
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 example = """export FOO=bar
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 export FLEEM=foobar"""
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 fd, name = tempfile.mkstemp()
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 os.write(fd, example)
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 os.close(fd)
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 print (open(name).read())
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 pprint(name)
4ae9b16a1483 read env variables
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 pprint(read_env(name))