Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and Python 3 support #110

Merged
12 commits merged into from
Sep 3, 2015
16 changes: 6 additions & 10 deletions src/actions/rebuild.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python2

import sys, os, glob, shutil, re, hashlib
import sys, os, glob, shutil, re

import lib.misc as misc
import lib.config as config
Expand Down Expand Up @@ -256,10 +256,8 @@ def main():
continue
if sfile.endswith('SHA256SUMS'):
continue

# FIXME: read in chunks
message.sub_debug('MD5 Checksumming', sfile)
checksum = hashlib.md5(misc.read_file(sfile)).hexdigest()
checksum = misc.generate_hash_for_file('md5', sfile)
misc.append_file(md5sums_file, checksum + ' .' + \
sfile.replace(config.ISO_DIR, '') +'\n')

Expand All @@ -273,10 +271,8 @@ def main():
continue
if sfile.endswith('SHA256SUMS'):
continue

# FIXME: read in chunks
message.sub_debug('SHA256 Checksumming', sfile)
checksum = hashlib.sha256(misc.read_file(sfile)).hexdigest()
checksum = misc.generate_hash_for_file('sha256', sfile)
misc.append_file(shasums_file, checksum + ' .' + \
sfile.replace(config.ISO_DIR, '') +'\n')

Expand All @@ -290,15 +286,15 @@ def main():
'-cache-inodes', '-input-charset', 'utf-8', '.'))

message.sub_info('Creating ISO checksums')
md5checksum = hashlib.md5(misc.read_file(iso_file)).hexdigest()
md5checksum = misc.generate_hash_for_file('md5', iso_file)
message.sub_info('ISO md5 checksum', md5checksum)
misc.append_file(md5sum_iso_file, md5checksum + ' .' + \
iso_file.replace(config.WORK_DIR, '') +'\n')
sha1checksum = hashlib.sha1(misc.read_file(iso_file)).hexdigest()
sha1checksum = misc.generate_hash_for_file('sha1', iso_file)
message.sub_info('ISO sha1 checksum', sha1checksum)
misc.append_file(sha1sum_iso_file, sha1checksum + ' .' + \
iso_file.replace(config.WORK_DIR, '') +'\n')
sha256checksum = hashlib.sha256(misc.read_file(iso_file)).hexdigest()
sha256checksum = misc.generate_hash_for_file('sha256', iso_file)
message.sub_info('ISO sha256 checksum', sha256checksum)
misc.append_file(sha256sum_iso_file, sha256checksum + ' .' + \
iso_file.replace(config.WORK_DIR, '') +'\n')
Expand Down
2 changes: 1 addition & 1 deletion src/actions/xnest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def main():
x = subprocess.Popen((misc.whereis('Xephyr'), '-ac', '-screen', \
config.RESOLUTION, '-br', ':13'))
x.poll()
if x.returncode > 0:
if x.returncode is not None and x.returncode > 0:
raise(message.exception('Failed to start Xephyr', x.returncode))

try:
Expand Down
54 changes: 38 additions & 16 deletions src/gui.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
# This is quick and dirty written GUI! #
############### END NOTE ###############

import sys, os, atexit, subprocess
import sys, os, atexit, subprocess, imp
sys.path.append("@PREFIX@/share/customizer")

import gui_ui
from PyQt4 import QtCore, QtGui

import lib.config as config
import lib.misc as misc
import lib.message as message
import actions.common as common
import actions.extract as extract
import actions.deb as deb
Expand Down Expand Up @@ -44,7 +45,7 @@ def msg_warning(msg):
QtGui.QMessageBox.warning(MainWindow, app.tr('Warning'), msg)

def msg_critical(msg):
QtGui.QMessageBox.critical(MainWindow, app.tr('Critical'), str(msg).decode('utf-8'))
QtGui.QMessageBox.critical(MainWindow, app.tr('Critical'), u''.format(msg))

# limit instances to one
lock = '/run/lock/customizer'
Expand All @@ -65,7 +66,6 @@ else:

if int(sys.version_info[0]) >= 3:
msg_critical(app.tr('You are attempting to run Customizer with Python 3.'))
sys.exit()

