Skip to content

Commit

Permalink
gui: Experimental workaround for debians dist-package
Browse files Browse the repository at this point in the history
  • Loading branch information
sahib committed May 11, 2016
1 parent 510e366 commit ecf1f8c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 21 deletions.
22 changes: 22 additions & 0 deletions gui/shredder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@

# Use boxy old menus or new popovers?
APP_USE_TRADITIONAL_MENU = False


def run_gui():
"""Fully take over and run the gui code."""
# Stdlib:
import sys

# Internal:
from shredder.cmdline import parse_arguments
from shredder.logger import create_logger

ROOT_LOGGER = create_logger(None)
OPTIONS = parse_arguments(ROOT_LOGGER)

# Later import due to logging.
from shredder.application import Application

if OPTIONS:
# Gtk will take over now.
APP = Application(OPTIONS)
ROOT_LOGGER.info('Starting up.')
sys.exit(APP.run([sys.argv[0]]))
21 changes: 2 additions & 19 deletions gui/shredder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
$ python -m shredder
"""

# Stdlib:
import sys
import shredder

# Internal:
from shredder.cmdline import parse_arguments
from shredder.logger import create_logger

ROOT_LOGGER = create_logger(None)
OPTIONS = parse_arguments(ROOT_LOGGER)


# Later import due to logging.
from shredder.application import Application


if OPTIONS:
# Gtk will take over now.
APP = Application(OPTIONS)
ROOT_LOGGER.info('Starting up.')
sys.exit(APP.run([sys.argv[0]]))
shredder.run_gui()
59 changes: 57 additions & 2 deletions lib/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,64 @@ static void rm_cmd_show_manpage(void) {
exit(0);
}

/*
* Debian and Ubuntu based distributions fuck up setuptools
* by expecting packages to be installed to dist-packages and not site-packages
* like expected by setuptools. This breaks a lot of packages with the reasoning
* to reduce conflicts between system and user packages:
*
* https://stackoverflow.com/questions/9387928/whats-the-difference-between-dist-packages-and-site-packages
*
* We try to work around this by manually installing dist-packages to the
* sys.path by first calling a small bootstrap script.
*/
static const char RM_PY_BOOTSTRAP[] = ""
"# This is a bootstrap script for the rmlint-gui. \n"
"# See the src/rmlint.c in rmlint's source for more info. \n"
"import sys \n"
" \n"
"# Also default to dist-packages on debian(-based): \n"
"paths = [p for p in sys.path if 'site-package' in p] \n"
"sys.path.extend([d.replace('site-packages', 'dist-packages') for d in paths]) \n"
" \n"
"# Run shredder by importing the main: \n"
"try: \n"
" import shredder \n"
" shredder.run_gui() \n"
"except ImportError as err: \n"
" print('oh', err) \n"
;


static void rm_cmd_start_gui(int argc, const char **argv) {
const char *commands[] = {"python3", "python", NULL};
const char **command = &commands[0];

GError *error = NULL;
gchar *bootstrap_path = NULL;
int bootstrap_fd = g_file_open_tmp(".shredder-bootstrap.py.XXXXXX", &bootstrap_path, &error);

if(bootstrap_fd < 0) {
rm_log_warning("Could not bootstrap gui: Unable to create tempfile: %s", error->message);
g_error_free(error);
return;
}

if(write(bootstrap_fd, RM_PY_BOOTSTRAP, sizeof(RM_PY_BOOTSTRAP)) < 0) {
rm_log_warning_line(
"Could not bootstrap gui: Unable to write to tempfile: %s",
g_strerror(errno)
);
return;
}

while(*command) {
const char *all_argv[512];
const char **argp = &all_argv[0];
memset(all_argv, 0, sizeof(all_argv));

*argp++ = *command;
*argp++ = "-m";
*argp++ = "shredder";
*argp++ = bootstrap_path;

for(size_t i = 0; i < (size_t)argc && i < sizeof(all_argv) / 2; i++) {
*argp++ = argv[i];
Expand All @@ -148,6 +194,15 @@ static void rm_cmd_start_gui(int argc, const char **argv) {
/* Try next command... */
command++;
}

close(bootstrap_fd);

if(g_remove(bootstrap_path) < 0) {
rm_log_warning_line(
"Could not delete bootstrap script: %s",
g_strerror(errno)
);
}
}

static int rm_cmd_maybe_switch_to_gui(int argc, const char **argv) {
Expand Down

0 comments on commit ecf1f8c

Please sign in to comment.