Exemplo n.º 1
0
    def _run_virtualenv(self, generator):
        generator.output_path = self.test_folder
        save_files(self.test_folder, generator.content)

        # Generate the list of commands to execute
        shell_commands = [
            self.commands.dump_env.format(filename=self.env_before),
            self.commands.find_program.format(program="conan",
                                              variable="__conan_pre_path__"),
            self.commands.find_program.format(program=self.app,
                                              variable="__exec_pre_path__"),
            self.commands.activate,
            self.commands.dump_env.format(filename=self.env_activated),
            self.commands.find_program.format(program="conan",
                                              variable="__conan_env_path__"),
            self.commands.find_program.format(program=self.app,
                                              variable="__exec_env_path__"),
            self.commands.deactivate,
            self.commands.dump_env.format(filename=self.env_after),
            self.commands.find_program.format(program="conan",
                                              variable="__conan_post_path__"),
            self.commands.find_program.format(program=self.app,
                                              variable="__exec_post_path__"),
            "",
        ]

        # Execute
        with environment_append({"PATH": [
                self.ori_path,
        ]}):
            shell = subprocess.Popen(self.commands.shell,
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE,
                                     cwd=self.test_folder)
            (stdout, stderr) = shell.communicate(
                to_file_bytes("\n".join(shell_commands)))
        stdout, stderr = decode_text(stdout), decode_text(stderr)

        # Consistency checks
        self.assertFalse(
            stderr, "Running shell resulted in error, output:\n%s" % stdout)

        env_before = _load_env_file(
            os.path.join(self.test_folder, self.env_before))
        env_after = _load_env_file(
            os.path.join(self.test_folder, self.env_after))
        there_was_ps1 = os.getenv("PS1")
        # FIXME: Not the best behavior
        # The deactivate sets PS1 always, but sometimes it didn't exist previously
        if platform.system() == "Darwin" or not there_was_ps1:
            env_after.pop(six.u("PS1"),
                          None)  # TODO: FIXME: Needed for the test to pass
            env_after.pop("PS1",
                          None)  # TODO: FIXME: Needed for the test to pass

        self.assertDictEqual(env_before,
                             env_after)  # Environment restored correctly
        return stdout, _load_env_file(
            os.path.join(self.test_folder, self.env_activated))
Exemplo n.º 2
0
    def test_scm_bad_filename(self):
        # Fixes: #3500
        badfilename = "\xE3\x81\x82badfile.txt"
        path, _ = create_local_git_repo({"goodfile.txt": "good contents"},
                                        branch="my_release")
        save(
            to_file_bytes(os.path.join(self.client.current_folder,
                                       badfilename)), "contents")
        self.client.run_command('git remote add origin "%s"' %
                                path.replace("\\", "/"),
                                cwd=path)

        conanfile = '''
import os
from conans import ConanFile, tools

class ConanLib(ConanFile):
    name = "lib"
    version = "0.1"
    scm = {
        "type": "git",
        "url": "auto",
        "revision": "auto"
    }

    def build(self):
        pass
'''
        self.client.current_folder = path
        self.client.save({"conanfile.py": conanfile})
        self.client.run("create . user/channel")
Exemplo n.º 3
0
    def excluded_files(self):
        ret = []
        try:

            file_paths = [
                os.path.normpath(
                    os.path.join(os.path.relpath(folder, self.folder),
                                 el)).replace("\\", "/")
                for folder, dirpaths, fs in walk(self.folder)
                for el in fs + dirpaths
            ]
            if file_paths:
                p = subprocess.Popen(['git', 'check-ignore', '--stdin'],
                                     stdout=PIPE,
                                     stdin=PIPE,
                                     stderr=STDOUT,
                                     cwd=self.folder)
                paths = to_file_bytes("\n".join(file_paths))
                grep_stdout = decode_text(p.communicate(input=paths)[0])
                ret = grep_stdout.splitlines()
        except (CalledProcessError, FileNotFoundError) as e:
            if self._output:
                self._output.warn("Error checking excluded git files: %s. "
                                  "Ignoring excluded files" % e)
            ret = []
        return ret
