Beispiel #1
0
    def test_rename(self):
        client = TestClient()
        tmpdir = client.current_folder

        sub_space_dir = "sub dir"
        with chdir(tmpdir):
            os.makedirs(sub_space_dir)
            os.makedirs(os.path.join(sub_space_dir, "dir.pdb"))
            os.makedirs(os.path.join(sub_space_dir, "middir"))
            os.makedirs(os.path.join(sub_space_dir, "middir", "deepdir"))

        client.save({
            os.path.join(sub_space_dir, "1.txt"):
            "",
            os.path.join(sub_space_dir, "1.pdb"):
            "",
            os.path.join(sub_space_dir, "1.pdb1"):
            "",
            os.path.join(sub_space_dir, "dir.pdb", "2.txt"):
            "",
            os.path.join(sub_space_dir, "dir.pdb", "2.pdb"):
            "",
            os.path.join(sub_space_dir, "dir.pdb", "2.pdb1"):
            "",
            os.path.join(sub_space_dir, "middir", "3.txt"):
            "",
            os.path.join(sub_space_dir, "middir", "3.pdb"):
            "",
            os.path.join(sub_space_dir, "middir", "3.pdb1"):
            "",
            os.path.join(sub_space_dir, "middir", "deepdir", "4.txt"):
            "",
            os.path.join(sub_space_dir, "middir", "deepdir", "4.pdb"):
            "",
            os.path.join(sub_space_dir, "middir", "deepdir", "4.pdb1"):
            ""
        })
        self.verify_dir(os.path.join(tmpdir, sub_space_dir))

        with chdir(tmpdir):
            rename(sub_space_dir, "dst dir")
            self.verify_dir(os.path.join(tmpdir, "dst dir"))

            rename("dst dir", "subdir")
            self.verify_dir(os.path.join(tmpdir, "subdir"))

            rename(os.path.join("subdir", "1.txt"), "t.txt")
            self.assertTrue(os.path.isfile(os.path.join(tmpdir, "t.txt")))
            self.assertFalse(
                os.path.isfile(os.path.join(tmpdir, "subdir", "1.txt")))
Beispiel #2
0
 def create_project(self,
                    files,
                    rel_project_path=None,
                    commit_msg='default commit message',
                    delete_checkout=True):
     tmp_dir = self.gimme_tmp()
     try:
         rel_project_path = rel_project_path or str(uuid.uuid4())
         # Do not use SVN class as it is what we will be testing
         subprocess.check_output('svn co "{url}" "{path}"'.format(
             url=self.repo_url, path=tmp_dir),
                                 shell=True)
         tmp_project_dir = os.path.join(tmp_dir, rel_project_path)
         os.makedirs(tmp_project_dir)
         save_files(tmp_project_dir, files)
         with chdir(tmp_project_dir):
             subprocess.check_output("svn add .", shell=True)
             subprocess.check_output(
                 'svn commit -m "{}"'.format(commit_msg), shell=True)
             rev = subprocess.check_output("svn info --show-item revision",
                                           shell=True).decode().strip()
         project_url = self.repo_url + "/" + quote(
             rel_project_path.replace("\\", "/"))
         return project_url, rev
     finally:
         if delete_checkout:
             shutil.rmtree(tmp_dir,
                           ignore_errors=False,
                           onerror=handleRemoveReadonly)
