Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Improved virtual environment activation #6

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FENV_VERSION = "0.0.12.0"
FENV_VERSION = "0.0.12.1"
14 changes: 4 additions & 10 deletions fenv/env_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,11 @@ def get_path_env_for_linux(self):

def get_terminal_prompt(self):
try:
if len(os.environ.get("PROMPT")) == 10:
return True
else:
return False
except:
return len(os.environ.get("PROMPT")) == 10
except Exception:
pass

def get_terminal_bash(self):
if "xterm" in os.environ.get("TERM") or "gnome-terminal" in os.environ.get(
return "xterm" in os.environ.get("TERM") or "gnome-terminal" in os.environ.get(
"TERM"
):
return True
else:
return False
)
4 changes: 1 addition & 3 deletions fenv/fenv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: str = "0.0.12.0"
version: str = '0.0.12.1'
""" Module fenv main """
from fenv.customizes.colors import Colors
from fenv.assets.commands import Commands
Expand Down Expand Up @@ -152,8 +152,6 @@ def check_command(args):
Cleanup().remove_lib_not_default_in_env()
elif args.__dict__["command"] == "activate":
StateEnv().activate()
elif args.__dict__["command"] == "deactivate":
StateEnv().deactivate()
elif args.__dict__["command"] == "test":
print("D:D:D:D:D:D:D:D")

Expand Down
110 changes: 43 additions & 67 deletions fenv/manage_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def create_file_main_py(self):
with open(self.file_path, "w") as f:
f.write(self.commands.get_main_py())
os.chmod(self.file_path, 0o777)
print(self.notice + f'Successfully created the file "{self.file_path}"')
print(f'{self.notice}Successfully created the file "{self.file_path}"')

def generate_tree(self):
self.output = ""
Expand Down Expand Up @@ -67,7 +67,7 @@ def create_file_readme_md(self):
)
)
os.chmod(markdown_path, 0o777)
print(self.notice + f'Successfully created the file "{markdown_path}"')
print(f'{self.notice}Successfully created the file "{markdown_path}"')

def create_file_freeze(self):
"""
Expand All @@ -77,7 +77,7 @@ def create_file_freeze(self):
with open("requirements.txt", "w") as f:
f.write(module_base)
os.chmod("requirements.txt", 0o777)
print(self.notice + f'Successfully created the file "requirements.txt"')
print(f'{self.notice}Successfully created the file "requirements.txt"')

def create_file_gitignore(self):
"""
Expand All @@ -86,7 +86,7 @@ def create_file_gitignore(self):
with open(".gitignore", "w") as f:
f.write(f"*.pyc\n/{self.env_directory}")
os.chmod(".gitignore", 0o777)
print(self.notice + f'Successfully created the file ".gitignore"')
print(f'{self.notice}Successfully created the file ".gitignore"')

def update_file_readme_md(self):
"""
Expand Down Expand Up @@ -134,7 +134,7 @@ def create_virtualenv(self):
os.system(f"virtualenv env_{self.name}")
EnvAll().create_lib_default_env()

print(self.notice + f'Successfully created the virtualenv "{self.name}"')
print(f'{self.notice}Successfully created the virtualenv "{self.name}"')

def create_setting_vscode(self):
"""
Expand All @@ -153,7 +153,7 @@ def create_setting_vscode(self):
os.makedirs(os.path.dirname(".vscode/settings.json"), exist_ok=True)
with open(".vscode/settings.json", "w", encoding="utf-8") as f:
f.write(text_vscode.format(name_env=self.name))
print(self.notice + f"Successfully created the .vscode/settings.json")
print(f"{self.notice}Successfully created the .vscode/settings.json")

def run_install_module_base(self):
"""
Expand All @@ -163,31 +163,28 @@ def run_install_module_base(self):
"""

