Format all code using black

This commit is contained in:
Micah Lee 2019-10-12 21:01:25 -07:00
parent 90c244ee2f
commit 3037727890
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
87 changed files with 4293 additions and 2374 deletions

View file

@ -33,12 +33,25 @@ import fileinput, argparse, re, os, codecs, json, sys
def arg_parser():
desc = __doc__.strip().splitlines()[0]
p = argparse.ArgumentParser(description=desc)
p.add_argument('-d', default='.', help='onionshare directory',
metavar='ONIONSHARE_DIR', dest='onionshare_dir')
p.add_argument('--show-all-keys', action='store_true',
help='show translation key in source and exit'),
p.add_argument('-l', default='all', help='language code (default: all)',
metavar='LANG_CODE', dest='lang_code')
p.add_argument(
"-d",
default=".",
help="onionshare directory",
metavar="ONIONSHARE_DIR",
dest="onionshare_dir",
)
p.add_argument(
"--show-all-keys",
action="store_true",
help="show translation key in source and exit",
),
p.add_argument(
"-l",
default="all",
help="language code (default: all)",
metavar="LANG_CODE",
dest="lang_code",
)
return p
@ -54,27 +67,29 @@ def main():
dir = args.onionshare_dir
src = files_in(dir, 'onionshare') + \
files_in(dir, 'onionshare_gui') + \
files_in(dir, 'onionshare_gui/mode') + \
files_in(dir, 'onionshare_gui/mode/share_mode') + \
files_in(dir, 'onionshare_gui/mode/receive_mode') + \
files_in(dir, 'onionshare_gui/mode/website_mode') + \
files_in(dir, 'install/scripts') + \
files_in(dir, 'tests')
pysrc = [p for p in src if p.endswith('.py')]
src = (
files_in(dir, "onionshare")
+ files_in(dir, "onionshare_gui")
+ files_in(dir, "onionshare_gui/mode")
+ files_in(dir, "onionshare_gui/mode/share_mode")
+ files_in(dir, "onionshare_gui/mode/receive_mode")
+ files_in(dir, "onionshare_gui/mode/website_mode")
+ files_in(dir, "install/scripts")
+ files_in(dir, "tests")
)
pysrc = [p for p in src if p.endswith(".py")]
lang_code = args.lang_code
translate_keys = set()
# load translate key from python source
for line in fileinput.input(pysrc, openhook=fileinput.hook_encoded('utf-8')):
for line in fileinput.input(pysrc, openhook=fileinput.hook_encoded("utf-8")):
# search `strings._('translate_key')`
# `strings._('translate_key', True)`
m = re.findall(r'strings\._\((.*?)\)', line)
m = re.findall(r"strings\._\((.*?)\)", line)
if m:
for match in m:
key = match.split(',')[0].strip('''"' ''')
key = match.split(",")[0].strip(""""' """)
translate_keys.add(key)
if args.show_all_keys:
@ -82,12 +97,16 @@ def main():
print(k)
sys.exit()
if lang_code == 'all':
locale_files = [f for f in files_in(dir, 'share/locale') if f.endswith('.json')]
if lang_code == "all":
locale_files = [f for f in files_in(dir, "share/locale") if f.endswith(".json")]
else:
locale_files = [f for f in files_in(dir, 'share/locale') if f.endswith('%s.json' % lang_code)]
locale_files = [
f
for f in files_in(dir, "share/locale")
if f.endswith("%s.json" % lang_code)
]
for locale_file in locale_files:
with codecs.open(locale_file, 'r', encoding='utf-8') as f:
with codecs.open(locale_file, "r", encoding="utf-8") as f:
trans = json.load(f)
# trans -> {"key1": "translate-text1", "key2": "translate-text2", ...}
locale_keys = set(trans.keys())
@ -97,11 +116,11 @@ def main():
locale, ext = os.path.splitext(os.path.basename(locale_file))
for k in sorted(disused):
print(locale, 'disused', k)
print(locale, "disused", k)
for k in sorted(lacked):
print(locale, 'lacked', k)
print(locale, "lacked", k)
if __name__ == '__main__':
if __name__ == "__main__":
main()

View file

