-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
137 lines (118 loc) · 4.04 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import json
from setuptools import setup
# Package Metadata filename
METADATA_FILENAME = 'freeport/package_metadata.json'
BASEPATH = os.path.dirname(os.path.abspath(__file__))
# Create a dictionary of our arguments, this way this script can be imported
# without running setup() to allow external scripts to see the setup settings.
setup_arguments = {
'name': 'freeport',
'version': '0.1.9',
'author': 'Yash Bathia',
'author_email': '[email protected]',
'url': 'http://github.com/yashbathia/freeport',
'license': 'ISC',
'packages': ['freeport'],
'long_description': 'Simple Python library to free a port',
'description': 'Simple Python library to free a port',
'classifiers': [
'License :: OSI Approved :: ISC License (ISCL)',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python',
],
'package_data': {
'freeport': ['package_metadata.json']
},
'include_package_data': True,
}
class Git(object):
"""
Simple wrapper class to the git command line tools
"""
version_list = ['0', '7', '0']
def __init__(self, version=None):
if version:
self.version_list = version.split('.')
@property
def version(self):
"""
Generate a Unique version value from the git information
:return:
"""
git_rev = len(os.popen('git rev-list HEAD').readlines())
if git_rev != 0:
self.version_list[-1] = '%d' % git_rev
version = '.'.join(self.version_list)
return version
@property
def branch(self):
"""
Get the current git branch
:return:
"""
return os.popen('git rev-parse --abbrev-ref HEAD').read().strip()
@property
def hash(self):
"""
Return the git hash for the current build
:return:
"""
return os.popen('git rev-parse HEAD').read().strip()
@property
def origin(self):
"""
Return the fetch url for the git origin
:return:
"""
for item in os.popen('git remote -v'):
split_item = item.strip().split()
if split_item[0] == 'origin' and split_item[-1] == '(push)':
return split_item[1]
def add_scripts_to_package():
"""
Update the "scripts" parameter of the setup_arguments with any scripts
found in the "scripts" directory.
:return:
"""
global setup_arguments
if os.path.isdir('scripts'):
setup_arguments['scripts'] = [
os.path.join('scripts', f) for f in os.listdir('scripts')
]
def get_and_update_package_metadata():
"""
Update the package metadata for this package if we are building the package.
:return:metadata - Dictionary of metadata information
"""
global setup_arguments
global METADATA_FILENAME
if not os.path.exists('.git') and os.path.exists(METADATA_FILENAME):
with open(METADATA_FILENAME) as fh:
metadata = json.load(fh)
else:
git = Git(version=setup_arguments['version'])
metadata = {
'version': git.version,
'long_description': 'Tool to pip install missing imports and more',
'git_hash': git.hash,
'git_origin': git.origin,
'git_branch': git.branch
}
for readme_file in ['README.rst', 'README.md', 'README.txt']:
if os.path.exists(readme_file):
with open(readme_file) as file_handle:
metadata['long_description'] = file_handle.read()
break
with open(METADATA_FILENAME, 'w') as fh:
json.dump(metadata, fh)
return metadata
if __name__ == '__main__':
# We're being run from the command line so call setup with our arguments
metadata = get_and_update_package_metadata()
setup_arguments['version'] = metadata['version']
setup_arguments['long_description'] = metadata['long_description']
add_scripts_to_package()
setup(**setup_arguments)