Beispiel #3
0
    def test_link_folder(self):
        # If there is a linked folder in the current directory that matches one file in the tar.
        # https://github.com/conan-io/conan/issues/4959

        # Once unpackaged, this is the content of the destination directory
        def check_files(destination_dir):
            d = os.listdir(destination_dir)
            self.assertListEqual(d, ["folder", "file1"])
            d_folder = os.listdir(os.path.join(destination_dir, "folder"))
            self.assertEqual(d_folder, ["file2"])

        working_dir = temp_folder()
        with chdir(working_dir):
            # Unpack and check
            destination_dir = os.path.join(self.tmp_folder, "dest")
            with open(self.tgz_file, 'rb') as file_handler:
                tar_extract(file_handler, destination_dir)
            check_files(destination_dir)

            # Unpack and check (now we have a symlinked local folder)
            os.symlink(temp_folder(), "folder")
            destination_dir = os.path.join(self.tmp_folder, "dest2")
            with open(self.tgz_file, 'rb') as file_handler:
                tar_extract(file_handler, destination_dir)
            check_files(destination_dir)
    def configure(self, source_folder=None):
        # TODO: environment?
        if not self._conanfile.should_configure:
            return

        source = self._conanfile.source_folder
        if source_folder:
            source = os.path.join(self._conanfile.source_folder, source_folder)

        build_folder = self._conanfile.build_folder
        if self._build_folder:
            build_folder = os.path.join(self._conanfile.build_folder,
                                        self._build_folder)

        mkdir(build_folder)
        arg_list = '-DCMAKE_TOOLCHAIN_FILE="{}" -DCMAKE_INSTALL_PREFIX="{}" "{}"'.format(
            CMakeToolchainBase.filename,
            self._conanfile.package_folder.replace("\\", "/"), source)

        generator = '-G "{}" '.format(
            self._generator) if self._generator else ""
        command = "%s %s%s" % (self._cmake_program, generator, arg_list)

        is_windows_mingw = platform.system(
        ) == "Windows" and self._generator == "MinGW Makefiles"
        self._conanfile.output.info("CMake command: %s" % command)
        with chdir(build_folder):
            if is_windows_mingw:
                with tools.remove_from_path("sh"):
                    self._conanfile.run(command)
            else:
                self._conanfile.run(command)
Beispiel #5
0
    def test_skip_toolset(self):
        settings = MockSettings({"build_type": "Debug",
                                 "compiler": "Visual Studio",
                                 "compiler.version": "15",
                                 "arch": "x86_64"})

        class Runner(object):

            def __init__(self):
                self.commands = []

            def __call__(self, *args, **kwargs):
                self.commands.append(args[0])

        with chdir(tools.mkdir_tmp()):
            runner = Runner()
            conanfile = MockConanfile(settings, runner=runner)
            msbuild = MSBuild(conanfile)
            msbuild.build("myproject", toolset=False)
            self.assertEqual(len(runner.commands), 1)
            self.assertNotIn("PlatformToolset", runner.commands[0])

            runner = Runner()
            conanfile = MockConanfile(settings, runner=runner)
            msbuild = MSBuild(conanfile)
            msbuild.build("myproject", toolset="mytoolset")
            self.assertEqual(len(runner.commands), 1)
            self.assertIn('/p:PlatformToolset="mytoolset"', runner.commands[0])
    def configure(self, source_folder=None):
        # TODO: environment?
        if not self._conanfile.should_configure:
            return

        source = self._conanfile.source_folder
        if source_folder:
            source = os.path.join(self._conanfile.source_folder, source_folder)

        build_folder = self._conanfile.build_folder
        if self._build_folder:
            build_folder = os.path.join(self._conanfile.build_folder, self._build_folder)

        defs = {"CMAKE_TOOLCHAIN_FILE": CMakeToolchainBase.filename}

        mkdir(build_folder)
        arg_list = join_arguments([
            defs_to_string(defs),
            args_to_string([source])
        ])
        generator = '-G "{}" '.format(self._generator) if self._generator else ""
        command = "%s %s%s" % (self._cmake_program, generator, arg_list)

        is_windows_mingw = platform.system() == "Windows" and self._generator == "MinGW Makefiles"
        self._conanfile.output.info("CMake command: %s" % command)
        with chdir(build_folder):
            if is_windows_mingw:
                with tools.remove_from_path("sh"):
                    self._conanfile.run(command)
            else:
                self._conanfile.run(command)
Beispiel #7
0
    def configure(self, source_folder=None):
        # TODO: environment?
        if not self._conanfile.should_configure:
            return

        source = self._conanfile.source_folder
        if source_folder:
            source = os.path.join(self._conanfile.source_folder, source_folder)

        build_folder = self._conanfile.build_folder
        if self._build_folder:
            build_folder = os.path.join(self._conanfile.build_folder,
                                        self._build_folder)

        mkdir(build_folder)
        arg_list = '-DCMAKE_TOOLCHAIN_FILE="{}" -DCMAKE_INSTALL_PREFIX="{}" "{}"'.format(
            CMakeToolchain.filename,
            self._conanfile.package_folder.replace("\\", "/"), source)

        if platform.system(
        ) == "Windows" and self._generator == "MinGW Makefiles":
            arg_list += ' -DCMAKE_SH="CMAKE_SH-NOTFOUND"'

        generator = '-G "{}" '.format(
            self._generator) if self._generator else ""
        command = "%s %s%s" % (self._cmake_program, generator, arg_list)

        self._conanfile.output.info("CMake command: %s" % command)
        with chdir(build_folder):
            vcvars = os.path.join(self._conanfile.install_folder,
                                  "conanvcvars")
            self._conanfile.run(command, env=["conanbuildenv", vcvars])
