2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-12 10:05:39 +00:00

Switch to pathlib (#3392)

* switch to pathlib

* more pathlib

* useconvert env to path

* fix typo

* use resolve instead of absolute

* fix gitppod

* also allow parents

* replace more os operations

* fix string replacement feature

* make sure root dirs exsist

* fix replace function

* refactor duplicate code

* reduce code

* make sure dirs exist

* fix typo

* also create parent dirs

* fix match statement

* fix statments expecting string

* return getMigrationFileNames to old behaviour

* fully resolve config file

* make sure comparison works

* use pathlib in tasks

* fix file count test

* reduce code duplication in test + add test for part

* fix test

* re-add os

* Make pathlib usage simpler
This commit is contained in:
Matthias Mair
2022-07-27 02:42:34 +02:00
committed by GitHub
parent 551f66ff90
commit 794e375009
20 changed files with 242 additions and 403 deletions

View File

@ -5,6 +5,7 @@ import os
import pathlib
import re
import sys
from pathlib import Path
from invoke import task
@ -52,23 +53,23 @@ def content_excludes():
return output
def localDir():
def localDir() -> Path:
"""Returns the directory of *THIS* file.
Used to ensure that the various scripts always run
in the correct directory.
"""
return os.path.dirname(os.path.abspath(__file__))
return Path(__file__).parent.resolve()
def managePyDir():
"""Returns the directory of the manage.py file."""
return os.path.join(localDir(), 'InvenTree')
return localDir().joinpath('InvenTree')
def managePyPath():
"""Return the path of the manage.py file."""
return os.path.join(managePyDir(), 'manage.py')
return managePyDir().joinpath('manage.py')
def manage(c, cmd, pty: bool = False):
@ -171,7 +172,7 @@ def translate_stats(c):
The file generated from this is needed for the UI.
"""
path = os.path.join('InvenTree', 'script', 'translation_stats.py')
path = Path('InvenTree', 'script', 'translation_stats.py')
c.run(f'python3 {path}')
@ -252,12 +253,11 @@ def export_records(c, filename='data.json', overwrite=False, include_permissions
"""
# Get an absolute path to the file
if not os.path.isabs(filename):
filename = os.path.join(localDir(), filename)
filename = os.path.abspath(filename)
filename = localDir().joinpath(filename).resolve()
print(f"Exporting database records to file '{filename}'")
if os.path.exists(filename) and overwrite is False:
if filename.exists() and overwrite is False:
response = input("Warning: file already exists. Do you want to overwrite? [y/N]: ")
response = str(response).strip().lower()
@ -306,7 +306,7 @@ def import_records(c, filename='data.json', clear=False):
"""Import database records from a file."""
# Get an absolute path to the supplied filename
if not os.path.isabs(filename):
filename = os.path.join(localDir(), filename)
filename = localDir().joinpath(filename)
if not os.path.exists(filename):
print(f"Error: File '{filename}' does not exist")
@ -442,8 +442,8 @@ def test_translations(c):
from django.conf import settings
# setup django
base_path = os.getcwd()
new_base_path = pathlib.Path('InvenTree').absolute()
base_path = Path.cwd()
new_base_path = pathlib.Path('InvenTree').resolve()
sys.path.append(str(new_base_path))
os.chdir(new_base_path)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'InvenTree.settings')
@ -487,8 +487,8 @@ def test_translations(c):
file_new.write(line)
# change out translation files
os.rename(file_path, str(file_path) + '_old')
os.rename(new_file_path, file_path)
file_path.rename(str(file_path) + '_old')
new_file_path.rename(file_path)
# compile languages
print("Compile languages ...")