mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-06 14:17:59 +00:00
@@ -5,6 +5,7 @@ Represent an action in dotdrop
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
# local imports
|
||||
from dotdrop.logger import Logger
|
||||
@@ -18,11 +19,30 @@ class Action:
|
||||
self.log = Logger()
|
||||
|
||||
def execute(self):
|
||||
ret = 1
|
||||
self.log.sub('executing \"%s\"' % (self.action))
|
||||
try:
|
||||
subprocess.call(self.action, shell=True)
|
||||
ret = subprocess.call(self.action, shell=True)
|
||||
except KeyboardInterrupt:
|
||||
self.log.warn('action interrupted')
|
||||
return ret == 0
|
||||
|
||||
def transform(self, arg0, arg1):
|
||||
'''execute transformation with {0} and {1}
|
||||
where {0} is the file to transform and
|
||||
{1} is the result file'''
|
||||
if os.path.exists(arg1):
|
||||
msg = 'transformation destination exists: %s'
|
||||
self.log.warn(msg % (arg1))
|
||||
return False
|
||||
ret = 1
|
||||
cmd = self.action.format(arg0, arg1)
|
||||
self.log.sub('transforming with \"%s\"' % (cmd))
|
||||
try:
|
||||
ret = subprocess.call(cmd, shell=True)
|
||||
except KeyboardInterrupt:
|
||||
self.log.warn('action interrupted')
|
||||
return ret == 0
|
||||
|
||||
def __str__(self):
|
||||
return 'key:%s -> \"%s\"' % (self.key, self.action)
|
||||
|
||||
@@ -18,6 +18,7 @@ class Cfg:
|
||||
key_config = 'config'
|
||||
key_dotfiles = 'dotfiles'
|
||||
key_actions = 'actions'
|
||||
key_trans = 'trans'
|
||||
key_dotpath = 'dotpath'
|
||||
key_profiles = 'profiles'
|
||||
key_profiles_dots = 'dotfiles'
|
||||
@@ -26,6 +27,7 @@ class Cfg:
|
||||
key_dotfiles_dst = 'dst'
|
||||
key_dotfiles_link = 'link'
|
||||
key_dotfiles_actions = 'actions'
|
||||
key_dotfiles_trans = 'trans'
|
||||
|
||||
def __init__(self, cfgpath):
|
||||
if not os.path.exists(cfgpath):
|
||||
@@ -41,6 +43,8 @@ class Cfg:
|
||||
# not linked to content
|
||||
self.actions = {}
|
||||
# not linked to content
|
||||
self.trans = {}
|
||||
# not linked to content
|
||||
self.prodots = {}
|
||||
if not self._load_file():
|
||||
raise ValueError('config is not valid')
|
||||
@@ -94,6 +98,12 @@ class Cfg:
|
||||
for k, v in self.content[self.key_actions].items():
|
||||
self.actions[k] = Action(k, v)
|
||||
|
||||
# parse all transformations
|
||||
if self.key_trans in self.content:
|
||||
if self.content[self.key_trans] is not None:
|
||||
for k, v in self.content[self.key_trans].items():
|
||||
self.trans[k] = Action(k, v)
|
||||
|
||||
# parse the profiles
|
||||
self.profiles = self.content[self.key_profiles]
|
||||
if self.profiles is None:
|
||||
@@ -117,8 +127,17 @@ class Cfg:
|
||||
entries = v[self.key_dotfiles_actions] if \
|
||||
self.key_dotfiles_actions in v else []
|
||||
actions = self._parse_actions(self.actions, entries)
|
||||
self.dotfiles[k] = Dotfile(k, dst, src,
|
||||
link=link, actions=actions)
|
||||
entries = v[self.key_dotfiles_trans] if \
|
||||
self.key_dotfiles_trans in v else []
|
||||
trans = self._parse_actions(self.trans, entries)
|
||||
if len(trans) > 0 and link:
|
||||
msg = 'transformations disabled for \"%s\"' % (dst)
|
||||
msg += ' as link is True' % (dst)
|
||||
self.log.warn(msg)
|
||||
trans = []
|
||||
self.dotfiles[k] = Dotfile(k, dst, src, link=link,
|
||||
actions=actions,
|
||||
trans=trans)
|
||||
|
||||
# assign dotfiles to each profile
|
||||
for k, v in self.profiles.items():
|
||||
|
||||
@@ -45,6 +45,7 @@ CUR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
LOG = Logger()
|
||||
HOSTNAME = os.uname()[1]
|
||||
TILD = '~'
|
||||
TRANS_SUFFIX = 'trans'
|
||||
|
||||
BANNER = """ _ _ _
|
||||
__| | ___ | |_ __| |_ __ ___ _ __
|
||||
@@ -101,7 +102,29 @@ def install(opts, conf):
|
||||
if hasattr(dotfile, 'link') and dotfile.link:
|
||||
r = inst.link(dotfile.src, dotfile.dst)
|
||||
else:
|
||||
r = inst.install(t, opts['profile'], dotfile.src, dotfile.dst)
|
||||
src = dotfile.src
|
||||
tmp = None
|
||||
if dotfile.trans:
|
||||
tmp = '%s.%s' % (src, TRANS_SUFFIX)
|
||||
err = False
|
||||
for trans in dotfile.trans:
|
||||
s = os.path.join(opts['dotpath'], src)
|
||||
temp = os.path.join(opts['dotpath'], tmp)
|
||||
if not trans.transform(s, temp):
|
||||
msg = 'transformation \"%s\" failed for %s'
|
||||
LOG.err(msg % (trans.key, dotfile.key))
|
||||
err = True
|
||||
break
|
||||
if err:
|
||||
if tmp and os.path.exists(tmp):
|
||||
remove(tmp)
|
||||
continue
|
||||
src = tmp
|
||||
r = inst.install(t, opts['profile'], src, dotfile.dst)
|
||||
if tmp:
|
||||
tmp = os.path.join(opts['dotpath'], tmp)
|
||||
if os.path.exists(tmp):
|
||||
remove(tmp)
|
||||
if len(r) > 0 and len(dotfile.actions) > 0:
|
||||
# execute action
|
||||
for action in dotfile.actions:
|
||||
@@ -140,6 +163,10 @@ def compare(opts, conf, tmp, focus=None):
|
||||
return ret
|
||||
|
||||
for dotfile in selected:
|
||||
if dotfile.trans:
|
||||
msg = 'ignore %s as it uses transformation(s)'
|
||||
LOG.log(msg % (dotfile.key))
|
||||
continue
|
||||
same, diff = inst.compare(t, tmp, opts['profile'],
|
||||
dotfile.src, dotfile.dst,
|
||||
opts=opts['dopts'])
|
||||
|
||||
@@ -7,7 +7,8 @@ represents a dotfile in dotdrop
|
||||
|
||||
class Dotfile:
|
||||
|
||||
def __init__(self, key, dst, src, actions=[], link=False):
|
||||
def __init__(self, key, dst, src,
|
||||
actions=[], trans=[], link=False):
|
||||
# key of dotfile in the config
|
||||
self.key = key
|
||||
# where to install this dotfile
|
||||
@@ -18,6 +19,8 @@ class Dotfile:
|
||||
self.link = link
|
||||
# list of actions
|
||||
self.actions = actions
|
||||
# list of transformations
|
||||
self.trans = trans
|
||||
|
||||
def __str__(self):
|
||||
return 'key:%s, src: %s, dst: %s, link: %s' % (self.key, self.src,
|
||||
|
||||
@@ -38,6 +38,11 @@ def get_tmpdir():
|
||||
return tempfile.mkdtemp(prefix='dotdrop-')
|
||||
|
||||
|
||||
def get_tmpfile():
|
||||
(fd, path) = tempfile.mkstemp(prefix='dotdrop-')
|
||||
return path
|
||||
|
||||
|
||||
def remove(path):
|
||||
''' Remove a file / directory / symlink '''
|
||||
if not os.path.exists(path):
|
||||
|
||||
Reference in New Issue
Block a user