mirror of
https://github.com/ItsDrike/dotfiles.git
synced 2025-04-28 05:02:26 +00:00
OOP: Added make_backup() method
This commit is contained in:
parent
68a58ca9ca
commit
61c44e1a76
2 changed files with 39 additions and 1 deletions
|
@ -1,12 +1,38 @@
|
||||||
|
from util import Path, Print
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
class Dotfiles:
|
class Dotfiles:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.backup_location = Path.join(Path.WORKING_FLODER, 'Backups')
|
||||||
|
self.dotfiles_location = Path.join(Path.WORKING_FLODER, 'files')
|
||||||
self.initial_checks()
|
self.initial_checks()
|
||||||
|
|
||||||
def initial_checks(self):
|
def initial_checks(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def make_backup(self):
|
def make_backup(self):
|
||||||
pass
|
Print.action('Creating current dotfiles backup')
|
||||||
|
# time will be used as backup directory name
|
||||||
|
cur_time = str(datetime.now()).replace(' ', '--')
|
||||||
|
backup_dir = Path.join(self.backup_location, cur_time)
|
||||||
|
Path.ensure_dirs(backup_dir)
|
||||||
|
|
||||||
|
# Loop through every file in dotfiles_location
|
||||||
|
for file in Path.get_all_files(self.dotfiles_location):
|
||||||
|
# Remove dotfiles_location from file path
|
||||||
|
file_blank = file.replace(f'{self.dotfiles_location}/', '')
|
||||||
|
|
||||||
|
# Use path in home directory
|
||||||
|
from_pos = Path.join(Path.get_home(), file_blank)
|
||||||
|
# Use path in backup directory
|
||||||
|
to_pos = Path.join(backup_dir, file_blank)
|
||||||
|
|
||||||
|
# If file is in home directory, back it up
|
||||||
|
if Path.check_file_exists(from_pos):
|
||||||
|
Path.copy(from_pos, to_pos)
|
||||||
|
|
||||||
|
Print.action('Backup complete')
|
||||||
|
|
||||||
def personalized_changes(self, file):
|
def personalized_changes(self, file):
|
||||||
pass
|
pass
|
||||||
|
|
12
util/Path.py
12
util/Path.py
|
@ -4,6 +4,8 @@ import pathlib
|
||||||
|
|
||||||
from util import Command, Print
|
from util import Command, Print
|
||||||
|
|
||||||
|
WORKING_FLODER = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
|
|
||||||
def check_dir_exists(paths):
|
def check_dir_exists(paths):
|
||||||
'''Check for directory/ies existence
|
'''Check for directory/ies existence
|
||||||
|
@ -145,3 +147,13 @@ def copy(path, copied_path):
|
||||||
ensure_dirs(copied_path, file_end=True)
|
ensure_dirs(copied_path, file_end=True)
|
||||||
Command.execute(f'cp {path} {copied_path}')
|
Command.execute(f'cp {path} {copied_path}')
|
||||||
Print.comment(f'Copied {path} to {copied_path}')
|
Print.comment(f'Copied {path} to {copied_path}')
|
||||||
|
|
||||||
|
|
||||||
|
def join(*paths):
|
||||||
|
'''Join paths together
|
||||||
|
(This function is here to avoid re-importing os module in other scripts)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str -- Joined paths
|
||||||
|
'''
|
||||||
|
return os.path.join(paths)
|
||||||
|
|
Loading…
Add table
Reference in a new issue