Skip to content

Implement blacklisting and whitelisting #176

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

Merged
merged 3 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import utils
from utils import zip_files, join_files
from utils import get_data_path, get_data_file_path
from util_classes import Setting
from util_classes import Setting, FileTree

from image_utils.pycns import save_icns
from pepy.pe import PEFile
Expand All @@ -69,6 +69,8 @@ def __init__(self, quiet=False):
self.readonly = True
self.update_json = True

self.file_tree = FileTree()

def init(self):
self.logger = config.logger
self.update_nw_versions(None)
Expand All @@ -89,7 +91,7 @@ def setup_nw_versions(self):
for line in f:
nw_version.values.append(line.strip())
except IOError:
nw_version.values.append(nw_version.default_value)
pass

def get_nw_versions(self):
"""Get the already downloaded nw versions from the settings"""
Expand Down Expand Up @@ -320,6 +322,7 @@ def get_versions(self):
nw_version = self.get_setting('nw_version')

old_versions = set(nw_version.values)

old_versions = old_versions.union(union_versions)
new_versions = set(re.findall(regex, html))

Expand Down Expand Up @@ -375,7 +378,7 @@ def download_file_with_error_handling(self):
if os.path.exists(setting.save_file_path(version, location)):
os.remove(setting.save_file_path(version, location))

exc_format = utils.format_exc_info(sys.exc_info)
exc_format = utils.format_exc_info(sys.exc_info())
self.show_error(exc_format)
self.enable_ui_after_error()

Expand Down Expand Up @@ -758,8 +761,12 @@ def replace_plist(self, app_path):
plist_dict['CFBundleDisplayName'] = self.project_name()
plist_dict['CFBundleName'] = self.project_name()
version_setting = self.get_setting('version')
plist_dict['CFBundleShortVersionString'] = version_setting.value
plist_dict['CFBundleVersion'] = version_setting.value
if version_setting.value is not None:
plist_dict['CFBundleShortVersionString'] = version_setting.value
plist_dict['CFBundleVersion'] = version_setting.value
else:
plist_dict['CFBundleShortVersionString'] = '0.0.0'
plist_dict['CFBundleVersion'] = '0.0.0'

plistlib.writePlist(plist_dict, plist_path)

Expand Down Expand Up @@ -870,6 +877,13 @@ def process_export_setting(self, ex_setting, output_name):
self.process_win_linux_setting(app_loc, output_dir,
ex_setting)

@property
def used_project_files(self):
return self.file_tree.files

@property
def used_project_dirs(self):
return self.file_tree.dirs

def make_output_dirs(self, write_json=True):
"""Create the output directories for the application to be copied"""
Expand All @@ -878,6 +892,17 @@ def make_output_dirs(self, write_json=True):

self.progress_text = 'Making new directories...\n'


whitelist_setting = self.get_setting('whitelist')
blacklist_setting = self.get_setting('blacklist')

output_blacklist = os.path.basename(self.output_dir())

self.file_tree.init(self.project_dir(),
blacklist=(blacklist_setting.value.split(',') +
['*'+output_blacklist+'*']),
whitelist=whitelist_setting.value.split(','))

self.copy_files_to_project_folder()

if write_json:
Expand Down Expand Up @@ -914,15 +939,28 @@ def get_app_nw_loc(self, temp_dir, output_dir):
"""Copy the temporary app to the output_dir"""
app_file = utils.path_join(temp_dir, self.project_name()+'.nw')

proj_dir = self.project_dir()

if self.uncompressed:
app_nw_folder = utils.path_join(temp_dir,
self.project_name()+'.nwf')
for dir in self.used_project_dirs:
if not os.path.exists(dir):
os.makedirs(dir)

for file in self.used_project_files:
src = utils.path_join(proj_dir, file)
dest = utils.path_join(app_nw_folder, file)

base, _ = os.path.split(dest)

if not os.path.exists(base):
os.makedirs(base)

utils.copytree(self.project_dir(), app_nw_folder,
ignore=shutil.ignore_patterns(output_dir))
utils.copy(src, dest)
return app_nw_folder
else:
zip_files(app_file, self.project_dir(), exclude_paths=[output_dir])
zip_files(app_file, proj_dir, *self.used_project_files)
return app_file

