Exemplo n.º 1
0
class Config():
    """Config data for this particular system.

    This currently includes the location of the local docker client and the
    local kernel version.
    """
    if not uname().sysname == 'Linux':
        raise (NotImplementedError(
            f"Your system, {uname().sysname}, has not been implemented yet"))
    dc = DockerClient('unix://run/docker.sock', version='1.30')
    network_name = "docker_application_network"
    try:
        application_network = dc.networks.list(names=[network_name])[0]
    except IndexError:
        application_network = dc.networks.create(network_name)
    kernel_version_match = find_pattern('\d\.\d\d?', uname().release)
    kernel_version = uname().release[kernel_version_match.start(
    ):kernel_version_match.end()]
    kernels = ['2.6', '3.10', '3.13', '3.16', '4.4', '4.9', '4.14', '4.15']
    kernel_index = kernels.index(kernel_version)
    dockerfile_template = getpath(sep, "usr", "share", "docker-gui",
                                  "Dockerfile.pytemplate")
    runscript_template = getpath(sep, "usr", "share", "docker-gui",
                                 "runscript.pytemplate")
    user = 1000
    group = 1000
    user = 1000
    group = 1000
Exemplo n.º 2
0
def list_recursively(f: str, *filepath: str) -> list:
    """Get a list of all files in a folder and its subfolders.

    :return: list: Absolute paths of all files in the folder.
    """
    if not isdir(getpath(f, *filepath)):
        # If the specified file isn't a directory, just return that one file.
        return [getpath(f, *filepath)]
    return [
        getpath(dp, f) for dp, dn, fn in walk(getpath(f, *filepath))
        for f in fn
    ]
Exemplo n.º 3
0
 def init_files(self):
     """Write all the files to use this Application at a later time."""
     self.working_directory = getpath('/', 'usr', 'share', 'docker-gui')
     check_isdir(self.working_directory)
     with open(getpath(self.working_directory, "Dockerfile.pytemplate"),
               'r') as dockerfile:
         self.dockerfile_template = dockerfile.read()
     self.application_directory = getpath(self.working_directory,
                                          self.application)
     check_isdir(self.application_directory)
     self.run_script_file = getpath(self.application_directory,
                                    f"run_{self.application}")
     self.render_dockerfile()
     self.write_run_script()
     self.write_desktop_file()
Exemplo n.º 4
0
def read_absolute(f: str, *fname: str) -> str:
    """Get the contents of a file from an absolute path.

    Path should be passed like with os.path.join:
    read_absolute(os.sep, 'path', 'to', 'file')
    """
    with open(getpath(f, *fname)) as file:
        return file.read()
Exemplo n.º 5
0
def read_relative(*fname: str) -> str:
    """Get the contents of a file in the current directory.

    NOTE: this means relative to the directory containing the misc_functions.py
    file!!! That may not be the directory in which your source file lies!

    Path should be passed like with os.path.join:
    read_relative('path', 'to', 'file')
    """
    with open(getpath(dirname(realpath(__file__)), *fname)) as file:
        return file.read()
Exemplo n.º 6
0
def main(*args, **kwargs):
    """The simple_shuffle script shuffles a folder of flac and ogg files.

    It does a true shuffle of all of the songs in the folder, without repeats,
    unless you've got duplicates. Specify the folder to be shuffled on the
    command line just after the name. By default it shuffles /home/$USER/Music.
    """
    if kwargs['shuffle_folder'] is None:
        Player(getpath(root, 'home', environ['USER'], 'Music'))
    else:
        Player(kwargs['shuffle_folder'])
Exemplo n.º 7
0
def perms(f: str, *filepath: str) -> int:
    """Get the permissions of a file as an octal number.

    The last three digits of the octal value are what you would put in for
    a standard bash chmod bitfield input. For example, you may receive
    0o100644 which would equate to rw-r--r-- or 644.

    :type filepath: tuple of strings to be passed to os.path.join
    """
    if f != root and f[0] != '/':
        raise ValueError("Path must be specified as a full path.")
    return stat(getpath(f, *filepath)).st_mode