Beispiel #8
0
    def test_plain_tgz_common_base(self):

        tmp_folder = temp_folder()
        with chdir(tmp_folder):
            # Create a couple of files
            ori_files_dir = os.path.join(tmp_folder, "subfolder-1.2.3")
            file1 = os.path.join(ori_files_dir, "folder", "file1")
            file2 = os.path.join(ori_files_dir, "folder", "file2")
            file3 = os.path.join(ori_files_dir, "folder", "file3")

            save(file1, "")
            save(file2, "")
            save(file3, "")

        tgz_folder = temp_folder()
        tgz_file = os.path.join(tgz_folder, "file.tar.gz")
        self._compress_folder(tmp_folder, tgz_file)

        # Tgz unzipped regularly
        extract_folder = temp_folder()
        untargz(tgz_file, destination=extract_folder, strip_root=True)
        self.assertFalse(
            os.path.exists(os.path.join(extract_folder, "subfolder-1.2.3")))
        self.assertTrue(
            os.path.exists(os.path.join(extract_folder, "folder", "file1")))
        self.assertTrue(
            os.path.exists(os.path.join(extract_folder, "folder", "file2")))
        self.assertTrue(
            os.path.exists(os.path.join(extract_folder, "folder", "file3")))
        def validate_additional_dependencies(libname, additional_dep):
            tempdir = temp_folder()
            with chdir(tempdir):
                conanfile = ConanFile(TestBufferConanOutput(), None)
                conanfile.initialize(Settings({}), EnvValues())

                ref = ConanFileReference.loads("MyPkg/0.1@user/testing")
                cpp_info = CppInfo(ref.name, "dummy_root_folder1")
                cpp_info.libs = [libname]
                conanfile.deps_cpp_info.add(ref.name, cpp_info)

                settings = Settings.loads(get_default_settings_yml())
                settings.os = "Windows"
                settings.arch = "x86_64"
                settings.build_type = "Release"
                settings.compiler = "Visual Studio"
                settings.compiler.version = "15"
                settings.compiler.runtime = "MD"
                settings.compiler.toolset = "v141"
                conanfile.settings = settings

                generator = VisualStudioMultiGenerator(conanfile)
                generator.output_path = ""
                content = generator.content

                self.assertIn('conanbuildinfo_release_x64_v141.props', content.keys())

                content_release = content['conanbuildinfo_release_x64_v141.props']
                self.assertIn("<ConanLibraries>%s;</ConanLibraries>" % additional_dep,
                              content_release)
                self.assertIn("<AdditionalDependencies>"
                              "$(ConanLibraries)%(AdditionalDependencies)"
                              "</AdditionalDependencies>", content_release)