def get_version_tuple(self):
Expand Down Expand Up @@ -1455,7 +1493,6 @@ class ArgParser(argparse.ArgumentParser):
"""Custom argparser that prints help if there is an error"""
def error(self, message):
sys.stderr.write('error: {}\n'.format(message))
self.print_help()
sys.exit(2)

def get_arguments(command_base):
Expand Down Expand Up @@ -1520,8 +1557,9 @@ def generate_setting_args(command_base, parser):
if setting_name == 'name':
kwargs.update({'default': command_base.project_name})
else:
kwargs.update({'required': setting.required,
'default': setting.default_value})
kwargs.update({'required': setting.required})
if setting.default_value is not None:
kwargs.update({'default': setting.default_value})
action = 'store'
option_name = setting_name.replace('_', '-')

Expand Down
11 changes: 11 additions & 0 deletions files/settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ linux_64_dir_prefix = 'nwjs-v{}-linux-x64'
default_value=''
type='string'
description='Type "%(" to see a list of options to reference. Name your output folder.\n Include slashes to make sub-directories.'
[[[blacklist]]]
display_name='Blacklist'
default_value=''
type='string'
description='Glob-style blacklist files/directories. Each line is a new pattern. Ex: *.jpeg, .git, *file[s].txt'
[[[whitelist]]]
display_name='Whitelist'
default_value=''
type='string'
description='Glob-style whitelist files/directories. Each line is a new pattern. Ex: *.jpeg, .git, *file[s].txt.\nWhitelist trumps blacklist.'

[[window_settings]]
[[[id]]]
Expand Down Expand Up @@ -238,6 +248,7 @@ linux_64_dir_prefix = 'nwjs-v{}-linux-x64'
[[download_settings]]
[[[nw_version]]]
display_name='NW.js version'
required=True
default_value=None
values=[]
type='list'
Expand Down
135 changes: 118 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from util_classes import ExistingProjectDialog
from util_classes import BackgroundThread, Validator
from util_classes import CompleterLineEdit, TagsCompleter
from util_classes import TreeBrowser

from PySide import QtGui, QtCore
from PySide.QtGui import (QApplication, QHBoxLayout, QVBoxLayout)
Expand Down Expand Up @@ -209,18 +210,6 @@ def open_recent_file(self):
if action:
self.load_project(action.data())

def setup_nw_versions(self):
"""Loads stored versions that were previously retrieved."""
nw_version = self.get_setting('nw_version')
try:
f = codecs.open(utils.get_data_file_path(config.VER_FILE),
encoding='utf-8')
for line in f:
nw_version.values.append(line.strip())
f.close()
except IOError:
nw_version.values.append(nw_version.default_value)

def create_application_layout(self):
"""Create all widgets and set the central widget."""
self.main_layout = QtGui.QVBoxLayout()
Expand Down Expand Up @@ -989,6 +978,15 @@ def load_project(self, directory, readonly=False):

self.set_window_icon()
self.open_export_button.setEnabled(True)

blacklist_setting = self.get_setting('blacklist')

output_blacklist = os.path.basename(self.output_dir())

self.tree_browser.init(directory,
blacklist=(blacklist_setting.value.split('\n') +
['*'+output_blacklist+'*']))

self.update_json = True

def init_main_field(self, directory):
Expand Down Expand Up @@ -1159,23 +1157,116 @@ def create_export_settings(self):

ex_setting_order = self.settings['order']['export_setting_order']

vlayout = self.create_layout(ex_setting_order, cols=4)
vlayout = self.create_layout(ex_setting_order, cols=1)
vlayout.setContentsMargins(0, 10, 0, 0)

output_name_layout = self.create_output_name_pattern_line()

output_layout = self.create_output_directory_line()

script_layout = self.create_script_layout()

hlayout = QtGui.QHBoxLayout()

platform_group = QtGui.QGroupBox('Platforms')
platform_group.setContentsMargins(0, 10, 0, 0)
playout = QtGui.QVBoxLayout()
playout.addLayout(vlayout)
platform_group.setLayout(playout)

hlayout.addWidget(platform_group)

tree_layout = self.create_blacklist_layout(hlayout)
tree_layout.setContentsMargins(0, 10, 0, 0)

