Skip to content

Commit af988d7

Browse files
Manage the case of running from source inside a virtualenv
1 parent 37f859c commit af988d7

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

cms/conf.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,16 @@ def __init__(self):
123123
self.pdf_printing_allowed = False
124124

125125
# Installed or from source?
126-
self.installed = (sys.argv[0].startswith("/usr/") and
127-
sys.argv[0] != '/usr/bin/ipython' and
128-
sys.argv[0] != '/usr/bin/python2' and
129-
sys.argv[0] != '/usr/bin/python')
130-
# Test for virtualenv (see http://stackoverflow.com/a/1883251/747654)
131-
self.installed |= hasattr(sys, 'real_prefix')
126+
# We declare we are running from installed if the program was
127+
# NOT invoked through some python flavor, and the file is in
128+
# the prefix (or real_prefix to accommodate virtualenvs).
129+
bin_path = os.path.join(os.getcwd(), sys.argv[0])
130+
bin_name = os.path.basename(bin_path)
131+
bin_is_python = bin_name in ["ipython", "python", "python2", "python3"]
132+
bin_in_installed_path = bin_path.startswith(sys.prefix) or (
133+
hasattr(sys, 'real_prefix')
134+
and bin_path.startswith(sys.real_prefix))
135+
self.installed = bin_in_installed_path and not bin_is_python
132136

133137
if self.installed:
134138
self.log_dir = os.path.join("/", "var", "local", "log", "cms")

cmsranking/Config.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ def __init__(self):
6363
self.buffer_size = 100 # Needs to be strictly positive.
6464

6565
# File system.
66-
self.installed = (sys.argv[0].startswith("/usr/") and
67-
sys.argv[0] != '/usr/bin/ipython' and
68-
sys.argv[0] != '/usr/bin/python2' and
69-
sys.argv[0] != '/usr/bin/python')
70-
# Test for virtualenv (see http://stackoverflow.com/a/1883251/747654)
71-
self.installed |= hasattr(sys, 'real_prefix')
66+
# TODO: move to cmscommon as it is used both here and in cms/conf.py
67+
bin_path = os.path.join(os.getcwd(), sys.argv[0])
68+
bin_name = os.path.basename(bin_path)
69+
bin_is_python = bin_name in ["ipython", "python", "python2", "python3"]
70+
bin_in_installed_path = bin_path.startswith(sys.prefix) or (
71+
hasattr(sys, 'real_prefix')
72+
and bin_path.startswith(sys.real_prefix))
73+
self.installed = bin_in_installed_path and not bin_is_python
7274

7375
self.web_dir = pkg_resources.resource_filename("cmsranking", "static")
7476
if self.installed:

0 commit comments

Comments
 (0)