Skip to content

Commit

Permalink
support None arguments. pru will solve on requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirroni committed May 28, 2024
1 parent 925761f commit a448505
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/pru/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ def main():
"-r",
"--requirement",
type=str,
default="requirements.txt",
default=None,
help=(
"Path to the requirements file. Defaults to using requirements.txt from "
"the current directory if it exists."
"the current directory if it exists. Use '.' to indicate no requirements "
"file."
),
)
parser.add_argument(
"-o",
"--output",
type=str,
help=(
"Output updated packages to this file. Defaults to overwriting the input"
"Output updated packages to this file. Defaults to overwriting the input "
"requirements file."
),
)
Expand Down
22 changes: 16 additions & 6 deletions src/pru/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import pkg_resources


def read_requirements(requirements_path):
def read_requirements(requirements_path=None):
if requirements_path is None:
requirements_path = get_requirements_path()
with open(requirements_path, "r") as f:
requirements = f.readlines()

Expand Down Expand Up @@ -56,7 +58,7 @@ def get_installed_packages_name_and_version():
return packages


def get_requirements_packages_name(requirements_path):
def get_requirements_packages_name(requirements_path=None):
requirements = read_requirements(requirements_path)

package_names = []
Expand All @@ -67,14 +69,14 @@ def get_requirements_packages_name(requirements_path):
return package_names


def get_installed_requirements_packages_and_version(requirements_path):
def get_installed_requirements_packages_and_version(requirements_path=None):
packages = {}
for package_name in get_requirements_packages_name(requirements_path):
packages[package_name] = get_package_version(package_name)
return packages


def replace_requirements_packages_versions(requirements_path, output_path=None):
def replace_requirements_packages_versions(requirements_path=None, output_path=None):
requirements = read_requirements(requirements_path)

installed_packages_name = get_installed_packages_name()
Expand All @@ -99,6 +101,8 @@ def replace_requirements_packages_versions(requirements_path, output_path=None):
updated_requirements.append(requirement)

if output_path is None:
if requirements_path is None:
requirements_path = get_requirements_path()
output_path = requirements_path

with open(output_path, "w") as f:
Expand All @@ -113,15 +117,21 @@ def verbose_subprocess(command):
print(line.rstrip().decode("utf-8"))


def upgrade_installed(requirements_path, command="pip install --upgrade --user"):
def upgrade_installed(requirements_path=None, command="pip install --upgrade --user"):
verbose_subprocess(
f"{command} {' '.join(get_requirements_packages_name(requirements_path))}"
)


def upgrade_requirements(
requirements_path, output_path=None, command="pip install --upgrade --user"
requirements_path=None, output_path=None, command="pip install --upgrade --user"
):
package_names = get_requirements_packages_name(requirements_path)
verbose_subprocess(f"{command} {' '.join(package_names)}")
replace_requirements_packages_versions(requirements_path, output_path)


def get_requirements_path():
# TODO: support finding requirements file
requirements_path = "requirements.txt"
return requirements_path

0 comments on commit a448505

Please sign in to comment.