Skip to content

Commit

Permalink
add ci && format code
Browse files Browse the repository at this point in the history
  • Loading branch information
张一涵 committed Apr 23, 2024
1 parent 8c39381 commit e2c3cf1
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 45 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI
on: [ push, pull_request ]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
OS: [ Ubuntu, macOS, Windows ]
python-version: [ "3.8" ]
include:
- os: Ubuntu
image: ubuntu-22.04
- os: Windows
image: windows-2022
- os: macOS
image: macos-12
fail-fast: false
defaults:
run:
shell: bash
steps:

- name: Checkout
uses: actions/checkout@v2

- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Static check
run: |
pip install isort==5.11.5 black==22.12.0 mypy==1.9.0
isort --check .
black --check .
mypy .
- name: Test
run: |
pip install .
pip install pytest coverage
coverage run --source=jillw -m pytest -s && coverage report && coverage xml
- name: Python Cov
uses: codecov/codecov-action@v2
with:
files: coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
41 changes: 41 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
release:
types: [ published ]
# 提交代码时,手动触发


permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
13 changes: 8 additions & 5 deletions jillw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from __future__ import annotations
import venv
import wisepy2
import shutil

import json
import shutil
import sys
import venv
from pathlib import Path
from typing import Any, Dict, Union

import wisepy2

root = Path("~/.jlenvs").expanduser()


class Ops:
@staticmethod
def get_config(new_conf: "dict | None" = None) -> dict:
def get_config(new_conf: "dict | None" = None) -> Union[Dict, Any]:
root.mkdir(mode=0o755, parents=True, exist_ok=True)
config_file = root.joinpath("config.json")
if config_file.exists():
Expand Down Expand Up @@ -48,7 +51,7 @@ def create_(
upstream=upstream,
unstable=unstable,
skip_symlinks=True,
reinstall=True
reinstall=True,
)
print(wisepy2.Green(f"Environment {name} created at {envdir.as_posix()}."))

Expand Down
47 changes: 26 additions & 21 deletions jillw/cli.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from __future__ import annotations
from jillw import Ops
from wisepy2 import wise
from pathlib import Path
import os

import contextlib
import io
import sys
import os
import shlex
import subprocess
import sys
from pathlib import Path

import wisepy2
import shlex
from wisepy2 import wise

from jillw import Ops


def find_julia_binary_dir(env: Path):
Expand Down Expand Up @@ -37,11 +40,10 @@ def expect_no_stdout():
msg = sio.getvalue()
print(wisepy2.Red(msg))


def append_PATH(PATH: str, *paths: Path):
return os.pathsep.join([
*map(os.path.normpath, paths),
*PATH.split(os.pathsep)
])
return os.pathsep.join([*map(os.path.normpath, paths), *PATH.split(os.pathsep)])


def run_with_activated_env(cmd: list[str]):
config = Ops.get_config()
Expand All @@ -67,18 +69,21 @@ def run_with_activated_env(cmd: list[str]):
del envdict["PYTHONHOME"]
except KeyError:
pass
envdict['PATH'] = append_PATH(os.environ["PATH"], jlbindir, env, env / "Scripts")
envdict["PATH"] = append_PATH(
os.environ["PATH"], jlbindir, env, env / "Scripts"
)
else:
envdict = os.environ.copy()
envdict["VIRTUAL_ENV"] = str(env)
try:
del envdict["PYTHONHOME"]
except KeyError:
pass
envdict['PATH'] = append_PATH(os.environ["PATH"], jlbindir, env, env / "bin")
envdict["PATH"] = append_PATH(os.environ["PATH"], jlbindir, env, env / "bin")

subprocess.run(cmd, env=envdict, shell=False)


class Main:
@staticmethod
def switch(name: str):
Expand All @@ -103,42 +108,42 @@ def create(
confirm: bool = False,
unstable: bool = False,
):
"""Create a new Julia environment
"""
"""Create a new Julia environment"""
with cmd_session():
Ops.create_(name, upstream, version, confirm, unstable)

@staticmethod
def remove(name: str):
"""Remove a Julia environment
"""
"""Remove a Julia environment"""
with cmd_session():
Ops.remove_(name)

@staticmethod
def list():
"""List all Julia environments
"""
"""List all Julia environments"""
with cmd_session():
for each in Ops.list():
print(wisepy2.Purple(each.name, "=>", each.as_posix(), sep=" "))

