Given a pacakge requirement this returns the information about this plugin.
(project, req)
| 25 | |
| 26 | |
| 27 | def add_package_to_project(project, req): |
| 28 | """Given a pacakge requirement this returns the information about this |
| 29 | plugin. |
| 30 | """ |
| 31 | if "@" in req: |
| 32 | name, version = req.split("@", 1) |
| 33 | version_hint = version |
| 34 | else: |
| 35 | name = req |
| 36 | version = None |
| 37 | version_hint = "latest release" |
| 38 | |
| 39 | cfg = project.open_config() |
| 40 | info = _get_package_version_from_project(cfg, name) |
| 41 | if info is not None: |
| 42 | raise RuntimeError("The package was already added to the project.") |
| 43 | |
| 44 | for choice in name, "lektor-" + name: |
| 45 | rv = requests.get("https://pypi.python.org/pypi/%s/json" % choice) |
| 46 | if rv.status_code != 200: |
| 47 | continue |
| 48 | |
| 49 | data = rv.json() |
| 50 | canonical_name = data["info"]["name"] |
| 51 | if version is None: |
| 52 | version = data["info"]["version"] |
| 53 | version_info = data["releases"].get(version) |
| 54 | if version_info is None: |
| 55 | raise RuntimeError( |
| 56 | "Latest requested version (%s) could not " "be found" % version_hint |
| 57 | ) |
| 58 | |
| 59 | cfg["packages.%s" % canonical_name] = version |
| 60 | cfg.save() |
| 61 | return {"name": canonical_name, "version": version} |
| 62 | |
| 63 | raise RuntimeError("The package could not be found on PyPI") |
| 64 | |
| 65 | |
| 66 | def remove_package_from_project(project, name): |