mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-16 17:55:16 +00:00
refactor folder to directory
This commit is contained in:
@@ -22,10 +22,10 @@ cfg="${cur}/config.yaml"
|
|||||||
sub="dotdrop"
|
sub="dotdrop"
|
||||||
|
|
||||||
# pivot
|
# pivot
|
||||||
cd "${cur}" || { echo "Folder \"${cur}\" doesn't exist, aborting." && exit; }
|
cd "${cur}" || { echo "Directory \"${cur}\" doesn't exist, aborting." && exit; }
|
||||||
# init/update the submodule
|
# init/update the submodule
|
||||||
git submodule update --init --recursive
|
git submodule update --init --recursive
|
||||||
# launch dotdrop
|
# launch dotdrop
|
||||||
PYTHONPATH=dotdrop python3 -m dotdrop.dotdrop --cfg="${cfg}" "${args[@]}"
|
PYTHONPATH=dotdrop python3 -m dotdrop.dotdrop --cfg="${cfg}" "${args[@]}"
|
||||||
# pivot back
|
# pivot back
|
||||||
cd "${opwd}" || { echo "Folder \"${opwd}\" doesn't exist, aborting." && exit; }
|
cd "${opwd}" || { echo "Directory \"${opwd}\" doesn't exist, aborting." && exit; }
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ class Installer:
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
def _handle_dir(self, templater, profile, src, dst):
|
def _handle_dir(self, templater, profile, src, dst):
|
||||||
'''Install a folder using templater for "profile"'''
|
'''Install a directory using templater for "profile"'''
|
||||||
ret = []
|
ret = []
|
||||||
for entry in os.listdir(src):
|
for entry in os.listdir(src):
|
||||||
f = os.path.join(src, entry)
|
f = os.path.join(src, entry)
|
||||||
@@ -143,14 +143,14 @@ class Installer:
|
|||||||
os.chmod(dst, rights)
|
os.chmod(dst, rights)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def _create_dirs(self, folder):
|
def _create_dirs(self, directory):
|
||||||
'''mkdir -p "folder"'''
|
'''mkdir -p "directory"'''
|
||||||
if not self.create and not os.path.exists(folder):
|
if not self.create and not os.path.exists(directory):
|
||||||
return False
|
return False
|
||||||
if os.path.exists(folder):
|
if os.path.exists(directory):
|
||||||
return True
|
return True
|
||||||
os.makedirs(folder)
|
os.makedirs(directory)
|
||||||
return os.path.exists(folder)
|
return os.path.exists(directory)
|
||||||
|
|
||||||
def _backup(self, path):
|
def _backup(self, path):
|
||||||
'''Backup the file'''
|
'''Backup the file'''
|
||||||
@@ -160,15 +160,15 @@ class Installer:
|
|||||||
self.log.log('backup %s to %s' % (path, dst))
|
self.log.log('backup %s to %s' % (path, dst))
|
||||||
os.rename(path, dst)
|
os.rename(path, dst)
|
||||||
|
|
||||||
def _install_to_temp(self, templater, profile, src, dst, tmpfolder):
|
def _install_to_temp(self, templater, profile, src, dst, tmpdir):
|
||||||
'''Install a dotfile to a tempfolder for comparing'''
|
'''Install a dotfile to a tempdir for comparing'''
|
||||||
sub = dst
|
sub = dst
|
||||||
if dst[0] == os.sep:
|
if dst[0] == os.sep:
|
||||||
sub = dst[1:]
|
sub = dst[1:]
|
||||||
tmpdst = os.path.join(tmpfolder, sub)
|
tmpdst = os.path.join(tmpdir, sub)
|
||||||
return self.install(templater, profile, src, tmpdst), tmpdst
|
return self.install(templater, profile, src, tmpdst), tmpdst
|
||||||
|
|
||||||
def compare(self, templater, tmpfolder, profile, src, dst, opts=''):
|
def compare(self, templater, tmpdir, profile, src, dst, opts=''):
|
||||||
'''Compare temporary generated dotfile with local one'''
|
'''Compare temporary generated dotfile with local one'''
|
||||||
self.comparing = True
|
self.comparing = True
|
||||||
retval = False, ''
|
retval = False, ''
|
||||||
@@ -186,7 +186,7 @@ class Installer:
|
|||||||
ret, tmpdst = self._install_to_temp(templater,
|
ret, tmpdst = self._install_to_temp(templater,
|
||||||
profile,
|
profile,
|
||||||
src, dst,
|
src, dst,
|
||||||
tmpfolder)
|
tmpdir)
|
||||||
if ret:
|
if ret:
|
||||||
diff = utils.diff(tmpdst, dst, raw=False, opts=opts)
|
diff = utils.diff(tmpdst, dst, raw=False, opts=opts)
|
||||||
if diff == '':
|
if diff == '':
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ TMPSUFFIX = '.dotdrop'
|
|||||||
|
|
||||||
|
|
||||||
def clean(path):
|
def clean(path):
|
||||||
'''Delete file or folder.'''
|
'''Delete file or directory.'''
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
return
|
return
|
||||||
if os.path.islink(path):
|
if os.path.islink(path):
|
||||||
@@ -33,13 +33,13 @@ def get_string(length):
|
|||||||
return ''.join(random.choice(alpha) for _ in range(length))
|
return ''.join(random.choice(alpha) for _ in range(length))
|
||||||
|
|
||||||
|
|
||||||
def get_tempfolder():
|
def get_tempdir():
|
||||||
'''Get a temporary folder'''
|
'''Get a temporary directory'''
|
||||||
return tempfile.mkdtemp(suffix=TMPSUFFIX)
|
return tempfile.mkdtemp(suffix=TMPSUFFIX)
|
||||||
|
|
||||||
|
|
||||||
def create_random_file(folder, content=None, binary=False):
|
def create_random_file(directory, content=None, binary=False):
|
||||||
'''Create a new file in folder with random content.'''
|
'''Create a new file in directory with random content.'''
|
||||||
fname = get_string(8)
|
fname = get_string(8)
|
||||||
mode = 'w'
|
mode = 'w'
|
||||||
if binary:
|
if binary:
|
||||||
@@ -49,14 +49,14 @@ def create_random_file(folder, content=None, binary=False):
|
|||||||
content = bytes(get_string(100), 'ascii')
|
content = bytes(get_string(100), 'ascii')
|
||||||
else:
|
else:
|
||||||
content = get_string(100)
|
content = get_string(100)
|
||||||
path = os.path.join(folder, fname)
|
path = os.path.join(directory, fname)
|
||||||
with open(path, mode) as f:
|
with open(path, mode) as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
return path, content
|
return path, content
|
||||||
|
|
||||||
|
|
||||||
def create_dir(path):
|
def create_dir(path):
|
||||||
'''Create a folder'''
|
'''Create a directory'''
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
os.mkdir(path)
|
os.mkdir(path)
|
||||||
return path
|
return path
|
||||||
@@ -93,10 +93,10 @@ def get_dotfile_from_yaml(dic, path):
|
|||||||
return [d for d in dotfiles.values() if d['src'] == src][0]
|
return [d for d in dotfiles.values() if d['src'] == src][0]
|
||||||
|
|
||||||
|
|
||||||
def create_fake_config(folder, configname='config.yaml',
|
def create_fake_config(directory, configname='config.yaml',
|
||||||
dotpath='dotfiles', backup=True, create=True):
|
dotpath='dotfiles', backup=True, create=True):
|
||||||
'''Create a fake config file'''
|
'''Create a fake config file'''
|
||||||
path = os.path.join(folder, configname)
|
path = os.path.join(directory, configname)
|
||||||
with open(path, 'w') as f:
|
with open(path, 'w') as f:
|
||||||
f.write('config:\n')
|
f.write('config:\n')
|
||||||
f.write(' backup: %s\n' % (str(backup)))
|
f.write(' backup: %s\n' % (str(backup)))
|
||||||
|
|||||||
@@ -49,23 +49,23 @@ class TestCompare(unittest.TestCase):
|
|||||||
|
|
||||||
def test_compare(self):
|
def test_compare(self):
|
||||||
'''Test the compare function'''
|
'''Test the compare function'''
|
||||||
# setup some folders
|
# setup some directories
|
||||||
fold_config = os.path.join(os.path.expanduser('~'), '.config')
|
fold_config = os.path.join(os.path.expanduser('~'), '.config')
|
||||||
create_dir(fold_config)
|
create_dir(fold_config)
|
||||||
fold_subcfg = os.path.join(os.path.expanduser('~'), '.config',
|
fold_subcfg = os.path.join(os.path.expanduser('~'), '.config',
|
||||||
get_string(5))
|
get_string(5))
|
||||||
create_dir(fold_subcfg)
|
create_dir(fold_subcfg)
|
||||||
self.addCleanup(clean, fold_subcfg)
|
self.addCleanup(clean, fold_subcfg)
|
||||||
fold_tmp = get_tempfolder()
|
fold_tmp = get_tempdir()
|
||||||
create_dir(fold_tmp)
|
create_dir(fold_tmp)
|
||||||
self.addCleanup(clean, fold_tmp)
|
self.addCleanup(clean, fold_tmp)
|
||||||
|
|
||||||
# create the folders
|
# create the directories
|
||||||
tmp = get_tempfolder()
|
tmp = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(tmp))
|
self.assertTrue(os.path.exists(tmp))
|
||||||
self.addCleanup(clean, tmp)
|
self.addCleanup(clean, tmp)
|
||||||
|
|
||||||
dotfilespath = get_tempfolder()
|
dotfilespath = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(dotfilespath))
|
self.assertTrue(os.path.exists(dotfilespath))
|
||||||
self.addCleanup(clean, dotfilespath)
|
self.addCleanup(clean, dotfilespath)
|
||||||
|
|
||||||
@@ -82,7 +82,7 @@ class TestCompare(unittest.TestCase):
|
|||||||
d4, c4 = create_random_file(fold_tmp, binary=True)
|
d4, c4 = create_random_file(fold_tmp, binary=True)
|
||||||
self.assertTrue(os.path.exists(d4))
|
self.assertTrue(os.path.exists(d4))
|
||||||
self.addCleanup(clean, d4)
|
self.addCleanup(clean, d4)
|
||||||
d5 = get_tempfolder()
|
d5 = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(d5))
|
self.assertTrue(os.path.exists(d5))
|
||||||
self.addCleanup(clean, d5)
|
self.addCleanup(clean, d5)
|
||||||
d6, _ = create_random_file(d5)
|
d6, _ = create_random_file(d5)
|
||||||
@@ -120,7 +120,7 @@ class TestCompare(unittest.TestCase):
|
|||||||
results = self.compare(opts, conf, tmp, len(dfiles))
|
results = self.compare(opts, conf, tmp, len(dfiles))
|
||||||
self.assertTrue(results == expected)
|
self.assertTrue(results == expected)
|
||||||
|
|
||||||
# add file in folder
|
# add file in directory
|
||||||
d7, _ = create_random_file(d5)
|
d7, _ = create_random_file(d5)
|
||||||
self.assertTrue(os.path.exists(d7))
|
self.assertTrue(os.path.exists(d7))
|
||||||
expected = {d1: False, d2: True, d3: True, d4: False, d5: False}
|
expected = {d1: False, d2: True, d3: True, d4: False, d5: False}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class TestConfig(unittest.TestCase):
|
|||||||
|
|
||||||
def test_config(self):
|
def test_config(self):
|
||||||
'''Test the config class'''
|
'''Test the config class'''
|
||||||
tmp = get_tempfolder()
|
tmp = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(tmp))
|
self.assertTrue(os.path.exists(tmp))
|
||||||
self.addCleanup(clean, tmp)
|
self.addCleanup(clean, tmp)
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ class TestConfig(unittest.TestCase):
|
|||||||
self.assertTrue(conf.dump() != '')
|
self.assertTrue(conf.dump() != '')
|
||||||
|
|
||||||
def test_include(self):
|
def test_include(self):
|
||||||
tmp = get_tempfolder()
|
tmp = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(tmp))
|
self.assertTrue(os.path.exists(tmp))
|
||||||
self.addCleanup(clean, tmp)
|
self.addCleanup(clean, tmp)
|
||||||
|
|
||||||
|
|||||||
@@ -53,12 +53,12 @@ class TestImport(unittest.TestCase):
|
|||||||
def test_import(self):
|
def test_import(self):
|
||||||
'''Test the import function'''
|
'''Test the import function'''
|
||||||
# on filesystem
|
# on filesystem
|
||||||
src = get_tempfolder()
|
src = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(src))
|
self.assertTrue(os.path.exists(src))
|
||||||
self.addCleanup(clean, src)
|
self.addCleanup(clean, src)
|
||||||
|
|
||||||
# in dotdrop
|
# in dotdrop
|
||||||
dotfilespath = get_tempfolder()
|
dotfilespath = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(dotfilespath))
|
self.assertTrue(os.path.exists(dotfilespath))
|
||||||
self.addCleanup(clean, dotfilespath)
|
self.addCleanup(clean, dotfilespath)
|
||||||
|
|
||||||
@@ -87,8 +87,8 @@ class TestImport(unittest.TestCase):
|
|||||||
dotfile4, content3 = create_random_file(homeconf)
|
dotfile4, content3 = create_random_file(homeconf)
|
||||||
self.addCleanup(clean, dotfile4)
|
self.addCleanup(clean, dotfile4)
|
||||||
|
|
||||||
# fake a folder containing dotfiles
|
# fake a directory containing dotfiles
|
||||||
dotfile5 = get_tempfolder()
|
dotfile5 = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(dotfile5))
|
self.assertTrue(os.path.exists(dotfile5))
|
||||||
self.addCleanup(clean, dotfile5)
|
self.addCleanup(clean, dotfile5)
|
||||||
sub1, _ = create_random_file(dotfile5)
|
sub1, _ = create_random_file(dotfile5)
|
||||||
@@ -98,8 +98,8 @@ class TestImport(unittest.TestCase):
|
|||||||
dotfile6, content6 = create_random_file(dotconfig)
|
dotfile6, content6 = create_random_file(dotconfig)
|
||||||
self.addCleanup(clean, dotfile6)
|
self.addCleanup(clean, dotfile6)
|
||||||
|
|
||||||
# fake a folder for symlink
|
# fake a directory for symlink
|
||||||
dotfile7 = get_tempfolder()
|
dotfile7 = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(dotfile7))
|
self.assertTrue(os.path.exists(dotfile7))
|
||||||
self.addCleanup(clean, dotfile7)
|
self.addCleanup(clean, dotfile7)
|
||||||
sub3, _ = create_random_file(dotfile7)
|
sub3, _ = create_random_file(dotfile7)
|
||||||
@@ -137,7 +137,7 @@ class TestImport(unittest.TestCase):
|
|||||||
self.assert_in_yaml(dotfile6, y, link=True)
|
self.assert_in_yaml(dotfile6, y, link=True)
|
||||||
self.assert_in_yaml(dotfile7, y, link=True)
|
self.assert_in_yaml(dotfile7, y, link=True)
|
||||||
|
|
||||||
# test have been imported in dotdrop dotpath folder
|
# test have been imported in dotdrop dotpath directory
|
||||||
indt1 = os.path.join(dotfilespath,
|
indt1 = os.path.join(dotfilespath,
|
||||||
self.CONFIG_DOTPATH,
|
self.CONFIG_DOTPATH,
|
||||||
get_path_strip_version(dotfile1))
|
get_path_strip_version(dotfile1))
|
||||||
|
|||||||
@@ -69,12 +69,12 @@ exec bspwm
|
|||||||
'''Test the install function'''
|
'''Test the install function'''
|
||||||
|
|
||||||
# dotpath location
|
# dotpath location
|
||||||
tmp = get_tempfolder()
|
tmp = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(tmp))
|
self.assertTrue(os.path.exists(tmp))
|
||||||
self.addCleanup(clean, tmp)
|
self.addCleanup(clean, tmp)
|
||||||
|
|
||||||
# where dotfiles will be installed
|
# where dotfiles will be installed
|
||||||
dst = get_tempfolder()
|
dst = get_tempdir()
|
||||||
self.assertTrue(os.path.exists(dst))
|
self.assertTrue(os.path.exists(dst))
|
||||||
self.addCleanup(clean, dst)
|
self.addCleanup(clean, dst)
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ exec bspwm
|
|||||||
self.addCleanup(clean, dst5)
|
self.addCleanup(clean, dst5)
|
||||||
d5 = Dotfile(get_string(6), dst5, os.path.basename(f5), link=True)
|
d5 = Dotfile(get_string(6), dst5, os.path.basename(f5), link=True)
|
||||||
|
|
||||||
# create the dotfile folders in dotdrop
|
# create the dotfile directories in dotdrop
|
||||||
dir1 = create_dir(os.path.join(tmp, get_string(6)))
|
dir1 = create_dir(os.path.join(tmp, get_string(6)))
|
||||||
self.assertTrue(os.path.exists(dir1))
|
self.assertTrue(os.path.exists(dir1))
|
||||||
self.addCleanup(clean, dir1)
|
self.addCleanup(clean, dir1)
|
||||||
@@ -119,7 +119,7 @@ exec bspwm
|
|||||||
# make up the dotfile
|
# make up the dotfile
|
||||||
d6 = Dotfile(get_string(6), dst6, os.path.basename(dir1))
|
d6 = Dotfile(get_string(6), dst6, os.path.basename(dir1))
|
||||||
|
|
||||||
# to test symlink folders
|
# to test symlink directories
|
||||||
dir2 = create_dir(os.path.join(tmp, get_string(6)))
|
dir2 = create_dir(os.path.join(tmp, get_string(6)))
|
||||||
self.assertTrue(os.path.exists(dir2))
|
self.assertTrue(os.path.exists(dir2))
|
||||||
self.addCleanup(clean, dir2)
|
self.addCleanup(clean, dir2)
|
||||||
|
|||||||
Reference in New Issue
Block a user