-
Notifications
You must be signed in to change notification settings - Fork 279
Fix Docker Build Errors and Refactor the code in src/flask.py #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shoaib-Programmer
wants to merge
8
commits into
cs50:main
Choose a base branch
from
Shoaib-Programmer:fix-docker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
β20
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cdec8d1
Configure Dockerfile to install system dependencies
Shoaib-Programmer eca1003
Remove the deprecated `version` attribute
Shoaib-Programmer 752cb7b
Added foo.db test file to .gitignore
Shoaib-Programmer a52d0c7
Fix deprecated warnings from pkgutil.get_loader
Shoaib-Programmer 7257d56
resolve Dockerfile
Shoaib-Programmer 99ce28b
resolve flask.py
Shoaib-Programmer 3e757ba
Merge branch 'main' into fix-docker
Shoaib-Programmer 089a18a
Alphabetized imports in flask.py
Shoaib-Programmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ build/ | |
*.db | ||
*.egg-info/ | ||
*.pyc | ||
foo.db |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
FROM cs50/cli | ||
|
||
RUN sudo apt update && sudo apt install --yes libmysqlclient-dev pgloader pkg-config postgresql | ||
RUN sudo apt update && sudo apt install --yes libmysqlclient-dev pgloader pkg-config postgresql && sudo rm -rf /var/lib/apt/lists/* | ||
RUN sudo pip3 install mysqlclient psycopg2-binary | ||
|
||
WORKDIR /mnt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,92 @@ | ||
import importlib.util | ||
import os | ||
import pkgutil | ||
import sys | ||
import warnings | ||
|
||
|
||
def _wrap_flask(f): | ||
""" | ||
Wraps the Flask class's __init__ method if necessary, | ||
primarily to add ProxyFix for CS50 IDE online environments. | ||
""" | ||
if f is None: | ||
return | ||
|
||
from packaging.version import Version, InvalidVersion | ||
from .cs50 import _formatException | ||
# Dynamically import packaging only when needed | ||
try: | ||
from packaging.version import Version, InvalidVersion | ||
except ImportError: | ||
# If packaging is not installed, cannot check version, so assume we can't wrap | ||
return | ||
|
||
|
||
try: | ||
# Check Flask version compatibility | ||
if Version(f.__version__) < Version("1.0"): | ||
return | ||
except InvalidVersion: | ||
except (InvalidVersion, AttributeError): | ||
# Handle invalid version strings or if Flask doesn't have __version__ | ||
return | ||
except Exception: | ||
return | ||
|
||
# Apply ProxyFix only in CS50 IDE online environment | ||
if os.getenv("CS50_IDE_TYPE") == "online": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rongxin-liu do we need this for cs50.dev? |
||
from werkzeug.middleware.proxy_fix import ProxyFix | ||
try: | ||
from werkzeug.middleware.proxy_fix import ProxyFix | ||
except ImportError: | ||
# If werkzeug (a Flask dependency) is missing or ProxyFix moved, cannot apply fix | ||
return | ||
|
||
_flask_init_before = f.Flask.__init__ | ||
|
||
def _flask_init_after(self, *args, **kwargs): | ||
_flask_init_before(self, *args, **kwargs) | ||
self.wsgi_app = ProxyFix( | ||
self.wsgi_app, x_proto=1 | ||
) # For HTTPS-to-HTTP proxy | ||
# Apply ProxyFix to handle reverse proxies in CS50 IDE (x_proto=1 trusts X-Forwarded-Proto header) | ||
self.wsgi_app = ProxyFix(self.wsgi_app, x_proto=1) | ||
|
||
# Monkey-patch Flask's __init__ | ||
f.Flask.__init__ = _flask_init_after | ||
|
||
|
||
# If Flask was imported before cs50 | ||
# --- Main Logic --- | ||
|
||
# Check if Flask was already imported before cs50 library was imported | ||
if "flask" in sys.modules: | ||
_wrap_flask(sys.modules["flask"]) | ||
|
||
# If Flask wasn't imported | ||
# If Flask hasn't been imported yet, set up patching for when it *is* imported | ||
else: | ||
flask_loader = pkgutil.get_loader("flask") | ||
if flask_loader: | ||
_exec_module_before = flask_loader.exec_module | ||
# Find the module specification for Flask using the recommended importlib utility | ||
# This replaces the deprecated pkgutil.get_loader | ||
flask_spec = importlib.util.find_spec("flask") | ||
|
||
# Check if the spec was found and if it has a loader | ||
# (Some namespace packages might not have a loader, but Flask should) | ||
if flask_spec and flask_spec.loader: | ||
|
||
# Ensure the loader has the exec_module method (standard loaders do) | ||
if hasattr(flask_spec.loader, 'exec_module'): | ||
|
||
# Get the original exec_module method from the loader | ||
_exec_module_before = flask_spec.loader.exec_module | ||
|
||
# Define a wrapper function for exec_module | ||
# This function will be called by the import system when Flask is loaded | ||
def _exec_module_after(module): | ||
# Execute the original module loading logic first | ||
_exec_module_before(module) | ||
# Now that the module ('flask') is fully loaded and present in sys.modules, | ||
# apply our custom wrapping logic. | ||
_wrap_flask(module) # Pass the loaded module object to _wrap_flask | ||
|
||
# Monkey-patch the loader's exec_module method with our wrapper | ||
flask_spec.loader.exec_module = _exec_module_after | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rongxin-liu mind reviewing? |
||
|
||
def _exec_module_after(*args, **kwargs): | ||
_exec_module_before(*args, **kwargs) | ||
_wrap_flask(sys.modules["flask"]) | ||
else: | ||
# Handle the unlikely case where the loader doesn't have exec_module | ||
warnings.warn("Flask loader doesn't support the expected exec_module interface") | ||
|
||
flask_loader.exec_module = _exec_module_after | ||
else: | ||
# Handle the case where Flask spec wasn't found (Flask not installed?) | ||
warnings.warn("Flask module specification not found. Flask may not be installed.") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.