| 1 |
|
| 2 |
''' |
| 3 |
A CLI front-end to a running salt-api system |
| 4 |
|
| 5 |
''' |
| 6 |
import json |
| 7 |
import os |
| 8 |
import re |
| 9 |
|
| 10 |
from distutils.core import setup |
| 11 |
from distutils.dist import Distribution |
| 12 |
from distutils.command import sdist, install_data |
| 13 |
|
| 14 |
setup_kwargs = { |
| 15 |
'name': 'salt-pepper', |
| 16 |
'description': __doc__.strip(), |
| 17 |
'author': 'Seth House', |
| 18 |
'author_email': '[email protected]', |
| 19 |
'url': 'http://saltstack.com', |
| 20 |
'classifiers': [ |
| 21 |
'Programming Language :: Python', |
| 22 |
'Programming Language :: Cython', |
| 23 |
'Programming Language :: Python :: 2.6', |
| 24 |
'Programming Language :: Python :: 2.7', |
| 25 |
'Development Status :: 5 - Production/Stable', |
| 26 |
'Environment :: Console', |
| 27 |
'Intended Audience :: Developers', |
| 28 |
'Intended Audience :: Information Technology', |
| 29 |
'Intended Audience :: System Administrators', |
| 30 |
'License :: OSI Approved :: Apache Software License', |
| 31 |
'Operating System :: POSIX :: Linux', |
| 32 |
'Topic :: System :: Clustering', |
| 33 |
'Topic :: System :: Distributed Computing', |
| 34 |
], |
| 35 |
'packages': [ |
| 36 |
'pepper', |
| 37 |
], |
| 38 |
'package_data': { |
| 39 |
'pepper': ['version.json'], |
| 40 |
}, |
| 41 |
'scripts': [ |
| 42 |
'scripts/pepper', |
| 43 |
] |
| 44 |
} |
| 45 |
|
| 46 |
def read_version_tag(): |
| 47 |
git_dir = os.path.join(os.path.dirname(__file__), '.git') |
| 48 |
|
| 49 |
if os.path.isdir(git_dir): |
| 50 |
import subprocess |
| 51 |
|
| 52 |
try: |
| 53 |
p = subprocess.Popen(['git', 'describe'], |
| 54 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 55 |
out, err = p.communicate() |
| 56 |
except Exception: |
| 57 |
pass |
| 58 |
else: |
| 59 |
return out.strip() or None |
| 60 |
|
| 61 |
else: |
| 62 |
return read_version_from_json_file() |
| 63 |
|
| 64 |
return None |
| 65 |
|
| 66 |
def read_version_from_json_file(): |
| 67 |
with open(os.path.join(os.path.dirname(__file__), "pepper", "version.json")) as f: |
| 68 |
return json.load(f)['version'] |
| 69 |
|
| 70 |
def parse_version_tag(tag): |
| 71 |
''' |
| 72 |
Parse the output from Git describe |
| 73 |
|
| 74 |
Returns a tuple of the version number, number of commits (if any), and the |
| 75 |
Git SHA (if available). |
| 76 |
''' |
| 77 |
if isinstance(tag, bytes): |
| 78 |
tag = tag.decode() |
| 79 |
if not tag or '-g' not in tag: |
| 80 |
return tag, None, None |
| 81 |
|
| 82 |
match = re.search('(?P<version>.*)-(?P<num_commits>[0-9]+)-g(?P<sha>[0-9a-fA-F]+)', tag) |
| 83 |
|
| 84 |
if not match: |
| 85 |
return tag, None, None |
| 86 |
|
| 87 |
match_dict = match.groupdict() |
| 88 |
return ( |
| 89 |
match_dict.get('version'), |
| 90 |
match_dict.get('num_commits'), |
| 91 |
match_dict.get('sha')) |
| 92 |
|
| 93 |
def get_version(): |
| 94 |
''' |
| 95 |
Return a tuple of the version and Git SHA |
| 96 |
''' |
| 97 |
version, num_commits, sha = parse_version_tag(read_version_tag()) |
| 98 |
if sha: |
| 99 |
version = '{0}.dev{1}'.format(version, num_commits) |
| 100 |
return version, sha |
| 101 |
|
| 102 |
def write_version_file(base_dir): |
| 103 |
ver_path = os.path.join(base_dir, 'pepper', 'version.json') |
| 104 |
version, sha = get_version() |
| 105 |
|
| 106 |
with open(ver_path, 'wb') as f: |
| 107 |
json.dump({'version': version, 'sha': sha}, f) |
| 108 |
|
| 109 |
class PepperSdist(sdist.sdist): |
| 110 |
''' |
| 111 |
Write the version.json file to the sdist tarball build directory |
| 112 |
''' |
| 113 |
def make_release_tree(self, base_dir, files): |
| 114 |
sdist.sdist.make_release_tree(self, base_dir, files) |
| 115 |
write_version_file(base_dir) |
| 116 |
|
| 117 |
class PepperInstallData(install_data.install_data): |
| 118 |
''' |
| 119 |
Write the version.json file to the installation directory |
| 120 |
''' |
| 121 |
def run(self): |
| 122 |
install_cmd = self.get_finalized_command('install') |
| 123 |
install_dir = getattr(install_cmd, 'install_lib') |
| 124 |
write_version_file(install_dir) |
| 125 |
|
| 126 |
return install_data.install_data.run(self) |
| 127 |
|
| 128 |
if __name__ == '__main__': |
| 129 |
version, sha = get_version() |
| 130 |
|
| 131 |
setup(cmdclass={ |
| 132 |
'sdist': PepperSdist, |
| 133 |
'install_data': PepperInstallData, |
| 134 |
}, version=version, git_sha=sha, **setup_kwargs) |