Beispiel #10
0
def _run_muted(cmd, folder=None):
    with chdir(folder) if folder else no_op():
        process = subprocess.Popen(cmd,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        process.communicate()
        return process.returncode
Beispiel #11
0
def create_local_svn_checkout(files, repo_url, rel_project_path=None,
                              commit_msg='default commit message', delete_checkout=True,
                              folder=None):
    tmp_dir = folder or temp_folder()
    try:
        rel_project_path = rel_project_path or str(uuid.uuid4())
        # Do not use SVN class as it is what we will be testing
        subprocess.check_output('svn co "{url}" "{path}"'.format(url=repo_url,
                                                                 path=tmp_dir),
                                shell=True)
        tmp_project_dir = os.path.join(tmp_dir, rel_project_path)
        mkdir(tmp_project_dir)
        save_files(tmp_project_dir, files)
        with chdir(tmp_project_dir):
            subprocess.check_output("svn add .", shell=True)
            subprocess.check_output('svn commit -m "{}"'.format(commit_msg), shell=True)
            if SVN.get_version() >= SVN.API_CHANGE_VERSION:
                rev = check_output_runner("svn info --show-item revision").strip()
            else:
                import xml.etree.ElementTree as ET
                output = check_output_runner("svn info --xml").strip()
                root = ET.fromstring(output)
                rev = root.findall("./entry")[0].get("revision")
        project_url = repo_url + "/" + quote(rel_project_path.replace("\\", "/"))
        return project_url, rev
    finally:
        if delete_checkout:
            shutil.rmtree(tmp_dir, ignore_errors=False, onerror=try_remove_readonly)
Beispiel #12
0
    def configure(self, source_folder=None):
        # TODO: environment?
        if not self._conanfile.should_configure:
            return

        source = self._conanfile.source_folder
        if source_folder:
            source = os.path.join(self._conanfile.source_folder, source_folder)

        build_folder = self._conanfile.build_folder
        generator_folder = self._conanfile.generators_folder

        mkdir(build_folder)

        arg_list = [self._cmake_program]
        if self._generator:
            arg_list.append('-G "{}"'.format(self._generator))
        if self._toolchain_file:
            if os.path.isabs(self._toolchain_file):
                toolpath = self._toolchain_file
            else:
                toolpath = os.path.join(generator_folder, self._toolchain_file)
            arg_list.append('-DCMAKE_TOOLCHAIN_FILE="{}"'.format(toolpath.replace("\\", "/")))
        if self._conanfile.package_folder:
            pkg_folder = self._conanfile.package_folder.replace("\\", "/")
            arg_list.append('-DCMAKE_INSTALL_PREFIX="{}"'.format(pkg_folder))
        if platform.system() == "Windows" and self._generator == "MinGW Makefiles":
            arg_list.append('-DCMAKE_SH="CMAKE_SH-NOTFOUND"')
        arg_list.append('"{}"'.format(source))

        command = " ".join(arg_list)
        self._conanfile.output.info("CMake command: %s" % command)
        with chdir(build_folder):
            self._conanfile.run(command)
Beispiel #13
0
 def test_api_conanfile_loader_shouldnt_cache(self):
     tmp = temp_folder()
     with environment_append({"CONAN_USER_HOME": tmp}):
         with chdir(tmp):
             try:
                 old_stdout = sys.stdout
                 result = StringIO()
                 sys.stdout = result
                 api, _, _ = ConanAPIV1.factory()
                 api._user_io.out = TestBufferConanOutput()
                 conanfile = dedent("""
                     from conans import ConanFile
                     class Pkg(ConanFile):
                         def build(self):
                             self.output.info("NUMBER 42!!")
                     """)
                 save("conanfile.py", conanfile)
                 api.create(".", "pkg", "version", "user", "channel")
                 self.assertIn("pkg/version@user/channel: NUMBER 42!!",
                               result.getvalue())
                 save("conanfile.py", conanfile.replace("42", "123"))
                 api.create(".", "pkg", "version", "user", "channel")
                 self.assertIn("pkg/version@user/channel: NUMBER 123!!",
                               result.getvalue())
             finally:
                 sys.stdout = old_stdout
Beispiel #14
0
    def test_invalid_flat(self):
        tmp_folder = temp_folder()
        with chdir(tmp_folder):
            # Not a single dir containing everything
            file1 = os.path.join(tmp_folder, "subfolder-1.2.3", "folder2",
                                 "file1")
            file2 = os.path.join(tmp_folder, "other-1.2.3", "folder", "file2")

            save(file1, "")
            save(file2, "")

        zip_folder = temp_folder()
        zip_file = os.path.join(zip_folder, "file.zip")
        self._zipdir(tmp_folder, zip_file)

        # Extract without the subfolder
        extract_folder = temp_folder()
        with six.assertRaisesRegex(
                self, ConanException,
                "The zip file contains more than 1 folder "
                "in the root"):
            unzip(zip_file,
                  destination=extract_folder,
                  strip_root=True,
                  output=Mock())
Beispiel #15
0
 def run(self, command):
     command = "%s %s" % (self.cmd_command, command)
     with chdir(self.folder) if self.folder else no_op():
         with environment_append({"LC_ALL": "en_US.UTF-8"}) if self._force_eng else no_op():
             if not self._runner:
                 return decode_text(subprocess.check_output(command, shell=True).strip())
             else:
                 return self._runner(command)
Beispiel #16
0
 def run(self, command):
     command = "%s %s" % (self.cmd_command, command)
     with chdir(self.folder) if self.folder else no_op():
         with environment_append({"LC_ALL": "en_US.UTF-8"}) if self._force_eng else no_op():
             with pyinstaller_bundle_env_cleaned():
                 if not self._runner:
                     return check_output_runner(command).strip()
                 else:
                     return self._runner(command)
Beispiel #17
0
    def remove_files_by_mask_test(self):
        tmpdir = temp_folder()

        with chdir(tmpdir):
            os.makedirs("subdir")
            os.makedirs("dir.pdb")
            os.makedirs(os.path.join("subdir", "deepdir"))

        save_files(
            tmpdir, {
                "1.txt": "",
                "1.pdb": "",
                "1.pdb1": "",
                os.path.join("subdir", "2.txt"): "",
                os.path.join("subdir", "2.pdb"): "",
                os.path.join("subdir", "2.pdb1"): "",
                os.path.join("subdir", "deepdir", "3.txt"): "",
                os.path.join("subdir", "deepdir", "3.pdb"): "",
                os.path.join("subdir", "deepdir", "3.pdb1"): ""
            })

        removed_files = remove_files_by_mask(tmpdir, "*.sh")
        self.assertEqual(removed_files, [])

        removed_files = remove_files_by_mask(tmpdir, "*.pdb")

        self.assertTrue(os.path.isdir(os.path.join(tmpdir, "dir.pdb")))

        self.assertTrue(os.path.isfile(os.path.join(tmpdir, "1.txt")))
        self.assertFalse(os.path.isfile(os.path.join(tmpdir, "1.pdb")))
        self.assertTrue(os.path.isfile(os.path.join(tmpdir, "1.pdb1")))

        self.assertTrue(os.path.isfile(os.path.join(tmpdir, "subdir",
                                                    "2.txt")))
        self.assertFalse(
            os.path.isfile(os.path.join(tmpdir, "subdir", "2.pdb")))
        self.assertTrue(
            os.path.isfile(os.path.join(tmpdir, "subdir", "2.pdb1")))

        self.assertTrue(
            os.path.isfile(os.path.join(tmpdir, "subdir", "deepdir", "3.txt")))
        self.assertFalse(
            os.path.isfile(os.path.join(tmpdir, "subdir", "deepdir", "3.pdb")))
        self.assertTrue(
            os.path.isfile(os.path.join(tmpdir, "subdir", "deepdir",
                                        "3.pdb1")))

        self.assertEqual(
            set(removed_files), {
                "1.pdb",
                os.path.join("subdir", "2.pdb"),
                os.path.join("subdir", "deepdir", "3.pdb")
            })

        removed_files = remove_files_by_mask(tmpdir, "*.pdb")
        self.assertEqual(removed_files, [])
Beispiel #18
0
    def _run_test(self, tuples):
        tmp_folder = temp_folder()
        with chdir(tmp_folder):
            for f, _ in tuples:
                save(f, "")

            apple_dot_clean(".")

            for f, expected_after in tuples:
                self.assertEqual(expected_after, os.path.exists(f))
Beispiel #19
0
def _parse_conanfile(conan_file_path):
    """ From a given path, obtain the in memory python import module
    """

    if not os.path.exists(conan_file_path):
        raise NotFoundException("%s not found!" % conan_file_path)

    module_id = str(uuid.uuid1())
    current_dir = os.path.dirname(conan_file_path)
    sys.path.insert(0, current_dir)
    try:
        old_modules = list(sys.modules.keys())
        with chdir(current_dir):
            old_dont_write_bytecode = sys.dont_write_bytecode
            sys.dont_write_bytecode = True
            loaded = imp.load_source(module_id, conan_file_path)
            sys.dont_write_bytecode = old_dont_write_bytecode

        required_conan_version = getattr(loaded, "required_conan_version",
                                         None)
        if required_conan_version:
            validate_conan_version(required_conan_version)

        # These lines are necessary, otherwise local conanfile imports with same name
        # collide, but no error, and overwrite other packages imports!!
        added_modules = set(sys.modules).difference(old_modules)
        for added in added_modules:
            module = sys.modules[added]
            if module:
                try:
                    try:
                        # Most modules will have __file__ != None
                        folder = os.path.dirname(module.__file__)
                    except (AttributeError, TypeError):
                        # But __file__ might not exist or equal None
                        # Like some builtins and Namespace packages py3
                        folder = module.__path__._path[0]
                except AttributeError:  # In case the module.__path__ doesn't exist
                    pass
                else:
                    if folder.startswith(current_dir):
                        module = sys.modules.pop(added)
                        sys.modules["%s.%s" % (module_id, added)] = module
    except ConanException:
        raise
    except Exception:
        import traceback
        trace = traceback.format_exc().split('\n')
        raise ConanException("Unable to load conanfile in %s\n%s" %
                             (conan_file_path, '\n'.join(trace[3:])))
    finally:
        sys.path.pop(0)

    return loaded, module_id
Beispiel #20
0
def tgz_with_contents(files):
    with chdir(mkdir_tmp()):
        import tarfile
        file_path = os.path.abspath("myfile.tar.gz")
        tar_file = tarfile.open(file_path, "w:gz")
        for name, content in files.items():
            info = tarfile.TarInfo(name=name)
            data = content.encode('utf-8')
            info.size = len(data)
            tar_file.addfile(tarinfo=info, fileobj=BytesIO(data))
            tar_file.close()
        return file_path
Beispiel #21
0
def _call_package(conanfile, package_id, source_folder, build_folder,
                  package_folder, install_folder, hook_manager, conanfile_path,
                  ref, local, copy_info):
    output = conanfile.output
    try:
        hook_manager.execute("pre_package",
                             conanfile=conanfile,
                             conanfile_path=conanfile_path,
                             reference=ref,
                             package_id=package_id)

        output.highlight("Calling package()")
        folders = [source_folder, build_folder
                   ] if source_folder != build_folder else [build_folder]
        conanfile.copy = FileCopier(folders, package_folder)
        with conanfile_exception_formatter(str(conanfile), "package"):
            with chdir(build_folder):
                with conan_v2_property(
                        conanfile, 'info',
                        "'self.info' access in package() method is deprecated"
                ):
                    conanfile.package()
    except Exception as e:
        if not local:
            os.chdir(build_folder)
            try:
                rmdir(package_folder)
            except Exception as e_rm:
                output.error("Unable to remove package folder %s\n%s" %
                             (package_folder, str(e_rm)))
                output.warn("**** Please delete it manually ****")

        if isinstance(e, ConanExceptionInUserConanfileMethod):
            raise
        raise ConanException(e)

    hook_manager.execute("post_package",
                         conanfile=conanfile,
                         conanfile_path=conanfile_path,
                         reference=ref,
                         package_id=package_id)

    manifest = _create_aux_files(install_folder, package_folder, conanfile,
                                 copy_info)
    package_output = ScopedOutput("%s package()" % output.scope, output)
    _report_files_from_manifest(package_output, manifest)
    package_id = package_id or os.path.basename(package_folder)

    output.success("Package '%s' created" % package_id)

    prev = manifest.summary_hash
    output.info("Created package revision %s" % prev)
    return prev
    def test_invalid_flat_single_file(self):
        tmp_folder = temp_folder()
        with chdir(tmp_folder):
            save("file1", "contentsfile1")

        zip_folder = temp_folder()
        zip_file = os.path.join(zip_folder, "file.zip")
        self._zipdir(tmp_folder, zip_file)

        # Extract without the subfolder
        extract_folder = temp_folder()
        with six.assertRaisesRegex(self, ConanException, "The zip file contains a file in the root"):
            unzip(zip_file, destination=extract_folder, strip_root=True)
Beispiel #23
0
    def test_invalid_flat_single_file(self):
        tmp_folder = temp_folder()
        with chdir(tmp_folder):
            save("file1", "contentsfile1")

        zip_folder = temp_folder()
        tgz_file = os.path.join(zip_folder, "file.tar.gz")
        self._compress_folder(tmp_folder, tgz_file)

        # Extract without the subfolder
        extract_folder = temp_folder()
        with six.assertRaisesRegex(self, ConanException, "The tgz file contains a file in the root"):
            unzip(tgz_file, destination=extract_folder, strip_root=True, output=Mock())
Beispiel #24
0
    def test_plain_zip(self):
        tmp_folder = temp_folder()
        with chdir(tmp_folder):
            ori_files_dir = os.path.join(tmp_folder, "subfolder-1.2.3")
            file1 = os.path.join(ori_files_dir, "file1")
            file2 = os.path.join(ori_files_dir, "folder", "file2")
            file3 = os.path.join(ori_files_dir, "file3")

            save(file1, "")
            save(file2, "")
            save(file3, "")

        zip_file = os.path.join(tmp_folder, "myzip.zip")
        # Zip with a "folder_entry" in the zip (not only for the files)
        self._zipdir(tmp_folder, zip_file, folder_entry="subfolder-1.2.3")

        # ZIP unzipped regularly
        extract_folder = temp_folder()
        output = TestBufferConanOutput()
        unzip(zip_file,
              destination=extract_folder,
              strip_root=False,
              output=output)
        self.assertNotIn("ERROR: Error extract", output)
        self.assertTrue(
            os.path.exists(os.path.join(extract_folder, "subfolder-1.2.3")))
        self.assertTrue(
            os.path.exists(
                os.path.join(extract_folder, "subfolder-1.2.3", "file1")))
        self.assertTrue(
            os.path.exists(
                os.path.join(extract_folder, "subfolder-1.2.3", "folder",
                             "file2")))
        self.assertTrue(
            os.path.exists(
                os.path.join(extract_folder, "subfolder-1.2.3", "file3")))

        # Extract without the subfolder
        extract_folder = temp_folder()
        output = TestBufferConanOutput()
        unzip(zip_file,
              destination=extract_folder,
              strip_root=True,
              output=output)
        self.assertNotIn("ERROR: Error extract", output)
        self.assertFalse(
            os.path.exists(os.path.join(extract_folder, "subfolder-1.2.3")))
        self.assertTrue(os.path.exists(os.path.join(extract_folder, "file1")))
        self.assertTrue(
            os.path.exists(os.path.join(extract_folder, "folder", "file2")))
        self.assertTrue(os.path.exists(os.path.join(extract_folder, "file3")))
Beispiel #25
0
    def test_propset_own(self):
        """ Apply svn:needs-lock property to every file in the own working-copy of the repository """

        conanfile = base_svn.format(directory="None", url=_quoted("auto"), revision="auto")
        project_url, _ = self.create_project(files={"conanfile.py": conanfile,})
        project_url = project_url.replace(" ", "%20")

        # Add property needs-lock to my own copy
        client = TestClient()
        client.runner('svn co "{url}" "{path}"'.format(url=project_url,
                                                       path=client.current_folder))
        with chdir(client.current_folder):
            for item in ['conanfile.py', ]:
                client.runner('svn propset svn:needs-lock yes {}'.format(item))
            client.runner('svn commit -m "lock some files"')

        client.run("export . user/channel")
    def test_invalid_flat(self):
        tmp_folder = temp_folder()
        with chdir(tmp_folder):
            # Not a single dir containing everything
            file1 = os.path.join(tmp_folder, "subfolder-1.2.3", "folder2", "file1")
            file2 = os.path.join(tmp_folder, "other-1.2.3", "folder", "file2")

            save(file1, "")
            save(file2, "")

        tgz_folder = temp_folder()
        tgz_file = os.path.join(tgz_folder, "file.tar.gz")
        self._compress_folder(tmp_folder, tgz_file)

        extract_folder = temp_folder()
        with six.assertRaisesRegex(self, ConanException, "The tgz file contains more than 1 folder "
                                                         "in the root"):
            untargz(tgz_file, destination=extract_folder, strip_root=True)
Beispiel #27
0
    def test_preprocessor_called_second_api_call(self):
        """"When calling twice to conan create with the Conan python API, the default
        settings shouldn't be cached. To test that the default profile is not cached,
        this test is verifying that the setting preprocessor is adjusting the runtime
        to MDd when build_type=Debug after a different call to conan create that could
        cache the runtime to MD (issue reported at: #4246) """
        tmp = temp_folder()
        with environment_append({"CONAN_USER_HOME": tmp}):
            with chdir(tmp):
                api = ConanAPIV1(output=TestBufferConanOutput())
                api.new(name="lib/1.0@conan/stable", bare=True)

                def get_conaninfo(info):
                    package_id = info["installed"][0]["packages"][0]["id"]
                    pref = PackageReference.loads("lib/1.0@conan/stable:%s" %
                                                  package_id)
                    cache = ClientCache(api.cache_folder,
                                        TestBufferConanOutput())
                    folder = cache.package_layout(pref.ref).package(pref)
                    return load(os.path.join(folder, "conaninfo.txt"))

                settings = [
                    "compiler=Visual Studio", "compiler.version=15",
                    "build_type=Release"
                ]
                info = api.create(".",
                                  name=None,
                                  version=None,
                                  user="******",
                                  channel="stable",
                                  settings=settings)
                self.assertIn("compiler.runtime=MD", get_conaninfo(info))

                settings = [
                    "compiler=Visual Studio", "compiler.version=15",
                    "build_type=Debug"
                ]
                info = api.create(".",
                                  name=None,
                                  version=None,
                                  user="******",
                                  channel="stable",
                                  settings=settings)
                self.assertIn("compiler.runtime=MDd", get_conaninfo(info))
Beispiel #28
0
    def test_api_conanfile_loader_shouldnt_cache(self):
        tmp = temp_folder()
        with environment_append({"CONAN_USER_HOME": tmp}):
            with chdir(tmp):
                output = TestBufferConanOutput()
                api = ConanAPIV1(output=output)

                conanfile = dedent("""
                    from conans import ConanFile
                    class Pkg(ConanFile):
                        def build(self):
                            self.output.info("NUMBER 42!!")
                    """)
                save("conanfile.py", conanfile)
                api.create(".", "pkg", "version", "user", "channel")
                self.assertIn("pkg/version@user/channel: NUMBER 42!!", output)
                save("conanfile.py", conanfile.replace("42", "123"))
                api.create(".", "pkg", "version", "user", "channel")
                self.assertIn("pkg/version@user/channel: NUMBER 123!!", output)
Beispiel #29
0
    def _load_module_from_file(hook_path):
        """ From a given path, obtain the in memory python import module
        """
        if not os.path.exists(hook_path):
            raise NotFoundException
        filename = os.path.splitext(os.path.basename(hook_path))[0]
        current_dir = os.path.dirname(hook_path)

        old_dont_write_bytecode = sys.dont_write_bytecode
        try:
            sys.path.append(current_dir)
            old_modules = list(sys.modules.keys())
            with chdir(current_dir):
                sys.dont_write_bytecode = True
                loaded = __import__(filename)
            # Put all imported files under a new package name
            module_id = uuid.uuid1()
            added_modules = set(sys.modules).difference(old_modules)
            for added in added_modules:
                module = sys.modules[added]
                if module:
                    try:
                        try:
                            # Most modules will have __file__ != None
                            folder = os.path.dirname(module.__file__)
                        except (AttributeError, TypeError):
                            # But __file__ might not exist or equal None
                            # Like some builtins and Namespace packages py3
                            folder = module.__path__._path[0]
                    except AttributeError:  # In case the module.__path__ doesn't exist
                        pass
                    else:
                        if folder.startswith(current_dir):
                            module = sys.modules.pop(added)
                            sys.modules["%s.%s" % (module_id, added)] = module
        except Exception:
            trace = traceback.format_exc().split('\n')
            raise ConanException("Unable to load Hook in %s\n%s" %
                                 (hook_path, '\n'.join(trace[3:])))
        finally:
            sys.dont_write_bytecode = old_dont_write_bytecode
            sys.path.pop()
        return loaded
Beispiel #30
0
    def _create_and_load(myfunc, value, subdir_name, add_subdir_init):
        subdir_content = textwrap.dedent("""
            def get_value():
                return {value}
            def {myfunc}():
                return "{myfunc}"
        """)

        side_content = textwrap.dedent("""
            def get_side_value():
                return {value}
            def side_{myfunc}():
                return "{myfunc}"
        """)

        conanfile = textwrap.dedent("""
            import pickle
            from {subdir}.api import get_value, {myfunc}
            from file import get_side_value, side_{myfunc}
            from fractions import Fraction
            def conanfile_func():
                return get_value(), {myfunc}(), get_side_value(), side_{myfunc}(), str(Fraction(1,1))
        """)
        expected_return = (value, myfunc, value, myfunc, "1")

        tmp = temp_folder()
        with chdir(tmp):
            save(
                "conanfile.py",
                conanfile.format(value=value,
                                 myfunc=myfunc,
                                 subdir=subdir_name))
            save("file.py", side_content.format(value=value, myfunc=myfunc))
            save("{}/api.py".format(subdir_name),
                 subdir_content.format(value=value, myfunc=myfunc))
            if add_subdir_init:
                save("__init__.py", "")
                save("{}/__init__.py".format(subdir_name), "")

        loaded, module_id = _parse_conanfile(os.path.join(tmp, "conanfile.py"))
        return loaded, module_id, expected_return