Browse Source

Make setup.py automatically set version numbers (#38)

adam j hartz 6 days ago
parent
commit
4a0965dbe7
5 changed files with 156 additions and 38 deletions
  1. 3
    0
      .gitignore
  2. 1
    0
      MANIFEST.in
  3. 1
    1
      catsoop/__init__.py
  4. 10
    0
      catsoop/main.py
  5. 141
    37
      setup.py

+ 3
- 0
.gitignore View File

@@ -1,3 +1,6 @@
1
+# Git hash from installing
2
+catsoop/dev.githash
3
+
1 4
 # Byte-compiled / optimized / DLL files
2 5
 __pycache__/
3 6
 *.py[cod]

+ 1
- 0
MANIFEST.in View File

@@ -4,3 +4,4 @@ recursive-include catsoop/__QTYPES__ *
4 4
 recursive-include catsoop/__STATIC__ *
5 5
 recursive-include catsoop/__UTIL__ *
6 6
 include requirements.txt
7
+include catsoop/dev.githash

+ 1
- 1
catsoop/__init__.py View File

@@ -1 +1 @@
1
-__version__ = "14.0.dev6"
1
+__version__ = "14.0.0"

+ 10
- 0
catsoop/main.py View File

@@ -28,6 +28,15 @@ def command_line_interface(args=None, arglist=None):
28 28
     """
29 29
 
30 30
     version = pkg_resources.require("catsoop")[0].version
31
+    if "dev" in version:
32
+        gitfile = os.path.join(os.path.dirname(__file__), "dev.githash")
33
+        if os.path.isfile(gitfile):
34
+            with open(gitfile, "r") as f:
35
+                try:
36
+                    hash_, date = f.read().split("|")
37
+                    version = "%s\nGit revision: %s\n%s" % (version, hash_, date)
38
+                except:
39
+                    pass
31 40
 
32 41
     help_text = """
33 42
 Example commands:
@@ -53,6 +62,7 @@ configure      : generate CAT-SOOP configuration file using an interactive wizar
53 62
     default_config_location = os.environ.get(
54 63
         "XDG_CONFIG_HOME", os.path.expanduser(os.path.join("~", ".config"))
55 64
     )
65
+    parser.add_argument("--version", action="version", version="catsoop v%s" % version)
56 66
     default_config_location = os.path.abspath(
57 67
         os.path.join(default_config_location, "catsoop", "config.py")
58 68
     )

+ 141
- 37
setup.py View File

@@ -1,40 +1,144 @@
1
-import glob
1
+# This file is part of CAT-SOOP
2
+# Copyright (c) 2011-2019 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
3
+#
4
+# This program is free software: you can redistribute it and/or modify it under
5
+# the terms of the GNU Affero General Public License as published by the Free
6
+# Software Foundation, either version 3 of the License, or (at your option) any
7
+# later version.
8
+#
9
+# This program is distributed in the hope that it will be useful, but WITHOUT
10
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
12
+# details.
13
+#
14
+# You should have received a copy of the GNU Affero General Public License
15
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
+#
17
+#
18
+# Portions of this file were forked from the setup.py file for xonsh
19
+# (https://xon.sh/), which is available under the MIT/Expat license.
20
+
21
+import os
22
+import sys
23
+import subprocess
24
+
2 25
 from setuptools import setup
26
+from setuptools.command.sdist import sdist
27
+from setuptools.command.install import install
28
+from setuptools.command.develop import develop
3 29
 
4
-from catsoop import __version__
5
-
6
-requirements = open("requirements.txt").read().split('\n')
7
-
8
-setup(
9
-    name='catsoop',
10
-    version=__version__,
11
-    author='CAT-SOOP Contributors',
12
-    author_email='catsoop-dev@mit.edu',
13
-    packages=['catsoop', 'catsoop.test', 'catsoop.thirdparty', 'catsoop.scripts'],
14
-    scripts=[],
15
-    url='https://catsoop.mit.edu',
16
-    license='AGPLv3+',
17
-    description='CAT-SOOP is a tool for automatic collection and assessment of online exercises.',
18
-    long_description=open('README.md').read(),
19
-    long_description_content_type='text/markdown',
20
-    include_package_data=True,
21
-    entry_points={
22
-        'console_scripts': [
23
-            'catsoop = catsoop.main:command_line_interface',
24
-            ],
25
-        },
26
-    install_requires=requirements,
27
-    package_dir={'catsoop': 'catsoop'},
28
-    package_data={'catsoop': ['scripts/*']},
29
-    test_suite="catsoop.test",
30
-    classifiers=[
31
-        "Development Status :: 4 - Beta",
32
-        "Intended Audience :: Education",
33
-        "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
34
-        "Programming Language :: Python :: 3.5",
35
-        "Programming Language :: Python :: 3.6",
36
-        "Programming Language :: Python :: 3.7",
37
-        "Topic :: Education",
38
-        "Topic :: Internet :: WWW/HTTP :: WSGI",
39
-    ]
30
+from catsoop import __version__ as CS_VERSION
31
+
32
+logo = (
33
+    "\\            "
34
+    "\n/    /\\__/\\  "
35
+    "\n\\__=(  o_O )="
36
+    "\n(__________) "
37
+    "\n |_ |_ |_ |_ "
38
+    "\n             "
39
+    "\n  CAT-SOOP   "
40 40
 )
41
+
42
+
43
+VERSION_FNAME = os.path.join(os.path.dirname(__file__), "catsoop", "__init__.py")
44
+ORIGINAL_VERSION = None
45
+
46
+
47
+def dirty_version():
48
+    """
49
+    If install/sdist is run from a git directory, add a devN suffix to reported
50
+    version number and write a gitignored file that holds the git hash of the
51
+    current state of the repo.
52
+    """
53
+    global CS_VERSION, ORIGINAL_VERSION
54
+    try:
55
+        last_version = subprocess.check_output(
56
+            ["git", "describe", "--tags", "--match", "v*"]
57
+        ).decode("ascii")
58
+    except Exception:
59
+        print("failed to find git tags", file=sys.stderr)
60
+        return
61
+    try:
62
+        _, N, sha = last_version.strip().split("-")
63
+        N = int(N)
64
+    except ValueError:  # tag name may contain "-"
65
+        print("failed to parse git version", file=sys.stderr)
66
+        return
67
+
68
+    # if we get to this point, we are not at a particular tag.  we'll modify
69
+    # the __version__ from catsoop/__init__.py to include a .devN suffix.
70
+    sha = sha.lstrip("g")
71
+    CS_VERSION = CS_VERSION + ".dev%s" % (N,)
72
+    _cmd = ["git", "show", "-s", "--format=%cD", sha]
73
+    try:
74
+        _date = subprocess.check_output(_cmd)
75
+        _date = _date.decode("ascii")
76
+        # remove weekday name for a shorter string
77
+        _date = "".join(_date.split(" ", 1)[1:])
78
+    except:
79
+        _date = ""
80
+        print("failed to get commit date", file=sys.stderr)
81
+    with open(
82
+        os.path.join(os.path.dirname(__file__), "catsoop", "dev.githash"), "w"
83
+    ) as f:
84
+        f.write("{}|{}".format(sha, _date))
85
+    with open(VERSION_FNAME, "r") as f:
86
+        ORIGINAL_VERSION = f.read()
87
+    with open(VERSION_FNAME, "w") as f:
88
+        f.write('__version__ = "%s"\n' % CS_VERSION)
89
+
90
+
91
+def main():
92
+    if sys.version_info[:2] < (3, 5):
93
+        sys.exit("catsoop currently requires Python 3.5+")
94
+
95
+    if "--name" not in sys.argv:
96
+        print(logo)
97
+
98
+    with open(os.path.join(os.path.dirname(__file__), "requirements.txt"), "r") as f:
99
+        requirements = f.read().split("\n")
100
+
101
+    with open(os.path.join(os.path.dirname(__file__), "README.md"), "r") as f:
102
+        readme = f.read()
103
+
104
+    dirty_version()
105
+
106
+    setup(
107
+        name="catsoop",
108
+        version=CS_VERSION,
109
+        author="CAT-SOOP Contributors",
110
+        author_email="catsoop-dev@mit.edu",
111
+        packages=["catsoop", "catsoop.test", "catsoop.thirdparty", "catsoop.scripts"],
112
+        scripts=[],
113
+        url="https://catsoop.mit.edu",
114
+        license="AGPLv3+",
115
+        description="CAT-SOOP is a tool for automatic collection and assessment of online exercises.",
116
+        long_description=readme,
117
+        long_description_content_type="text/markdown",
118
+        include_package_data=True,
119
+        entry_points={
120
+            "console_scripts": ["catsoop = catsoop.main:command_line_interface"]
121
+        },
122
+        install_requires=requirements,
123
+        package_dir={"catsoop": "catsoop"},
124
+        package_data={"catsoop": ["scripts/*"]},
125
+        test_suite="catsoop.test",
126
+        classifiers=[
127
+            "Development Status :: 4 - Beta",
128
+            "Intended Audience :: Education",
129
+            "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
130
+            "Programming Language :: Python :: 3.5",
131
+            "Programming Language :: Python :: 3.6",
132
+            "Programming Language :: Python :: 3.7",
133
+            "Topic :: Education",
134
+            "Topic :: Internet :: WWW/HTTP :: WSGI",
135
+        ],
136
+    )
137
+
138
+    if ORIGINAL_VERSION is not None:
139
+        with open(VERSION_FNAME, "w") as f:
140
+            f.write(ORIGINAL_VERSION)
141
+
142
+
143
+if __name__ == "__main__":
144
+    main()

Loading…
Cancel
Save