-
Notifications
You must be signed in to change notification settings - Fork 42
/
env.py
54 lines (47 loc) · 1.68 KB
/
env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# vi: ts=4 sw=4 et
#
# env.py: setup environment variabels.
#
# This small utility outputs a bourne shell fragment that sets up the
# PATH and PYTHONPATH environment variables such that Python-AD can be used
# from within its source directory. This is required for the test suite,
# and is helpful for developing. Kudos to the py-lib team for the idea.
#
# This file is part of Python-ASN1. Python-ASN1 is free software that is
# made available under the MIT license. Consult the file "LICENSE" that
# is distributed together with this file for the exact licensing terms.
#
# Python-ASN1 is copyright (c) 2007 by the Python-ASN1 authors. See the
# file "AUTHORS" for a complete overview.
import os
import os.path
import sys
def prepend_path(name, value):
if sys.platform == 'win32':
sep = ';'
else:
sep = ':'
env_path = os.environ.get(name, '')
parts = [ x for x in env_path.split(sep) if x ]
while value in parts:
del parts[parts.index(value)]
parts.insert(0, value)
return setenv(name, sep.join(parts))
def setenv(name, value):
shell = os.environ.get('SHELL', '')
comspec = os.environ.get('COMSPEC', '')
if shell.endswith('csh'):
cmd = 'setenv %s "%s"' % (name, value)
elif shell.endswith('sh'):
cmd = '%s="%s"; export %s' % (name, value, name)
elif comspec.endswith('cmd.exe'):
cmd = '@set %s=%s' % (name, value)
else:
assert False, 'Shell not supported.'
return cmd
abspath = os.path.abspath(sys.argv[0])
topdir, fname = os.path.split(abspath)
bindir = os.path.join(topdir, 'bin')
print prepend_path('PATH', bindir)
pythondir = os.path.join(topdir, 'lib')
print prepend_path('PYTHONPATH', pythondir)