-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christopher Doris
committed
Feb 11, 2022
0 parents
commit c307531
Showing
9 changed files
with
341 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.egg-info | ||
__pycache__ | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Christopher Rowley | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# pyjuliapkg | ||
|
||
## Install | ||
|
||
```sh | ||
pip install juliapkg | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[metadata] | ||
name = juliapkg | ||
version = 0.1.0 | ||
description = Julia version manager and package manager | ||
long_description = file: README.md | ||
long_description_content_type = text/markdown | ||
license = MIT | ||
classifiers = | ||
Framework :: Django | ||
License :: OSI Approved :: MIT License | ||
Programming Language :: Python :: 3 | ||
project_urls = | ||
Home = http://github.com/cjdoris/pyjuliapkg | ||
|
||
[options] | ||
packages = find: | ||
package_dir = | ||
=src | ||
install_requires = | ||
juliaup ~=0.1.1 | ||
semantic_version ~=2.9 | ||
|
||
[options.packages.find] | ||
where = src |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import re | ||
import semantic_version as sv | ||
|
||
|
||
Version = sv.Version | ||
|
||
|
||
class Compat: | ||
"""A Julia compat specifier.""" | ||
|
||
re_range = re.compile(r'^([~^])?([0-9]+)(?:\.([0-9]+)(?:\.([0-9]+))?)?$') | ||
|
||
def __init__(self, clauses=[]): | ||
self.clauses = list(clauses) | ||
|
||
def __str__(self): | ||
return ', '.join(str(clause) for clause in self.clauses) | ||
|
||
def __repr__(self): | ||
return f'{type(self).__name__}({self.clauses!r})' | ||
|
||
def __contains__(self, v): | ||
return any(v in clause for clause in self.clauses) | ||
|
||
def __and__(self, other): | ||
clauses = [] | ||
for clause1 in self.clauses: | ||
for clause2 in other.clauses: | ||
clause = clause1 & clause2 | ||
if clause is not None: | ||
clauses.append(clause) | ||
return Compat(clauses) | ||
|
||
@classmethod | ||
def parse(cls, verstr): | ||
"""Parse a Julia compat specifier from a string. | ||
A specifier is a comma-separated list of clauses. The prefixes '^', '~' and '==' | ||
are supported. No prefix is equivalent to '^'. | ||
""" | ||
clauses = [] | ||
if verstr.strip(): | ||
for part in verstr.split(','): | ||
part = part.strip() | ||
m = cls.re_range.match(part) | ||
if m is not None: | ||
prefix, major, minor, patch = m.groups() | ||
prefix = prefix or '^' | ||
assert prefix in ('^', '~') | ||
major = int(major) | ||
minor = None if minor is None else int(minor) | ||
patch = None if patch is None else int(patch) | ||
version = Version(major=major, minor=minor or 0, patch=patch or 0) | ||
if prefix == '^': | ||
if major != 0 or minor is None: | ||
nfixed = 1 | ||
elif minor != 0 or patch is None: | ||
nfixed = 2 | ||
else: | ||
nfixed = 3 | ||
elif prefix == '~': | ||
if minor is None: | ||
nfixed = 1 | ||
else: | ||
nfixed = 2 | ||
clause = Range(version, nfixed) | ||
elif part.startswith('=='): | ||
version = Version(part[2:]) | ||
clause = Eq(version) | ||
else: | ||
raise ValueError(f'invalid version: {part!r}') | ||
clauses.append(clause) | ||
return cls(clauses) | ||
|
||
|
||
class Eq: | ||
|
||
def __init__(self, version): | ||
self.version = version | ||
|
||
def __str__(self): | ||
return f'=={self.version}' | ||
|
||
def __repr__(self): | ||
return f'{type(self).__name__}({self.version!r})' | ||
|
||
def __contains__(self, v): | ||
if isinstance(v, Version): | ||
return v == self.version | ||
return False | ||
|
||
def __and__(self, other): | ||
if self.version in other: | ||
return self | ||
|
||
|
||
class Range: | ||
|
||
def __init__(self, version, nfixed): | ||
self.version = version | ||
self.nfixed = nfixed | ||
|
||
def __str__(self): | ||
v = self.version | ||
n = self.nfixed | ||
if n == 1: | ||
if v.major != 0: | ||
if v.patch != 0: | ||
return f'^{v.major}.{v.minor}.{v.patch}' | ||
elif v.minor != 0: | ||
return f'^{v.major}.{v.minor}' | ||
else: | ||
return f'^{v.major}' | ||
elif v.minor == 0 and v.patch == 0: | ||
return f'^0' | ||
else: | ||
assert False | ||
elif n == 2: | ||
if v.major == 0: | ||
if v.minor == 0: | ||
if v.patch == 0: | ||
return f'~0.0' | ||
else: | ||
return f'~0.0.{v.patch}' | ||
else: | ||
if v.patch == 0: | ||
return f'^0.{v.minor}' | ||
else: | ||
return f'^0.{v.minor}.{v.patch}' | ||
else: | ||
if v.patch == 0: | ||
return f'~{v.major}.{v.minor}' | ||
else: | ||
return f'~{v.major}.{v.minor}.{v.patch}' | ||
elif n == 3: | ||
if v.major == 0 and v.minor == 0: | ||
return f'^0.0.{v.patch}' | ||
else: | ||
assert False | ||
else: | ||
assert False | ||
|
||
def __repr__(self): | ||
return f'{type(self).__name__}({self.version!r}, {self.nfixed!r})' | ||
|
||
def __contains__(self, v): | ||
if isinstance(v, Version): | ||
n = self.nfixed | ||
v0 = self.version | ||
return (n < 1 or v.major == v0.major) and (n < 2 or v.minor == v0.minor) and (n < 3 or v.patch == v0.patch) and v >= v0 | ||
|
||
def __and__(self, other): | ||
if isinstance(other, Range): | ||
n0 = self.nfixed | ||
v0 = self.version | ||
n1 = other.nfixed | ||
v1 = other.version | ||
nmin = min(n0, n1) | ||
nmax = max(n0, n1) | ||
vmax = max(v0, v1) | ||
if (nmin < 1 or v0.major == v1.major) and (nmin < 2 or v0.minor == v1.minor) and (nmin < 3 or v0.patch == v1.patch): | ||
if vmax in self and vmax in other: | ||
return Range(vmax, nmax) | ||
elif isinstance(other, Eq): | ||
if other.version in self: | ||
return other | ||
else: | ||
return NotImplemented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import juliaup | ||
import shutil | ||
|
||
from subprocess import run | ||
from .releases import best_release | ||
from .compat import Version, Compat | ||
|
||
def version(exe): | ||
"""The version of the Julia executable.""" | ||
try: | ||
words = run([exe, '--version'], check=True, capture_output=True, encoding='utf8').stdout.strip().split() | ||
if words[0].lower() == 'julia' and words[1].lower() == 'version': | ||
return Version(words[2]) | ||
except: | ||
pass | ||
|
||
def find_julia(compat=None, best=False, install=False): | ||
"""Find a Julia executable compatible with compat. | ||
Args: | ||
compat: A juliapkg.compat.Compat specifying the allowed versions of Julia. | ||
install: If True, Julia will be installed (using JuliaUp) automatically if there | ||
is no compatible version. | ||
best: Use the best released compatible version of Julia. Implies install=True. | ||
As a special case, if best=True but JuliaUp is not installed and a compatible version | ||
of Julia is found in the PATH, that is used. The rationale is that if Julia is already | ||
installed, then the user is managing their own versions. | ||
""" | ||
# try juliaup if already installed | ||
jup = shutil.which('juliaup') | ||
if jup: | ||
return jup_find(compat, best=best, install=install) | ||
# try the PATH | ||
exe = shutil.which('julia') | ||
ver = version(exe) | ||
if ver is not None: | ||
if compat is None or ver in compat: | ||
return (exe, ver) | ||
# finally install juliaup and try that | ||
return jup_find(compat, best=best, install=install) | ||
|
||
def jup_find_best(compat=None): | ||
# find the best compatible release | ||
release = best_release(compat) | ||
if not release: | ||
raise Exception(f'no Julia release satisfies {compat}') | ||
v, _ = release | ||
compat = Compat.parse(f'=={v}') | ||
print(compat) | ||
# see if it is already installed | ||
ans = jup_find_current(compat) | ||
if ans: | ||
return ans | ||
# otherwise install it | ||
channel = f'{v.major}.{v.minor}.{v.patch}' | ||
juliaup.add(channel) | ||
# now find it | ||
ans = jup_find_current(compat) | ||
if ans: | ||
return ans | ||
# installing didn't work | ||
raise Exception(f'added JuliaUp channel {channel} but could not find Julia {version}') | ||
|
||
def jup_find_current(compat=None): | ||
meta = juliaup.meta() | ||
versions = [] | ||
for (verstr, info) in meta.get('InstalledVersions', {}).items(): | ||
# juliaup records '1.2.3' as '1.2.3+0~ARCH' | ||
# so we filter out the ARCH and any zeros in the build | ||
ver = Version(verstr.split('~')[0]) | ||
ver = Version(major=ver.major, minor=ver.minor, patch=ver.patch, partial=ver.partial, build=tuple(x for x in ver.build if x != '0')) | ||
if compat is None or ver in compat: | ||
exe = info['Executable'] | ||
ver = version(exe) | ||
if ver is not None: | ||
if compat is None or ver in compat: | ||
versions.append((exe, ver)) | ||
if versions: | ||
return max(versions, key=lambda x: x[1]) | ||
|
||
def jup_find(compat=None, best=False, install=False): | ||
if best: | ||
return jup_find_best(compat) | ||
ans = jup_find_current(compat) | ||
if ans: | ||
return ans | ||
if install: | ||
return jup_find_best(compat) | ||
raise Exception(f'JuliaUp does not have Julia {compat} installed') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import json | ||
|
||
from urllib.request import urlopen | ||
from .compat import Version | ||
|
||
RELEASES_URL = 'https://julialang-s3.julialang.org/bin/versions.json' | ||
RELEASES = None | ||
|
||
def releases(refresh=False): | ||
"""The parsed contents of Julia's versions.json.""" | ||
global RELEASES | ||
if RELEASES is None or refresh: | ||
with urlopen(RELEASES_URL) as fp: | ||
RELEASES = json.load(fp) | ||
return RELEASES | ||
|
||
def best_release(compat=None, stable=True): | ||
"""The best release compatible with compat.""" | ||
# TODO: check the arch/os/etc is compatible too. | ||
# TODO: query juliaup for this information? | ||
versions = [] | ||
for (verstr, info) in releases().items(): | ||
version = Version(verstr) | ||
if compat is None or version in compat: | ||
if info['stable'] or not stable: | ||
versions.append((version, info)) | ||
if versions: | ||
return max(versions, key=lambda x: x[0]) |