Skip to content

Commit 789207f

Browse files
committed
deps: apply tmpl
1 parent 08fbd3b commit 789207f

File tree

10 files changed

+198
-107
lines changed

10 files changed

+198
-107
lines changed

.github/workflows/python-package.yml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jobs:
1717
# python-3.6 in it: https://github.com/actions/setup-python/issues/544#issuecomment-1320295576
1818
# To fix it: use os: [ubuntu-20.04] instead
1919
os: [ubuntu-latest]
20-
python-version: [3.7, 3.8]
20+
python-version: [3.9, "3.10", 3.11, 3.12]
2121

2222
steps:
23-
- uses: actions/checkout@v2
23+
- uses: actions/checkout@v5
2424
- name: Set up Python ${{ matrix.python-version }}
2525
uses: actions/setup-python@v2
2626
with:
@@ -34,6 +34,11 @@ jobs:
3434
- name: Install npm dependencies
3535
run: |
3636
if [ -f package.json ]; then npm install; fi
37+
38+
# manually add module binary path to github ci
39+
echo "add node module path: $GITHUB_WORKSPACE/node_modules/.bin/"
40+
echo "$GITHUB_WORKSPACE/node_modules/.bin/" >> $GITHUB_PATH
41+
3742
- name: Install apt dependencies
3843
run: |
3944
if [ -f packages.txt ]; then cat packages.txt | xargs sudo apt-get install; fi
@@ -50,9 +55,14 @@ jobs:
5055
cd ..
5156
python setup.py install
5257
cd -
53-
sudo env "PATH=$PATH:$(npm bin)" pytest -v
5458
55-
- uses: actions/upload-artifact@v2
59+
if [ -f sudo_test ]; then
60+
sudo env "PATH=$PATH" pytest -v
61+
else
62+
pytest -v
63+
fi
64+
65+
- uses: actions/upload-artifact@v4
5666
if: failure()
5767
with:
5868
path: test/
@@ -63,10 +73,10 @@ jobs:
6373
strategy:
6474
matrix:
6575
os: [ubuntu-latest]
66-
python-version: [3.7, 3.8]
76+
python-version: [3.9, "3.10", 3.11, 3.12]
6777

6878
steps:
69-
- uses: actions/checkout@v2
79+
- uses: actions/checkout@v5
7080
- name: Set up Python ${{ matrix.python-version }}
7181
uses: actions/setup-python@v2
7282
with:
@@ -89,10 +99,10 @@ jobs:
8999
strategy:
90100
matrix:
91101
os: [ubuntu-latest]
92-
python-version: [3.7, 3.8]
102+
python-version: [3.9, "3.10", 3.11, 3.12]
93103

94104
steps:
95-
- uses: actions/checkout@v2
105+
- uses: actions/checkout@v5
96106

97107
- name: Set up Python ${{ matrix.python-version }}
98108
uses: actions/setup-python@v2

.github/workflows/python-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ jobs:
1414
runs-on: ubuntu-latest
1515

1616
steps:
17-
- uses: actions/checkout@v2
17+
- uses: actions/checkout@v5
1818
- name: Set up Python
19-
uses: actions/setup-python@v2
19+
uses: actions/setup-python@v5
2020
with:
2121
python-version: '3.x'
2222
- name: Install dependencies

_building/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ vcs/pykit3/k3handy/
1414

1515
# Publish python package:
1616

17-
- `make release` does the following steps:
17+
- `make build_setup_py` does the following steps:
1818
- Builds the `setup.py` and commit it.
1919
- Add a git tag with the name of `"v" + __init__.__ver__`.
2020

_building/__init__.py

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# coding: utf-8
33

4-
import imp
4+
import importlib.util
55
import sys
66

77
# sys.path.insert(0, os.path.abspath('..'))
@@ -13,10 +13,10 @@
1313
# __url__ = 'https://requests.readthedocs.io'
1414
# __version__ = '2.23.0'
1515

16-
__author__ = 'Zhang Yanpo'
17-
__author_email__ = 'drdr.xp@gmail.com'
18-
__license__ = 'MIT'
19-
__copyright__ = 'Copyright 2020 Zhang Yanpo'
16+
__author__ = "Zhang Yanpo"
17+
__author_email__ = "drdr.xp@gmail.com"
18+
__license__ = "MIT"
19+
__copyright__ = "Copyright 2020 Zhang Yanpo"
2020

2121

2222
# Configuration file for the Sphinx documentation builder.
@@ -31,14 +31,14 @@
3131
# ones.
3232
extensions = [
3333
"sphinx.ext.autodoc",
34-
'sphinx.ext.napoleon',
34+
"sphinx.ext.napoleon",
3535
# "sphinx.ext.intersphinx",
3636
# "sphinx.ext.todo",
3737
# "sphinx.ext.viewcode",
3838
]
3939

4040
# Add any paths that contain templates here, relative to this directory.
41-
templates_path = ['_templates']
41+
templates_path = ["_templates"]
4242

4343
# List of patterns, relative to source directory, that match files and
4444
# directories to ignore when looking for source files.
@@ -54,7 +54,7 @@
5454
# The theme to use for HTML and HTML Help pages. See the documentation for
5555
# a list of builtin themes.
5656
#
57-
html_theme = 'alabaster'
57+
html_theme = "alabaster"
5858

5959
# Add any paths that contain custom static files (such as style sheets) here,
6060
# relative to this directory. They are copied after the builtin static files,
@@ -63,19 +63,57 @@
6363
html_static_path = []
6464

6565

