Skip to content

Commit 0130798

Browse files
committed
Try to improve version detection in python code
1 parent a772fdf commit 0130798

File tree

10 files changed

+61
-21
lines changed

10 files changed

+61
-21
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
include debian/patches/fix-cgfs_types
22
include debian/patches/fix-fusepy
33
include debian/patches/series
4+
include package.json

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,17 @@ test-quick: test
7070
check: check-format mypy lint test
7171

7272
.PHONY: build
73-
build: install-deps check build-$(UNAME)
73+
build: install check build-$(UNAME)
7474

7575
.PHONY: build-quick
76-
build-quick: install-deps build-$(UNAME)
76+
build-quick: install build-$(UNAME)
7777

7878
dist/cgfs: codegra_fs/*.py
7979
pyinstaller \
8080
--noconfirm \
8181
--onedir \
8282
--specpath dist \
83+
--additional-hooks-dir=./pyinstaller_hooks \
8384
--name cgfs \
8485
codegra_fs/cgfs.py
8586

@@ -88,6 +89,7 @@ dist/cgapi-consumer: codegra_fs/*.py
8889
--noconfirm \
8990
--onedir \
9091
--specpath dist \
92+
--additional-hooks-dir=./pyinstaller_hooks \
9193
--name cgapi-consumer \
9294
codegra_fs/api_consumer.py
9395

build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import subprocess
66

77
import requests
8-
from codegra_fs import __version__
98

109
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
1110
os.chdir(BASE_DIR)
@@ -19,6 +18,7 @@ def pyinstaller(module, name):
1918
'pyinstaller',
2019
'--noconfirm',
2120
'--onedir',
21+
'--additional-hooks-dir=./pyinstaller_hooks',
2222
'--specpath',
2323
'dist',
2424
'--name',

codegra_fs/__init__.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
# SPDX-License-Identifier: AGPL-3.0-only
22

3-
__version__ = (1, 1, 2)
3+
import sys
4+
import typing as t
5+
6+
import pkg_resources
7+
import packaging.version
8+
9+
if t.TYPE_CHECKING:
10+
from typing_extensions import Final, Literal
11+
12+
__version__ = 'UNKNOWN' # type: t.Union['Literal["UNKNOWN"]', packaging.version.Version]
13+
try:
14+
__version__ = packaging.version.Version(
15+
pkg_resources.get_distribution('codegrade-fs').version
16+
)
17+
except:
18+
import traceback
19+
print('Could not read version:', file=sys.stderr)
20+
traceback.print_exc()
21+
finally:
22+
del pkg_resources
23+
424
__author__ = 'Olmo Kramer, Thomas Schaper'
525
__email__ = 'info@CodeGra.de'
626
__license__ = 'AGPL-3.0-only'

codegra_fs/cgapi.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from urllib.parse import quote
1010

1111
import requests
12-
1312
import codegra_fs
1413

1514
DEFAULT_CGAPI_BASE_URL = os.getenv(
@@ -18,9 +17,7 @@
1817

1918
T_CALL = t.TypeVar('T_CALL', bound=t.Callable)
2019

21-
USER_AGENT = 'CodeGradeFS/{}'.format(
22-
'.'.join(map(str, codegra_fs.__version__))
23-
)
20+
USER_AGENT = 'CodeGradeFS/{}'.format(codegra_fs.__version__)
2421

2522
logger = logging.getLogger(__name__)
2623

@@ -117,7 +114,9 @@ def get_file_rename(self, file_id, new_path):
117114
return (
118115
'{base}/code/{file_id}?operation='
119116
'rename&new_path={new_path}'
120-
).format(base=self.base, file_id=file_id, new_path=quote(new_path))
117+
).format(
118+
base=self.base, file_id=file_id, new_path=quote(new_path)
119+
)
121120

122121
def get_submission_feedbacks(self, submission_id):
123122
return '{base}/submissions/{submission_id}/feedbacks/'.format(
@@ -130,18 +129,21 @@ def get_feedbacks(self, assignment_id):
130129
)
131130

132131
def get_feedback(self, file_id):
133-
return ('{base}/code/{file_id}?type=feedback'
134-
).format(base=self.base, file_id=file_id)
132+
return ('{base}/code/{file_id}?type=feedback').format(
133+
base=self.base, file_id=file_id
134+
)
135135

136136
def add_feedback(self, file_id, line):
137-
return ('{base}/code/{file_id}/comments/{line}'
138-
).format(base=self.base, file_id=file_id, line=line)
137+
return ('{base}/code/{file_id}/comments/{line}').format(
138+
base=self.base, file_id=file_id, line=line
139+
)
139140

140141
delete_feedback = add_feedback
141142

142143
def get_assignment(self, assignment_id):
143-
return ('{base}/assignments/{assignment_id}'
144-
).format(base=self.base, assignment_id=assignment_id)
144+
return ('{base}/assignments/{assignment_id}').format(
145+
base=self.base, assignment_id=assignment_id
146+
)
145147

146148

147149
class APICodes(IntEnum):

codegra_fs/cgfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2481,7 +2481,7 @@ def main() -> None:
24812481
dest='version',
24822482
action='version',
24832483
version=(
2484-
'%(prog)s {}'.format('.'.join(map(str, codegra_fs.__version__)))
2484+
'%(prog)s {}'.format(codegra_fs.__version__)
24852485
),
24862486
help='Display version.',
24872487
)

codegra_fs/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import requests
99
import codegra_fs
10+
import packaging.version
1011

1112
T = t.TypeVar('T')
1213
Y = t.TypeVar('Y')
@@ -63,13 +64,18 @@ def get_fuse_install_message() -> t.Optional[t.Tuple[str, t.Optional[str]]]:
6364

6465

6566
def newer_version_available() -> bool:
67+
my_version = codegra_fs.__version__
68+
# The isinstance check is needed for mypy
69+
if isinstance(my_version, str) and my_version == 'UNKNOWN':
70+
return False
71+
6672
try:
6773
req = requests.get('https://fs.codegrade.com/.cgfs.json', timeout=2)
6874
req.raise_for_status()
69-
latest = tuple(map(int, req.json()['version']))
75+
latest = packaging.version.Version('.'.join(req.json()['version']))
76+
return my_version < latest
7077
except:
7178
return False
72-
return latest > codegra_fs.__version__
7379

7480

7581
def find_all_dups(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from PyInstaller.utils.hooks import copy_metadata
2+
3+
datas = copy_metadata('codegrade-fs')

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pylint>=2.3.1
66
pytest>=4.4.1
77
requests>=2.20.0
88
yapf>=0.27.0
9+
packaging>=20.8

setup.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#!/usr/bin/env python3
22

3+
import os
34
import sys
5+
import json
46

7+
import packaging.version
58
from setuptools import setup
69

7-
import codegra_fs
8-
9-
version = '.'.join(map(str, codegra_fs.__version__))
10+
with open(os.path.join(os.path.dirname(__file__), 'package.json')) as f:
11+
version = json.load(f)['version']
12+
# Make sure we can parse this version
13+
packaging.version.Version(version)
1014

1115
if sys.version_info < (3, 5):
1216
print('Sorry only python 3.5 and up is supported', file=sys.stderr)
@@ -15,6 +19,7 @@
1519
requires = [
1620
'requests>=2.20.0',
1721
'fusepy>3.0.0,<4.0.0',
22+
'packaging>=20.8',
1823
]
1924
if sys.platform.startswith('win32'):
2025
requires += [

0 commit comments

Comments
 (0)