Skip to content

Commit

Permalink
Use timezone-aware datetime objects, not utcnow()
Browse files Browse the repository at this point in the history
  • Loading branch information
remram44 committed Nov 19, 2023
1 parent ab60db1 commit a02ae1a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
10 changes: 5 additions & 5 deletions reproserver/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from base64 import b64decode, b64encode
from datetime import datetime
from datetime import datetime, timezone
import logging
import os
from sqlalchemy import Column, ForeignKey, create_engine
Expand Down Expand Up @@ -32,7 +32,7 @@ class Experiment(Base):

hash = Column(String(64), primary_key=True)
last_access = Column(DateTime, nullable=False,
default=lambda: datetime.utcnow())
default=lambda: datetime.now(timezone.utc))
size = Column(Integer, nullable=False)
info = Column(Text, nullable=False)

Expand Down Expand Up @@ -85,7 +85,7 @@ class Upload(Base):
submitted_ip = Column(Text, nullable=True)
repository_key = Column(Text, nullable=True, index=True)
timestamp = Column(DateTime, nullable=False,
default=lambda: datetime.utcnow())
default=lambda: datetime.now(timezone.utc))

@property
def short_id(self):
Expand Down Expand Up @@ -172,7 +172,7 @@ class Run(Base):
ondelete='RESTRICT'))
upload = relationship('Upload', uselist=False)
submitted = Column(DateTime, nullable=False,
default=lambda: datetime.utcnow())
default=lambda: datetime.now(timezone.utc))
started = Column(DateTime, nullable=True)
done = Column(DateTime, nullable=True)

Expand Down Expand Up @@ -227,7 +227,7 @@ class RunLogLine(Base):
run_id = Column(Integer, ForeignKey('runs.id', ondelete='CASCADE'))
run = relationship('Run', uselist=False, back_populates='log')
timestamp = Column(DateTime, nullable=False,
default=lambda: datetime.utcnow())
default=lambda: datetime.now(timezone.utc))
line = Column(Text, nullable=False)

def __repr__(self):
Expand Down
10 changes: 5 additions & 5 deletions reproserver/run/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import hashlib
import json
import subprocess
from datetime import datetime
from datetime import datetime, timezone
import logging
import os
from sqlalchemy.orm import joinedload
Expand Down Expand Up @@ -269,7 +269,7 @@ async def run_started(self, run_id):
if run.started:
logger.warning("Starting run which has already been started")
else:
run.started = datetime.utcnow()
run.started = datetime.now(timezone.utc)
db.commit()

async def run_progress(self, run_id, percent, text):
Expand All @@ -285,13 +285,13 @@ async def run_progress(self, run_id, percent, text):
async def run_done(self, run_id):
db = self.DBSession()
run = db.query(database.Run).get(run_id)
run.done = datetime.utcnow()
run.done = datetime.now(timezone.utc)
db.commit()

async def run_failed(self, run_id, error):
db = self.DBSession()
run = db.query(database.Run).get(run_id)
run.done = datetime.utcnow()
run.done = datetime.now(timezone.utc)
db.add(database.RunLogLine(run_id=run.id, line=error))
db.commit()

Expand Down Expand Up @@ -577,7 +577,7 @@ def log(self, run_id, msg, *args):
return self.log_multiple(run_id, [line])

async def log_multiple(self, run_id, lines):
now = datetime.utcnow().isoformat()
now = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S')
await self.http_client.fetch(
'{0}/runners/run/{1}/log'.format(
self.api_endpoint,
Expand Down
8 changes: 6 additions & 2 deletions reproserver/web/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from datetime import datetime
from datetime import datetime, timezone
import functools
import hashlib
import logging
Expand Down Expand Up @@ -148,7 +148,11 @@ def post(self, run_id):
self.db.add(database.RunLogLine(
run_id=run_id,
line=line['msg'],
timestamp=datetime.fromisoformat(line['time']),
timestamp=(
datetime
.fromisoformat(line['time'])
.replace(tzinfo=timezone.utc)
),
))
self.db.commit()
self.set_status(204)
Expand Down
18 changes: 9 additions & 9 deletions reproserver/web/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone
from hashlib import sha256
import json
import logging
Expand Down Expand Up @@ -59,7 +59,7 @@ async def store_uploaded_rpz(
# Check for existence of experiment
experiment = db.query(database.Experiment).get(filehash)
if experiment:
experiment.last_access = datetime.utcnow()
experiment.last_access = datetime.now(timezone.utc)
logger.info("File exists in storage")
else:
# Insert it in database
Expand Down Expand Up @@ -291,10 +291,10 @@ async def get(self, repo, repo_path):
self.set_status(404)
return await self.render('setup_badfile.html', message=str(e))
else:
upload.last_access = datetime.utcnow()
upload.last_access = datetime.now(timezone.utc)

# Also updates last access
upload.experiment.last_access = datetime.utcnow()
upload.experiment.last_access = datetime.now(timezone.utc)
self.db.commit()

repo_name = get_repository_name(repo)
Expand Down Expand Up @@ -325,8 +325,8 @@ def get(self, upload_short_id):
return self.render('setup_notfound.html')

# Also updates last access
upload.last_access = datetime.utcnow()
upload.experiment.last_access = datetime.utcnow()
upload.last_access = datetime.now(timezone.utc)
upload.experiment.last_access = datetime.now(timezone.utc)
self.db.commit()

return self.reproduce(upload)
Expand Down Expand Up @@ -358,8 +358,8 @@ async def post(self, upload_short_id):
experiment = upload.experiment

# Update last access
upload.last_access = datetime.utcnow()
upload.experiment.last_access = datetime.utcnow()
upload.last_access = datetime.now(timezone.utc)
upload.experiment.last_access = datetime.now(timezone.utc)

# New run entry
run = database.Run(experiment_hash=experiment.hash,
Expand Down Expand Up @@ -492,7 +492,7 @@ def get(self, run_short_id):
for extension in run.experiment.extensions
}
# Update last access
run.experiment.last_access = datetime.utcnow()
run.experiment.last_access = datetime.now(timezone.utc)
self.db.commit()

def get_port_url(port_number):
Expand Down
14 changes: 7 additions & 7 deletions reproserver/web/webcapture.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from datetime import datetime
from datetime import datetime, timezone
import json
import logging
import os
Expand Down Expand Up @@ -187,8 +187,8 @@ def post(self, upload_short_id):
experiment = upload.experiment

# Update last access
upload.last_access = datetime.utcnow()
upload.experiment.last_access = datetime.utcnow()
upload.last_access = datetime.now(timezone.utc)
upload.experiment.last_access = datetime.now(timezone.utc)

# New run entry
run = database.Run(experiment_hash=experiment.hash,
Expand Down Expand Up @@ -250,8 +250,8 @@ def post(self, upload_short_id):
experiment = upload.experiment

# Update last access
upload.last_access = datetime.utcnow()
upload.experiment.last_access = datetime.utcnow()
upload.last_access = datetime.now(timezone.utc)
upload.experiment.last_access = datetime.now(timezone.utc)

# New run entry
run = database.Run(experiment_hash=experiment.hash,
Expand Down Expand Up @@ -404,8 +404,8 @@ def post(self, upload_short_id):
experiment = upload.experiment

# Update last access
upload.last_access = datetime.utcnow()
upload.experiment.last_access = datetime.utcnow()
upload.last_access = datetime.now(timezone.utc)
upload.experiment.last_access = datetime.now(timezone.utc)

# New run entry
run = database.Run(experiment_hash=experiment.hash,
Expand Down

0 comments on commit a02ae1a

Please sign in to comment.