class Thread(QtCore.QThread):
''' Worker thread '''
Expand Down Expand Up @@ -153,18 +153,21 @@ def setup_gui():
ui.extraCustomizationBox.setEnabled(False)

def run_core(args, terminal=True):
message.sub_debug('GUI Running', 'customizer ' + args)
if terminal:
terminal = None
for term in ('xterm', 'lxterminal', 'xfce4-terminal', 'terminal',
'konsole', 'mate-terminal', 'gnome-terminal', 'terminator', \
'sakura', 'urxterm', 'aterm', 'Eterm'):
'sakura', 'urxterm', 'aterm', 'Eterm', 'x-terminal-emulator'):
spath = misc.whereis(term)
if spath:
terminal = spath
message.sub_debug('Found terminal', terminal)

if not terminal:
msg_critical(app.tr('No supported terminal emulator detected.'))
return
message.sub_debug('Selected last found terminal', terminal)

try:
if terminal and terminal.endswith('konsole'):
Expand All @@ -188,14 +191,15 @@ def change_value(sec, var, val):
conf = open('/etc/customizer.conf', 'w')
if not config.conf.has_section(sec):
config.conf.add_section(sec)
config.conf.set(sec, var, val.encode('utf-8'))
config.conf.set(sec, var, u'{}'.format(val))
message.debug('Writing Configuration file', '/etc/customizer.conf')
config.conf.write(conf)
except Exception as detail:
msg_critical(detail)
finally:
if conf:
conf.close()
reload(config)
imp.reload(config)

def worker_started():
ui.progressBar.setRange(0, 0)
Expand Down Expand Up @@ -229,27 +233,31 @@ def run_extract():
config.ISO, app.tr('ISO Files (*.iso);;All Files (*)'))
if not sfile:
return
sfile = unicode(sfile)
sfile = u'{}'.format(sfile)
change_value('saved', 'iso', sfile)
extract.config.ISO = sfile
message.sub_debug('Extracting ISO', sfile)
try:
worker(extract.main)
except Exception as detail:
msg_critical(detail)

def run_rebuild():
message.sub_debug('Starting Rebuild...')
try:
worker(rebuild.main)
except Exception as detail:
msg_critical(detail)

def run_clean():
message.sub_debug('Starting Cleaning...')
try:
worker(clean.main)
except Exception as detail:
msg_critical(detail)

def edit_sources():
message.sub_debug('Trying to edit sources...')
editor = None
for edit in ('mousepad', 'leafpad', 'pluma', 'gedit', 'kate', 'kwrite', \
'medit', 'nedit', 'geany', 'bluefish', 'ultraedit'):
Expand All @@ -275,14 +283,16 @@ def run_deb():
config.DEB, app.tr('Deb Files (*.deb);;All Files (*)'))
if not sfile:
return
sfile = unicode(sfile)
sfile = u'{}'.format(sfile)
change_value('saved', 'deb', sfile)
deb.config.DEB = sfile
message.sub_debug('Installing DEB package', sfile)
worker(deb.main)
except Exception as detail:
msg_critical(detail)

def run_pkgm():
message.sub_debug('Starting Graphical Package Manager...')
run_core('-p')

def run_hook():
Expand All @@ -291,31 +301,36 @@ def run_hook():
config.HOOK, app.tr('Shell Scripts (*.sh);;All Files (*)'))
if not sfile:
return
sfile = unicode(sfile)
sfile = u'{}'.format(sfile)
change_value('saved', 'hook', sfile)
hook.config.HOOK = sfile
message.sub_debug('Attempting to run hook', sfile)
worker(hook.main)
except Exception as detail:
msg_critical(detail)

def run_chroot():
message.sub_debug('Opening terminal...')
run_core('-c')

def run_xnest():
message.sub_debug('Opening Nested GUI Desktop...')
try:
worker(xnest.main)
except Exception as detail:
msg_critical(detail)

def run_qemu():
message.sub_debug('Starting qemu...')
try:
worker(qemu.main)
except Exception as detail:
msg_critical(detail)

