From a08bdbeb4ef0e3c4705505b5ce27b112b36c93b8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 15 May 2019 16:42:52 +0530 Subject: [PATCH] Dont depend on calibre in polyglot --- manual/build.py | 2 +- recipes/apple_daily.recipe | 2 +- recipes/ming_pao.recipe | 2 +- recipes/ming_pao_toronto.recipe | 2 +- recipes/ming_pao_vancouver.recipe | 2 +- recipes/singtaohk.recipe | 2 +- setup/__init__.py | 2 -- setup/build.py | 2 +- setup/publish.py | 4 ++-- src/calibre/__init__.py | 14 ++++---------- src/calibre/constants.py | 9 ++------- src/calibre/db/cli/cmd_set_metadata.py | 4 ++-- src/calibre/db/cli/cmd_show_metadata.py | 5 ++--- src/calibre/debug.py | 4 ++-- src/calibre/ebooks/docx/to_html.py | 6 +++--- src/calibre/utils/localunzip.py | 6 +++--- src/calibre/utils/matcher.py | 6 +++--- src/calibre/utils/rapydscript.py | 4 ++-- src/calibre/utils/serve_coffee.py | 4 ++-- src/calibre/utils/test_lock.py | 6 +++--- src/calibre/utils/zipfile.py | 6 +++--- src/calibre/web/feeds/news.py | 6 +++--- src/polyglot/builtins.py | 12 ++++++------ 23 files changed, 49 insertions(+), 63 deletions(-) diff --git a/manual/build.py b/manual/build.py index f43ff901ba..37c868a809 100755 --- a/manual/build.py +++ b/manual/build.py @@ -45,7 +45,7 @@ def build_manual(language, base): onlinedir = sb(t='online') epubdir = sb('myepub', 'epub') latexdir = sb('mylatex', 'latex') - pwd = os.getcwdu() + pwd = os.getcwd() os.chdir(latexdir) def run_cmd(cmd): diff --git a/recipes/apple_daily.recipe b/recipes/apple_daily.recipe index eaf284ab0e..4715589a83 100644 --- a/recipes/apple_daily.recipe +++ b/recipes/apple_daily.recipe @@ -183,7 +183,7 @@ class AppleDaily(BasicNewsRecipe): if mp is not None and os.access(mp, os.R_OK): from calibre.ebooks.metadata.opf2 import Guide ref = Guide.Reference(os.path.basename( - self.masthead_path), os.getcwdu()) + self.masthead_path), os.getcwd()) ref.type = 'masthead' ref.title = 'Masthead Image' opf.guide.append(ref) diff --git a/recipes/ming_pao.recipe b/recipes/ming_pao.recipe index fb6e402390..f77db49be9 100644 --- a/recipes/ming_pao.recipe +++ b/recipes/ming_pao.recipe @@ -1062,7 +1062,7 @@ class MPRecipe(BasicNewsRecipe): if mp is not None and os.access(mp, os.R_OK): from calibre.ebooks.metadata.opf2 import Guide ref = Guide.Reference(os.path.basename( - self.masthead_path), os.getcwdu()) + self.masthead_path), os.getcwd()) ref.type = 'masthead' ref.title = 'Masthead Image' opf.guide.append(ref) diff --git a/recipes/ming_pao_toronto.recipe b/recipes/ming_pao_toronto.recipe index f73c2a72f2..24792ae76a 100644 --- a/recipes/ming_pao_toronto.recipe +++ b/recipes/ming_pao_toronto.recipe @@ -900,7 +900,7 @@ class MPRecipe(BasicNewsRecipe): if mp is not None and os.access(mp, os.R_OK): from calibre.ebooks.metadata.opf2 import Guide ref = Guide.Reference(os.path.basename( - self.masthead_path), os.getcwdu()) + self.masthead_path), os.getcwd()) ref.type = 'masthead' ref.title = 'Masthead Image' opf.guide.append(ref) diff --git a/recipes/ming_pao_vancouver.recipe b/recipes/ming_pao_vancouver.recipe index b1c5c097f9..f8b1e9309d 100644 --- a/recipes/ming_pao_vancouver.recipe +++ b/recipes/ming_pao_vancouver.recipe @@ -900,7 +900,7 @@ class MPRecipe(BasicNewsRecipe): if mp is not None and os.access(mp, os.R_OK): from calibre.ebooks.metadata.opf2 import Guide ref = Guide.Reference(os.path.basename( - self.masthead_path), os.getcwdu()) + self.masthead_path), os.getcwd()) ref.type = 'masthead' ref.title = 'Masthead Image' opf.guide.append(ref) diff --git a/recipes/singtaohk.recipe b/recipes/singtaohk.recipe index ef779fb81d..ed362eedd5 100644 --- a/recipes/singtaohk.recipe +++ b/recipes/singtaohk.recipe @@ -412,7 +412,7 @@ class STHKRecipe(BasicNewsRecipe): if mp is not None and os.access(mp, os.R_OK): from calibre.ebooks.metadata.opf2 import Guide ref = Guide.Reference(os.path.basename( - self.masthead_path), os.getcwdu()) + self.masthead_path), os.getcwd()) ref.type = 'masthead' ref.title = 'Masthead Image' opf.guide.append(ref) diff --git a/setup/__init__.py b/setup/__init__.py index 2ae25238b1..fdbe266bd5 100644 --- a/setup/__init__.py +++ b/setup/__init__.py @@ -24,8 +24,6 @@ sys.path.insert(0, SRC) sys.resources_location = os.path.join(os.path.dirname(SRC), 'resources') sys.extensions_location = os.path.abspath(os.environ.get('CALIBRE_SETUP_EXTENSIONS_PATH', os.path.join(SRC, 'calibre', 'plugins'))) sys.running_from_setup = True -if not hasattr(os, 'getcwdu'): - os.getcwdu = os.getcwd __version__ = __appname__ = modules = functions = basenames = scripts = None diff --git a/setup/build.py b/setup/build.py index 41afd98173..211bb11986 100644 --- a/setup/build.py +++ b/setup/build.py @@ -502,7 +502,7 @@ class Build(Command): proname = '%s.pro' % sip['target'] with open(os.path.join(src_dir, proname), 'wb') as f: f.write(pro.encode('utf-8')) - cwd = os.getcwdu() + cwd = os.getcwd() qmc = [] if iswindows: qmc += ['-spec', qmakespec] diff --git a/setup/publish.py b/setup/publish.py index 07c589bf3b..ef2e2c5c53 100644 --- a/setup/publish.py +++ b/setup/publish.py @@ -220,7 +220,7 @@ class Manual(Command): subprocess.check_call(jobs[0][0]) if not parallel_build(jobs[1:], self.info): raise SystemExit(1) - cwd = os.getcwdu() + cwd = os.getcwd() try: os.chdir(self.j(tdir, 'en', 'html')) for x in os.listdir(tdir): @@ -311,7 +311,7 @@ class ManPages(Command): subprocess.check_call(jobs[0][0]) if not parallel_build(jobs[1:], self.info, verbose=False): raise SystemExit(1) - cwd = os.getcwdu() + cwd = os.getcwd() os.chdir(dest) try: for x in tuple(os.listdir('.')): diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index 89a234619a..359a6b49e0 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -5,21 +5,15 @@ __docformat__ = 'restructuredtext en' import sys, os, re, time, random, warnings from polyglot.builtins import (builtins, codepoint_to_chr, iteritems, - itervalues, unicode_type, range, filter) + itervalues, unicode_type, range, filter, hasenv) builtins.__dict__['dynamic_property'] = lambda func: func(None) from math import floor from functools import partial -if 'CALIBRE_SHOW_DEPRECATION_WARNINGS' not in os.environ: +if not hasenv('CALIBRE_SHOW_DEPRECATION_WARNINGS'): warnings.simplefilter('ignore', DeprecationWarning) try: - os.getcwdu() -except AttributeError: - os.getcwdu = os.getcwd - try: - os.getcwd() - except EnvironmentError: - os.chdir(os.path.expanduser('~')) + os.getcwd() except EnvironmentError: os.chdir(os.path.expanduser('~')) @@ -446,7 +440,7 @@ class CurrentDir(object): self.cwd = None def __enter__(self, *args): - self.cwd = os.getcwdu() + self.cwd = os.getcwd() os.chdir(self.path) return self.cwd diff --git a/src/calibre/constants.py b/src/calibre/constants.py index 85c57ec2c7..7b4e7b4b6f 100644 --- a/src/calibre/constants.py +++ b/src/calibre/constants.py @@ -2,7 +2,7 @@ # vim:fileencoding=utf-8 # License: GPLv3 Copyright: 2015, Kovid Goyal from __future__ import print_function, unicode_literals -from polyglot.builtins import map, unicode_type, hasenv, getenv, environ_item +from polyglot.builtins import map, unicode_type, environ_item, hasenv, getenv import sys, locale, codecs, os, importlib, collections __appname__ = 'calibre' @@ -300,12 +300,7 @@ def get_version(): def get_portable_base(): 'Return path to the directory that contains calibre-portable.exe or None' if isportable: - return os.path.dirname(os.path.dirname(get_unicode_windows_env_var('CALIBRE_PORTABLE_BUILD'))) - - -def get_unicode_windows_env_var(name): - getenv = plugins['winutil'][0].getenv - return getenv(unicode_type(name)) + return os.path.dirname(os.path.dirname(getenv('CALIBRE_PORTABLE_BUILD'))) def get_windows_username(): diff --git a/src/calibre/db/cli/cmd_set_metadata.py b/src/calibre/db/cli/cmd_set_metadata.py index 02ac4462de..fb3b190806 100644 --- a/src/calibre/db/cli/cmd_set_metadata.py +++ b/src/calibre/db/cli/cmd_set_metadata.py @@ -11,7 +11,7 @@ from calibre.ebooks.metadata.book.base import field_from_string from calibre.ebooks.metadata.book.serialize import read_cover from calibre.ebooks.metadata.opf import get_metadata from calibre.srv.changes import metadata -from polyglot.builtins import iteritems, unicode_type +from polyglot.builtins import iteritems, unicode_type, getcwd readonly = False version = 0 # change this if you change signature of implementation() @@ -148,7 +148,7 @@ def main(opts, args, dbctx): with lopen(opf, 'rb') as stream: mi = get_metadata(stream)[0] if mi.cover: - mi.cover = os.path.join(os.path.dirname(opf), os.path.relpath(mi.cover, os.getcwdu())) + mi.cover = os.path.join(os.path.dirname(opf), os.path.relpath(mi.cover, getcwd())) final_mi = dbctx.run('set_metadata', 'opf', book_id, read_cover(mi)) if not final_mi: raise SystemExit(_('No book with id: %s in the database') % book_id) diff --git a/src/calibre/db/cli/cmd_show_metadata.py b/src/calibre/db/cli/cmd_show_metadata.py index a0a84f71d4..ca76735070 100644 --- a/src/calibre/db/cli/cmd_show_metadata.py +++ b/src/calibre/db/cli/cmd_show_metadata.py @@ -4,12 +4,11 @@ from __future__ import absolute_import, division, print_function, unicode_literals -import os import sys from calibre import prints from calibre.ebooks.metadata.opf2 import OPFCreator -from polyglot.builtins import unicode_type +from polyglot.builtins import unicode_type, getcwd readonly = True version = 0 # change this if you change signature of implementation() @@ -50,7 +49,7 @@ def main(opts, args, dbctx): if mi is None: raise SystemExit('Id #%d is not present in database.' % id) if opts.as_opf: - mi = OPFCreator(os.getcwdu(), mi) + mi = OPFCreator(getcwd(), mi) mi.render(sys.stdout) else: prints(unicode_type(mi)) diff --git a/src/calibre/debug.py b/src/calibre/debug.py index 2445c8b4c7..e0566fae42 100644 --- a/src/calibre/debug.py +++ b/src/calibre/debug.py @@ -11,7 +11,7 @@ import sys, os, functools from calibre.utils.config import OptionParser from calibre.constants import iswindows from calibre import prints -from polyglot.builtins import exec_path, raw_input, unicode_type +from polyglot.builtins import exec_path, raw_input, unicode_type, getcwd def get_debug_executable(): @@ -174,7 +174,7 @@ def add_simple_plugin(path_to_plugin): tdir = tempfile.mkdtemp() open(os.path.join(tdir, 'custom_plugin.py'), 'wb').write(open(path_to_plugin, 'rb').read()) - odir = os.getcwdu() + odir = getcwd() os.chdir(tdir) zf = zipfile.ZipFile('plugin.zip', 'w') zf.write('custom_plugin.py') diff --git a/src/calibre/ebooks/docx/to_html.py b/src/calibre/ebooks/docx/to_html.py index e2477be214..8141e6a4f2 100644 --- a/src/calibre/ebooks/docx/to_html.py +++ b/src/calibre/ebooks/docx/to_html.py @@ -29,7 +29,7 @@ from calibre.ebooks.docx.fields import Fields from calibre.ebooks.docx.settings import Settings from calibre.ebooks.metadata.opf2 import OPFCreator from calibre.utils.localization import canonicalize_lang, lang_as_iso639_1 -from polyglot.builtins import iteritems, itervalues, filter +from polyglot.builtins import iteritems, itervalues, filter, getcwd NBSP = '\xa0' @@ -70,7 +70,7 @@ class Convert(object): self.notes_text = notes_text or _('Notes') self.notes_nopb = notes_nopb self.nosupsub = nosupsub - self.dest_dir = dest_dir or os.getcwdu() + self.dest_dir = dest_dir or getcwd() self.mi = self.docx.metadata self.body = BODY() self.theme = Theme(self.namespace) @@ -831,7 +831,7 @@ if __name__ == '__main__': import shutil from calibre.utils.logging import default_log default_log.filter_level = default_log.DEBUG - dest_dir = os.path.join(os.getcwdu(), 'docx_input') + dest_dir = os.path.join(getcwd(), 'docx_input') if os.path.exists(dest_dir): shutil.rmtree(dest_dir) os.mkdir(dest_dir) diff --git a/src/calibre/utils/localunzip.py b/src/calibre/utils/localunzip.py index eb1010663c..b43a45f7d6 100644 --- a/src/calibre/utils/localunzip.py +++ b/src/calibre/utils/localunzip.py @@ -19,7 +19,7 @@ from struct import calcsize, unpack, pack from collections import namedtuple, OrderedDict from tempfile import SpooledTemporaryFile -from polyglot.builtins import itervalues +from polyglot.builtins import itervalues, getcwd HEADER_SIG = 0x04034b50 HEADER_BYTE_SIG = pack(b'' else os.path.dirname(filename), + 'basedir': getcwd() if not filename or filename == '' else os.path.dirname(filename), 'filename': filename, } c.g.rs_source_code = data diff --git a/src/calibre/utils/serve_coffee.py b/src/calibre/utils/serve_coffee.py index 114aa7b1a2..c3c3f4f25c 100644 --- a/src/calibre/utils/serve_coffee.py +++ b/src/calibre/utils/serve_coffee.py @@ -17,7 +17,7 @@ from threading import Lock, local from polyglot import socketserver from polyglot.http_server import HTTPServer, SimpleHTTPRequestHandler -from polyglot.builtins import error_message +from polyglot.builtins import error_message, getcwd # Compiler {{{ @@ -272,7 +272,7 @@ def serve(resources={}, port=8000, host='0.0.0.0'): Handler.special_resources = resources Handler.compiler = compile_coffeescript httpd = Server((host, port), Handler) - print('serving %s at %s:%d with PID=%d'%(os.getcwdu(), host, port, os.getpid())) + print('serving %s at %s:%d with PID=%d'%(getcwd(), host, port, os.getpid())) try: httpd.serve_forever() except KeyboardInterrupt: diff --git a/src/calibre/utils/test_lock.py b/src/calibre/utils/test_lock.py index 35757f9906..ebdc853b60 100644 --- a/src/calibre/utils/test_lock.py +++ b/src/calibre/utils/test_lock.py @@ -19,7 +19,7 @@ from calibre.utils.tdir_in_cache import ( clean_tdirs_in, is_tdir_locked, retry_lock_tdir, tdir_in_cache, tdirs_in, unlock_file ) -from polyglot.builtins import iteritems +from polyglot.builtins import iteritems, getcwd def FastFailEF(name): @@ -177,13 +177,13 @@ def other3(): def other4(): - cache_dir.ans = os.getcwdu() + cache_dir.ans = getcwd() tdir_in_cache('t') time.sleep(30) def other5(): - cache_dir.ans = os.getcwdu() + cache_dir.ans = getcwd() if not os.path.isdir(tdir_in_cache('t')): raise SystemExit(1) diff --git a/src/calibre/utils/zipfile.py b/src/calibre/utils/zipfile.py index bf6e66bcec..be913a4ff8 100644 --- a/src/calibre/utils/zipfile.py +++ b/src/calibre/utils/zipfile.py @@ -11,7 +11,7 @@ from tempfile import SpooledTemporaryFile from calibre import sanitize_file_name from calibre.constants import filesystem_encoding from calibre.ebooks.chardet import detect -from polyglot.builtins import unicode_type, string_or_bytes +from polyglot.builtins import unicode_type, string_or_bytes, getcwd try: import zlib # We may need its compression method @@ -1083,7 +1083,7 @@ class ZipFile: member = self.getinfo(member) if path is None: - path = os.getcwdu() + path = getcwd() return self._extract_member(member, path, pwd) @@ -1334,7 +1334,7 @@ class ZipFile: ''' if prefix: self.writestr(prefix+'/', b'', 0o755) - cwd = os.path.abspath(os.getcwdu()) + cwd = os.path.abspath(getcwd()) try: os.chdir(path) fp = (prefix + ('/' if prefix else '')).replace('//', '/') diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index 6ae6b157db..4b7e9db435 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -29,7 +29,7 @@ from calibre.utils.icu import numeric_sort_key from calibre.utils.img import save_cover_data_to, add_borders_to_image, image_to_data from calibre.utils.localization import canonicalize_lang from calibre.utils.logging import ThreadSafeWrapper -from polyglot.builtins import unicode_type, string_or_bytes +from polyglot.builtins import unicode_type, string_or_bytes, getcwd from polyglot.urllib import urlparse, urlsplit @@ -870,7 +870,7 @@ class BasicNewsRecipe(Recipe): self.title = unicode_type(self.title, 'utf-8', 'replace') self.debug = options.verbose > 1 - self.output_dir = os.path.abspath(os.getcwdu()) + self.output_dir = os.path.abspath(getcwd()) self.verbose = options.verbose self.test = options.test if self.test and not isinstance(self.test, tuple): @@ -1457,7 +1457,7 @@ class BasicNewsRecipe(Recipe): mp = getattr(self, 'masthead_path', None) if mp is not None and os.access(mp, os.R_OK): from calibre.ebooks.metadata.opf2 import Guide - ref = Guide.Reference(os.path.basename(self.masthead_path), os.getcwdu()) + ref = Guide.Reference(os.path.basename(self.masthead_path), getcwd()) ref.type = 'masthead' ref.title = 'Masthead Image' opf.guide.append(ref) diff --git a/src/polyglot/builtins.py b/src/polyglot/builtins.py index 5b6f7a80c1..12781595b1 100644 --- a/src/polyglot/builtins.py +++ b/src/polyglot/builtins.py @@ -74,6 +74,8 @@ if is_py3: string_or_bytes = str, bytes long_type = int raw_input = input + getcwd = os.getcwd + getenv = os.getenv def error_message(exc): args = getattr(exc, 'args', None) @@ -92,9 +94,6 @@ if is_py3: x = x.decode('utf-8') return x - def getenv(x, default=None): - return os.environ.get(environ_item(x), default) - def exec_path(path, ctx=None): ctx = ctx or {} with open(path, 'rb') as f: @@ -132,6 +131,7 @@ else: raw_input = builtins.raw_input cmp = builtins.cmp int_to_byte = chr + getcwd = os.getcwdu def error_message(exc): ans = exc.message @@ -152,16 +152,16 @@ else: if hasattr(sys, 'getwindowsversion'): def getenv(x, default=None): - from calibre.constants import get_unicode_windows_env_var + from win32api import GetEnvironmentVariableW if isinstance(x, bytes): x = x.decode('mbcs', 'replace') - ans = get_unicode_windows_env_var(x) + ans = GetEnvironmentVariableW(x) if ans is None: ans = default return ans else: def getenv(x, default=None): - ans = os.environ.get(environ_item(x), default) + ans = os.getenv(x, default) if isinstance(ans, bytes): ans = ans.decode('utf-8', 'replace') return ans