Exemplo n.º 4
0
    def _run_virtualenv(self, generator):
        with environment_append({"PATH": [
                self.ori_path,
        ]}):
            # FIXME: I need this context because restore values for the 'deactivate' script are
            #        generated at the 'generator.content' and not when the 'activate' is called.
            save_files(self.test_folder, generator.content)

            # Generate the list of commands to execute
            shell_commands = [
                self.commands.dump_env.format(filename=self.env_before),
                self.commands.find_program.format(
                    program="conan", variable="__conan_pre_path__"),
                self.commands.find_program.format(
                    program=self.app, variable="__exec_pre_path__"),
                self.commands.activate,
                self.commands.dump_env.format(filename=self.env_activated),
                self.commands.find_program.format(
                    program="conan", variable="__conan_env_path__"),
                self.commands.find_program.format(
                    program=self.app, variable="__exec_env_path__"),
                self.commands.deactivate,
                self.commands.dump_env.format(filename=self.env_after),
                self.commands.find_program.format(
                    program="conan", variable="__conan_post_path__"),
                self.commands.find_program.format(
                    program=self.app, variable="__exec_post_path__"),
                "",
            ]

            # Execute
            shell = subprocess.Popen(self.commands.shell,
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE,
                                     cwd=self.test_folder)
            (stdout, stderr) = shell.communicate(
                to_file_bytes("\n".join(shell_commands)))
            stdout, stderr = decode_text(stdout), decode_text(stderr)

        # Consistency checks
        self.assertFalse(
            stderr, "Running shell resulted in error, output:\n%s" % stdout)

        env_before = _load_env_file(
            os.path.join(self.test_folder, self.env_before))
        env_after = _load_env_file(
            os.path.join(self.test_folder, self.env_after))
        if platform.system() == "Darwin":
            env_after.pop(six.u("PS1"),
                          None)  # TODO: FIXME: Needed for the test to pass
            env_after.pop("PS1",
                          None)  # TODO: FIXME: Needed for the test to pass
        self.assertDictEqual(env_before,
                             env_after)  # Environment restored correctly

        return stdout, _load_env_file(
            os.path.join(self.test_folder, self.env_activated))
Exemplo n.º 5
0
 def walk_encoding_test(self):
     badfilename = "\xE3\x81\x82badfile.txt"
     folder = temp_folder()
     filepath = os.path.join(folder, badfilename)
     save(to_file_bytes(filepath), "contents")
     if six.PY2:
         folder = unicode(folder)
     a_file = [f[0] for _, _, f in walk(folder)][0]
     self.assertTrue(a_file.endswith("badfile.txt"))
Exemplo n.º 6
0
 def excluded_files(self):
     try:
         file_paths = [os.path.normpath(os.path.join(os.path.relpath(folder, self.folder), f))
                       for folder, _, fs in os.walk(self.folder) for f in fs]
         p = subprocess.Popen(['git', 'check-ignore', '--stdin'],
                              stdout=PIPE, stdin=PIPE, stderr=STDOUT, cwd=self.folder)
         paths = to_file_bytes('\n'.join(file_paths))
         grep_stdout = decode_text(p.communicate(input=paths)[0])
         tmp = grep_stdout.splitlines()
     except CalledProcessError:
         tmp = []
     tmp.append(".git")
     return tmp
Exemplo n.º 7
0
    def excluded_files(self):
        try:

            file_paths = [os.path.normpath(os.path.join(os.path.relpath(folder, self.folder), el)).replace("\\", "/")
                          for folder, dirpaths, fs in os.walk(self.folder)
                          for el in fs + dirpaths]
            p = subprocess.Popen(['git', 'check-ignore', '--stdin'],
                                 stdout=PIPE, stdin=PIPE, stderr=STDOUT, cwd=self.folder)
            paths = to_file_bytes("\n".join(file_paths))
            grep_stdout = decode_text(p.communicate(input=paths)[0])
            tmp = grep_stdout.splitlines()
        except CalledProcessError:
            tmp = []
        return tmp
