1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-10 17:24:15 +00:00

use magic for determining file types and remove mkdir dependency

This commit is contained in:
deadc0de6
2020-11-05 22:17:26 +01:00
parent 29d23fe549
commit 8bc6519a25
4 changed files with 19 additions and 13 deletions

View File

@@ -133,11 +133,10 @@ $ ./dotdrop.sh --cfg <my-config-file> files
Beside the python dependencies defined in [requirements.txt](https://github.com/deadc0de6/dotdrop/blob/master/requirements.txt), Beside the python dependencies defined in [requirements.txt](https://github.com/deadc0de6/dotdrop/blob/master/requirements.txt),
dotdrop depends on following tools: dotdrop depends on following tools:
* `file`
* `diff` * `diff`
* `mkdir` * `file` (if libmagic is not installed, see <https://github.com/ahupp/python-magic>)
* `git` (for the entry point script [dotdrop.sh](https://github.com/deadc0de6/dotdrop/blob/master/dotdrop.sh)) * `git` (only if using the entry point script [dotdrop.sh](https://github.com/deadc0de6/dotdrop/blob/master/dotdrop.sh))
* `readlink` or `realpath` (for the entry point script [dotdrop.sh](https://github.com/deadc0de6/dotdrop/blob/master/dotdrop.sh)) * `readlink` or `realpath` (only if using the entry point script [dotdrop.sh](https://github.com/deadc0de6/dotdrop/blob/master/dotdrop.sh))
For MacOS users, make sure to install `realpath` (part of `coreutils`) through [homebrew](https://brew.sh/). For MacOS users, make sure to install `realpath` (part of `coreutils`) through [homebrew](https://brew.sh/).

View File

@@ -18,7 +18,7 @@ from dotdrop.installer import Installer
from dotdrop.updater import Updater from dotdrop.updater import Updater
from dotdrop.comparator import Comparator from dotdrop.comparator import Comparator
from dotdrop.utils import get_tmpdir, remove, strip_home, \ from dotdrop.utils import get_tmpdir, remove, strip_home, \
run, uniq_list, patch_ignores, dependencies_met uniq_list, patch_ignores, dependencies_met
from dotdrop.linktypes import LinkTypes from dotdrop.linktypes import LinkTypes
from dotdrop.exceptions import YamlException, UndefinedException from dotdrop.exceptions import YamlException, UndefinedException
@@ -438,12 +438,13 @@ def cmd_importer(o):
if o.debug: if o.debug:
LOG.dbg('will overwrite: {}'.format(overwrite)) LOG.dbg('will overwrite: {}'.format(overwrite))
if overwrite: if overwrite:
cmd = ['mkdir', '-p', '{}'.format(os.path.dirname(srcf))] cmd = 'mkdir -p {}'.format(os.path.dirname(srcf))
if o.dry: if o.dry:
LOG.dry('would run: {}'.format(' '.join(cmd))) LOG.dry('would run: {}'.format(cmd))
else: else:
r, _ = run(cmd, raw=False, debug=o.debug, checkerr=True) try:
if not r: os.makedirs(os.path.dirname(srcf), exist_ok=True)
except Exception:
LOG.err('importing \"{}\" failed!'.format(path)) LOG.err('importing \"{}\" failed!'.format(path))
ret = False ret = False
continue continue

View File

@@ -146,12 +146,17 @@ class Templategen:
def _handle_file(self, src): def _handle_file(self, src):
"""generate the file content from template""" """generate the file content from template"""
_, filetype = utils.run(['file', '-b', src], try:
raw=False, debug=self.debug) import magic
filetype = filetype.strip() filetype = magic.from_file(src, mime=True)
istext = filetype.startswith('text')
except ImportError:
_, filetype = utils.run(['file', '-b', src],
raw=False, debug=self.debug)
filetype = filetype.strip()
istext = self._is_text(filetype)
if self.debug: if self.debug:
self.log.dbg('filetype \"{}\": {}'.format(src, filetype)) self.log.dbg('filetype \"{}\": {}'.format(src, filetype))
istext = self._is_text(filetype)
if self.debug: if self.debug:
self.log.dbg('is text \"{}\": {}'.format(src, istext)) self.log.dbg('is text \"{}\": {}'.format(src, istext))
if not istext: if not istext:

View File

@@ -1,3 +1,4 @@
Jinja2; python_version > '3.4' Jinja2; python_version > '3.4'
docopt; python_version > '3.4' docopt; python_version > '3.4'
ruamel.yaml; python_version > '3.4' ruamel.yaml; python_version > '3.4'
python-magic; python_version > '3.4'