Пример #1
0
    def test_permissions(self):
        if platform.system() != "Windows":
            for keep_permissions in [True, False]:
                for perm_set in [stat.S_IRWXU, stat.S_IRUSR]:
                    tmp_dir = temp_folder()
                    file_path = os.path.join(tmp_dir, "a_file.txt")
                    save(file_path, "contents")
                    os.chmod(file_path, perm_set)
                    zf = zipfile.ZipFile(os.path.join(tmp_dir, 'zipfile.zip'),
                                         mode='w')
                    zf.write(file_path, basename(file_path))
                    zf.close()

                    # Unzip and check permissions are kept
                    dest_dir = temp_folder()
                    unzip(os.path.join(tmp_dir, 'zipfile.zip'),
                          dest_dir,
                          keep_permissions=keep_permissions,
                          output=ConanOutput(StringIO()))

                    dest_file = os.path.join(dest_dir, "a_file.txt")
                    if keep_permissions:
                        self.assertEquals(
                            stat.S_IMODE(os.stat(dest_file).st_mode), perm_set)
                    else:
                        self.assertNotEquals(
                            stat.S_IMODE(os.stat(dest_file).st_mode), perm_set)
Пример #2
0
def get(url,
        md5='',
        sha1='',
        sha256='',
        destination=".",
        filename="",
        keep_permissions=False,
        pattern=None):
    """ high level downloader + unzipper + (optional hash checker) + delete temporary zip
    """
    if not filename and ("?" in url or "=" in url):
        raise ConanException(
            "Cannot deduce file name form url. Use 'filename' parameter.")

    filename = filename or os.path.basename(url)
    download(url, filename)

    if md5:
        check_md5(filename, md5)
    if sha1:
        check_sha1(filename, sha1)
    if sha256:
        check_sha256(filename, sha256)

    unzip(filename,
          destination=destination,
          keep_permissions=keep_permissions,
          pattern=pattern)
    os.unlink(filename)
Пример #3
0
Файл: net.py Проект: zesem/conan
def get(url):
    """ high level downloader + unziper + delete temporary zip
    """
    filename = os.path.basename(url)
    download(url, filename)
    unzip(filename)
    os.unlink(filename)
Пример #4
0
def _process_zip_file(config,
                      zippath,
                      cache,
                      output,
                      tmp_folder,
                      remove=False):
    unzip(zippath, tmp_folder, output=output)
    if remove:
        os.unlink(zippath)
    _process_folder(config, tmp_folder, cache, output)
Пример #5
0
    def test(self):
        tmp_dir = temp_folder()
        file_path = os.path.join(tmp_dir, "a_file.txt")
        save(file_path, "my content!")
        txz = os.path.join(tmp_dir, "sample.tar.xz")
        with tarfile.open(txz, "w:xz") as tar:
            tar.add(file_path, "a_file.txt")

        dest_folder = temp_folder()
        unzip(txz, dest_folder, output=ConanOutput(StringIO()))
        content = load(os.path.join(dest_folder, "a_file.txt"))
        self.assertEqual(content, "my content!")
Пример #6
0
def get(url, md5='', sha1='', sha256='', destination="."):
    """ high level downloader + unzipper + (optional hash checker) + delete temporary zip
    """
    filename = os.path.basename(url)
    download(url, filename)

    if md5:
        check_md5(filename, md5)
    if sha1:
        check_sha1(filename, sha1)
    if sha256:
        check_sha256(filename, sha256)

    unzip(filename, destination=destination)
    os.unlink(filename)
