Example #1
0
 def test_find_package_directory(self):
     info = os.path.join(self.temp_dir, ".click", "info")
     path = os.path.join(self.temp_dir, "file")
     Click.ensuredir(info)
     touch(path)
     pkgdir = Click.find_package_directory(path)
     self.assertEqual(self.temp_dir, pkgdir)
Example #2
0
 def _create_mock_framework_dir(self, frameworks_dir=None):
     if frameworks_dir is None:
         frameworks_dir = os.path.join(self.temp_dir, "frameworks")
         patcher = mock.patch('click_package.paths.frameworks_dir',
                              frameworks_dir)
         patcher.start()
         self.addCleanup(patcher.stop)
     Click.ensuredir(frameworks_dir)
     return frameworks_dir
Example #3
0
 def test_get_frameworks_ignores_other_files(self):
     with self.run_in_subprocess("click_get_frameworks_dir") as (enter,
                                                                 preloads):
         enter()
         frameworks_dir = os.path.join(self.temp_dir, "frameworks")
         Click.ensuredir(frameworks_dir)
         touch(os.path.join(frameworks_dir, "file"))
         preloads["click_get_frameworks_dir"].side_effect = (
             lambda: self.make_string(frameworks_dir))
         self.assertEqual([], Click.Framework.get_frameworks())
Example #4
0
 def test_get_frameworks_ignores_unopenable_files(self):
     with self.run_in_subprocess("click_get_frameworks_dir") as (enter,
                                                                 preloads):
         enter()
         frameworks_dir = os.path.join(self.temp_dir, "frameworks")
         Click.ensuredir(frameworks_dir)
         os.symlink("nonexistent",
                    os.path.join(frameworks_dir, "foo.framework"))
         preloads["click_get_frameworks_dir"].side_effect = (
             lambda: self.make_string(frameworks_dir))
         self.assertEqual([], Click.Framework.get_frameworks())
Example #5
0
def mkfile_utf8(path, mode="w"):
    Click.ensuredir(os.path.dirname(path))
    if sys.version < "3":
        import codecs
        with codecs.open(path, mode, "UTF-8") as f:
            yield f
    else:
        # io.open is available from Python 2.6, but we only use it with
        # Python 3 because it raises exceptions when passed bytes.
        import io
        with io.open(path, mode, encoding="UTF-8") as f:
            yield f
Example #6
0
 def _setup_frameworks(self, preloads, frameworks_dir=None, frameworks={}):
     if frameworks_dir is None:
         frameworks_dir = os.path.join(self.temp_dir, "frameworks")
     Click.ensuredir(frameworks_dir)
     for framework_name in frameworks:
         framework_path = os.path.join(frameworks_dir,
                                       "%s.framework" % framework_name)
         with open(framework_path, "w") as framework:
             for key, value in frameworks[framework_name].items():
                 print("%s: %s" % (key, value), file=framework)
     preloads["click_get_frameworks_dir"].side_effect = (
         lambda: self.make_string(frameworks_dir))
Example #7
0
def write_desktop_file(target_path, source_path, profile):
    Click.ensuredir(os.path.dirname(target_path))
    with io.open(source_path, encoding="UTF-8") as source, \
         io.open(target_path, "w", encoding="UTF-8") as target:
        source_dir = Click.find_package_directory(source_path)
        written_comment = False
        seen_path = False
        for line in source:
            if not line.rstrip("\n") or line.startswith("#"):
                # Comment
                target.write(line)
            elif line.startswith("["):
                # Group header
                target.write(line)
                if not written_comment:
                    print(COMMENT, file=target)
            elif "=" not in line:
                # Who knows?
                target.write(line)
            else:
                key, value = line.split("=", 1)
                key = key.strip()
                value = value.strip()
                if key == "Exec":
                    target.write("%s=aa-exec-click -p %s -- %s\n" %
                                 (key, quote_for_desktop_exec(profile), value))
                elif key == "Path":
                    target.write("%s=%s\n" % (key, source_dir))
                    seen_path = True
                elif key == "Icon":
                    icon_path = os.path.join(source_dir, value)
                    if os.path.exists(icon_path):
                        target.write("%s=%s\n" % (key, icon_path))
                    else:
                        target.write("%s=%s\n" % (key, value))
                else:
                    target.write("%s=%s\n" % (key, value))
        if not seen_path:
            target.write("Path=%s\n" % source_dir)
Example #8
0
    def make_fake_package(self,
                          control_fields=None,
                          manifest=None,
                          control_scripts=None,
                          data_files=None):
        """Build a fake package with given contents."""
        control_fields = {} if control_fields is None else control_fields
        control_scripts = {} if control_scripts is None else control_scripts
        data_files = {} if data_files is None else data_files

        data_dir = os.path.join(self.temp_dir, "fake-package")
        control_dir = os.path.join(self.temp_dir, "DEBIAN")
        with mkfile(os.path.join(control_dir, "control")) as control:
            for key, value in control_fields.items():
                print('%s: %s' % (key.title(), value), file=control)
            print(file=control)
        if manifest is not None:
            with mkfile(os.path.join(control_dir, "manifest")) as f:
                json.dump(manifest, f)
                print(file=f)
        for name, contents in control_scripts.items():
            with mkfile(os.path.join(control_dir, name)) as script:
                script.write(contents)
        Click.ensuredir(data_dir)
        for name, path in data_files.items():
            Click.ensuredir(os.path.dirname(os.path.join(data_dir, name)))
            if path is None:
                touch(os.path.join(data_dir, name))
            elif os.path.isdir(path):
                shutil.copytree(path, os.path.join(data_dir, name))
            else:
                shutil.copy2(path, os.path.join(data_dir, name))
        package_path = '%s.click' % data_dir
        ClickBuilder()._pack(self.temp_dir, control_dir, data_dir,
                             package_path)
        return package_path
Example #9
0
def make_file_with_content(filename, content, mode=0o644):
    """Create a file with the given content and mode"""
    Click.ensuredir(os.path.dirname(filename))
    with open(filename, "w") as f:
        f.write(content)
    os.chmod(filename, mode)
Example #10
0
def mkfile(path, mode="w"):
    Click.ensuredir(os.path.dirname(path))
    with open(path, mode) as f:
        yield f