Skip to content

Commit

Permalink
Upgrade python to 3.11 (#165)
Browse files Browse the repository at this point in the history
* Upgrade python to 3.11

* Version bump

* Fix CI python

* Fixing poetry env selection
  • Loading branch information
devinmatte authored Dec 17, 2023
1 parent 5fdc0fe commit e53753f
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 361 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
node-version: ['18']
python-version: ['3.9', '3.10', '3.11']
python-version: ['3.11']
steps:
- name: Checkout repo
uses: actions/checkout@v3
Expand All @@ -35,6 +35,10 @@ jobs:
backend:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['18']
python-version: ['3.11']
steps:
- name: Checkout repo
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Shows new MBTA Orange, Red, and Green Line trains as they come into service.
## Install & Run
Dependencies:
- `node` `18.x`
- `python` `3.9`
- `python` `3.11`
- [`poetry`](https://python-poetry.org/)

Run:
Expand Down
13 changes: 9 additions & 4 deletions devops/deploy-playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
repo: ppa:deadsnakes/ppa
update_cache: yes

- name: install python3.9
- name: install python3.11
become: yes
become_user: root
apt:
name: python3
state: 3.9
name: python3.11
state: latest

- name: install poetry
shell: curl -sSL https://install.python-poetry.org | python3 -
Expand All @@ -47,7 +47,7 @@
repo: https://github.com/transitmatters/new-train-tracker.git
dest: /home/ubuntu/new-train-tracker
force: yes
version: main
version: python3.11-upgrade

- name: copy systemd service file
become: yes
Expand Down Expand Up @@ -82,6 +82,11 @@
state: restarted
daemon_reload: yes

- name: ensure using python 3.11 poetry environment
shell: /home/ubuntu/.local/bin/poetry env use 3.11
args:
chdir: /home/ubuntu/new-train-tracker

- name: install app dependencies
shell: npm ci
args:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "new-train-tracker",
"version": "2.3.1",
"version": "2.3.2",
"description": "New Train Tracker by TransitMattters",
"main": "index.tsx",
"scripts": {
Expand Down
470 changes: 157 additions & 313 deletions poetry.lock

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
[tool.poetry]
name = "new-train-tracker"
version = "2.3.1"
version = "2.3.2"
description = "New Train Tracker by TransitMattters"
authors = ["TransitMatters Labs Team"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.9, <3.12"
flask = "2.3.3"
aiohttp = "3.8.5"
boto3 = "1.26.153"
gunicorn = "20.1.0"
python = "~3.11"
flask = "3.0.0"
aiohttp = "3.9.1"
boto3 = "1.34.2"
gunicorn = "21.2.0"
json-api-doc = "0.15.0"
pytz = "2023.3"
MarkupSafe = "2.1.3"
schedule = "1.2.1"


[tool.poetry.dev-dependencies]
pip = ">=23.0"
black = "21.12b0"
pip = ">=23.1"
black = "^23.12.0"
pylint = "2.13.5"
flake8 = "6.1.0"


[tool.black]
line-length = 120
target-version = ['py311']


[build-system]
requires = ["poetry-core>=1.6.0"]
build-backend = "poetry.core.masonry.api"
14 changes: 4 additions & 10 deletions server/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ def static_files(filename):
@application.route("/trains/<route_ids_string>")
def trains(route_ids_string):
route_ids = route_ids_string.split(",")
vehicle_data = asyncio.run(
mbta_api.vehicle_data_for_routes(route_ids)
)
return flask.Response(json.dumps(vehicle_data), mimetype="application/json", headers={
"Cache-Control": "no-cache"
})
vehicle_data = asyncio.run(mbta_api.vehicle_data_for_routes(route_ids))
return flask.Response(json.dumps(vehicle_data), mimetype="application/json", headers={"Cache-Control": "no-cache"})


# takes a single route id
Expand Down Expand Up @@ -70,9 +66,7 @@ def root():
shell = flask.request.args.get("shell")
if shell:
return static_files("index.html")
payload = asyncio.run(
initial_data.initial_request_data(DEFAULT_ROUTE_IDS)
)
payload = asyncio.run(initial_data.initial_request_data(DEFAULT_ROUTE_IDS))
payload["static_data"] = get_static_data()

response = flask.make_response(flask.render_template("index.html", initial_data=payload))
Expand All @@ -81,7 +75,7 @@ def root():


def get_static_data():
with open('./static/static_data.json') as f:
with open("./static/static_data.json") as f:
static_data = json.load(f)
return static_data

Expand Down
1 change: 1 addition & 0 deletions server/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Copied from https://schedule.readthedocs.io/en/stable/background-execution.html


def run_continuously(interval_s=1) -> threading.Event:
"""Continuously run, while executing pending jobs at each
elapsed time interval.
Expand Down
2 changes: 1 addition & 1 deletion server/fleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_is_new_dict(route_ids, test_fn):
"Orange": orange_is_new,
**get_is_new_dict(GREEN_ROUTE_IDS, green_is_new),
**get_is_new_dict(SILVER_ROUTE_IDS, silver_is_new),
"Blue": blue_is_new
"Blue": blue_is_new,
}


Expand Down
20 changes: 12 additions & 8 deletions server/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def file_age_s(pathname):
def run():
checks = [
lambda: len(secrets.MBTA_V3_API_KEY) > 0,
lambda: file_age_s(LAST_SEEN_JSON_PATH) < 1800 # allow up to 30 minutes of outdated last_seen.json
lambda: file_age_s(LAST_SEEN_JSON_PATH) < 1800, # allow up to 30 minutes of outdated last_seen.json
]

for i in range(0, len(checks)):
Expand All @@ -24,11 +24,15 @@ def run():
checks[i] = False

if all(checks):
return Response(json.dumps({
"status": "pass"
}), mimetype="application/json", status=200)
return Response(json.dumps({"status": "pass"}), mimetype="application/json", status=200)

return Response(json.dumps({
"status": "fail",
"check_failed": checks.index(False),
}), mimetype="application/json", status=500)
return Response(
json.dumps(
{
"status": "fail",
"check_failed": checks.index(False),
}
),
mimetype="application/json",
status=500,
)
4 changes: 2 additions & 2 deletions server/initial_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def calc_stats(vehicle_array):
"Blue": {
"totalActive": len(totalBlue),
"totalNew": len(filter_new(totalBlue)),
"totalOld": len(filter_old(totalBlue))
}
"totalOld": len(filter_old(totalBlue)),
},
}

return vehicle_stats
Expand Down
2 changes: 1 addition & 1 deletion server/last_seen.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def update_recent_sightings():
LAST_SEEN_TIMES[line] = {
"car": vehicle["label"],
# Python isoformat() doesn't include TZ, but we know this is UTC because we used utcnow() above
"time": now.isoformat()[:-3] + "Z"
"time": now.isoformat()[:-3] + "Z",
}

with open(JSON_PATH, "w", encoding="utf-8") as file:
Expand Down
8 changes: 2 additions & 6 deletions server/mbta_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ async def inner(some_session):
eastern = pytz.timezone("US/Eastern")
now_eastern = datetime.datetime.now(eastern)
if response.status >= 400:
print(
f"[{now_eastern}] API returned {response.status} for {url} -- it says {response_json}"
)
print(f"[{now_eastern}] API returned {response.status} for {url} -- it says {response_json}")
try:
return json_api_doc.parse(response_json)
except Exception as e:
Expand Down Expand Up @@ -158,9 +156,7 @@ async def stops_for_route(route_id):
"route": stop["route"],
},
filter(
lambda stop: stop_belongs_to_custom_route(
stop["id"], route_id, normalized_route_name
),
lambda stop: stop_belongs_to_custom_route(stop["id"], route_id, normalized_route_name),
stops,
),
)
Expand Down
4 changes: 2 additions & 2 deletions server/secrets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MBTA_V3_API_KEY = ''
# False by default, because debug Flask spawns two processes, and two last seen updaters will trample each other!
LAST_SEEN_UPDATE = False
'''
"""
If you put your api key here, you may want to run
`git update-index --assume-unchanged server/secrets.py`
so that it doesn't pollute your `git status`
Expand All @@ -14,4 +14,4 @@
hidden = !git ls-files -v | grep "^[[:lower:]]"
Then run `git hide server/secrets.py`
'''
"""

0 comments on commit e53753f

Please sign in to comment.