-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconvenience.jl
More file actions
181 lines (165 loc) · 4.63 KB
/
convenience.jl
File metadata and controls
181 lines (165 loc) · 4.63 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
174
175
176
177
178
179
180
181
@static if VERSION >= v"0.7.0-"
using InteractiveUtils: versioninfo
else
versioninfo(io; verbose=false) = Base.versioninfo(io, verbose)
end
function envinfo(io::IO = stdout; verbosity::Int = 1)
if verbosity > 0
versioninfo(io; verbose = verbosity > 1)
println(io)
end
for ex in [:(PyCall.pyprogramname),
:(PyCall.pyversion),
:(PyCall.libpython),
:(PyCall.conda),
:(pyversion("IPython")),
:(pyversion("julia")),
]
Base.show_unquoted(io, ex)
print(io, " = ")
show(io, eval(ex))
println(io)
end
nothing
end
function pkg_resources_version(name)
try
return pyimport("pkg_resources")[:get_distribution](name)[:version]
catch err
if err isa PyCall.PyError || err isa KeyError
return
end
rethrow()
end
end
function _pyversion(name)
package = try
pyimport(name)
catch err
if ! (err isa PyCall.PyError)
rethrow()
end
return
end
try
return package[:__version__]
catch err
if ! (err isa KeyError)
rethrow()
end
end
end
function pyversion(name)
version = pkg_resources_version(name)
if version !== nothing
return version
end
return _pyversion(name)
end
function yes_or_no(prompt = string("Type \"yes\" and press enter if ",
"you want to run this command.");
input = stdin,
output = stdout)
print(output, prompt, " [yes/no]: ")
answer = readline(input)
if answer == "yes"
return true
elseif answer == "no"
return false
end
@warn "Please enter \"yes\" or \"no\". Got: $answer"
return false
end
conda_packages = ("ipython", "pytest")
NOT_INSTALLABLE = (false, "", Nothing)
function condajl_installation(package)
if PyCall.conda && package in conda_packages
message = """
Installing $package via Conda.jl
Execute?:
Conda.add($package)
"""
install = () -> Conda.add(package)
return (true, message, install)
end
return NOT_INSTALLABLE
end
function conda_installation(package)
conda = joinpath(dirname(PyCall.pyprogramname), "conda")
if isfile(conda) && package in conda_packages
prefix = dirname(dirname(PyCall.pyprogramname))
command = `$conda install --prefix $prefix -c conda-forge $package`
message = """
Installing $package with $conda
Execute?:
$command
"""
install = () -> run(command)
return (true, message, install)
end
return NOT_INSTALLABLE
end
function pip_installation(package)
if package in (conda_packages...,
"mock", "ipython-dev", "ipython-pre", "julia")
args = package
if package == "ipython-dev"
args = `"git+git://github.com/ipython/ipython#egg=ipython"`
elseif package == "ipython-pre"
args = `--pre ipython`
end
command = `$(PyCall.pyprogramname) -m pip install --upgrade $args`
message = """
Installing $package for $(PyCall.pyprogramname)
Execute?:
$command
"""
install = () -> run(command)
return (true, message, install)
end
return NOT_INSTALLABLE
end
function install_dependency(package; force=false, dry_run=false)
for check_installer in [condajl_installation,
conda_installation,
pip_installation]
found, message, install = check_installer(package)
if found
@info message
if !dry_run && (force || yes_or_no())
install()
end
return
end
end
@warn "Installing $package not supported."
end
function test_ipython_jl(args=``; inprocess=false, kwargs...)
if inprocess
test_ipython_jl_inprocess(args; kwargs...)
else
test_ipython_jl_cli(args)
end
end
function test_ipython_jl_inprocess(args; revise=true, check=true)
IPython._start_ipython(:ipython_options) # setup ipython_jl.core._Main
if revise
pyimport("ipython_jl")[:revise]()
end
cd(@__DIR__) do
code = pyimport("pytest")[:main](collect(args))
if !check
return code
end
if code != 0
error("$(`pytest $args`) failed with code $code")
end
end
end
function test_ipython_jl_cli(args)
command = `$(PyCall.pyprogramname) -m pytest $args`
@info command
cd(@__DIR__) do
run(command)
end
end