From c60517a454823283f49fea548de0a44caf505dc2 Mon Sep 17 00:00:00 2001 From: Jonathan Raviotta Date: Tue, 3 Sep 2019 16:11:43 -0400 Subject: [PATCH] added function to find project root --- freqtrade/misc.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/freqtrade/misc.py b/freqtrade/misc.py index 12a90a14d..7eef795e9 100644 --- a/freqtrade/misc.py +++ b/freqtrade/misc.py @@ -3,6 +3,7 @@ Various tool function for Freqtrade and scripts """ import gzip import logging +from os import chdir import re from datetime import datetime from pathlib import Path @@ -114,3 +115,23 @@ def deep_merge_dicts(source, destination): destination[key] = value return destination + + +def find_project_root(path=None): + """Locates root directory of the project. + Root is defined by existence of "LICENSE" and the dir 'freqtrade'. + :param path: pathlib.Path object, or string pointing to project root directory. + :return: path to project root directory + """ + if path is None: + path = Path(__file__).parent + i = 0 + try: + chdir(path) + assert Path('LICENSE').is_file() and Path('freqtrade').exists() + except AssertionError: + while i < 5 and not (Path(path, 'LICENSE').is_file() and Path(path, 'freqtrade').exists()): + path = Path(path).parent + i += 1 + logger.info(f'project_root is {path}') + return path