Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1080)

Unified Diff: chrome/browser/resources/safe_browsing/gen_file_type_proto.py

Issue 2341173004: Allow gen_file_type_proto.py to work in virtualenv. (Closed)
Patch Set: Added comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/safe_browsing/gen_file_type_proto.py
diff --git a/chrome/browser/resources/safe_browsing/gen_file_type_proto.py b/chrome/browser/resources/safe_browsing/gen_file_type_proto.py
index be43adc28a5216f572f900d4a352388e0874170e..3ec8c238dcd3972eeaaf86b1963e93f136f115f9 100755
--- a/chrome/browser/resources/safe_browsing/gen_file_type_proto.py
+++ b/chrome/browser/resources/safe_browsing/gen_file_type_proto.py
@@ -17,6 +17,15 @@ import subprocess
import sys
import traceback
+# If the script is run in a virtualenv (https://virtualenv.pypa.io/en/stable/),
+# then no google.protobuf library should be brought in from site-packages.
+# Passing -S into the interpreter in a virtualenv actually destroys the ability
+# to import standard library functions like optparse, so this script should not
+# be wrapped if we're in a virtualenv.
+def IsInVirtualEnv():
+ # This is the way used by pip and other software to detect virtualenv.
+ return hasattr(sys, 'real_prefix')
+
def ImportProtoModules(paths):
"""Import the protobuf modiles we need. |paths| is list of import paths"""
@@ -208,8 +217,12 @@ def main():
parser.print_help()
return 1
- if opts.wrap:
- # Run this script again with different args to the interpreter.
+ if opts.wrap and not IsInVirtualEnv():
+ # Run this script again with different args to the interpreter to suppress
+ # the inclusion of libraries, like google.protobuf, from site-packages,
+ # which is checked before sys.path when resolving imports. We want to
+ # specifically import the libraries injected into the sys.path in
+ # ImportProtoModules().
command = [sys.executable, '-S', '-s', sys.argv[0]]
if opts.type is not None:
command += ['-t', opts.type]
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698