vbox = QtGui.QVBoxLayout()
vbox.addLayout(vlayout)
vbox.addLayout(hlayout)
vbox.addLayout(output_name_layout)
vbox.addLayout(output_layout)
vbox.addLayout(script_layout)

group_box.setLayout(vbox)
return group_box

def create_blacklist_layout(self, blacklist_layout):

self.tree_browser = TreeBrowser()
self.tree_browser.setContentsMargins(0, 0, 0, 0)

self.blacklist_text = QtGui.QPlainTextEdit()
self.whitelist_text = QtGui.QPlainTextEdit()

hlayout = QtGui.QHBoxLayout()

blacklayout = QtGui.QVBoxLayout()
whitelayout = QtGui.QHBoxLayout()

blacklayout.addWidget(self.blacklist_text)
whitelayout.addWidget(self.whitelist_text)

whitelist_setting = self.get_setting('whitelist')
blacklist_setting = self.get_setting('blacklist')

self.blacklist_text.setStatusTip(blacklist_setting.description)
self.whitelist_text.setStatusTip(whitelist_setting.description)

self.blacklist_text.setObjectName(blacklist_setting.name)
self.whitelist_text.setObjectName(whitelist_setting.name)

blackgroup = QtGui.QGroupBox(blacklist_setting.display_name)
whitegroup = QtGui.QGroupBox(whitelist_setting.display_name)

blackgroup.setLayout(blacklayout)
whitegroup.setLayout(whitelayout)

blacklist_layout.addWidget(blackgroup)
blacklist_layout.addWidget(whitegroup)
blacklist_layout.addWidget(self.tree_browser)

self.blacklist_text.textChanged.connect(
self.call_with_object('setting_changed',
self.blacklist_text,
blacklist_setting)
)

self.whitelist_text.textChanged.connect(
self.call_with_object('setting_changed',
self.whitelist_text,
whitelist_setting)
)

self.blacklist_text.textChanged.connect(
self.call_with_object('blacklist_changed',
self.blacklist_text,
blacklist_setting)
)

self.whitelist_text.textChanged.connect(
self.call_with_object('whitelist_changed',
self.whitelist_text,
whitelist_setting)
)

return blacklist_layout

def blacklist_changed(self, text, blacklist_setting):
new_val = text.toPlainText()
output_blacklist = os.path.basename(self.output_dir())
self.tree_browser.refresh(blacklist=(new_val.split('\n') +
['*'+output_blacklist+'*']))

def whitelist_changed(self, text, whitelist_setting):
new_val = text.toPlainText()
self.tree_browser.refresh(whitelist=new_val.split('\n'))

@property
def used_project_files(self):
return self.tree_browser.files

@property
def used_project_dirs(self):
return self.tree_browser.dirs

def create_output_name_pattern_line(self):
output_name_layout = QtGui.QHBoxLayout()

Expand Down Expand Up @@ -1415,7 +1506,10 @@ def reset_settings(self):
old_val = setting.default_value

setting.value = old_val.replace('\\', '\\\\')
widget.setText(old_val)
if hasattr(widget, 'setText'):
widget.setText(old_val)
elif hasattr(widget, 'setPlainText'):
widget.setPlainText(old_val)
elif setting.type == 'strings':
old_val = []
if setting.default_value is not None:
Expand Down Expand Up @@ -1477,7 +1571,11 @@ def setting_changed(self, obj, setting, *args):
setting.type == 'file' or
setting.type == 'folder' or
setting.type == 'int'):
setting.value = args[0]
if args:
setting.value = args[0]
else:
setting.value = obj.toPlainText()

if not setting.value:
setting.value = setting.default_value
elif setting.type == 'strings':
Expand Down Expand Up @@ -1629,7 +1727,10 @@ def load_package_json(self, json_path=None):
setting.type == 'folder' or
setting.type == 'int'):
val_str = self.convert_val_to_str(setting.value)
setting_field.setText(setting.filter_name(val_str))
if hasattr(setting_field, 'setText'):
setting_field.setText(setting.filter_name(val_str))
elif hasattr(setting_field, 'setPlainText'):
setting_field.setPlainText(setting.filter_name(val_str))
if setting.type == 'strings':
vals = [self.convert_val_to_str(v) for v in setting.value]
setting_field.setText(','.join(vals))
Expand Down
Loading