-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinfo.py
More file actions
173 lines (144 loc) · 5.88 KB
/
info.py
File metadata and controls
173 lines (144 loc) · 5.88 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
from bionetgen.core.utils.logging import BNGLogger
class BNGInfo:
"""
Used by the Cement app to execute the info subcommand, which involves gathering, preparing, and printing
relevant version and path information.
The gatherInfo() method finds and stores all necessary information.
The messageGeneration() method prepares this information in a text string.
The run() method simply outputs all the information.
Requires a configuration file. An additional set of arguments are optional.
"""
def __init__(self, config, args=None, app=None):
self.config = config
self.args = args
self.app = app
self.logger = BNGLogger(app=self.app)
def gatherInfo(self):
"""
Gathers information about relevant versions and paths into a dictionary.
"""
import subprocess
import os
import bionetgen
import numpy
import pandas
import roadrunner
self.logger.debug("Gathering info", loc=f"{__file__} : BNGInfo.gatherInfo()")
self.info = {}
# Add some description for the following information
self.info["\nThe following are related to BioNetGen and its execution"] = ""
self.logger.debug("BNG info", loc=f"{__file__} : BNGInfo.gatherInfo()")
# Get BNG version
with open(
os.path.join(
*[os.path.dirname(bionetgen.__file__), "assets", "BNGVERSION"]
),
"r",
) as f:
read_data = f.read()
self.info["BNG version"] = read_data[10:15]
# Get BNG2.pl path
self.info["BNG2.pl path"] = (
self.config.get("bionetgen", "bngpath") + " (the main executable for BNG)"
)
self.logger.debug("Perl info", loc=f"{__file__} : BNGInfo.gatherInfo()")
# Get Perl version
# Read in CLI text
result = subprocess.run(["perl", "-v"], stdout=subprocess.PIPE)
text = str(result.stdout)
# Find start & end indices
num_start = text.find("(v") + 2
num_end = text.find(")")
# Save version info
self.info["Perl version"] = text[num_start:num_end] + " (used to run BNG2.pl)"
# Get NFsim version (if available on PATH or adjacent to BNG2.pl)
self.logger.debug("NFsim info", loc=f"{__file__} : BNGInfo.gatherInfo()")
nf_version_text = "not found"
try:
# Try the standard PATH lookup first
result = subprocess.run(
["NFsim", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=10,
)
if result.returncode == 0:
nf_version_text = result.stdout.strip().splitlines()[0]
else:
nf_version_text = f"exit {result.returncode}"
except FileNotFoundError:
# If NFsim isn't on PATH, attempt to locate it relative to BNG2.pl
try:
bng2_path = self.config.get("bionetgen", "bngpath")
bng2_dir = os.path.dirname(bng2_path)
candidates = [
os.path.join(bng2_dir, "bin", "NFsim"),
os.path.join(bng2_dir, "bin", "NFsim.exe"),
]
for cmd in candidates:
if os.path.isfile(cmd):
result = subprocess.run(
[cmd, "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=10,
)
if result.returncode == 0:
nf_version_text = result.stdout.strip().splitlines()[0]
break
except Exception:
pass
except Exception as e:
nf_version_text = f"error: {e}"
self.info["NFsim version"] = nf_version_text
self.logger.debug("PyBNG info", loc=f"{__file__} : BNGInfo.gatherInfo()")
# Get CLI version
with open(
os.path.join(*[os.path.dirname(bionetgen.__file__), "assets", "VERSION"]),
"r",
) as f:
read_data = f.read()
self.info["CLI version"] = read_data
# Get pyBNG path
self.info["pyBNG path"] = (
os.path.dirname(bionetgen.__file__) + " (the PyBNG installation)"
)
self.logger.debug(
"Info on installed python libraries",
loc=f"{__file__} : BNGInfo.gatherInfo()",
)
# Add some description for the following information
self.info["\nThe following libraries are required by PyBioNetGen"] = ""
# Get numpy version
self.info["numpy version"] = numpy.version.version
# Get pandas version
self.info["pandas version"] = pandas.__version__
# Get libRoadRunner version
text = roadrunner.getVersionStr()
self.info["libRoadRunner version"] = text[0:5]
return self.info
def messageGeneration(self):
"""
Takes the dictionary created by gatherInfo() and
converts it to a string of text for printing.
"""
self.logger.debug(
"Generating message", loc=f"{__file__} : BNGInfo.messageGeneration()"
)
self.message = " "
# Create lines of message using self.info dictionary
message_lines = []
for key in self.info:
text = str(key + ": " + self.info[key] + "\n")
message_lines.append(text)
# Join lines to create entire message
self.message = "".join((message_lines))
return self.message
def run(self):
"""
Simply prints out the created information message.
"""
self.logger.debug("Printing message", loc=f"{__file__} : BNGInfo.run()")
print(self.message)