66+
def load_parent_package():
67+
"""
68+
Load the parent directory as a package module.
69+
70+
Returns:
71+
tuple: (package_name, package_module)
72+
"""
73+
import os
74+
75+
parent_dir = os.path.dirname(os.path.dirname(__file__))
76+
if parent_dir not in sys.path:
77+
sys.path.insert(0, parent_dir)
78+
79+
# Read the __init__.py file to get the package name
80+
init_file = os.path.join(parent_dir, "__init__.py")
81+
package_name = None
82+
83+
with open(init_file, "r") as f:
84+
for line in f:
85+
if line.strip().startswith("__name__"):
86+
# Extract package name from __name__ = "package_name"
87+
package_name = line.split("=")[1].strip().strip("\"'")
88+
break
89+
90+
if not package_name:
91+
# Fallback: use directory name
92+
package_name = os.path.basename(parent_dir)
93+
94+
# Load the module with proper package context using importlib
95+
spec = importlib.util.spec_from_file_location(package_name, init_file)
96+
pkg = importlib.util.module_from_spec(spec)
97+
sys.modules[package_name] = pkg # Add to sys.modules so relative imports work
98+
spec.loader.exec_module(pkg)
99+
100+
return package_name, pkg
101+
102+
66103
def sphinx_confs():
67104
"""
68-
Load repo dir as a package
105+
Load repo dir as a package
69106
70-
`readthedocs` use branch name as dir!
71-
Thus the following does not work::
107+
`readthedocs` use branch name as dir!
108+
Thus the following does not work::
72109
73-
import pk3proc
110+
import pk3proc
74111
"""
75112

76113
print("sys.path:", sys.path)
77114

78-
pkg = imp.load_source("foo", '../../__init__.py')
115+
package_name, pkg = load_parent_package()
116+
79117
return (
80118
pkg.__name__,
81119
pkg,

_building/build_readme.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,41 @@
22
# coding: utf-8
33

44
import doctest
5-
import imp
65
import os
76
import sys
87

98
import jinja2
109
import yaml
1110

11+
from __init__ import load_parent_package
12+
1213
# xxx/_building/build_readme.py
1314
this_base = os.path.dirname(__file__)
1415

1516
j2vars = {}
1617

1718
# let it be able to find indirectly dependent package locally
1819
# e.g.: `k3fs` depends on `k3confloader`
19-
sys.path.insert(0, os.path.abspath('..'))
20+
sys.path.insert(0, os.path.abspath(".."))
2021

2122
# load package name from __init__.py
22-
pkg = imp.load_source("_foo", '__init__.py')
23-
j2vars["name"] = pkg.__name__
23+
package_name, pkg = load_parent_package()
24+
j2vars["name"] = package_name
2425

2526

2627
def get_gh_config():
27-
with open('.github/settings.yml', 'r') as f:
28+
with open(".github/settings.yml", "r") as f:
2829
cont = f.read()
2930

3031
cfg = yaml.safe_load(cont)
31-
tags = cfg['repository']['topics'].split(',')
32+
tags = cfg["repository"]["topics"].split(",")
3233
tags = [x.strip() for x in tags]
33-
cfg['repository']['topics'] = tags
34+
cfg["repository"]["topics"] = tags
3435
return cfg
3536

3637

3738
cfg = get_gh_config()
38-
j2vars['description'] = cfg['repository']['description']
39+
j2vars["description"] = cfg["repository"]["description"]
3940

4041

4142
def get_examples(pkg):
@@ -44,41 +45,41 @@ def get_examples(pkg):
4445
es = parser.get_examples(doc)
4546
rst = []
4647
for e in es:
47-
rst.append('>>> ' + e.source.strip())
48+
rst.append(">>> " + e.source.strip())
4849
rst.append(e.want.strip())
4950

50-
rst = '\n'.join(rst)
51+
rst = "\n".join(rst)
5152

52-
for fn in ("synopsis.txt",
53-
"synopsis.py",
54-
):
53+
for fn in (
54+
"synopsis.txt",
55+
"synopsis.py",
56+
):
5557
try:
56-
with open(fn, 'r') as f:
57-
rst += '\n' + f.read()
58+
with open(fn, "r") as f:
59+
rst += "\n" + f.read()
5860

5961
except FileNotFoundError:
6062
pass
6163

6264
return rst
6365

6466

65-
j2vars['synopsis'] = get_examples(pkg)
66-
j2vars['package_doc'] = pkg.__doc__
67+
j2vars["synopsis"] = get_examples(pkg)
68+
j2vars["package_doc"] = pkg.__doc__
6769

6870

6971
def render_j2(tmpl_path, tmpl_vars, output_path):
70-
template_loader = jinja2.FileSystemLoader(searchpath='./')
71-
template_env = jinja2.Environment(loader=template_loader,
72-
undefined=jinja2.StrictUndefined)
72+
template_loader = jinja2.FileSystemLoader(searchpath="./")
73+
template_env = jinja2.Environment(
74+
loader=template_loader, undefined=jinja2.StrictUndefined
75+
)
7376
template = template_env.get_template(tmpl_path)
7477

7578
txt = template.render(tmpl_vars)
7679

77-
with open(output_path, 'w') as f:
80+
with open(output_path, "w") as f:
7881
f.write(txt)
7982

8083

8184
if __name__ == "__main__":
82-
render_j2('_building/README.md.j2',
83-
j2vars,
84-
'README.md')
85+
render_j2("_building/README.md.j2", j2vars, "README.md")

0 commit comments

Comments
 (0)