forked from psychopy/psychopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createInitFile.py
160 lines (136 loc) · 5.22 KB
/
createInitFile.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Writes the current version, build platform etc.
"""
from __future__ import absolute_import, print_function
from setuptools.config import read_configuration
import os, copy, platform, subprocess
thisLoc = os.path.split(__file__)[0]
# import versioneer
# get version from file
with open('version') as f:
version = f.read().strip()
def createInitFile(dist=None, version=None, sha=None):
"""Create psychopy/__init__.py
:param:`dist` can be:
None:
writes __version__
'sdist':
for python setup.py sdist - writes git id (__git_sha__)
'bdist':
for python setup.py bdist - writes git id (__git_sha__)
and __build_platform__
"""
# get default values if None
if version is None:
with open(os.path.join(thisLoc,'version')) as f:
version = f.read().strip()
if sha is None:
sha = _getGitShaString(dist)
platformStr = _getPlatformString(dist)
metadata = read_configuration('setup.cfg')['metadata']
infoDict = {'version': version,
'author': metadata['author'],
'author_email': metadata['author_email'],
'maintainer_email': metadata['maintainer_email'],
'url': metadata['url'],
'download_url': metadata['download_url'],
'license': metadata['license'],
'shaStr': sha,
'platform': platformStr}
# write it
with open(os.path.join(thisLoc, 'psychopy','__init__.py'), 'w') as f:
outStr = template.format(**infoDict)
f.write(outStr)
print('wrote init for ', version, sha)
# and return it
return outStr
template = """#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Part of the PsychoPy library
# Copyright (C) 2018 Jonathan Peirce
# Distributed under the terms of the GNU General Public License (GPL).
# --------------------------------------------------------------------------
# This file is automatically generated during build (do not edit directly).
# --------------------------------------------------------------------------
import os
import sys
__version__ = '{version}'
__license__ = '{license}'
__author__ = '{author}'
__author_email__ = '{author_email}'
__maintainer_email__ = '{maintainer_email}'
__url__ = '{url}'
__download_url__ = '{download_url}'
__git_sha__ = '{shaStr}'
__build_platform__ = '{platform}'
__all__ = ["gui", "misc", "visual", "core",
"event", "data", "sound", "microphone"]
# for developers the following allows access to the current git sha from
# their repository
if __git_sha__ == 'n/a':
import subprocess
# see if we're in a git repo and fetch from there
try:
thisFileLoc = os.path.split(__file__)[0]
output = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'],
cwd=thisFileLoc, stderr=subprocess.PIPE)
except Exception:
output = False
if output:
__git_sha__ = output.strip() # remove final linefeed
# update preferences and the user paths
if 'installing' not in locals():
from psychopy.preferences import prefs
for pathName in prefs.general['paths']:
sys.path.append(pathName)
from psychopy.tools.versionchooser import useVersion, ensureMinimal
"""
def _getGitShaString(dist=None, sha=None):
"""If generic==True then returns empty __git_sha__ string
"""
shaStr = 'n/a'
if dist is not None:
proc = subprocess.Popen('git rev-parse --short HEAD',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd='.', shell=True)
repo_commit, _ = proc.communicate()
del proc # to get rid of the background process
if repo_commit:
shaStr = "{}".format(repo_commit.strip())
if shaStr.startswith("b'"):
shaStr = shaStr.replace("b'", "").replace("'", "")
else:
shaStr = 'n/a'
#this looks neater but raises errors on win32
# output = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).split()[0]
# if output:
# shaStr = output
return shaStr
def _getPlatformString(dist=None):
"""If generic==True then returns empty __build_platform__ string
"""
if dist=='bdist':
# get platform-specific info
if os.sys.platform == 'darwin':
OSXver, _, architecture = platform.mac_ver()
systemInfo = "OSX_%s_%s" % (OSXver, architecture)
elif os.sys.platform == 'linux':
systemInfo = '%s_%s_%s' % (
'Linux',
':'.join([x for x in platform.dist() if x != '']),
platform.release())
elif os.sys.platform == 'win32':
ver=os.sys.getwindowsversion()
if len(ver[4])>0:
systemInfo = "win32_v%i.%i.%i (%s)" %(ver[0], ver[1], ver[2], ver[4])
else:
systemInfo = "win32_v%i.%i.%i" % (ver[0], ver[1], ver[2])
else:
systemInfo = platform.system() + platform.release()
else:
systemInfo = "n/a"
return systemInfo
if __name__ == "__main__":
createInitFile()