Repository: conan-io/conan

Issue:
ValueError exception in client/tools/win.py **bug**
Upgrading to conan 1.3.3 broke my build slave with the following stack trace:

Traceback (most recent call last):
  File "build.py", line 30, in <module>
    builder.run()
  File "E:\Program Files\Python36\lib\site-packages\cpt\packager.py", line 328, in run
    self.run_builds(base_profile_name=base_profile_name)
  File "E:\Program Files\Python36\lib\site-packages\cpt\packager.py", line 404, in run_builds
    r.run()
  File "E:\Program Files\Python36\lib\site-packages\cpt\runner.py", line 48, in run
    with context:
  File "E:\Program Files\Python36\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "E:\Program Files\Python36\lib\site-packages\conans\client\tools\win.py", line 326, in vcvars
    new_env = vcvars_dict(*args, **kwargs)
  File "E:\Program Files\Python36\lib\site-packages\conans\client\tools\win.py", line 307, in vcvars_dict
    name_var, value = line.split("=", 1)
ValueError: not enough values to unpack (expected 2, got 1)

Details: Conan version 1.3.3, Windows 7, CMake 3.8.2, Visual Studio 2013 Win64


By looking in to it, what happens is line.split("=", 1) on an empty line, in vcvars_dict(). No idea where the empty line comes from (likely from one of VS env scripts), but the following patch solves the symptom. 

```
 conans/client/tools/win.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/conans/client/tools/win.py b/conans/client/tools/win.py
index d025ec4..56dcbbd 100644
--- a/conans/client/tools/win.py
+++ b/conans/client/tools/win.py
@@ -304,8 +304,11 @@ def vcvars_dict(settings, arch=None, compiler_version=None, force=False, filter_
             if "__BEGINS__" in line:
                 start_reached = True
             continue
-        name_var, value = line.split("=", 1)
-        new_env[name_var] = value
+        try:
+            name_var, value = line.split("=", 1)
+            new_env[name_var] = value
+        except (ValueError):
+            pass
 
     if filter_known_paths:
         def relevant_path(path):



```


Discussion:
Hi @foobar222 

I see you are using conan package tools right? Could you indicate the version you are using?
Version of conan_package_tools:

E:\src\build>pip show conan_package_tools
Name: conan-package-tools
Version: 0.17.1
Summary: Packaging tools for Conan C/C++ package manager
Home-page: https://github.com/conan-io/conan-package-tools
Author: JFrog LTD. Luis Martinez de Bartolome
Author-email: luism@jfrog.com
License: MIT
Location: e:\program files\python36\lib\site-packages
Requires: six, requests, conan, tabulate
Required-by:

Resolving patch:
diff --git a/conans/client/tools/win.py b/conans/client/tools/win.py
--- a/conans/client/tools/win.py
+++ b/conans/client/tools/win.py
@@ -300,12 +300,18 @@ def vcvars_dict(settings, arch=None, compiler_version=None, force=False, filter_
     new_env = {}
     start_reached = False
     for line in ret.splitlines():
+        line = line.strip()
         if not start_reached:
             if "__BEGINS__" in line:
                 start_reached = True
             continue
-        name_var, value = line.split("=", 1)
-        new_env[name_var] = value
+        if line == "\n" or not line:
+            continue
+        try:
+            name_var, value = line.split("=", 1)
+            new_env[name_var] = value
+        except ValueError:
+            pass
 
     if filter_known_paths:
         def relevant_path(path):


Test patch:
diff --git a/conans/test/util/tools_test.py b/conans/test/util/tools_test.py
--- a/conans/test/util/tools_test.py
+++ b/conans/test/util/tools_test.py
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+import mock
 import os
 import platform
 import unittest
@@ -625,6 +626,43 @@ def build(self):
             client.run("create . conan/testing")
             self.assertIn("VCINSTALLDIR set to: None", client.out)
 
+    def vcvars_dict_test(self):
+        # https://github.com/conan-io/conan/issues/2904
+        output_with_newline_and_spaces = """__BEGINS__
+     PROCESSOR_ARCHITECTURE=AMD64
+
+PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
+
+
+ PROCESSOR_LEVEL=6 
+
+PROCESSOR_REVISION=9e09    
+
+                         
+set nl=^
+env_var=
+without_equals_sign
+
+ProgramFiles(x86)=C:\Program Files (x86)
+       
+""".encode("utf-8")
+
+        def vcvars_command_mock(settings, arch, compiler_version, force):  # @UnusedVariable
+            return "unused command"
+
+        def subprocess_check_output_mock(cmd, shell):
+            self.assertIn("unused command", cmd)
+            return output_with_newline_and_spaces
+
+        with mock.patch('conans.client.tools.win.vcvars_command', new=vcvars_command_mock):
+            with mock.patch('subprocess.check_output', new=subprocess_check_output_mock):
+                vars = tools.vcvars_dict(None)
+                self.assertEqual(vars["PROCESSOR_ARCHITECTURE"], "AMD64")
+                self.assertEqual(vars["PROCESSOR_IDENTIFIER"], "Intel64 Family 6 Model 158 Stepping 9, GenuineIntel")
+                self.assertEqual(vars["PROCESSOR_LEVEL"], "6")
+                self.assertEqual(vars["PROCESSOR_REVISION"], "9e09")
+                self.assertEqual(vars["ProgramFiles(x86)"], "C:\Program Files (x86)")
+
     def run_in_bash_test(self):
         if platform.system() != "Windows":
             return