From 8bc6519a2519bac2c95d05d04c6d41233ecee3eb Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Thu, 5 Nov 2020 22:17:26 +0100 Subject: [PATCH] use magic for determining file types and remove mkdir dependency --- docs/installation.md | 7 +++---- dotdrop/dotdrop.py | 11 ++++++----- dotdrop/templategen.py | 13 +++++++++---- requirements.txt | 1 + 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/docs/installation.md b/docs/installation.md index 0e234b5..5a7b10d 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -133,11 +133,10 @@ $ ./dotdrop.sh --cfg files Beside the python dependencies defined in [requirements.txt](https://github.com/deadc0de6/dotdrop/blob/master/requirements.txt), dotdrop depends on following tools: -* `file` * `diff` -* `mkdir` -* `git` (for 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)) +* `file` (if libmagic is not installed, see ) +* `git` (only if using 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/). diff --git a/dotdrop/dotdrop.py b/dotdrop/dotdrop.py index c6ca4a6..a12fd40 100644 --- a/dotdrop/dotdrop.py +++ b/dotdrop/dotdrop.py @@ -18,7 +18,7 @@ from dotdrop.installer import Installer from dotdrop.updater import Updater from dotdrop.comparator import Comparator 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.exceptions import YamlException, UndefinedException @@ -438,12 +438,13 @@ def cmd_importer(o): if o.debug: LOG.dbg('will overwrite: {}'.format(overwrite)) if overwrite: - cmd = ['mkdir', '-p', '{}'.format(os.path.dirname(srcf))] + cmd = 'mkdir -p {}'.format(os.path.dirname(srcf)) if o.dry: - LOG.dry('would run: {}'.format(' '.join(cmd))) + LOG.dry('would run: {}'.format(cmd)) else: - r, _ = run(cmd, raw=False, debug=o.debug, checkerr=True) - if not r: + try: + os.makedirs(os.path.dirname(srcf), exist_ok=True) + except Exception: LOG.err('importing \"{}\" failed!'.format(path)) ret = False continue diff --git a/dotdrop/templategen.py b/dotdrop/templategen.py index feb7db0..ae40617 100644 --- a/dotdrop/templategen.py +++ b/dotdrop/templategen.py @@ -146,12 +146,17 @@ class Templategen: def _handle_file(self, src): """generate the file content from template""" - _, filetype = utils.run(['file', '-b', src], - raw=False, debug=self.debug) - filetype = filetype.strip() + try: + import magic + 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: self.log.dbg('filetype \"{}\": {}'.format(src, filetype)) - istext = self._is_text(filetype) if self.debug: self.log.dbg('is text \"{}\": {}'.format(src, istext)) if not istext: diff --git a/requirements.txt b/requirements.txt index 212fe0e..7ae81ea 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ Jinja2; python_version > '3.4' docopt; python_version > '3.4' ruamel.yaml; python_version > '3.4' +python-magic; python_version > '3.4'