Пример #7
0
def get(conanfile,
        url,
        md5='',
        sha1='',
        sha256='',
        destination=".",
        filename="",
        keep_permissions=False,
        pattern=None,
        verify=True,
        retry=None,
        retry_wait=None,
        overwrite=False,
        auth=None,
        headers=None,
        strip_root=False):
    """ high level downloader + unzipper + (optional hash checker) + delete temporary zip
    """
    requester = conanfile._conan_requester
    output = conanfile.output
    if not filename:  # deduce filename from the URL
        url_base = url[0] if isinstance(url, (list, tuple)) else url
        if "?" in url_base or "=" in url_base:
            raise ConanException(
                "Cannot deduce file name from the url: '{}'. Use 'filename' "
                "parameter.".format(url_base))
        filename = os.path.basename(url_base)

    download(url,
             filename,
             out=output,
             requester=requester,
             verify=verify,
             retry=retry,
             retry_wait=retry_wait,
             overwrite=overwrite,
             auth=auth,
             headers=headers,
             md5=md5,
             sha1=sha1,
             sha256=sha256)
    unzip(filename,
          destination=destination,
          keep_permissions=keep_permissions,
          pattern=pattern,
          output=output,
          strip_root=strip_root)
    os.unlink(filename)
Пример #8
0
def get(url,
        md5='',
        sha1='',
        sha256='',
        destination=".",
        filename="",
        keep_permissions=False,
        pattern=None,
        requester=None,
        output=None,
        verify=True,
        retry=None,
        retry_wait=None,
        overwrite=False,
        auth=None,
        headers=None):
    """ high level downloader + unzipper + (optional hash checker) + delete temporary zip
    """
    if not filename and ("?" in url or "=" in url):
        raise ConanException(
            "Cannot deduce file name form url. Use 'filename' parameter.")

    filename = filename or os.path.basename(url)
    download(url,
             filename,
             out=output,
             requester=requester,
             verify=verify,
             retry=retry,
             retry_wait=retry_wait,
             overwrite=overwrite,
             auth=auth,
             headers=headers)

    if md5:
        check_md5(filename, md5)
    if sha1:
        check_sha1(filename, sha1)
    if sha256:
        check_sha256(filename, sha256)

    unzip(filename,
          destination=destination,
          keep_permissions=keep_permissions,
          pattern=pattern,
          output=output)
    os.unlink(filename)
Пример #9
0
    def test_patterns(self):
        # Test setup
        src_dir = temp_folder()
        files = {
            "foo/file.cpp": "code",
            "foo/bar/file.txt": "text",
            "foo/bar/file.cpp": "more code",
            "foo/bar/baz/file.txt": "more text"
        }
        matches = {
            "*.cpp": ["foo/file.cpp", "foo/bar/file.cpp"],
            "*.txt": ["foo/bar/file.txt", "foo/bar/baz/file.txt"],
            "foo/bar/*":
            ["foo/bar/file.txt", "foo/bar/file.cpp", "foo/bar/baz/file.txt"],
            "foo/bar/baz/*": ["foo/bar/baz/file.txt"],
            "*": [
                "foo/file.cpp", "foo/bar/file.txt", "foo/bar/file.cpp",
                "foo/bar/baz/file.txt"
            ],
            "nothing": []
        }
        save_files(src_dir, files)

        for extension in ["zip", "tar", "tar.gz"]:
            # GIVEN an archive with files inside
            archive_dir = temp_folder()
            archive = os.path.join(archive_dir, "archive.%s" % extension)
            create_archive(archive, src_dir, files)

            for (pattern, paths) in matches.items():
                # WHEN a pattern is used for file extraction
                dst_dir = temp_folder()
                unzip(archive,
                      dst_dir,
                      pattern=pattern,
                      output=ConanOutput(sys.stdout))

                # THEN only and all files matching the pattern are extracted
                actual = set()
                expected = set(
                    map(lambda x: os.path.join(dst_dir, *x.split("/")), paths))
                for extracted_dir, _, extracted_files in os.walk(dst_dir):
                    actual.update(
                        map(lambda x: os.path.join(extracted_dir, x),
                            extracted_files))

                self.assertSetEqual(expected, actual)
Пример #10
0
def unzip(*args, **kwargs):
    return tools_files.unzip(output=_global_output, *args, **kwargs)
Пример #11
0
 def test_error_python2(self):
     with six.assertRaisesRegex(self, ConanException,
                                "XZ format not supported in Python 2"):
         dest_folder = temp_folder()
         unzip("somefile.tar.xz", dest_folder, output=self.output)