Exemplo n.º 8
0
    def write_desktop_file(self):
        """Create the .desktop file for this Applicationself.

        Creating and storing this file in /usr/share/applications allows the
        user to run the application from their app menu or launcher.
        """
        try:
            desktop_file = open(
                getpath('/', 'usr', 'share', 'applications',
                        "%s.docker.desktop" % self.application), 'w')
            desktop_file.write(
                dedent(f"""
                    [Desktop Entry]
                    Version=From {self.distro.distro}
                    Name={self.application}
                    Exec={self.run_script_file}
                    Terminal=false
                    Type=Application
                    Categories=Containerized
                    """))
        except PermissionError:
            runcmd(
                dedent("""
                sudo su -c 'echo "{0}" > "{1}" && chown {2}:{3} {1}'
                """).format(
                    dedent(f"""
                            [Desktop Entry]
                            Version=From {self.distro.distro}
                            Name={self.application}
                            Exec={self.run_script_file}
                            Terminal=false
                            Type=Application
                            Categories=Containerized
                            """),
                    getpath('/', 'usr', 'share', 'applications',
                            "%s.docker.desktop" % self.application), getuid(),
                    getgid()))
            self.write_desktop_file()
Exemplo n.º 9
0
 def render_dockerfile(self):
     """Render a Dockerfile for building this application."""
     with open(Config.dockerfile_template, 'r') as templatefile:
         template = JinjaTemplate(templatefile.read())
     with open(getpath(self.application_directory, "Dockerfile"),
               'w') as dockerfile:
         dockerfile.write(
             template.render(package=self.package,
                             application=self.application,
                             distro=self.distro,
                             uid=getuid(),
                             gid=getgid()) +
             '\n'  # a hack because apparently rendering the
         )  # template eats the trailing newline
Exemplo n.º 10
0
    def test_desktop_file(self):
        """Make sure the desktop file for the test application exists.

        Checks the content too.
        """
        test_desktop_file = dedent(f"""
            [Desktop Entry]
            Version=From {self.distro['name'].capitalize()}, version {self.distro['version']}
            Name={self.application_name}
            Exec={getpath(self.get_test_application().application_directory, f"run_{self.application_name}")}
            Terminal=false
            Type=Application
            Categories=Containerized
            """)
        with open(
                getpath('/', 'usr', 'share', 'applications',
                        f"{self.application_name}.docker.desktop"),
                'r') as desktop_file:
            assert desktop_file.read() == test_desktop_file, dedent(f"""
                Desktop file should've been {test_desktop_file} but it was/is
                {desktop_file.read()}""")
Exemplo n.º 11
0
def read(*fname: str) -> str:
    """Get the contents of a file in the current directory."""
    return open(getpath(dirname(__file__), *fname)).read()
Exemplo n.º 12
0
    )
    try:
        return get_user(desired_user)
    except KeyError:
        print("User", desired_user, "not found.")
        return get_desired_user()
user = get_desired_user()

if not access('/usr/share/docker-gui', is_writable | is_executable)\
        or not isdir('/usr/share/docker-gui'):
    runcmd(f"sudo mkdir -p /usr/share/docker-gui && sudo chown {user.pw_uid}:{user.pw_gid} /usr/share/docker-gui")
try:
    copy(
        getpath(
            dirname(realpath(__file__)),
            "container_gui",
            "runscript.pytemplate"
        ),
        getpath('/', 'usr', 'share', 'docker-gui', 'runscript.pytemplate')
    )
except FileExistsError:
    pass
chown(
    path=getpath('/', 'usr', 'share', 'docker-gui', 'runscript.pytemplate'),
    uid=user.pw_uid,
    gid=user.pw_gid
)
try:
    copy(
        getpath(
            dirname(realpath(__file__)),
Exemplo n.º 13
0
def get_parent_dir(name: str) -> str:
    """Retrieve the parent directory of a named instance."""
    return getpath(root, "usr", "share", "quick_deployments", "static", name)