Переглянути джерело

Raise exception when local file is not found

Bertrand Chenal 7 роки тому
батько
коміт
b96c3f8292
4 змінених файлів з 21 додано та 7 видалено
  1. 9 2
      baker.py
  2. 9 2
      tests/base_test.py
  3. 2 2
      tests/load_test.yaml
  4. 1 1
      tests/task_only_test.yaml

+ 9 - 2
baker.py

@@ -552,6 +552,8 @@ def run_remote(task, host, env, cli):
         local_path = env.fmt(task.send)
         remote_path = env.fmt(task.to)
         logger.info(f'[send] {local_path} -> {host}:{remote_path}')
+        if not os.path.exists(local_path):
+            BakerException('Path "%s" not found'  % local_path)
         if cli.dry_run:
             logger.info('[dry-run]')
             return
@@ -559,7 +561,7 @@ def run_remote(task, host, env, cli):
             with client.open_sftp() as sftp:
                 if os.path.isfile(local_path):
                     sftp.put(os.path.abspath(local_path), remote_path)
-                else:
+                elif os.path.isdir(local_path):
                     for root, subdirs, files in os.walk(local_path):
                         rel_dir = os.path.relpath(root, local_path)
                         rel_dirs = os.path.split(rel_dir)
@@ -569,6 +571,9 @@ def run_remote(task, host, env, cli):
                             rel_f = os.path.join(root, f)
                             rem_file = posixpath.join(rem_dir, f)
                             sftp.put(os.path.abspath(rel_f), rem_file)
+                else:
+                    msg = 'Unexpected path "%s" (not a file, not a directory)'
+                    BakerException(msg % local_path)
     else:
         raise BakerException('Unable to run task "%s"' % task.name)
 
@@ -697,7 +702,7 @@ def load_cfg(path, prefix=None):
         key_fn = lambda x: '/'.join(prefix + [x])
         # Apply prefix
         for section in load_sections:
-            if not cfg.get(section):
+            if not section in cfg:
                 continue
             items = cfg[section].items()
             cfg[section] = {key_fn(k): v for k, v in items}
@@ -714,6 +719,8 @@ def load_cfg(path, prefix=None):
             child_cfg = load_cfg(child_path, child_prefix.split('/'))
 
             for section in load_sections:
+                if not section in cfg:
+                    continue
                 cfg[section].update(child_cfg.get(section, {}))
     return cfg
 

+ 9 - 2
tests/base_test.py

@@ -1,8 +1,9 @@
+import os
 from shlex import shlex
 
 import pytest
 
-from baker import run_batch, load_cli
+from baker import run_batch, load_cli, Env
 
 
 def test_all_conf(test_cfg, log_buff):
@@ -15,8 +16,14 @@ def test_all_conf(test_cfg, log_buff):
         args = list(lexer)
     cli = load_cli(args)
 
+    base_env = Env(
+        cli.env,
+        cli.cfg.get('env'),
+        os.environ,
+    )
+
     for task in cli.tasks:
-        run_batch(task, cli.hosts, cli, cli.env)
+        run_batch(task, cli.hosts, cli, base_env)
 
     actual_lines = log_buff.getvalue().splitlines()
     actual_lines = [l.strip() for l in filter(None, actual_lines)]

+ 2 - 2
tests/load_test.yaml

@@ -3,6 +3,6 @@ output: |
   Load config examples/load.yaml
   Load config examples/network_only.yaml
   echo-host
-  [DRY-RUN] echo web1.example.com
+  [dry-run] echo web1.example.com
   echo-host
-  [DRY-RUN] echo web2.example.com
+  [dry-run] echo web2.example.com

+ 1 - 1
tests/task_only_test.yaml

@@ -2,4 +2,4 @@ cli: "--dry-run -c examples/task_only.yaml time"
 output: |
   Load config examples/task_only.yaml
   Print current time (on local machine)
-  [DRY-RUN] date -Iseconds
+  [dry-run] date -Iseconds