40
40
c) validate packages and publish to PyPI
41
41
"""
42
42
import contextlib
43
+ import glob
43
44
import os
44
45
import tempfile
45
46
from importlib .util import module_from_spec , spec_from_file_location
49
50
import setuptools
50
51
import setuptools .command .egg_info
51
52
52
- _PACKAGE_NAME = os .environ .get ("PACKAGE_NAME" , "lightning" )
53
+ _PACKAGE_NAME = os .environ .get ("PACKAGE_NAME" )
53
54
_PACKAGE_MAPPING = {
54
55
"lightning" : "lightning" ,
55
56
"pytorch" : "pytorch_lightning" ,
@@ -87,22 +88,17 @@ def _set_manifest_path(manifest_dir: str, aggregate: bool = False) -> Generator:
87
88
# aggregate all MANIFEST.in contents into a single temporary file
88
89
manifest_path = _named_temporary_file (manifest_dir )
89
90
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
92
93
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 ())
103
98
# convert lightning_foo to lightning/foo
104
99
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 )))
106
102
with open (manifest_path , mode = "w" ) as fp :
107
103
fp .writelines (lines )
108
104
else :
@@ -122,41 +118,49 @@ def _set_manifest_path(manifest_dir: str, aggregate: bool = False) -> Generator:
122
118
if __name__ == "__main__" :
123
119
assistant = _load_py_module (name = "assistant" , location = os .path .join (_PATH_ROOT , ".actions" , "assistant.py" ))
124
120
125
- if os .path .exists (_PATH_SRC ):
121
+ if os .path .isdir (_PATH_SRC ):
126
122
# copy the version information to all packages
127
123
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 )
161
145
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 ):
162
155
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