def change_user():
message.sub_debug('Attempting to change username...')
try:
value = unicode(ui.userEdit.text())
value = u'{}'.format(ui.userEdit.text())
if not value.strip():
msg_critical(app.tr('Live user can not be empty.'))
ui.userEdit.setText('ubuntu')
Expand All @@ -326,41 +341,48 @@ def change_user():
msg_critical(detail)

def change_hostname():
message.sub_debug('Attempting to change hostname...')
try:
common.set_value(misc.join_paths(config.FILESYSTEM_DIR, \
'etc/casper.conf'), 'export HOST=', unicode(ui.hostnameEdit.text()))
'etc/casper.conf'), 'export HOST=', u'{}'.format(ui.hostnameEdit.text()))
except Exception as detail:
msg_critical(detail)

def change_work_dir():
message.sub_debug('Attempting to change hostname...')
try:
spath = QtGui.QFileDialog.getExistingDirectory(MainWindow, \
'Directory', config.WORK_DIR)
if not spath:
return
spath = unicode(spath)
spath = u'{}'.format(spath)
change_value('preferences', 'work_dir', spath)
ui.workDirEdit.setText(spath)
except Exception as detail:
msg_critical(detail)

def change_locales():
current = unicode(ui.localesBox.currentText())
message.sub_debug('Attempting to change Locales...')
current = u'{}'.format(ui.localesBox.currentText())
change_value('preferences', 'locales', current)

def change_resolution():
current = unicode(ui.resolutionBox.currentText())
message.sub_debug('Attempting to change qemu resolution...')
current = u'{}'.format(ui.resolutionBox.currentText())
change_value('preferences', 'resolution', current)

def change_vram():
current = unicode(ui.vramBox.currentText())
message.sub_debug('Attempting to change qemu VRAM size...')
current = u'{}'.format(ui.vramBox.currentText())
change_value('preferences', 'vram', current)

def change_compression():
current = unicode(ui.compressionBox.currentText())
message.sub_debug('Attempting to change compression type...')
current = u'{}'.format(ui.compressionBox.currentText())
change_value('preferences', 'compression', current)

def browse_dir(sdir):
message.sub_debug('Attempting to open GUI file browser for', sdir)
try:
manager = None
for term in ('dolphin', 'thunar', 'pcmanfm', 'nautilus', 'emelfm', \
Expand Down
26 changes: 16 additions & 10 deletions src/lib/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/python2

import os, ConfigParser
import os
try: # Importing python3 style first.
import configparser as ConfigParser
except: # Fall back to python2.
import ConfigParser

import lib.message as message

Expand All @@ -18,22 +22,24 @@
)

if not os.path.isfile('/etc/customizer.conf'):
message.warning('Configuration file does not exists', '/etc/customizer.conf')
message.warning('Configuration file does not exist', '/etc/customizer.conf')

conf.read('/etc/customizer.conf')
message.info('Read Configuration file', '/etc/customizer.conf')
for section in ('preferences', 'saved'):
if not conf.has_section(section):
conf.add_section(section)

WORK_DIR = conf.get('preferences', 'WORK_DIR')
LOCALES = conf.get('preferences', 'LOCALES')
RESOLUTION = conf.get('preferences', 'RESOLUTION')
COMPRESSION = conf.get('preferences', 'COMPRESSION')
VRAM = conf.get('preferences', 'VRAM')
# Make sure these end up to be strings in both python2 and python3.
WORK_DIR = '{}'.format(conf.get('preferences', 'WORK_DIR'))
LOCALES = '{}'.format(conf.get('preferences', 'LOCALES'))
RESOLUTION = '{}'.format(conf.get('preferences', 'RESOLUTION'))
COMPRESSION = '{}'.format(conf.get('preferences', 'COMPRESSION'))
VRAM = '{}'.format(conf.get('preferences', 'VRAM'))

ISO = conf.get('saved', 'ISO')
DEB = conf.get('saved', 'DEB')
HOOK = conf.get('saved', 'HOOK')
ISO = '{}'.format(conf.get('saved', 'ISO'))
DEB = '{}'.format(conf.get('saved', 'DEB'))
HOOK = '{}'.format(conf.get('saved', 'HOOK'))

MOUNT_DIR = '/media'
FILESYSTEM_DIR = os.path.join(WORK_DIR, 'FileSystem')
Expand Down
Loading