Move all resources (locale, images, html, version.txt) into central resources dir, and clean up logic to find absolute paths to resources

This commit is contained in:
Micah Lee 2016-04-12 15:14:02 -07:00
parent 7c18d77fb2
commit e81f809882
34 changed files with 59 additions and 137 deletions

View file

@ -1,12 +1,10 @@
include LICENSE include LICENSE
include README.md include README.md
include BUILD.md include BUILD.md
include version.txt include resources/*
include onionshare/index.html include resources/images/*
include onionshare/404.html include resources/locale/*
include onionshare/strings.json include resources/html/*
include install/onionshare.desktop include install/onionshare.desktop
include install/onionshare.appdata.xml include install/onionshare.appdata.xml
include install/onionshare80.xpm include install/onionshare80.xpm
include images/*.png
include locale/*.json

View file

@ -3,7 +3,7 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
cd $DIR cd $DIR
VERSION=`cat version.txt` VERSION=`cat resources/version.txt`
# clean up from last build # clean up from last build
rm -r deb_dist >/dev/null 2>&1 rm -r deb_dist >/dev/null 2>&1

View file

@ -3,7 +3,7 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
cd $DIR cd $DIR
VERSION=`cat version.txt` VERSION=`cat resources/version.txt`
# clean up from last build # clean up from last build
rm -r build dist >/dev/null 2>&1 rm -r build dist >/dev/null 2>&1

View file

@ -26,51 +26,34 @@ def get_platform():
""" """
return platform.system() return platform.system()
def get_onionshare_dir(): def get_resource_path(filename):
""" """
Returns the OnionShare directory. Returns the absolute path of a resource, regardless of whether OnionShare is installed
""" systemwide, and whether regardless of platform
return os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
def get_pyinstaller_resource_path(filename):
"""
Returns the path a resource file in a frozen PyInstall app
""" """
p = get_platform()
if p == 'Linux':
# OnionShare is installed systemwide in Linux
if len(sys.argv) > 0 and sys.argv[0].startswith('/usr/bin/onionshare'):
resources_dir = os.path.join(sys.prefix, 'share/onionshare')
# Look for resources directory relative to python file
else:
resources_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))), 'resources')
else:
# Check if app is "frozen" with pyinstaller # Check if app is "frozen" with pyinstaller
# https://pythonhosted.org/PyInstaller/#run-time-information # https://pythonhosted.org/pyinstaller/#run-time-information
if getattr(sys, 'frozen', False): if getattr(sys, 'frozen', false):
p = get_platform() resources_dir = sys._meipass
if p == 'Darwin':
return os.path.join(os.path.join(os.path.dirname(sys._MEIPASS), 'Resources'), filename)
elif p == 'Windows':
return os.path.join(sys._MEIPASS, filename)
else: else:
return os.path.join(os.path.dirname(os.path.dirname(__file__)), filename) resources_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))), 'resources')
def get_html_path(filename):
"""
Returns the path of the html files.
"""
p = get_platform()
if p == 'Darwin' or p == 'Windows':
prefix = get_pyinstaller_resource_path('html')
else:
prefix = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
return os.path.join(prefix, filename)
return os.path.join(resources_dir, filename)
def get_version(): def get_version():
""" """
Returns the version of OnionShare that is running. Returns the version of OnionShare that is running.
""" """
p = get_platform() return open(get_resource_path('version.txt')).read().strip()
if p == 'Linux':
version_filename = os.path.join(sys.prefix, 'share/onionshare/version.txt')
elif p == 'Darwin' or p == 'Windows':
version_filename = get_pyinstaller_resource_path('version.txt')
else:
return None
return open(version_filename).read().strip()
def constant_time_compare(val1, val2): def constant_time_compare(val1, val2):

View file

@ -33,12 +33,7 @@ def load_strings(default="en"):
p = helpers.get_platform() p = helpers.get_platform()
# find locale dir # find locale dir
if p == 'Linux': locale_dir = helpers.get_resource_path('locale')
locale_dir = os.path.join(sys.prefix, 'share/onionshare/locale')
elif p == 'Darwin' or p == 'Windows':
locale_dir = helpers.get_pyinstaller_resource_path('locale')
else:
locale_dir = ''
# load all translations # load all translations
translations = {} translations = {}

View file

@ -153,7 +153,7 @@ def index(slug_candidate):
add_request(REQUEST_LOAD, request.path) add_request(REQUEST_LOAD, request.path)
return render_template_string( return render_template_string(
open(helpers.get_html_path('index.html')).read(), open(helpers.get_resource_path('html/index.html')).read(),
slug=slug, slug=slug,
file_info=file_info, file_info=file_info,
filename=os.path.basename(zip_filename), filename=os.path.basename(zip_filename),
@ -261,7 +261,7 @@ def page_not_found(e):
404 error page. 404 error page.
""" """
add_request(REQUEST_OTHER, request.path) add_request(REQUEST_OTHER, request.path)
return render_template_string(open(helpers.get_html_path('404.html')).read()) return render_template_string(open(helpers.get_resource_path('html/404.html')).read())
# shutting down the server only works within the context of flask, so the easiest way to do it is over http # shutting down the server only works within the context of flask, so the easiest way to do it is over http
shutdown_slug = helpers.random_string(16) shutdown_slug = helpers.random_string(16)

View file

@ -1,50 +0,0 @@
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
Copyright (C) 2016 Micah Lee <micah@micahflee.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os, sys, inspect, platform
from onionshare import helpers
def get_onionshare_gui_dir():
"""
Returns the OnionShare gui directory.
"""
p = helpers.get_platform()
if p == 'Darwin':
onionshare_gui_dir = os.path.dirname(__file__)
else:
onionshare_gui_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
return onionshare_gui_dir
onionshare_gui_dir = get_onionshare_gui_dir()
def get_image_path(filename):
"""
Returns the OnionShare image path.
"""
p = helpers.get_platform()
if p == 'Linux':
prefix = os.path.join(sys.prefix, 'share/onionshare/images')
elif p == 'Darwin' or p == 'Windows':
prefix = locale_dir = helpers.get_pyinstaller_resource_path('images')
else:
return None
return os.path.join(prefix, filename)

View file

@ -23,8 +23,6 @@ from PyQt5 import QtCore, QtWidgets
from onionshare import strings, helpers from onionshare import strings, helpers
from . import common
class Download(object): class Download(object):
def __init__(self, download_id, total_bytes): def __init__(self, download_id, total_bytes):

View file

@ -22,8 +22,6 @@ from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings, helpers from onionshare import strings, helpers
from . import common
class FileList(QtWidgets.QListWidget): class FileList(QtWidgets.QListWidget):
""" """
The list of files and folders in the GUI. The list of files and folders in the GUI.
@ -49,7 +47,7 @@ class FileList(QtWidgets.QListWidget):
self.setAlignment(QtCore.Qt.AlignCenter) self.setAlignment(QtCore.Qt.AlignCenter)
if image: if image:
self.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(common.get_image_path('drop_files.png')))) self.setPixmap(QtGui.QPixmap.fromImage(QtGui.QImage(helpers.get_resource_path('images/drop_files.png'))))
else: else:
self.setText(strings._('gui_drag_and_drop', True)) self.setText(strings._('gui_drag_and_drop', True))
self.setStyleSheet('color: #999999;') self.setStyleSheet('color: #999999;')

View file

@ -21,8 +21,6 @@ from __future__ import division
import os, sys, subprocess, inspect, platform, argparse, threading, time, math, inspect, platform import os, sys, subprocess, inspect, platform, argparse, threading, time, math, inspect, platform
from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5 import QtCore, QtWidgets, QtGui
from . import common
import onionshare import onionshare
from onionshare import strings, helpers, web from onionshare import strings, helpers, web
@ -306,7 +304,7 @@ def main():
# create the onionshare icon # create the onionshare icon
global window_icon global window_icon
window_icon = QtGui.QIcon(common.get_image_path('logo.png')) window_icon = QtGui.QIcon(helpers.get_resource_path('images/logo.png'))
# validation # validation
if filenames: if filenames:

View file

@ -21,8 +21,6 @@ from PyQt5 import QtCore, QtWidgets
from onionshare import strings, helpers from onionshare import strings, helpers
from . import common
class Options(QtWidgets.QHBoxLayout): class Options(QtWidgets.QHBoxLayout):
""" """
The extra onionshare options in the GUI. The extra onionshare options in the GUI.

View file

@ -22,8 +22,6 @@ from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings, helpers from onionshare import strings, helpers
from . import common
class ServerStatus(QtWidgets.QVBoxLayout): class ServerStatus(QtWidgets.QVBoxLayout):
""" """
The server status chunk of the GUI. The server status chunk of the GUI.
@ -46,9 +44,9 @@ class ServerStatus(QtWidgets.QVBoxLayout):
self.file_selection = file_selection self.file_selection = file_selection
# server layout # server layout
self.status_image_stopped = QtGui.QImage(common.get_image_path('server_stopped.png')) self.status_image_stopped = QtGui.QImage(helpers.get_resource_path('images/server_stopped.png'))
self.status_image_working = QtGui.QImage(common.get_image_path('server_working.png')) self.status_image_working = QtGui.QImage(helpers.get_resource_path('images/server_working.png'))
self.status_image_started = QtGui.QImage(common.get_image_path('server_started.png')) self.status_image_started = QtGui.QImage(helpers.get_resource_path('images/server_started.png'))
self.status_image_label = QtWidgets.QLabel() self.status_image_label = QtWidgets.QLabel()
self.status_image_label.setFixedWidth(30) self.status_image_label.setFixedWidth(30)
self.server_button = QtWidgets.QPushButton() self.server_button = QtWidgets.QPushButton()

View file

Before

Width:  |  Height:  |  Size: 2 KiB

After

Width:  |  Height:  |  Size: 2 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 346 B

After

Width:  |  Height:  |  Size: 346 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 286 B

After

Width:  |  Height:  |  Size: 286 B

Before After
Before After

View file

Before

Width:  |  Height:  |  Size: 338 B

After

Width:  |  Height:  |  Size: 338 B

Before After
Before After

View file

@ -33,7 +33,7 @@ def file_list(path):
files.append(os.path.join(path, filename)) files.append(os.path.join(path, filename))
return files return files
version = open('version.txt').read().strip() version = open('resources/version.txt').read().strip()
description = ( description = (
"""OnionShare lets you securely and anonymously share a file of any size with someone. """ """OnionShare lets you securely and anonymously share a file of any size with someone. """
@ -48,27 +48,32 @@ long_description = description + " " + (
) )
images = [ images = [
'images/logo.png', 'resources/images/logo.png',
'images/drop_files.png', 'resources/images/drop_files.png',
'images/server_stopped.png', 'resources/images/server_stopped.png',
'images/server_started.png', 'resources/images/server_started.png',
'images/server_working.png' 'resources/images/server_working.png'
] ]
locale = [ locale = [
'locale/cs.json', 'resources/locale/cs.json',
'locale/de.json', 'resources/locale/de.json',
'locale/en.json', 'resources/locale/en.json',
'locale/eo.json', 'resources/locale/eo.json',
'locale/es.json', 'resources/locale/es.json',
'locale/fi.json', 'resources/locale/fi.json',
'locale/fr.json', 'resources/locale/fr.json',
'locale/it.json', 'resources/locale/it.json',
'locale/nl.json', 'resources/locale/nl.json',
'locale/no.json', 'resources/locale/no.json',
'locale/pt.json', 'resources/locale/pt.json',
'locale/ru.json', 'resources/locale/ru.json',
'locale/tr.json' 'resources/locale/tr.json'
]
html = [
'resources/html/index.html',
'resources/html/404.html',
] ]
setup( setup(
@ -88,8 +93,9 @@ setup(
(os.path.join(sys.prefix, 'share/applications'), ['install/onionshare.desktop']), (os.path.join(sys.prefix, 'share/applications'), ['install/onionshare.desktop']),
(os.path.join(sys.prefix, 'share/appdata'), ['install/onionshare.appdata.xml']), (os.path.join(sys.prefix, 'share/appdata'), ['install/onionshare.appdata.xml']),
(os.path.join(sys.prefix, 'share/pixmaps'), ['install/onionshare80.xpm']), (os.path.join(sys.prefix, 'share/pixmaps'), ['install/onionshare80.xpm']),
(os.path.join(sys.prefix, 'share/onionshare'), ['version.txt']), (os.path.join(sys.prefix, 'share/onionshare'), ['resources/version.txt']),
(os.path.join(sys.prefix, 'share/onionshare/images'), images), (os.path.join(sys.prefix, 'share/onionshare/images'), images),
(os.path.join(sys.prefix, 'share/onionshare/locale'), locale) (os.path.join(sys.prefix, 'share/onionshare/locale'), locale),
(os.path.join(sys.prefix, 'share/onionshare/html'), html)
] ]
) )