@staticmethod
def devhere():
"""Create a Development.toml at the current directory.
"""
"""Create a Development.toml at the current directory."""
with cmd_session():
from . configloader import write_empty_config_
from .configloader import write_empty_config_

write_empty_config_()


def main():
wise(Main)()


def julia():
extra_options: list[str] = []

if os.environ.get("DEV", "").strip():
from jillw.configloader import get_options

extra_options = get_options()

run_with_activated_env(["julia", *extra_options, *sys.argv[1:]])
51 changes: 32 additions & 19 deletions jillw/configloader.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations
import tomli

import json
import os
import pathlib
from typing import Any, List, Union

import tomli
import wisepy2
import json

EMPTY_CONFIG = \
r"""
EMPTY_CONFIG = r"""
[julia]
min-latency = false
quiet-start = false
Expand All @@ -26,33 +28,42 @@
files = []
"""


def get_bool(conf: dict, name: str) -> bool | None:
if conf.get(name, name):
var = conf[name]
if not isinstance(var, bool):
raise ValueError("'{}' must be a boolean".format(name))
return var
return None


def get_int(conf: dict, name: str) -> int | None:
if conf.get(name, name):
var = conf[name]
if not isinstance(var, int):
raise ValueError("'{}' must be a int".format(name))
return var
return None


def get_str_list(conf: dict, name: str) -> list[str] | None:
def get_str_list(conf: dict, name: str) -> Union[List[str], Any]:
if conf.get(name, name):
var = conf[name]
if not isinstance(var, list) and not (all(isinstance(e, str) for e in var)):
raise ValueError("'{}' must be a string list".format(name))
return var
return None


def get_str(conf: dict, name: str) -> str | None:
if conf.get(name, name):
var = conf[name]
if not isinstance(var, str):
raise ValueError("'{}' must be a string".format(name))
return var
return None


def write_empty_config_():
cwd = pathlib.Path(os.getcwd())
Expand All @@ -64,32 +75,33 @@ def write_empty_config_():
with dev.open("w", encoding="utf-8") as f:
f.write(EMPTY_CONFIG)

def get_options() -> list[str]:

def get_options() -> List[str]:
cwd = pathlib.Path(os.getcwd())
dev = cwd / "Development.toml"
if dev.is_file():
io = dev.open('rb')
conf = tomli.load(io) # type: ignore
io = dev.open("rb")
conf = tomli.load(io)
if not isinstance(conf, dict):
return []
conf = conf.get("julia")
if not isinstance(conf, dict):
julia_conf = conf.get("julia")
if not isinstance(julia_conf, dict):
return []
else:
return []
opts: list[str] = []
opts: List = []

if get_bool(conf, 'min-latency'):
if get_bool(julia_conf, "min-latency"):
opts.append("--compile=min")
opts.append("-O0")

if project := get_str(conf, "project"):
if project := get_str(julia_conf, "project"):
opts.append("--project={}".format(project))

if get_bool(conf, "interactive"):
opts.append("-i")

if get_bool(conf, 'quiet-start'):
if get_bool(conf, "quiet-start"):
opts.append("--quiet")

if sysimage := get_str(conf, "sysimage"):
Expand All @@ -99,18 +111,19 @@ def get_options() -> list[str]:
if get_bool(conf, "no-startup-file"):
opts.append("--startup-file=no")

if preload_modules := get_str_list(conf, 'using'):
if preload_modules := get_str_list(conf, "using"):
for each in preload_modules:
opts.append("-e")
opts.append("using {}".format(each))

if preincluded_files := get_str_list(conf, 'files'):
if preincluded_files := get_str_list(conf, "files"):
for file in preincluded_files:
opts.append("-e")
opts.append(
'include(' +
'raw' +json.dumps(str(cwd / file), ensure_ascii=False) +
')'
"include("
+ "raw"
+ json.dumps(str(cwd / file), ensure_ascii=False)
+ ")"
)

return opts
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
jillw = 'jillw.cli:main'
julia = 'jillw.cli:julia'

[tool.poetry.group.test.dependencies]
pytest = "^7.2.2"
pytest-cov = "^4.1.0"

[tool.poetry.group.dev.dependencies]
isort = "5.11.5"
black = "22.12.0"
mypy = "^1.9.0"

[tool.isort]
profile = "black"
skip = [".venv", "logs"]


[tool.mypy]
python_version = "3.8"
warn_return_any = true
warn_unused_configs = true
check_untyped_defs = true
ignore_missing_imports = true
exclude = [".venv", "logs"]
Loading

0 comments on commit e2c3cf1

Please sign in to comment.