Skip to content

Commit 5f45920

Browse files
authored
Merge pull request #247 from mxmeinhold/better-versions
2 parents a02a887 + 7aff72e commit 5f45920

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

.github/workflows/python-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
strategy:
1717
matrix:
18-
python-version: [3.7, 3.8]
18+
python-version: [3.9]
1919

2020
steps:
2121
- name: Install ldap dependencies

Dockerfile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
FROM python:3.7-slim-buster
1+
FROM python:3.9-slim-buster
22
MAINTAINER Devin Matte <matted@csh.rit.edu>
33

4+
ENV DD_LOGS_INJECTION=true
5+
46
RUN apt-get -yq update && \
5-
apt-get -yq --no-install-recommends install gcc curl libsasl2-dev libldap2-dev libssl-dev gnupg2 && \
7+
apt-get -yq --no-install-recommends install gcc curl libsasl2-dev libldap2-dev libssl-dev gnupg2 git && \
68
apt-get -yq clean all
79

810
RUN mkdir /opt/packet
@@ -30,4 +32,7 @@ RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && \
3032

3133
RUN ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
3234

33-
CMD ["ddtrace-run", "gunicorn", "packet:app", "--bind=0.0.0.0:8080", "--access-logfile=-", "--timeout=600"]
35+
# Set version for apm
36+
RUN echo "export DD_VERSION=$(python3 packet/git.py)" >> /tmp/version
37+
38+
CMD ["/bin/bash", "-c", "source /tmp/version && ddtrace-run gunicorn packet:app --bind=0.0.0.0:8080 --access-logfile=- --timeout=600"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# CSH Web Packet
22

3-
[![Python 3.7](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/downloads/release/python-370/)
3+
[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-390/)
44
[![Build Status](https://travis-ci.com/ComputerScienceHouse/packet.svg?branch=develop)](https://travis-ci.com/ComputerScienceHouse/packet)
55

66
Packet is used by CSH to facilitate the freshmen packet portion of our introductory member evaluation process. This is
77
the second major iteration of packet on the web. The first version was
88
[Tal packet](https://github.com/TalCohen/CSHWebPacket).
99

1010
## Setup
11-
**Requires Python 3.7 or newer.**
11+
**Requires Python 3.9 or newer.**
1212

1313
To get the server working you'll just need the Python dependencies and some secrets. There will be some UI issues due
1414
to missing assets though. To solve that you'll want to set up the front end dependencies or download a copy of the

packet/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from sentry_sdk.integrations.flask import FlaskIntegration
2020
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
2121

22+
from .git import get_version
23+
2224
app = Flask(__name__)
2325
gzip = Gzip(app)
2426

@@ -31,9 +33,8 @@
3133
if os.path.exists(_pyfile_config):
3234
app.config.from_pyfile(_pyfile_config)
3335

34-
# Fetch the version number from the npm package file
35-
with open(os.path.join(_root_dir, 'package.json')) as package_file:
36-
app.config['VERSION'] = json.load(package_file)['version']
36+
# Fetch the version number
37+
app.config['VERSION'] = get_version()
3738

3839
# Logger configuration
3940
logging.getLogger().setLevel(app.config['LOG_LEVEL'])

packet/git.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import json
2+
import os
3+
import subprocess
4+
5+
def get_short_sha(commit_ish: str = 'HEAD'):
6+
"""
7+
Get the short hash of a commit-ish
8+
Returns '' if unfound
9+
"""
10+
11+
try:
12+
rev_parse = subprocess.run(f'git rev-parse --short {commit_ish}'.split(), capture_output=True, check=True)
13+
return rev_parse.stdout.decode('utf-8').strip()
14+
except subprocess.CalledProcessError:
15+
return ''
16+
17+
def get_tag(commit_ish: str = 'HEAD'):
18+
"""
19+
Get the name of the tag at a given commit-ish
20+
Returns '' if untagged
21+
"""
22+
23+
try:
24+
describe = subprocess.run(f'git describe --exact-match {commit_ish}'.split(), capture_output=True, check=True)
25+
return describe.stdout.decode('utf-8').strip()
26+
except subprocess.CalledProcessError:
27+
return ''
28+
29+
def get_version(commit_ish: str = 'HEAD'):
30+
"""
31+
Get the version string of a commit-ish
32+
33+
If we have a commit and the commit is tagged, version is `tag (commit-sha)`
34+
If we have a commit but not a tag, version is `commit-sha`
35+
If we have neither, version is the version field of package.json
36+
"""
37+
38+
if sha := get_short_sha(commit_ish):
39+
if tag := get_tag(commit_ish):
40+
return f'{tag} ({sha})'
41+
else:
42+
return sha
43+
else:
44+
root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
45+
with open(os.path.join(root_dir, 'package.json')) as package_file:
46+
return json.load(package_file)['version']
47+
48+
if __name__ == '__main__':
49+
print(get_version())

0 commit comments

Comments
 (0)