Skip to content

Commit fee671a

Browse files
authored
fixing install from src package & CI release (#16027)
* debug install * hotfix * local * refactor * lit swap * pruning
1 parent ec336bc commit fee671a

File tree

5 files changed

+65
-58
lines changed

5 files changed

+65
-58
lines changed

.github/actions/pkg-install/action.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ runs:
3333
python -c "import ${{ env.PKG_IMPORT }}; print(${{ env.PKG_IMPORT }}.__version__)"
3434
shell: bash
3535

36-
# FIXME !!!
37-
# - name: Install package - archive
38-
# working-directory: ${{ inputs.pkg-folder }}
39-
# run: |
40-
# pip install ${PKG_SOURCE} ${{ inputs.pip-flags }}
41-
# pip list | grep lightning
42-
# python -c "import ${{ env.PKG_IMPORT }}; print(${{ env.PKG_IMPORT }}.__version__)"
43-
# shell: bash
36+
- name: Install package - archive
37+
working-directory: ${{ inputs.pkg-folder }}
38+
run: |
39+
pip install ${PKG_SOURCE} ${{ inputs.pip-flags }}
40+
pip list | grep lightning
41+
python -c "import ${{ env.PKG_IMPORT }}; print(${{ env.PKG_IMPORT }}.__version__)"
42+
shell: bash

.github/workflows/_build-packages.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ jobs:
5050
python-version: 3.9
5151

5252
- run: python -c "print('NB_DIRS=' + str(2 if '${{ matrix.pkg-name }}' == 'pytorch' else 1))" >> $GITHUB_ENV
53-
- uses: ./.github/actions/pkg-check
53+
- name: Build & check package
54+
uses: ./.github/actions/pkg-check
5455
with:
5556
pkg-name: ${{ matrix.pkg-name }}
5657
nb-dirs: ${{ env.NB_DIRS }}

.github/workflows/ci-pkg-install.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ jobs:
5858

5959
- run: |
6060
python -c "print('PKG_DIR=' + {'notset': 'lightning'}.get('${{matrix.pkg-name}}', '${{matrix.pkg-name}}'))" >> $GITHUB_ENV
61-
- uses: ./.github/actions/pkg-install
61+
- name: Install package - wheel & archive
62+
uses: ./.github/actions/pkg-install
6263
with:
6364
pkg-folder: dist/${{ env.PKG_DIR }}
6465
pkg-name: ${{ matrix.pkg-name }}

.github/workflows/release-pypi.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ jobs:
122122
needs: build-packages
123123
if: startsWith(github.event.ref, 'refs/tags') || github.event_name == 'release'
124124
steps:
125+
- uses: actions/checkout@v3 # needed for local action bellow
125126
- uses: actions/download-artifact@v3
126127
with:
127128
name: dist-packages-${{ github.sha }}
@@ -152,6 +153,7 @@ jobs:
152153
needs: [build-packages, waiting]
153154
if: startsWith(github.event.ref, 'refs/tags') || github.event_name == 'release'
154155
steps:
156+
- uses: actions/checkout@v3 # needed for local action bellow
155157
- uses: actions/download-artifact@v3
156158
with:
157159
name: dist-packages-${{ github.sha }}

setup.py

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
c) validate packages and publish to PyPI
4141
"""
4242
import contextlib
43+
import glob
4344
import os
4445
import tempfile
4546
from importlib.util import module_from_spec, spec_from_file_location
@@ -49,7 +50,7 @@
4950
import setuptools
5051
import setuptools.command.egg_info
5152

52-
_PACKAGE_NAME = os.environ.get("PACKAGE_NAME", "lightning")
53+
_PACKAGE_NAME = os.environ.get("PACKAGE_NAME")
5354
_PACKAGE_MAPPING = {
5455
"lightning": "lightning",
5556
"pytorch": "pytorch_lightning",
@@ -87,22 +88,17 @@ def _set_manifest_path(manifest_dir: str, aggregate: bool = False) -> Generator:
8788
# aggregate all MANIFEST.in contents into a single temporary file
8889
manifest_path = _named_temporary_file(manifest_dir)
8990
mapping = _PACKAGE_MAPPING.copy()
90-
del mapping["lightning"]
91-
lines = ["include src/lightning/version.info\n"]
91+
lines = ["include src/lightning/version.info\n", "include requirements/base.txt\n"]
92+
# load manifest and aggregated all manifests
9293
for pkg in mapping.values():
93-
pkg_path = os.path.join(_PATH_SRC, pkg)
94-
if not os.path.exists(pkg_path):
95-
# this function is getting called with `pip install .` and `pip install *.tar.gz`, however, it only
96-
# should be called with the former. i haven't found a way to differentiate the two so this is the hacky
97-
# solution to avoid an error
98-
print(f"{pkg_path!r} does not exist")
99-
yield
100-
return
101-
with open(os.path.join(pkg_path, "MANIFEST.in")) as fh:
102-
lines.extend(fh.readlines())
94+
pkg_manifest = os.path.join(_PATH_SRC, pkg, "MANIFEST.in")
95+
if os.path.isfile(pkg_manifest):
96+
with open(pkg_manifest) as fh:
97+
lines.extend(fh.readlines())
10398
# convert lightning_foo to lightning/foo
10499
for new, old in mapping.items():
105-
lines = [line.replace(old, f"lightning/{new}") for line in lines]
100+
lines += [ln.replace(old, f"lightning/{new}") for ln in lines]
101+
lines = sorted(set(filter(lambda ln: not ln.strip().startswith("#"), lines)))
106102
with open(manifest_path, mode="w") as fp:
107103
fp.writelines(lines)
108104
else:
@@ -122,41 +118,49 @@ def _set_manifest_path(manifest_dir: str, aggregate: bool = False) -> Generator:
122118
if __name__ == "__main__":
123119
assistant = _load_py_module(name="assistant", location=os.path.join(_PATH_ROOT, ".actions", "assistant.py"))
124120

125-
if os.path.exists(_PATH_SRC):
121+
if os.path.isdir(_PATH_SRC):
126122
# copy the version information to all packages
127123
assistant.distribute_version(_PATH_SRC)
128-
129-
package_to_install = _PACKAGE_NAME or "lightning"
130-
print(f"Installing the {package_to_install} package") # requires `-v` to appear
131-
is_wheel_install = "PEP517_BUILD_BACKEND" in os.environ
132-
print("is_wheel_install:", is_wheel_install)
133-
if package_to_install not in _PACKAGE_MAPPING or (not is_wheel_install and _PACKAGE_NAME is None):
134-
raise ValueError(f"Unexpected package name: {_PACKAGE_NAME}. Possible choices are: {list(_PACKAGE_MAPPING)}")
135-
is_wheel_install &= _PACKAGE_NAME is None
136-
137-
if package_to_install == "lightning": # install everything
138-
# merge all requirements files
139-
assistant._load_aggregate_requirements(_PATH_REQUIRE, _FREEZE_REQUIREMENTS)
140-
# replace imports and copy the code
141-
assistant.create_mirror_package(_PATH_SRC, _PACKAGE_MAPPING)
142-
143-
# if it's a wheel install (hence _PACKAGE_NAME should not be set), iterate over all possible packages until we find
144-
# one that can be installed. the wheel should have included only the relevant files of the package to install
145-
possible_packages = _PACKAGE_MAPPING.values() if is_wheel_install else [_PACKAGE_MAPPING[_PACKAGE_NAME]]
146-
for pkg in possible_packages:
147-
pkg_path = os.path.join(_PATH_SRC, pkg)
148-
pkg_setup = os.path.join(pkg_path, "__setup__.py")
149-
if os.path.exists(pkg_setup):
150-
print(f"{pkg_setup} exists. Running `setuptools.setup`")
151-
setup_module = _load_py_module(name=f"{pkg}_setup", location=pkg_setup)
152-
setup_args = setup_module._setup_args()
153-
is_main_pkg = pkg == "lightning"
154-
if is_wheel_install and not is_main_pkg:
155-
setuptools.setup(**setup_args)
156-
else:
157-
# we are installing from source, set the correct manifest path
158-
with _set_manifest_path(pkg_path, aggregate=is_main_pkg):
159-
setuptools.setup(**setup_args)
160-
break
124+
print(f"Requested package: '{_PACKAGE_NAME}'") # requires `-v` to appear
125+
126+
local_pkgs = [
127+
os.path.basename(p)
128+
for p in glob.glob(os.path.join(_PATH_SRC, "*"))
129+
if os.path.isdir(p) and not p.endswith(".egg-info")
130+
]
131+
print(f"Local package candidates: {local_pkgs}")
132+
is_source_install = len(local_pkgs) > 2
133+
print(f"Installing from source: {is_source_install}")
134+
if is_source_install:
135+
if _PACKAGE_NAME is not None and _PACKAGE_NAME not in _PACKAGE_MAPPING:
136+
raise ValueError(
137+
f"Unexpected package name: {_PACKAGE_NAME}. Possible choices are: {list(_PACKAGE_MAPPING)}"
138+
)
139+
package_to_install = _PACKAGE_MAPPING.get(_PACKAGE_NAME, "lightning")
140+
if package_to_install == "lightning": # install everything
141+
# merge all requirements files
142+
assistant._load_aggregate_requirements(_PATH_REQUIRE, _FREEZE_REQUIREMENTS)
143+
# replace imports and copy the code
144+
assistant.create_mirror_package(_PATH_SRC, _PACKAGE_MAPPING)
161145
else:
146+
assert len(local_pkgs) > 0
147+
# PL as a package is distributed together with Lite, so in such case there are more than one candidate
148+
package_to_install = "pytorch_lightning" if "pytorch_lightning" in local_pkgs else local_pkgs[0]
149+
print(f"Installing package: {package_to_install}")
150+
151+
# going to install with `setuptools.setup`
152+
pkg_path = os.path.join(_PATH_SRC, package_to_install)
153+
pkg_setup = os.path.join(pkg_path, "__setup__.py")
154+
if not os.path.exists(pkg_setup):
162155
raise RuntimeError(f"Something's wrong, no package was installed. Package name: {_PACKAGE_NAME}")
156+
setup_module = _load_py_module(name=f"{package_to_install}_setup", location=pkg_setup)
157+
setup_args = setup_module._setup_args()
158+
is_main_pkg = package_to_install == "lightning"
159+
print(f"Installing as the main package: {is_main_pkg}")
160+
if is_source_install:
161+
# we are installing from source, set the correct manifest path
162+
with _set_manifest_path(pkg_path, aggregate=is_main_pkg):
163+
setuptools.setup(**setup_args)
164+
else:
165+
setuptools.setup(**setup_args)
166+
print("Finished setup configuration.")

0 commit comments

Comments
 (0)