+ Verify that we are in a git repo before proceeding

This commit is contained in:
j3rome 2021-06-28 12:39:47 -04:00
parent 4253e368fc
commit ba2811fc65
2 changed files with 16 additions and 4 deletions

10
main.py
View File

@ -7,7 +7,7 @@ from datetime import datetime
from urllib.request import urlopen
from utils import load_packages_from_requirements, get_mapping_files_from_pipreqs, user_response_multi_choices
from utils import get_date_last_modified_python_file, get_python_filename_at_root
from utils import get_date_last_modified_python_file, get_python_filename_at_root, validate_cwd_is_git_repo
# TODO : Propose choice between date of first import or Added in requirements
# TODO : Other choices : When project was created, last commit (That wasnt on md file) get_date_last_modified_python_file()
@ -166,7 +166,7 @@ def guess_package_versions(package_list, from_import_to_package_mapping, from_pa
available_versions = get_pypi_history(pypi_package_name, ignore_release_candidat=True)
if available_versions is None:
print(f" [INFO] Couldn't find Pypi releases for package '{package_name}'")
print(f"[INFO] Couldn't find Pypi releases for package '{package_name}', ignoring")
continue
# Retrieve candidate version based on the first time the package was imported in *.py
@ -231,9 +231,11 @@ if __name__ == "__main__":
print("Python requirements guesser")
print("="*60)
print("\nFollow the steps to guess package versions based on when they were added to git")
if not validate_cwd_is_git_repo():
print("[ERROR] py-reqs-guesser must be runned inside a git repository")
exit(1)
# TODO : Detect that CWD is a git repo
print("\nFollow the steps to guess package versions based on when they were added to git")
args = parser.parse_args()

View File

@ -122,6 +122,16 @@ def get_date_last_modified_python_file():
return datetime.fromtimestamp(int(timestamp))
def validate_cwd_is_git_repo():
try:
subprocess.check_output("git rev-parse --is-inside-work-tree 2>/dev/null", shell=True)
except:
# git rev-parse return non-zero exit code if not in repo
return False
return True
def detect_os():
pass