if platform.system() == "Windows":
os.system(
f".\env_{self.name}\Scripts\python.exe -m pip install -r requirements.txt"
)
print(self.notice + f'Successfully installed module in "requirements.txt"')
os.system(
f".\env_{self.name}\Scripts\python.exe -m pip freeze > requirements.txt"
)
print(self.notice + f'Successfully updated the file "requirements.txt"')
os.system(
f".\env_{self.name}\Scripts\python.exe -m pip install --upgrade pip"
self._extracted_from_run_install_module_base_9(
".\env_",
"\Scripts\python.exe -m pip install -r requirements.txt",
"\Scripts\python.exe -m pip freeze > requirements.txt",
"\Scripts\python.exe -m pip install --upgrade pip",
)

elif platform.system() == "Linux":
os.system(
f"bash -c 'source env_{self.name}/bin/activate && pip install -r requirements.txt'"
)
print(self.notice + f'Successfully installed module in "requirements.txt"')
os.system(
f"bash -c 'source env_{self.name}/bin/activate && pip freeze > requirements.txt'"
)
print(self.notice + f'Successfully updated the file "requirements.txt"')
os.system(
f"bash -c 'source env_{self.name}/bin/activate && pip install --upgrade pip'"
self._extracted_from_run_install_module_base_9(
"bash -c 'source env_",
"/bin/activate && pip install -r requirements.txt'",
"/bin/activate && pip freeze > requirements.txt'",
"/bin/activate && pip install --upgrade pip'",
)

# TODO Rename this here and in `run_install_module_base`
def _extracted_from_run_install_module_base_9(self, arg0, arg1, arg2, arg3):
os.system(f"{arg0}{self.name}{arg1}")
print(f'{self.notice}Successfully installed module in "requirements.txt"')
os.system(f"{arg0}{self.name}{arg2}")
print(f'{self.notice}Successfully updated the file "requirements.txt"')
os.system(f"{arg0}{self.name}{arg3}")

def create_folder(self):
"""
It creates a folder with the name of the argument passed to it
Expand All @@ -202,10 +199,10 @@ def create_folder(self):
try:
os.mkdir(self.name)
except FileExistsError:
print(self.notice + f"{self.name} already exists.")
print(f"{self.notice}{self.name} already exists.")
return 1
else:
print(self.notice + f'Successfully created the directory "{self.name}"')
print(f'{self.notice}Successfully created the directory "{self.name}"')

def process_create_base_file_and_update(self):
"""
Expand Down Expand Up @@ -263,7 +260,7 @@ def create_name_env(self) -> str:
).replace(" ", "_")

if bool(re.match("^[A-Za-z0-9_#]+$", self.name)):
return "{}".format(self.name[:10])
return f"{self.name[:10]}"
else:
return self.create_name_env_auto()

Expand All @@ -286,7 +283,7 @@ def create_virtualenv_not_change_dir(self):
os.system(f"virtualenv env_{self._name_env}")
EnvAll().create_lib_default_env()

print(self.notice + f'Successfully created the virtualenv "{self._name_env}"')
print(f'{self.notice}Successfully created the virtualenv "{self._name_env}"')

def run_process(self):
"""
Expand Down Expand Up @@ -394,11 +391,7 @@ def install_package_only(self):
response = input(
"We couldn't find the fenv virtual environment. Would you like to set up a new one? (y/n): "
)
if (
response.lower() == "y"
or response.lower() == "yes"
or response.lower() == ""
):
if response.lower() in ["y", "yes", ""]:
OnlyVirtualEnv().run_process()
folder_name = "env*"
folder_name_env = str(
Expand Down Expand Up @@ -449,19 +442,14 @@ def run_install_main(folder_name_env):
)
install_package_follow_env(folder_name_env)
print(
self.notice
+ f'Successfully installed module from "requirements.txt"'
f'{self.notice}Successfully installed module from "requirements.txt"'
)
else:
while True:
response = input(
"We couldn't find the fenv virtual environment. Would you like to set up a new one? (y/n): "
)
if (
response.lower() == "y"
or response.lower() == "yes"
or response.lower() == ""
):
if response.lower() in ["y", "yes", ""]:
OnlyVirtualEnv().run_process()
folder_name = "env*"
folder_name_env = str(
Expand All @@ -472,12 +460,11 @@ def run_install_main(folder_name_env):
)
install_package_follow_env(folder_name_env)
print(
self.notice
+ f'Successfully installed modules from "requirements.txt"'
f'{self.notice}Successfully installed modules from "requirements.txt"'
)
break
elif response.lower() == "n":
os.system(f"pip install -r requirements.txt")
os.system("pip install -r requirements.txt")
break

if requirements_file in os.listdir("."):
Expand Down Expand Up @@ -513,7 +500,7 @@ def cmd_uninstall_package(self):

try:
package_dependency_list = self.pip_show_to_dict()["Requires"].split(", ")
print(self.notice + f"Uninstalling...{self.colors.ORANGE}")
print(f"{self.notice}Uninstalling...{self.colors.ORANGE}")
if platform.system() == "Windows":
os.system(
f".\{self.env_directory}\Scripts\python.exe -m pip uninstall {self.package_name.uninstall} -y"
Expand Down Expand Up @@ -595,8 +582,8 @@ def pip_show_to_dict(self):
for line in output.splitlines():
match = re.match(r"^([^:]+):\s*(.*)$", line)
if match:
key = match.group(1)
value = match.group(2)
key = match[1]
value = match[2]
result[key] = value
return result
except subprocess.CalledProcessError:
Expand All @@ -621,10 +608,7 @@ def remove_lib_not_default_in_env(self):
"""
Removes all the libraries that are not in the default environment
"""
data_lib_all = []
for item in os.listdir(self.path_lib_all):
data_lib_all.append(item)

data_lib_all = list(os.listdir(self.path_lib_all))
diff_list_1 = set(data_lib_all) - set(self.lib_default_env)
diff_list_2 = set(self.lib_default_env) - set(data_lib_all)
result = diff_list_1.union(diff_list_2)
Expand All @@ -633,35 +617,27 @@ def remove_lib_not_default_in_env(self):
for item in result:
if os.path.isdir(item):
shutil.rmtree(item)
print(
self.notice
+ f"{self.colors.SALMON}{item}{self.colors.ENDC} has been removed"
)
else:
os.remove(item)
print(
self.notice
+ f"{self.colors.SALMON}{item}{self.colors.ENDC} has been removed"
)
print(
f"{self.notice}{self.colors.SALMON}{item}{self.colors.ENDC} has been removed"
)
os.chdir("../../..")
print(
self.notice
+ f"{self.colors.SKY_BLUE}All the libraries have been removed.{self.colors.ENDC}"
f"{self.notice}{self.colors.SKY_BLUE}All the libraries have been removed.{self.colors.ENDC}"
)
if platform.system() == "Windows":
os.system(
f".\env_{self.env_name }\Scripts\python.exe -m pip freeze > requirements.txt"
)
print(
self.notice
+ f'{self.colors.SKY_BLUE}Successfully updated the file "requirements.txt"{self.colors.ENDC}'
f'{self.notice}{self.colors.SKY_BLUE}Successfully updated the file "requirements.txt"{self.colors.ENDC}'
)

elif platform.system() == "Linux":
os.system(
f"bash -c 'source env_{self.env_name }/bin/activate && pip freeze > requirements.txt'"
)
print(
self.notice
+ f'{self.colors.SKY_BLUE}Successfully updated the file "requirements.txt"{self.colors.ENDC}'
f'{self.notice}{self.colors.SKY_BLUE}Successfully updated the file "requirements.txt"{self.colors.ENDC}'
)