Skip to content

Commit 09a5423

Browse files
added leftover cleaners
1 parent 2329cd8 commit 09a5423

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

not_gitmodules/core.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import os
2-
from .parts import delete_git_folder, ensure_dir_exists, extract_repo_name_from_url, clone_repo, read_yaml
2+
from .parts import delete_git_folder, ensure_dir_exists, clone_repo, read_yaml, clean_github_leftovers
33

44

55
def initializer(
66
yaml_config_path: str = 'notgitmodules.yaml',
77
root_dir_name="my_gitmodules"
88
):
9+
# Read yaml
10+
# Ensure root_dir exists
11+
# Clone the repo to root dir
12+
# Clean-up
13+
914
"""
1015
:param yaml_config_path: The path to notgitmodules.yaml file
1116
:param root_dir_name: The name of directory where modules will be downloaded to.
@@ -15,10 +20,8 @@ def initializer(
1520
ensure_dir_exists(root_dir_name)
1621

1722
for directory, repo_url in not_gitmodules.items():
18-
delete_git_folder(root_dir_name)
19-
2023
clone_repo(root_dir_name, directory, repo_url)
21-
repo_name = extract_repo_name_from_url(repo_url)
2224

23-
repo = os.path.join(root_dir_name, repo_name)
24-
delete_git_folder(repo)
25+
module_path = os.path.join(root_dir_name, directory)
26+
delete_git_folder(module_path)
27+
clean_github_leftovers(module_path)

not_gitmodules/parts/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
from .clone_repo import clone_repo
66
from .move_folder import move_folder
77
from .yaml_reader import read_yaml
8+
from .clean_repo import clean_github_leftovers
89

not_gitmodules/parts/clean_repo.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pathlib import Path
2+
import shutil
3+
4+
LEFTOVER_FILES = '.gitignore', 'README.md', 'LICENSE'
5+
LEFTOVER_FOLDERS = '.github',
6+
7+
8+
def clean_github_leftovers(module_path):
9+
"""
10+
Deletes files like .gitignore, README.md, LICENSE and folders like .github from the cloned module.
11+
"""
12+
module_path = Path(module_path)
13+
14+
for file_name in LEFTOVER_FILES:
15+
file_path = module_path / file_name
16+
if file_path.exists():
17+
file_path.unlink()
18+
19+
for folder_name in LEFTOVER_FOLDERS:
20+
folder_path = module_path / folder_name
21+
if folder_path.exists() and folder_path.is_dir():
22+
shutil.rmtree(folder_path)
23+
24+
print('Leftover files were successfully removed.')

not_gitmodules/parts/delete_git.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import os, shutil, stat
2-
import subprocess
3-
4-
5-
def delete_using_subprocess(path):
6-
subprocess.run(["rm", path, "-r", "-f"], check=True)
2+
from pathlib import Path
73

84

95
def drop_read_only(path):
106
try:
117
os.chmod(path, stat.S_IWRITE)
128
except PermissionError:
13-
delete_using_subprocess(path)
9+
...
1410

1511

1612
def delete_git_folder(directory):
@@ -20,15 +16,10 @@ def delete_git_folder(directory):
2016
if os.path.exists(git_folder_path):
2117
try:
2218
shutil.rmtree(git_folder_path)
23-
except PermissionError:
24-
drop_read_only(git_folder_path)
25-
shutil.rmtree(git_folder_path)
26-
27-
28-
def force_del_file(path):
29-
return delete_using_subprocess(path)
30-
31-
32-
def force_del_folder(path):
33-
while os.path.exists(path):
34-
subprocess.run(["rm", "-r", path, "-f"], check=True)
19+
except PermissionError as err:
20+
read_only_fp = str(err).split('denied:')[1].strip().strip("'")
21+
p = Path(read_only_fp)
22+
drop_read_only(p)
23+
return delete_git_folder(directory)
24+
else:
25+
print(git_folder_path, 'was successfully deleted.')

0 commit comments

Comments
 (0)