Exemplo n.º 8
0
def save(path, content, append=False):
    """
    This method is duplicated to the conans.util.files, because the
    intent is to modify the internally used save() methods for unicode
    encodings
    """
    try:
        os.makedirs(os.path.dirname(path))
    except:
        pass

    mode = "ab" if append else "wb"
    with open(path, mode) as handle:
        handle.write(to_file_bytes(content))
Exemplo n.º 9
0
 def write_chunks(chunks, path):
     ret = None
     downloaded_size = 0
     if path:
         mkdir(os.path.dirname(path))
         with open(path, 'wb') as file_handler:
             for chunk in chunks:
                 file_handler.write(to_file_bytes(chunk))
                 downloaded_size += len(chunk)
     else:
         ret_data = bytearray()
         for chunk in chunks:
             ret_data.extend(chunk)
             downloaded_size += len(chunk)
         ret = bytes(ret_data)
     return ret, downloaded_size
Exemplo n.º 10
0
 def download_chunks(file_handler=None, ret_buffer=None):
     """Write to a buffer or to a file handler"""
     chunk_size = 1024 if not file_path else 1024 * 100
     download_size = 0
     last_progress = None
     for data in response.iter_content(chunk_size):
         download_size += len(data)
         if ret_buffer is not None:
             ret_buffer.extend(data)
         if file_handler is not None:
             file_handler.write(to_file_bytes(data))
         if self.output:
             units = progress_units(download_size, total_length)
             progress = human_readable_progress(download_size, total_length)
             if last_progress != units:  # Avoid screen refresh if nothing has change
                 print_progress(self.output, units, progress)
                 last_progress = units
     return download_size
Exemplo n.º 11
0
 def excluded_files(self):
     ret = []
     try:
         file_paths = [os.path.normpath(
                             os.path.join(
                                 os.path.relpath(folder, self.folder), el)).replace("\\", "/")
                       for folder, dirpaths, fs in walk(self.folder)
                       for el in fs + dirpaths]
         if file_paths:
             paths = to_file_bytes("\n".join(file_paths))
             out = input_runner(['git', 'check-ignore', '--stdin'], paths, self.folder)
             grep_stdout = decode_text(out)
             ret = grep_stdout.splitlines()
     except (CalledProcessError, IOError, OSError) as e:
         if self._output:
             self._output.warn("Error checking excluded git files: %s. "
                               "Ignoring excluded files" % e)
         ret = []
     return ret
Exemplo n.º 12
0
def save(conanfile, path, content, append=False):
    if append:
        mode = "ab"
        try:
            os.makedirs(os.path.dirname(path))
        except Exception:
            pass
    else:
        mode = "wb"
        dir_path = os.path.dirname(path)
        if not os.path.isdir(dir_path):
            try:
                os.makedirs(dir_path)
            except OSError as error:
                if error.errno not in (errno.EEXIST, errno.ENOENT):
                    raise OSError(
                        "The folder {} does not exist and could not be created ({})."
                        .format(dir_path, error.strerror))
            except Exception:
                raise

    with open(path, mode) as handle:
        handle.write(to_file_bytes(content, encoding="utf-8"))
Exemplo n.º 13
0
            def download_chunks(file_handler=None, ret_buffer=None):
                """Write to a buffer or to a file handler"""
                chunk_size = 1024 if not file_path else 1024 * 100
                download_size = 0
                last_time = 0
                if progress_bar is not None:
                    progress_bar.desc = "Downloading {}".format(
                        os.path.basename(file_path))

                for data in response.iter_content(chunk_size):
                    download_size += len(data)
                    if ret_buffer is not None:
                        ret_buffer.extend(data)
                    if file_handler is not None:
                        file_handler.write(to_file_bytes(data))
                    if progress_bar is not None:
                        progress_bar.update(len(data))
                    elif self.output and time.time(
                    ) - last_time > TIMEOUT_BEAT_SECONDS:
                        last_time = time.time()
                        self.output.write(TIMEOUT_BEAT_CHARACTER)

                return download_size