208def load(cls, env=None, with_dependencies=False):
210 Load installed packages.
212 from pathlib
import Path
214 from poetry.utils.env
import Env
as sys_env
215 from poetry.core.packages.dependency
import Dependency
217 from dulwich.errors
import NotGitRepository
223 if not env: env = sys_env
224 for entry
in reversed(env.sys_path):
225 if not entry.strip():
227 "Project environment contains an empty path in <c1>sys_path</>,"
232 for distribution
in sorted(
233 metadata.distributions(path=[entry]),
234 key=
lambda d: str(d._path),
236 path = Path(str(distribution._path))
241 name = distribution.metadata.get(
"name")
245 "Project environment contains an invalid distribution"
246 " (<c1>%s</>). Consider removing it manually or recreate"
254 name = canonicalize_name(name)
259 try: package = cls.create_package_from_distribution(distribution, env)
260 except NotGitRepository:
continue
262 if with_dependencies:
263 for require
in distribution.metadata.get_all(
"requires-dist", []):
264 dep = Dependency.create_from_pep_508(require)
265 package.add_dependency(dep)
267 seen.add(package.name)
268 repo.add_package(package)
276 Build a RecordEntry object, from values of the elements.
280 for row in parse_record_file(f):
281 record = RecordEntry.from_elements(row[0], row[1], row[2])
283 Meaning of each element is specified in :pep:`376`.
285 :param path: first element (file's path)
286 :param hash\_: second element (hash of the file's contents)
287 :param size: third element (file's size in bytes)
288 :raises InvalidRecordEntry: if any element is invalid
290 from typing
import Optional
291 from installer.records
import Hash, InvalidRecordEntry
296 issues.append(
"`path` cannot be empty")
300 hash_value = Hash.parse(hash_)
302 issues.append(
"`hash` does not follow the required format")
308 size_value = int(size)
310 issues.append(
"`size` cannot be non-integer")
315 raise InvalidRecordEntry(elements=(path, hash_, size), issues=issues)
317 return cls(path=path, hash_=hash_value, size=size_value)
356 Poetry build compatibility helper interface
359 from poetry
import core
361 packages = kwargs.get(
"packages",[])
362 if os.path.exists(
"pyproject.toml"):
363 with open(
"pyproject.toml",
"r")
as f: content = f.readlines()
364 else: content = str(
"")
366 if not packages
and content:
367 packages = [line
for line
in content
if line.split(
"=")[0].strip()
in [
"packages"]]
368 config = next(iter(packages[0].split(
"=",1)[1:]));
369 config = config.strip(); config = config.replace(
"=",
":")
370 config = config.replace(
'from',
'"from"'); config = config.replace(
'include',
'"include"')
371 packages = ast.literal_eval(config)
372 else: packages.append(dict())
374 if not any([str(
"tool.poetry.build")
in line
for line
in content]):
return
376 _ = [os.remove(x)
for x
in os.listdir(os.getcwd())
if x.startswith(
"1.0")]
378 sys.path.extend([os.path.join(x,
"site-packages")
for x
in sys.path
if "lib" in x.lower()])
379 sys.path.extend([os.path.join(os.path.dirname(x),
"src",
"PyXMake",
"src")
for x
in sys.path
if "lib" in x.lower()])
381 venv = os.path.abspath(os.path.join(os.getcwd(),
".venv"))
382 if os.path.exists(venv):
384 os.environ.update({
"PATH":os.pathsep.join([os.getenv(
"PATH")]+[os.path.join(venv,
"bin")])})
386 sys.path.extend([os.path.join(venv,
"src",
"PyXMake",
"src")])
387 sys.path.extend([os.path.abspath(os.path.join(venv,
"lib",os.listdir(os.path.join(venv,
"lib"))[0]
if str(platform.system()).lower()
in [
"linux"]
else "",
"site-packages"))])
390 child = subprocess.Popen([sys.executable,
"-m",
"poetry",
"run",
"python",
"-c",
"import site; print(site.getusersitepackages())"], stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
391 usersitepackages = child.communicate()[0].decode().strip()
393 if child.returncode != 0:
raise RuntimeError
395 if not usersitepackages: usersitepackages = site.getusersitepackages()
397 except: usersitepackages = site.getusersitepackages()
399 output = packages[0].get(
"from",
"")
or packages[0].get(
"include",
"")
401 if not os.path.exists(usersitepackages): os.makedirs(usersitepackages)
403 _ = [os.remove(os.path.abspath(os.path.join(usersitepackages,x)))
for x
in os.listdir(usersitepackages)
if x.startswith(
"pyx_poetry_build_extension")]
405 for index, path
in enumerate(sys.path,0):
407 if not os.path.exists(path):
continue
409 if str(
"poetry")
in path.split(
"site-packages")[-1]:
continue
411 with open(os.path.join(usersitepackages,
"pyx_poetry_build_extension_%s.pth" % str(index)),
"w+")
as f: f.write(path)
413 if not os.path.exists(os.path.join(output,
"__init__.py")):
415 for x
in os.listdir(output):
417 if os.path.isdir(os.path.join(output,x)):
continue
419 try: subprocess.check_call(shlex.split(
"git ls-files --error-unmatch %s" % x,posix=
not os.name.lower()
in [
"nt"]),
420 stdout=open(os.devnull,
'wb'), stderr=open(os.devnull,
'wb'), cwd=output)
423 command =
"git update-index --assume-unchanged %s" % x
425 subprocess.call(shlex.split(command,posix=
not os.name.lower()
in [
"nt"]), cwd=output)
427 if len(os.listdir(output)) <= 1: os.remove(os.path.join(output,x))
429 if not os.path.exists(os.path.join(output,
"__init__.py")):
431 if parse(core.__version__) >= parse(
"1.6.1"): open(os.path.join(output,
"__init__.py"),
"w+")
433 elif str(platform.system()).lower()
in [
"linux",
"windows"]
and parse(
".".join([str(x)
for x
in sys.version_info[:2]])) > parse(
"2.6"): open(os.path.join(output,
"__init__.py"),
"w+")
437 Main command line parser.
439 if not kwargs.get(
"method",
""):
440 parser = argparse.ArgumentParser(description=
'CLI wrapper options for Poetry.')
441 parser.add_argument(
'method', metavar=
'option', type=str, nargs=1,
442 help=
'An option identifier. Adding one new option named <setup>. All other CLI arguments are directly parsed to Poetry.')
444 args, _ = parser.parse_known_args()
445 method = kwargs.get(
"method",str(args.method[0]))
446 else: method = kwargs.get(
"method")
448 command =
" ".join([
"poetry"] + sys.argv[1:])
449 if not method
in globals()
or ( len(sys.argv[1:]) > 1
and not method
in globals() ):
450 subprocess.call(shlex.split(command,posix=
not os.name.lower()
in [
"nt"]),stderr=sys.stderr, stdout=sys.stdout)
451 else: globals()[method](**kwargs)