We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
conan install -u traceback
When running `conan install -u` while no previous `conan install` was run (so e.g. no conan conanbuildinfo.cmake exists) I get the following traceback:

![image](https://cloud.githubusercontent.com/assets/11735226/17244088/59567f7c-5580-11e6-8a6f-4b026be70816.png)


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `conans/client/proxy.py`
Content:
```
1 from conans.client.output import ScopedOutput
2 from conans.util.files import path_exists, rmdir
3 from conans.model.ref import PackageReference
4 from conans.errors import (ConanException, ConanConnectionError, ConanOutdatedClient,
5                            NotFoundException)
6 from conans.client.remote_registry import RemoteRegistry
7 from conans.util.log import logger
8 import os
9 
10 
11 class ConanProxy(object):
12     """ Class to access the conan storage, to perform typical tasks as to get packages,
13     getting conanfiles, uploading, removing from remote, etc.
14     It uses the RemoteRegistry to control where the packages come from.
15     """
16     def __init__(self, paths, user_io, remote_manager, remote_name,
17                  update=False, check_updates=False, check_integrity=False):
18         self._paths = paths
19         self._out = user_io.out
20         self._remote_manager = remote_manager
21         self._registry = RemoteRegistry(self._paths.registry, self._out)
22         self._remote_name = remote_name
23         self._update = update
24         self._check_updates = check_updates or update  # Update forces check
25         self._check_integrity = check_integrity
26 
27     @property
28     def registry(self):
29         return self._registry
30 
31     def get_package(self, package_reference, force_build):
32         """ obtain a package, either from disk or retrieve from remotes if necessary
33         and not necessary to build
34         """
35         output = ScopedOutput(str(package_reference.conan), self._out)
36         package_folder = self._paths.package(package_reference)
37 
38         # Check current package status
39         if path_exists(package_folder, self._paths.store):
40             if self._check_integrity or self._check_updates:
41                 read_manifest, expected_manifest = self._paths.package_manifests(package_reference)
42 
43             if self._check_integrity:  # Check if package is corrupted
44                 if read_manifest.file_sums != expected_manifest.file_sums:
45                     # If not valid package, ensure empty folder
46                     output.warn("Bad package '%s' detected! Removing "
47                                 "package directory... " % str(package_reference.package_id))
48                     rmdir(package_folder)
49 
50             if self._check_updates:
51                 try:  # get_conan_digest can fail, not in server
52                     upstream_manifest = self.get_package_digest(package_reference)
53                     if upstream_manifest.file_sums != read_manifest.file_sums:
54                         if upstream_manifest.time > read_manifest.time:
55                             output.warn("Current package is older than remote upstream one")
56                             if self._update:
57                                 output.warn("Removing it to retrieve or build an updated one")
58                                 rmdir(package_folder)
59                         else:
60                             output.warn("Current package is newer than remote upstream one")
61                 except ConanException:
62                     pass
63 
64         if not force_build:
65             local_package = os.path.exists(package_folder)
66             if local_package:
67                 output = ScopedOutput(str(package_reference.conan), self._out)
68                 output.info('Already installed!')
69                 return True
70             return self._retrieve_remote_package(package_reference, output)
71 
72         return False
73 
74     def get_conanfile(self, conan_reference):
75         output = ScopedOutput(str(conan_reference), self._out)
76 
77         def _refresh():
78             conan_dir_path = self._paths.export(conan_reference)
79             rmdir(conan_dir_path)
80             rmdir(self._paths.source(conan_reference))
81             current_remote, _ = self._get_remote(conan_reference)
82             output.info("Retrieving from remote '%s'..." % current_remote.name)
83             self._remote_manager.get_conanfile(conan_reference, current_remote)
84             if self._update:
85                 output.info("Updated!")
86             else:
87                 output.info("Installed!")
88 
89         # check if it is in disk
90         conanfile_path = self._paths.conanfile(conan_reference)
91         is_min_path = conan_reference in self._paths.short_path_refs
92         if not is_min_path:
93             path_exist = path_exists(conanfile_path, self._paths.store)
94         else:  # Directory doesn't contain the reference, so we don't need to compare the cases
95             path_exist = os.path.exists(conanfile_path)
96 
97         if path_exist:
98             if self._check_integrity:  # Check if package is corrupted
99                 read_manifest, expected_manifest = self._paths.conan_manifests(conan_reference)
100                 if read_manifest.file_sums != expected_manifest.file_sums:
101                     output.warn("Bad conanfile detected! Removing export directory... ")
102                     _refresh()
103             else:  # Check for updates
104                 if self._check_updates:
105                     ret = self.update_available(conan_reference)
106                     if ret != 0:  # Found and not equal
107                         remote, ref_remote = self._get_remote(conan_reference)
108                         if ret == 1:
109                             if not self._update:
110                                 if remote != ref_remote:  # Forced new remote
111                                     output.warn("There is a new conanfile in '%s' remote. "
112                                                 "Execute 'install -u -r %s' to update it."
113                                                 % (remote.name, remote.name))
114                                 else:
115                                     output.warn("There is a new conanfile in '%s' remote. "
116                                                 "Execute 'install -u' to update it."
117                                                 % remote.name)
118                                 output.warn("Refused to install!")
119                             else:
120                                 if remote != ref_remote:
121                                     # Delete packages, could be non coherent with new remote
122                                     rmdir(self._paths.packages(conan_reference))
123                                 _refresh()
124                         elif ret == -1:
125                             if not self._update:
126                                 output.info("Current conanfile is newer "
127                                             "than %s's one" % remote.name)
128                             else:
129                                 output.error("Current conanfile is newer than %s's one. "
130                                              "Run 'conan remove %s' and run install again "
131                                              "to replace it." % (remote.name, conan_reference))
132 
133         else:
134             self._retrieve_conanfile(conan_reference, output)
135         return conanfile_path
136 
137     def update_available(self, conan_reference):
138         """Returns 0 if the conanfiles are equal, 1 if there is an update and -1 if
139         the local is newer than the remote"""
140         if not conan_reference:
141             return 0
142         read_manifest, _ = self._paths.conan_manifests(conan_reference)
143         try:  # get_conan_digest can fail, not in server
144             upstream_manifest = self.get_conan_digest(conan_reference)
145             if upstream_manifest.file_sums != read_manifest.file_sums:
146                 return 1 if upstream_manifest.time > read_manifest.time else -1
147         except ConanException:
148             pass
149 
150         return 0
151 
152     def _retrieve_conanfile(self, conan_reference, output):
153         """ returns the requested conanfile object, retrieving it from
154         remotes if necessary. Can raise NotFoundException
155         """
156         def _retrieve_from_remote(remote):
157             output.info("Trying with '%s'..." % remote.name)
158             result = self._remote_manager.get_conanfile(conan_reference, remote)
159             self._registry.set_ref(conan_reference, remote)
160             return result
161 
162         if self._remote_name:
163             output.info("Not found, retrieving from server '%s' " % self._remote_name)
164             remote = self._registry.remote(self._remote_name)
165             return _retrieve_from_remote(remote)
166         else:
167             output.info("Not found, looking in remotes...")
168 
169         remotes = self._registry.remotes
170         for remote in remotes:
171             logger.debug("Trying with remote %s" % remote.name)
172             try:
173                 return _retrieve_from_remote(remote)
174             # If exception continue with the next
175             except (ConanOutdatedClient, ConanConnectionError) as exc:
176                 output.warn(str(exc))
177                 if remote == remotes[-1]:  # Last element not found
178                     raise ConanConnectionError("All remotes failed")
179             except NotFoundException as exc:
180                 if remote == remotes[-1]:  # Last element not found
181                     logger.debug("Not found in any remote, raising...%s" % exc)
182                     raise NotFoundException("Unable to find '%s' in remotes"
183                                             % str(conan_reference))
184 
185         raise ConanException("No remote defined")
186 
187     def upload_conan(self, conan_reference):
188         """ upload to defined remote in (-r=remote), to current remote
189         or to default remote, in that order.
190         If the remote is not set, set it
191         """
192         remote, ref_remote = self._get_remote(conan_reference)
193 
194         result = self._remote_manager.upload_conan(conan_reference, remote)
195         if not ref_remote:
196             self._registry.set_ref(conan_reference, remote)
197         return result
198 
199     def _get_remote(self, conan_ref=None):
200         # Prioritize -r , then reference registry and then the default remote
201         ref_remote = self._registry.get_ref(conan_ref) if conan_ref else None
202         if self._remote_name:
203             remote = self._registry.remote(self._remote_name)
204         else:
205             if ref_remote:
206                 remote = ref_remote
207             else:
208                 remote = self._registry.default_remote
209         return remote, ref_remote
210 
211     def upload_package(self, package_reference):
212         remote, current_remote = self._get_remote(package_reference.conan)
213 
214         if not current_remote:
215             self._out.warn("Remote for '%s' not defined, uploading to %s"
216                            % (str(package_reference.conan), remote.name))
217         result = self._remote_manager.upload_package(package_reference, remote)
218         if not current_remote:
219             self._registry.set_ref(package_reference.conan, remote)
220         return result
221 
222     def get_conan_digest(self, conan_ref):
223         """ used by update to check the date of packages, require force if older
224         """
225         remote, current_remote = self._get_remote(conan_ref)
226         result = self._remote_manager.get_conan_digest(conan_ref, remote)
227         if not current_remote:
228             self._registry.set_ref(conan_ref, remote)
229         return result
230 
231     def get_package_digest(self, package_reference):
232         """ used by update to check the date of packages, require force if older
233         """
234         remote, ref_remote = self._get_remote(package_reference.conan)
235         result = self._remote_manager.get_package_digest(package_reference, remote)
236         if not ref_remote:
237             self._registry.set_ref(package_reference.conan, remote)
238         return result
239 
240     def search(self, pattern=None, ignorecase=True):
241         remote, _ = self._get_remote()
242         return self._remote_manager.search(remote, pattern, ignorecase)
243 
244     def remove(self, conan_ref):
245         if not self._remote_name:
246             raise ConanException("Cannot remove, remote not defined")
247         remote = self._registry.remote(self._remote_name)
248         result = self._remote_manager.remove(conan_ref, remote)
249         current_remote = self._registry.get_ref(conan_ref)
250         if current_remote == remote:
251             self._registry.remove_ref(conan_ref)
252         return result
253 
254     def remove_packages(self, conan_ref, remove_ids):
255         if not self._remote_name:
256             raise ConanException("Cannot remove, remote not defined")
257         remote = self._registry.remote(self._remote_name)
258         return self._remote_manager.remove_packages(conan_ref, remove_ids, remote)
259 
260     def download_packages(self, reference, package_ids):
261         assert(isinstance(package_ids, list))
262         remote, _ = self._get_remote(reference)
263         self._remote_manager.get_conanfile(reference, remote)
264         self._registry.set_ref(reference, remote)
265         output = ScopedOutput(str(reference), self._out)
266         for package_id in package_ids:
267             package_reference = PackageReference(reference, package_id)
268             self._retrieve_remote_package(package_reference, output, remote)
269 
270     def _retrieve_remote_package(self, package_reference, output, remote=None):
271 
272         if remote is None:
273             remote = self._registry.get_ref(package_reference.conan)
274         if not remote:
275             output.warn("Package doesn't have a remote defined. "
276                         "Probably created locally and not uploaded")
277             return False
278         package_id = str(package_reference.package_id)
279         try:
280             output.info("Looking for package %s in remote '%s' " % (package_id, remote.name))
281             # Will raise if not found NotFoundException
282             self._remote_manager.get_package(package_reference, remote)
283             output.success('Package installed %s' % package_id)
284             return True
285         except ConanConnectionError:
286             raise  # This shouldn't be skipped
287         except ConanException as e:
288             output.warn('Binary for %s not in remote: %s' % (package_id, str(e)))
289             return False
290 
291     def authenticate(self, name, password):
292         if not name:  # List all users, from all remotes
293             remotes = self._registry.remotes
294             if not remotes:
295                 self._out.error("No remotes defined")
296             for remote in remotes:
297                 self._remote_manager.authenticate(remote, None, None)
298             return
299         remote, _ = self._get_remote()
300         return self._remote_manager.authenticate(remote, name, password)
301
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/conans/client/proxy.py b/conans/client/proxy.py
--- a/conans/client/proxy.py
+++ b/conans/client/proxy.py
@@ -140,12 +140,13 @@
         if not conan_reference:
             return 0
         read_manifest, _ = self._paths.conan_manifests(conan_reference)
-        try:  # get_conan_digest can fail, not in server
-            upstream_manifest = self.get_conan_digest(conan_reference)
-            if upstream_manifest.file_sums != read_manifest.file_sums:
-                return 1 if upstream_manifest.time > read_manifest.time else -1
-        except ConanException:
-            pass
+        if read_manifest:
+            try:  # get_conan_digest can fail, not in server
+                upstream_manifest = self.get_conan_digest(conan_reference)
+                if upstream_manifest.file_sums != read_manifest.file_sums:
+                    return 1 if upstream_manifest.time > read_manifest.time else -1
+            except ConanException:
+                pass
 
         return 0

{"golden_diff": "diff --git a/conans/client/proxy.py b/conans/client/proxy.py\n--- a/conans/client/proxy.py\n+++ b/conans/client/proxy.py\n@@ -140,12 +140,13 @@\n         if not conan_reference:\n             return 0\n         read_manifest, _ = self._paths.conan_manifests(conan_reference)\n-        try:  # get_conan_digest can fail, not in server\n-            upstream_manifest = self.get_conan_digest(conan_reference)\n-            if upstream_manifest.file_sums != read_manifest.file_sums:\n-                return 1 if upstream_manifest.time > read_manifest.time else -1\n-        except ConanException:\n-            pass\n+        if read_manifest:\n+            try:  # get_conan_digest can fail, not in server\n+                upstream_manifest = self.get_conan_digest(conan_reference)\n+                if upstream_manifest.file_sums != read_manifest.file_sums:\n+                    return 1 if upstream_manifest.time > read_manifest.time else -1\n+            except ConanException:\n+                pass\n \n         return 0\n", "issue": "conan install -u traceback\nWhen running `conan install -u` while no previous `conan install` was run (so e.g. no conan conanbuildinfo.cmake exists) I get the following traceback:\n\n![image](https://cloud.githubusercontent.com/assets/11735226/17244088/59567f7c-5580-11e6-8a6f-4b026be70816.png)\n\n", "before_files": [{"content": "from conans.client.output import ScopedOutput\nfrom conans.util.files import path_exists, rmdir\nfrom conans.model.ref import PackageReference\nfrom conans.errors import (ConanException, ConanConnectionError, ConanOutdatedClient,\n                           NotFoundException)\nfrom conans.client.remote_registry import RemoteRegistry\nfrom conans.util.log import logger\nimport os\n\n\nclass ConanProxy(object):\n    \"\"\" Class to access the conan storage, to perform typical tasks as to get packages,\n    getting conanfiles, uploading, removing from remote, etc.\n    It uses the RemoteRegistry to control where the packages come from.\n    \"\"\"\n    def __init__(self, paths, user_io, remote_manager, remote_name,\n                 update=False, check_updates=False, check_integrity=False):\n        self._paths = paths\n        self._out = user_io.out\n        self._remote_manager = remote_manager\n        self._registry = RemoteRegistry(self._paths.registry, self._out)\n        self._remote_name = remote_name\n        self._update = update\n        self._check_updates = check_updates or update  # Update forces check\n        self._check_integrity = check_integrity\n\n    @property\n    def registry(self):\n        return self._registry\n\n    def get_package(self, package_reference, force_build):\n        \"\"\" obtain a package, either from disk or retrieve from remotes if necessary\n        and not necessary to build\n        \"\"\"\n        output = ScopedOutput(str(package_reference.conan), self._out)\n        package_folder = self._paths.package(package_reference)\n\n        # Check current package status\n        if path_exists(package_folder, self._paths.store):\n            if self._check_integrity or self._check_updates:\n                read_manifest, expected_manifest = self._paths.package_manifests(package_reference)\n\n            if self._check_integrity:  # Check if package is corrupted\n                if read_manifest.file_sums != expected_manifest.file_sums:\n                    # If not valid package, ensure empty folder\n                    output.warn(\"Bad package '%s' detected! Removing \"\n                                \"package directory... \" % str(package_reference.package_id))\n                    rmdir(package_folder)\n\n            if self._check_updates:\n                try:  # get_conan_digest can fail, not in server\n                    upstream_manifest = self.get_package_digest(package_reference)\n                    if upstream_manifest.file_sums != read_manifest.file_sums:\n                        if upstream_manifest.time > read_manifest.time:\n                            output.warn(\"Current package is older than remote upstream one\")\n                            if self._update:\n                                output.warn(\"Removing it to retrieve or build an updated one\")\n                                rmdir(package_folder)\n                        else:\n                            output.warn(\"Current package is newer than remote upstream one\")\n                except ConanException:\n                    pass\n\n        if not force_build:\n            local_package = os.path.exists(package_folder)\n            if local_package:\n                output = ScopedOutput(str(package_reference.conan), self._out)\n                output.info('Already installed!')\n                return True\n            return self._retrieve_remote_package(package_reference, output)\n\n        return False\n\n    def get_conanfile(self, conan_reference):\n        output = ScopedOutput(str(conan_reference), self._out)\n\n        def _refresh():\n            conan_dir_path = self._paths.export(conan_reference)\n            rmdir(conan_dir_path)\n            rmdir(self._paths.source(conan_reference))\n            current_remote, _ = self._get_remote(conan_reference)\n            output.info(\"Retrieving from remote '%s'...\" % current_remote.name)\n            self._remote_manager.get_conanfile(conan_reference, current_remote)\n            if self._update:\n                output.info(\"Updated!\")\n            else:\n                output.info(\"Installed!\")\n\n        # check if it is in disk\n        conanfile_path = self._paths.conanfile(conan_reference)\n        is_min_path = conan_reference in self._paths.short_path_refs\n        if not is_min_path:\n            path_exist = path_exists(conanfile_path, self._paths.store)\n        else:  # Directory doesn't contain the reference, so we don't need to compare the cases\n            path_exist = os.path.exists(conanfile_path)\n\n        if path_exist:\n            if self._check_integrity:  # Check if package is corrupted\n                read_manifest, expected_manifest = self._paths.conan_manifests(conan_reference)\n                if read_manifest.file_sums != expected_manifest.file_sums:\n                    output.warn(\"Bad conanfile detected! Removing export directory... \")\n                    _refresh()\n            else:  # Check for updates\n                if self._check_updates:\n                    ret = self.update_available(conan_reference)\n                    if ret != 0:  # Found and not equal\n                        remote, ref_remote = self._get_remote(conan_reference)\n                        if ret == 1:\n                            if not self._update:\n                                if remote != ref_remote:  # Forced new remote\n                                    output.warn(\"There is a new conanfile in '%s' remote. \"\n                                                \"Execute 'install -u -r %s' to update it.\"\n                                                % (remote.name, remote.name))\n                                else:\n                                    output.warn(\"There is a new conanfile in '%s' remote. \"\n                                                \"Execute 'install -u' to update it.\"\n                                                % remote.name)\n                                output.warn(\"Refused to install!\")\n                            else:\n                                if remote != ref_remote:\n                                    # Delete packages, could be non coherent with new remote\n                                    rmdir(self._paths.packages(conan_reference))\n                                _refresh()\n                        elif ret == -1:\n                            if not self._update:\n                                output.info(\"Current conanfile is newer \"\n                                            \"than %s's one\" % remote.name)\n                            else:\n                                output.error(\"Current conanfile is newer than %s's one. \"\n                                             \"Run 'conan remove %s' and run install again \"\n                                             \"to replace it.\" % (remote.name, conan_reference))\n\n        else:\n            self._retrieve_conanfile(conan_reference, output)\n        return conanfile_path\n\n    def update_available(self, conan_reference):\n        \"\"\"Returns 0 if the conanfiles are equal, 1 if there is an update and -1 if\n        the local is newer than the remote\"\"\"\n        if not conan_reference:\n            return 0\n        read_manifest, _ = self._paths.conan_manifests(conan_reference)\n        try:  # get_conan_digest can fail, not in server\n            upstream_manifest = self.get_conan_digest(conan_reference)\n            if upstream_manifest.file_sums != read_manifest.file_sums:\n                return 1 if upstream_manifest.time > read_manifest.time else -1\n        except ConanException:\n            pass\n\n        return 0\n\n    def _retrieve_conanfile(self, conan_reference, output):\n        \"\"\" returns the requested conanfile object, retrieving it from\n        remotes if necessary. Can raise NotFoundException\n        \"\"\"\n        def _retrieve_from_remote(remote):\n            output.info(\"Trying with '%s'...\" % remote.name)\n            result = self._remote_manager.get_conanfile(conan_reference, remote)\n            self._registry.set_ref(conan_reference, remote)\n            return result\n\n        if self._remote_name:\n            output.info(\"Not found, retrieving from server '%s' \" % self._remote_name)\n            remote = self._registry.remote(self._remote_name)\n            return _retrieve_from_remote(remote)\n        else:\n            output.info(\"Not found, looking in remotes...\")\n\n        remotes = self._registry.remotes\n        for remote in remotes:\n            logger.debug(\"Trying with remote %s\" % remote.name)\n            try:\n                return _retrieve_from_remote(remote)\n            # If exception continue with the next\n            except (ConanOutdatedClient, ConanConnectionError) as exc:\n                output.warn(str(exc))\n                if remote == remotes[-1]:  # Last element not found\n                    raise ConanConnectionError(\"All remotes failed\")\n            except NotFoundException as exc:\n                if remote == remotes[-1]:  # Last element not found\n                    logger.debug(\"Not found in any remote, raising...%s\" % exc)\n                    raise NotFoundException(\"Unable to find '%s' in remotes\"\n                                            % str(conan_reference))\n\n        raise ConanException(\"No remote defined\")\n\n    def upload_conan(self, conan_reference):\n        \"\"\" upload to defined remote in (-r=remote), to current remote\n        or to default remote, in that order.\n        If the remote is not set, set it\n        \"\"\"\n        remote, ref_remote = self._get_remote(conan_reference)\n\n        result = self._remote_manager.upload_conan(conan_reference, remote)\n        if not ref_remote:\n            self._registry.set_ref(conan_reference, remote)\n        return result\n\n    def _get_remote(self, conan_ref=None):\n        # Prioritize -r , then reference registry and then the default remote\n        ref_remote = self._registry.get_ref(conan_ref) if conan_ref else None\n        if self._remote_name:\n            remote = self._registry.remote(self._remote_name)\n        else:\n            if ref_remote:\n                remote = ref_remote\n            else:\n                remote = self._registry.default_remote\n        return remote, ref_remote\n\n    def upload_package(self, package_reference):\n        remote, current_remote = self._get_remote(package_reference.conan)\n\n        if not current_remote:\n            self._out.warn(\"Remote for '%s' not defined, uploading to %s\"\n                           % (str(package_reference.conan), remote.name))\n        result = self._remote_manager.upload_package(package_reference, remote)\n        if not current_remote:\n            self._registry.set_ref(package_reference.conan, remote)\n        return result\n\n    def get_conan_digest(self, conan_ref):\n        \"\"\" used by update to check the date of packages, require force if older\n        \"\"\"\n        remote, current_remote = self._get_remote(conan_ref)\n        result = self._remote_manager.get_conan_digest(conan_ref, remote)\n        if not current_remote:\n            self._registry.set_ref(conan_ref, remote)\n        return result\n\n    def get_package_digest(self, package_reference):\n        \"\"\" used by update to check the date of packages, require force if older\n        \"\"\"\n        remote, ref_remote = self._get_remote(package_reference.conan)\n        result = self._remote_manager.get_package_digest(package_reference, remote)\n        if not ref_remote:\n            self._registry.set_ref(package_reference.conan, remote)\n        return result\n\n    def search(self, pattern=None, ignorecase=True):\n        remote, _ = self._get_remote()\n        return self._remote_manager.search(remote, pattern, ignorecase)\n\n    def remove(self, conan_ref):\n        if not self._remote_name:\n            raise ConanException(\"Cannot remove, remote not defined\")\n        remote = self._registry.remote(self._remote_name)\n        result = self._remote_manager.remove(conan_ref, remote)\n        current_remote = self._registry.get_ref(conan_ref)\n        if current_remote == remote:\n            self._registry.remove_ref(conan_ref)\n        return result\n\n    def remove_packages(self, conan_ref, remove_ids):\n        if not self._remote_name:\n            raise ConanException(\"Cannot remove, remote not defined\")\n        remote = self._registry.remote(self._remote_name)\n        return self._remote_manager.remove_packages(conan_ref, remove_ids, remote)\n\n    def download_packages(self, reference, package_ids):\n        assert(isinstance(package_ids, list))\n        remote, _ = self._get_remote(reference)\n        self._remote_manager.get_conanfile(reference, remote)\n        self._registry.set_ref(reference, remote)\n        output = ScopedOutput(str(reference), self._out)\n        for package_id in package_ids:\n            package_reference = PackageReference(reference, package_id)\n            self._retrieve_remote_package(package_reference, output, remote)\n\n    def _retrieve_remote_package(self, package_reference, output, remote=None):\n\n        if remote is None:\n            remote = self._registry.get_ref(package_reference.conan)\n        if not remote:\n            output.warn(\"Package doesn't have a remote defined. \"\n                        \"Probably created locally and not uploaded\")\n            return False\n        package_id = str(package_reference.package_id)\n        try:\n            output.info(\"Looking for package %s in remote '%s' \" % (package_id, remote.name))\n            # Will raise if not found NotFoundException\n            self._remote_manager.get_package(package_reference, remote)\n            output.success('Package installed %s' % package_id)\n            return True\n        except ConanConnectionError:\n            raise  # This shouldn't be skipped\n        except ConanException as e:\n            output.warn('Binary for %s not in remote: %s' % (package_id, str(e)))\n            return False\n\n    def authenticate(self, name, password):\n        if not name:  # List all users, from all remotes\n            remotes = self._registry.remotes\n            if not remotes:\n                self._out.error(\"No remotes defined\")\n            for remote in remotes:\n                self._remote_manager.authenticate(remote, None, None)\n            return\n        remote, _ = self._get_remote()\n        return self._remote_manager.authenticate(remote, name, password)\n", "path": "conans/client/proxy.py"}], "after_files": [{"content": "from conans.client.output import ScopedOutput\nfrom conans.util.files import path_exists, rmdir\nfrom conans.model.ref import PackageReference\nfrom conans.errors import (ConanException, ConanConnectionError, ConanOutdatedClient,\n                           NotFoundException)\nfrom conans.client.remote_registry import RemoteRegistry\nfrom conans.util.log import logger\nimport os\n\n\nclass ConanProxy(object):\n    \"\"\" Class to access the conan storage, to perform typical tasks as to get packages,\n    getting conanfiles, uploading, removing from remote, etc.\n    It uses the RemoteRegistry to control where the packages come from.\n    \"\"\"\n    def __init__(self, paths, user_io, remote_manager, remote_name,\n                 update=False, check_updates=False, check_integrity=False):\n        self._paths = paths\n        self._out = user_io.out\n        self._remote_manager = remote_manager\n        self._registry = RemoteRegistry(self._paths.registry, self._out)\n        self._remote_name = remote_name\n        self._update = update\n        self._check_updates = check_updates or update  # Update forces check\n        self._check_integrity = check_integrity\n\n    @property\n    def registry(self):\n        return self._registry\n\n    def get_package(self, package_reference, force_build):\n        \"\"\" obtain a package, either from disk or retrieve from remotes if necessary\n        and not necessary to build\n        \"\"\"\n        output = ScopedOutput(str(package_reference.conan), self._out)\n        package_folder = self._paths.package(package_reference)\n\n        # Check current package status\n        if path_exists(package_folder, self._paths.store):\n            if self._check_integrity or self._check_updates:\n                read_manifest, expected_manifest = self._paths.package_manifests(package_reference)\n\n            if self._check_integrity:  # Check if package is corrupted\n                if read_manifest.file_sums != expected_manifest.file_sums:\n                    # If not valid package, ensure empty folder\n                    output.warn(\"Bad package '%s' detected! Removing \"\n                                \"package directory... \" % str(package_reference.package_id))\n                    rmdir(package_folder)\n\n            if self._check_updates:\n                try:  # get_conan_digest can fail, not in server\n                    upstream_manifest = self.get_package_digest(package_reference)\n                    if upstream_manifest.file_sums != read_manifest.file_sums:\n                        if upstream_manifest.time > read_manifest.time:\n                            output.warn(\"Current package is older than remote upstream one\")\n                            if self._update:\n                                output.warn(\"Removing it to retrieve or build an updated one\")\n                                rmdir(package_folder)\n                        else:\n                            output.warn(\"Current package is newer than remote upstream one\")\n                except ConanException:\n                    pass\n\n        if not force_build:\n            local_package = os.path.exists(package_folder)\n            if local_package:\n                output = ScopedOutput(str(package_reference.conan), self._out)\n                output.info('Already installed!')\n                return True\n            return self._retrieve_remote_package(package_reference, output)\n\n        return False\n\n    def get_conanfile(self, conan_reference):\n        output = ScopedOutput(str(conan_reference), self._out)\n\n        def _refresh():\n            conan_dir_path = self._paths.export(conan_reference)\n            rmdir(conan_dir_path)\n            rmdir(self._paths.source(conan_reference))\n            current_remote, _ = self._get_remote(conan_reference)\n            output.info(\"Retrieving from remote '%s'...\" % current_remote.name)\n            self._remote_manager.get_conanfile(conan_reference, current_remote)\n            if self._update:\n                output.info(\"Updated!\")\n            else:\n                output.info(\"Installed!\")\n\n        # check if it is in disk\n        conanfile_path = self._paths.conanfile(conan_reference)\n        is_min_path = conan_reference in self._paths.short_path_refs\n        if not is_min_path:\n            path_exist = path_exists(conanfile_path, self._paths.store)\n        else:  # Directory doesn't contain the reference, so we don't need to compare the cases\n            path_exist = os.path.exists(conanfile_path)\n\n        if path_exist:\n            if self._check_integrity:  # Check if package is corrupted\n                read_manifest, expected_manifest = self._paths.conan_manifests(conan_reference)\n                if read_manifest.file_sums != expected_manifest.file_sums:\n                    output.warn(\"Bad conanfile detected! Removing export directory... \")\n                    _refresh()\n            else:  # Check for updates\n                if self._check_updates:\n                    ret = self.update_available(conan_reference)\n                    if ret != 0:  # Found and not equal\n                        remote, ref_remote = self._get_remote(conan_reference)\n                        if ret == 1:\n                            if not self._update:\n                                if remote != ref_remote:  # Forced new remote\n                                    output.warn(\"There is a new conanfile in '%s' remote. \"\n                                                \"Execute 'install -u -r %s' to update it.\"\n                                                % (remote.name, remote.name))\n                                else:\n                                    output.warn(\"There is a new conanfile in '%s' remote. \"\n                                                \"Execute 'install -u' to update it.\"\n                                                % remote.name)\n                                output.warn(\"Refused to install!\")\n                            else:\n                                if remote != ref_remote:\n                                    # Delete packages, could be non coherent with new remote\n                                    rmdir(self._paths.packages(conan_reference))\n                                _refresh()\n                        elif ret == -1:\n                            if not self._update:\n                                output.info(\"Current conanfile is newer \"\n                                            \"than %s's one\" % remote.name)\n                            else:\n                                output.error(\"Current conanfile is newer than %s's one. \"\n                                             \"Run 'conan remove %s' and run install again \"\n                                             \"to replace it.\" % (remote.name, conan_reference))\n\n        else:\n            self._retrieve_conanfile(conan_reference, output)\n        return conanfile_path\n\n    def update_available(self, conan_reference):\n        \"\"\"Returns 0 if the conanfiles are equal, 1 if there is an update and -1 if\n        the local is newer than the remote\"\"\"\n        if not conan_reference:\n            return 0\n        read_manifest, _ = self._paths.conan_manifests(conan_reference)\n        if read_manifest:\n            try:  # get_conan_digest can fail, not in server\n                upstream_manifest = self.get_conan_digest(conan_reference)\n                if upstream_manifest.file_sums != read_manifest.file_sums:\n                    return 1 if upstream_manifest.time > read_manifest.time else -1\n            except ConanException:\n                pass\n\n        return 0\n\n    def _retrieve_conanfile(self, conan_reference, output):\n        \"\"\" returns the requested conanfile object, retrieving it from\n        remotes if necessary. Can raise NotFoundException\n        \"\"\"\n        def _retrieve_from_remote(remote):\n            output.info(\"Trying with '%s'...\" % remote.name)\n            result = self._remote_manager.get_conanfile(conan_reference, remote)\n            self._registry.set_ref(conan_reference, remote)\n            return result\n\n        if self._remote_name:\n            output.info(\"Not found, retrieving from server '%s' \" % self._remote_name)\n            remote = self._registry.remote(self._remote_name)\n            return _retrieve_from_remote(remote)\n        else:\n            output.info(\"Not found, looking in remotes...\")\n\n        remotes = self._registry.remotes\n        for remote in remotes:\n            logger.debug(\"Trying with remote %s\" % remote.name)\n            try:\n                return _retrieve_from_remote(remote)\n            # If exception continue with the next\n            except (ConanOutdatedClient, ConanConnectionError) as exc:\n                output.warn(str(exc))\n                if remote == remotes[-1]:  # Last element not found\n                    raise ConanConnectionError(\"All remotes failed\")\n            except NotFoundException as exc:\n                if remote == remotes[-1]:  # Last element not found\n                    logger.debug(\"Not found in any remote, raising...%s\" % exc)\n                    raise NotFoundException(\"Unable to find '%s' in remotes\"\n                                            % str(conan_reference))\n\n        raise ConanException(\"No remote defined\")\n\n    def upload_conan(self, conan_reference):\n        \"\"\" upload to defined remote in (-r=remote), to current remote\n        or to default remote, in that order.\n        If the remote is not set, set it\n        \"\"\"\n        remote, ref_remote = self._get_remote(conan_reference)\n\n        result = self._remote_manager.upload_conan(conan_reference, remote)\n        if not ref_remote:\n            self._registry.set_ref(conan_reference, remote)\n        return result\n\n    def _get_remote(self, conan_ref=None):\n        # Prioritize -r , then reference registry and then the default remote\n        ref_remote = self._registry.get_ref(conan_ref) if conan_ref else None\n        if self._remote_name:\n            remote = self._registry.remote(self._remote_name)\n        else:\n            if ref_remote:\n                remote = ref_remote\n            else:\n                remote = self._registry.default_remote\n        return remote, ref_remote\n\n    def upload_package(self, package_reference):\n        remote, current_remote = self._get_remote(package_reference.conan)\n\n        if not current_remote:\n            self._out.warn(\"Remote for '%s' not defined, uploading to %s\"\n                           % (str(package_reference.conan), remote.name))\n        result = self._remote_manager.upload_package(package_reference, remote)\n        if not current_remote:\n            self._registry.set_ref(package_reference.conan, remote)\n        return result\n\n    def get_conan_digest(self, conan_ref):\n        \"\"\" used by update to check the date of packages, require force if older\n        \"\"\"\n        remote, current_remote = self._get_remote(conan_ref)\n        result = self._remote_manager.get_conan_digest(conan_ref, remote)\n        if not current_remote:\n            self._registry.set_ref(conan_ref, remote)\n        return result\n\n    def get_package_digest(self, package_reference):\n        \"\"\" used by update to check the date of packages, require force if older\n        \"\"\"\n        remote, ref_remote = self._get_remote(package_reference.conan)\n        result = self._remote_manager.get_package_digest(package_reference, remote)\n        if not ref_remote:\n            self._registry.set_ref(package_reference.conan, remote)\n        return result\n\n    def search(self, pattern=None, ignorecase=True):\n        remote, _ = self._get_remote()\n        return self._remote_manager.search(remote, pattern, ignorecase)\n\n    def remove(self, conan_ref):\n        if not self._remote_name:\n            raise ConanException(\"Cannot remove, remote not defined\")\n        remote = self._registry.remote(self._remote_name)\n        result = self._remote_manager.remove(conan_ref, remote)\n        current_remote = self._registry.get_ref(conan_ref)\n        if current_remote == remote:\n            self._registry.remove_ref(conan_ref)\n        return result\n\n    def remove_packages(self, conan_ref, remove_ids):\n        if not self._remote_name:\n            raise ConanException(\"Cannot remove, remote not defined\")\n        remote = self._registry.remote(self._remote_name)\n        return self._remote_manager.remove_packages(conan_ref, remove_ids, remote)\n\n    def download_packages(self, reference, package_ids):\n        assert(isinstance(package_ids, list))\n        remote, _ = self._get_remote(reference)\n        self._remote_manager.get_conanfile(reference, remote)\n        self._registry.set_ref(reference, remote)\n        output = ScopedOutput(str(reference), self._out)\n        for package_id in package_ids:\n            package_reference = PackageReference(reference, package_id)\n            self._retrieve_remote_package(package_reference, output, remote)\n\n    def _retrieve_remote_package(self, package_reference, output, remote=None):\n\n        if remote is None:\n            remote = self._registry.get_ref(package_reference.conan)\n        if not remote:\n            output.warn(\"Package doesn't have a remote defined. \"\n                        \"Probably created locally and not uploaded\")\n            return False\n        package_id = str(package_reference.package_id)\n        try:\n            output.info(\"Looking for package %s in remote '%s' \" % (package_id, remote.name))\n            # Will raise if not found NotFoundException\n            self._remote_manager.get_package(package_reference, remote)\n            output.success('Package installed %s' % package_id)\n            return True\n        except ConanConnectionError:\n            raise  # This shouldn't be skipped\n        except ConanException as e:\n            output.warn('Binary for %s not in remote: %s' % (package_id, str(e)))\n            return False\n\n    def authenticate(self, name, password):\n        if not name:  # List all users, from all remotes\n            remotes = self._registry.remotes\n            if not remotes:\n                self._out.error(\"No remotes defined\")\n            for remote in remotes:\n                self._remote_manager.authenticate(remote, None, None)\n            return\n        remote, _ = self._get_remote()\n        return self._remote_manager.authenticate(remote, name, password)\n", "path": "conans/client/proxy.py"}]}