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 README.md
include BUILD.md
include version.txt
include onionshare/index.html
include onionshare/404.html
include onionshare/strings.json
include resources/*
include resources/images/*
include resources/locale/*
include resources/html/*
include install/onionshare.desktop
include install/onionshare.appdata.xml
include install/onionshare80.xpm
include images/*.png
include locale/*.json

View File

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

View File

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

View File

@ -26,51 +26,34 @@ def get_platform():
"""
return platform.system()
def get_onionshare_dir():
def get_resource_path(filename):
"""
Returns the OnionShare directory.
"""
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
"""
# Check if app is "frozen" with pyinstaller
# https://pythonhosted.org/PyInstaller/#run-time-information
if getattr(sys, 'frozen', False):
p = get_platform()
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:
return os.path.join(os.path.dirname(os.path.dirname(__file__)), filename)
def get_html_path(filename):
"""
Returns the path of the html files.
Returns the absolute path of a resource, regardless of whether OnionShare is installed
systemwide, and whether regardless of platform
"""
p = get_platform()
if p == 'Darwin' or p == 'Windows':
prefix = get_pyinstaller_resource_path('html')
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:
prefix = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
return os.path.join(prefix, filename)
# Check if app is "frozen" with pyinstaller
# https://pythonhosted.org/pyinstaller/#run-time-information
if getattr(sys, 'frozen', false):
resources_dir = sys._meipass
else:
resources_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))), 'resources')
return os.path.join(resources_dir, filename)
def get_version():
"""
Returns the version of OnionShare that is running.
"""
p = get_platform()
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()
return open(get_resource_path('version.txt')).read().strip()
def constant_time_compare(val1, val2):

View File

@ -33,12 +33,7 @@ def load_strings(default="en"):
p = helpers.get_platform()
# find locale dir
if p == 'Linux':
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 = ''
locale_dir = helpers.get_resource_path('locale')
# load all translations
translations = {}

View File

@ -153,7 +153,7 @@ def index(slug_candidate):
add_request(REQUEST_LOAD, request.path)
return render_template_string(
open(helpers.get_html_path('index.html')).read(),
open(helpers.get_resource_path('html/index.html')).read(),
slug=slug,
file_info=file_info,
filename=os.path.basename(zip_filename),
@ -261,7 +261,7 @@ def page_not_found(e):
404 error page.
"""
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
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 . import common
class Download(object):
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 . import common
class FileList(QtWidgets.QListWidget):
"""
The list of files and folders in the GUI.
@ -49,7 +47,7 @@ class FileList(QtWidgets.QListWidget):
self.setAlignment(QtCore.Qt.AlignCenter)
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:
self.setText(strings._('gui_drag_and_drop', True))
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
from PyQt5 import QtCore, QtWidgets, QtGui
from . import common
import onionshare
from onionshare import strings, helpers, web
@ -306,7 +304,7 @@ def main():
# create the onionshare 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
if filenames:

View File

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

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

Before

Width:  |  Height:  |  Size: 346 B

After

Width:  |  Height:  |  Size: 346 B

View File

Before

Width:  |  Height:  |  Size: 286 B

After

Width:  |  Height:  |  Size: 286 B

View File

Before

Width:  |  Height:  |  Size: 338 B

After

Width:  |  Height:  |  Size: 338 B

View File

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