@ -34,17 +34,24 @@ import shutil
import subprocess
import requests
def main():
dmg_url = 'https://archive.torproject.org/tor-package-archive/torbrowser/8.5.5/TorBrowser-8.5.5-osx64_en-US.dmg'
dmg_filename = 'TorBrowser-8.5.5-osx64_en-US.dmg'
expected_dmg_sha256 = '9c1b7840bd251a4c52f0c919991e57cafb9178c55e11fa49f83ffacce3c20511'
dmg_url = "https://archive.torproject.org/tor-package-archive/torbrowser/8.5.5/TorBrowser-8.5.5-osx64_en-US.dmg"
dmg_filename = "TorBrowser-8.5.5-osx64_en-US.dmg"
expected_dmg_sha256 = (
"9c1b7840bd251a4c52f0c919991e57cafb9178c55e11fa49f83ffacce3c20511"
)
# Build paths
root_path = os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
working_path = os.path.join(root_path, 'build', 'tor')
dmg_tor_path = os.path.join('/Volumes', 'Tor Browser', 'Tor Browser.app', 'Contents')
root_path = os.path.dirname(
os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
)
working_path = os.path.join(root_path, "build", "tor")
dmg_tor_path = os.path.join(
"/Volumes", "Tor Browser", "Tor Browser.app", "Contents"
)
dmg_path = os.path.join(working_path, dmg_filename)
dist_path = os.path.join(root_path, 'dist', 'OnionShare.app', 'Contents')
dist_path = os.path.join(root_path, "dist", "OnionShare.app", "Contents")
# Make sure the working folder exists
if not os.path.exists(working_path):
@ -54,10 +61,10 @@ def main():
if not os.path.exists(dmg_path):
print("Downloading {}".format(dmg_url))
r = requests.get(dmg_url)
open(dmg_path, 'wb').write(r.content)
open(dmg_path, "wb").write(r.content)
dmg_sha256 = hashlib.sha256(r.content).hexdigest()
else:
dmg_data = open(dmg_path, 'rb').read()
dmg_data = open(dmg_path, "rb").read()
dmg_sha256 = hashlib.sha256(dmg_data).hexdigest()
# Compare the hash
@ -68,34 +75,52 @@ def main():
sys.exit(-1)
# Mount the dmg, copy data to the working path
subprocess.call(['hdiutil', 'attach', dmg_path])
subprocess.call(["hdiutil", "attach", dmg_path])
# Make sure Resources/tor exists before copying files
if os.path.exists(os.path.join(dist_path, 'Resources', 'Tor')):
shutil.rmtree(os.path.join(dist_path, 'Resources', 'Tor'))
os.makedirs(os.path.join(dist_path, 'Resources', 'Tor'))
if os.path.exists(os.path.join(dist_path, 'MacOS', 'Tor')):
shutil.rmtree(os.path.join(dist_path, 'MacOS', 'Tor'))
os.makedirs(os.path.join(dist_path, 'MacOS', 'Tor'))
if os.path.exists(os.path.join(dist_path, "Resources", "Tor")):
shutil.rmtree(os.path.join(dist_path, "Resources", "Tor"))
os.makedirs(os.path.join(dist_path, "Resources", "Tor"))
if os.path.exists(os.path.join(dist_path, "MacOS", "Tor")):
shutil.rmtree(os.path.join(dist_path, "MacOS", "Tor"))
os.makedirs(os.path.join(dist_path, "MacOS", "Tor"))
# Modify the tor script to adjust the path
tor_script = open(os.path.join(dmg_tor_path, 'Resources', 'TorBrowser', 'Tor', 'tor'), 'r').read()
tor_script = tor_script.replace('../../../MacOS/Tor', '../../MacOS/Tor')
open(os.path.join(dist_path, 'Resources', 'Tor', 'tor'), 'w').write(tor_script)
tor_script = open(
os.path.join(dmg_tor_path, "Resources", "TorBrowser", "Tor", "tor"), "r"
).read()
tor_script = tor_script.replace("../../../MacOS/Tor", "../../MacOS/Tor")
open(os.path.join(dist_path, "Resources", "Tor", "tor"), "w").write(tor_script)
# Copy into dist
shutil.copyfile(os.path.join(dmg_tor_path, 'Resources', 'TorBrowser', 'Tor', 'geoip'), os.path.join(dist_path, 'Resources', 'Tor', 'geoip'))
shutil.copyfile(os.path.join(dmg_tor_path, 'Resources', 'TorBrowser', 'Tor', 'geoip6'), os.path.join(dist_path, 'Resources', 'Tor', 'geoip6'))
os.chmod(os.path.join(dist_path, 'Resources', 'Tor', 'tor'), 0o755)
shutil.copyfile(os.path.join(dmg_tor_path, 'MacOS', 'Tor', 'tor.real'), os.path.join(dist_path, 'MacOS', 'Tor', 'tor.real'))
shutil.copyfile(os.path.join(dmg_tor_path, 'MacOS', 'Tor', 'libevent-2.1.6.dylib'), os.path.join(dist_path, 'MacOS', 'Tor', 'libevent-2.1.6.dylib'))
os.chmod(os.path.join(dist_path, 'MacOS', 'Tor', 'tor.real'), 0o755)
shutil.copyfile(
os.path.join(dmg_tor_path, "Resources", "TorBrowser", "Tor", "geoip"),
os.path.join(dist_path, "Resources", "Tor", "geoip"),
)
shutil.copyfile(
os.path.join(dmg_tor_path, "Resources", "TorBrowser", "Tor", "geoip6"),
os.path.join(dist_path, "Resources", "Tor", "geoip6"),
)
os.chmod(os.path.join(dist_path, "Resources", "Tor", "tor"), 0o755)
shutil.copyfile(
os.path.join(dmg_tor_path, "MacOS", "Tor", "tor.real"),
os.path.join(dist_path, "MacOS", "Tor", "tor.real"),
)
shutil.copyfile(
os.path.join(dmg_tor_path, "MacOS", "Tor", "libevent-2.1.6.dylib"),
os.path.join(dist_path, "MacOS", "Tor", "libevent-2.1.6.dylib"),
)
os.chmod(os.path.join(dist_path, "MacOS", "Tor", "tor.real"), 0o755)
# obfs4proxy binary
shutil.copyfile(os.path.join(dmg_tor_path, 'MacOS', 'Tor', 'PluggableTransports', 'obfs4proxy'), os.path.join(dist_path, 'Resources', 'Tor', 'obfs4proxy'))
os.chmod(os.path.join(dist_path, 'Resources', 'Tor', 'obfs4proxy'), 0o755)
shutil.copyfile(
os.path.join(dmg_tor_path, "MacOS", "Tor", "PluggableTransports", "obfs4proxy"),
os.path.join(dist_path, "Resources", "Tor", "obfs4proxy"),
)
os.chmod(os.path.join(dist_path, "Resources", "Tor", "obfs4proxy"), 0o755)
# Eject dmg
subprocess.call(['diskutil', 'eject', '/Volumes/Tor Browser'])
subprocess.call(["diskutil", "eject", "/Volumes/Tor Browser"])
if __name__ == '__main__':
if __name__ == "__main__":
main()

