-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.py
141 lines (113 loc) · 4.57 KB
/
install.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
import sys
import os
import logging
import glob
import importlib
import shutil
import subprocess
from setuptools.command.build_py import build_py
# Where to install the pure-python executable and parameter file #
if not 'HEADAS' in os.environ:
raise RuntimeError('heasoft needs to be initialized before running this script')
# packages that use heasoftpy
# Typically, this means they have their code under
# $HEADAS/../{mission}/x86../lib/python/heasoftpy/{subpackage}
SUBPACKAGES = ['nicer', 'ixpe']
## --- setup logger --- ##
logger = logging.getLogger('heasoftpy-install')
logger.setLevel(logging.DEBUG)
# log to screen
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
tformat = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter('%(asctime)s - %(levelname)5s - %(message)s', tformat)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(ch)
## -------------------- ##
class HSPInstallCommand(build_py):
"""Run install.py to generate the wrappers before doing the standard install
This is triggered by tool.setuptools.cmdclass in pyproject.toml
"""
def run(self):
_do_install()
super().run()
def _do_install():
logger.info('-'*60)
logger.info('Starting heasoftpy installation ...')
# python wrappers for heasoft-native tools
_create_py_wrappers()
# add subpackages
_add_sub_packages()
## ---------------------------------- ##
## python wrappers for built-in tools ##
def _create_py_wrappers():
# the following prevents sub-package from being imported
# as they may depend on the functions in heasoftpy,that we will install here.
os.environ['__INSTALLING_HSP'] = 'yes'
# add heasoftpy location to sys.path as it is not installed yet
current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, current_dir)
from heasoftpy.utils import generate_py_code
logger.info('-'*30)
logger.info('Creating python wrappers ...')
try:
list_of_files = generate_py_code()
# add the generated files to heasoftpy.egg-info so they are
# installed correctly
with open(f'heasoftpy.egg-info/SOURCES.txt', 'a') as fp:
fp.write('\n' + ('\n'.join(list_of_files)))
except:
logger.error('Failed in generating python wrappers')
raise
logger.info('Python wrappers created sucessfully!')
logger.info('-'*30)
# remove the __INSTALLING_HSP variable we added at the start
del os.environ['__INSTALLING_HSP']
## ---------------------------------- ##
## ---------------------------------- ##
## python wrappers for built-in tools ##
def _add_sub_packages():
"""Find and install subpackages from other places in heasoft"""
if not 'HEADAS' in os.environ:
msg = 'HEADAS not defined. Please initialize Heasoft!'
logger.error(msg)
raise ValueError(msg)
# find installation folder name
headas = os.environ['HEADAS']
inst_dir = os.path.basename(headas)
# loop through subpackages
logger.info('-'*30)
logger.info('Looking for subpackages ...')
list_of_files = []
for subpackage in SUBPACKAGES:
pth = f"{headas}/../{subpackage}/{inst_dir}/lib/python/heasoftpy/{subpackage}"
if os.path.exists(pth):
logger.info(f'Found {subpackage} ...')
if os.path.exists(f'heasoftpy/{subpackage}'):
lines1 = []
if os.path.exists(f'{pth}/__init__.py'):
lines1 = open(f'{pth}/__init__.py').readlines()
lines2 = open(f'heasoftpy/{subpackage}/__init__.py').readlines()
os.system(f'cp -r -n {pth}/* heasoftpy/{subpackage}/')
with open(f'heasoftpy/{subpackage}/__init__.py', 'w') as fp:
fp.write('\n'.join(lines2+lines1))
else:
os.system(f'cp -r {pth} heasoftpy/')
list_of_files += [lf for lf in
glob.glob(f'heasoftpy/{subpackage}/**', recursive=True)
if lf[-3:] == '.py']
else:
logger.info(f'No {subpackage} subpackage, skipping ...')
# add the generated files to heasoftpy.egg-info so they are
# installed correctly
with open(f'heasoftpy.egg-info/SOURCES.txt', 'a') as fp:
fp.write('\n' + ('\n'.join(list_of_files)))
if __name__ == '__main__':
help_txt = """This script is not meant to run directly.
Please use: pip install .
Then, copy build/lib/heasoftpy to a location where python can find it,
or add build/lib to your PYTHONPATH.
"""
print(help_txt)