View file

@ -32,15 +32,22 @@ import shutil
import subprocess
import requests
def main():
exe_url = 'https://archive.torproject.org/tor-package-archive/torbrowser/8.5.5/torbrowser-install-8.5.5_en-US.exe'
exe_filename = 'torbrowser-install-8.5.5_en-US.exe'
expected_exe_sha256 = 'a3aa7e626d1d2365dcecc6f17055f467f31c4ff9558a769e51d4b90640e48bb0'
exe_url = "https://archive.torproject.org/tor-package-archive/torbrowser/8.5.5/torbrowser-install-8.5.5_en-US.exe"
exe_filename = "torbrowser-install-8.5.5_en-US.exe"
expected_exe_sha256 = (
"a3aa7e626d1d2365dcecc6f17055f467f31c4ff9558a769e51d4b90640e48bb0"
)
# Build paths
root_path = os.path.dirname(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
working_path = os.path.join(os.path.join(root_path, 'build'), 'tor')
root_path = os.path.dirname(
os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
)
working_path = os.path.join(os.path.join(root_path, "build"), "tor")
exe_path = os.path.join(working_path, exe_filename)
dist_path = os.path.join(os.path.join(os.path.join(root_path, 'dist'), 'onionshare'), 'tor')
dist_path = os.path.join(
os.path.join(os.path.join(root_path, "dist"), "onionshare"), "tor"
)
# Make sure the working folder exists
if not os.path.exists(working_path):
@ -50,10 +57,10 @@ def main():
if not os.path.exists(exe_path):
print("Downloading {}".format(exe_url))
r = requests.get(exe_url)
open(exe_path, 'wb').write(r.content)
open(exe_path, "wb").write(r.content)
exe_sha256 = hashlib.sha256(r.content).hexdigest()
else:
exe_data = open(exe_path, 'rb').read()
exe_data = open(exe_path, "rb").read()
exe_sha256 = hashlib.sha256(exe_data).hexdigest()
# Compare the hash
@ -64,8 +71,22 @@ def main():
sys.exit(-1)
# Extract the bits we need from the exe
cmd = ['7z', 'e', '-y', exe_path, 'Browser\TorBrowser\Tor', '-o%s' % os.path.join(working_path, 'Tor')]
cmd2 = ['7z', 'e', '-y', exe_path, 'Browser\TorBrowser\Data\Tor\geoip*', '-o%s' % os.path.join(working_path, 'Data')]
cmd = [
"7z",
"e",
"-y",
exe_path,
"Browser\TorBrowser\Tor",
"-o%s" % os.path.join(working_path, "Tor"),
]
cmd2 = [
"7z",
"e",
"-y",
exe_path,
"Browser\TorBrowser\Data\Tor\geoip*",
"-o%s" % os.path.join(working_path, "Data"),
]
subprocess.Popen(cmd).wait()
subprocess.Popen(cmd2).wait()
@ -73,8 +94,11 @@ def main():
if os.path.exists(dist_path):
shutil.rmtree(dist_path)
os.makedirs(dist_path)
shutil.copytree( os.path.join(working_path, 'Tor'), os.path.join(dist_path, 'Tor') )
shutil.copytree( os.path.join(working_path, 'Data'), os.path.join(dist_path, 'Data', 'Tor') )
shutil.copytree(os.path.join(working_path, "Tor"), os.path.join(dist_path, "Tor"))
shutil.copytree(
os.path.join(working_path, "Data"), os.path.join(dist_path, "Data", "Tor")
)
if __name__ == '__main__':
if __name__ == "__main__":
main()

View file

@ -5,7 +5,8 @@ import locale
import subprocess
import urllib
import gi
gi.require_version('Nautilus', '3.0')
gi.require_version("Nautilus", "3.0")
from gi.repository import Nautilus
from gi.repository import GObject
@ -15,12 +16,12 @@ class OnionShareExtension(GObject.GObject, Nautilus.MenuProvider):
def __init__(self):
# Get the localized string for "Share via OnionShare" label
self.label = None
default_label = 'Share via OnionShare'
default_label = "Share via OnionShare"
try:
# Re-implement localization in python2
default_locale = 'en'
locale_dir = os.path.join(sys.prefix, 'share/onionshare/locale')
default_locale = "en"
locale_dir = os.path.join(sys.prefix, "share/onionshare/locale")
if os.path.exists(locale_dir):
# Load all translations
strings = {}
@ -28,7 +29,7 @@ class OnionShareExtension(GObject.GObject, Nautilus.MenuProvider):
for filename in os.listdir(locale_dir):
abs_filename = os.path.join(locale_dir, filename)
lang, ext = os.path.splitext(filename)
if ext == '.json':
if ext == ".json":
with open(abs_filename) as f:
translations[lang] = json.load(f)
@ -42,7 +43,7 @@ class OnionShareExtension(GObject.GObject, Nautilus.MenuProvider):
if key in translations[lang]:
strings[key] = translations[lang][key]
self.label = strings['share_via_onionshare']
self.label = strings["share_via_onionshare"]
except:
self.label = default_label
@ -63,30 +64,29 @@ class OnionShareExtension(GObject.GObject, Nautilus.MenuProvider):
self.label = 'Share via OnionShare'
"""
def url2path(self,url):
def url2path(self, url):
file_uri = url.get_activation_uri()
arg_uri = file_uri[7:]
path = urllib.url2pathname(arg_uri)
return path
def exec_onionshare(self, filenames):
# Would prefer this method but there is a conflict between GTK 2.0 vs GTK 3.0 components being loaded at once
# (nautilus:3090): Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
# sys.argv = ["", "--filenames"] + filenames
# sys.exit(onionshare_gui.main())
path = os.path.join(os.sep, 'usr', 'bin', 'onionshare-gui')
# Would prefer this method but there is a conflict between GTK 2.0 vs GTK 3.0 components being loaded at once
# (nautilus:3090): Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
# sys.argv = ["", "--filenames"] + filenames
# sys.exit(onionshare_gui.main())
path = os.path.join(os.sep, "usr", "bin", "onionshare-gui")
cmd = [path, "--filenames"] + filenames
subprocess.Popen(cmd)
def get_file_items(self, window, files):
menuitem = Nautilus.MenuItem(name='OnionShare::Nautilus',
label=self.label,
tip='',
icon='')
menuitem = Nautilus.MenuItem(
name="OnionShare::Nautilus", label=self.label, tip="", icon=""
)
menu = Nautilus.Menu()
menu.append_item(menuitem)
menuitem.connect("activate", self.menu_activate_cb, files)
return menuitem,
return (menuitem,)
def menu_activate_cb(self, menu, files):
file_list = []