Changeset 6d4a8bc in trunk


Ignore:
Timestamp:
2016-01-15T19:15:38Z (9 years ago)
Author:
Daira Hopwood <daira@…>
Branches:
master
Children:
6226f6b
Parents:
41cf6008 (diff), f282445 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge pull request #220 from tahoe-lafs/2669.magic-folder-misc-patches.1

This PR is a bunch of miscellaneous patches that are on the Magic Folder branches, but independent of Magic Folder itself.

Location:
src/allmydata
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/node.py

    r41cf6008 r6d4a8bc  
    1313from allmydata.util.fileutil import abspath_expanduser_unicode
    1414from allmydata.util.encodingutil import get_filesystem_encoding, quote_output
     15from allmydata.util import configutil
    1516
    1617# Add our application versions to the data that Foolscap's LogPublisher
     
    134135            return default
    135136
    136     def set_config(self, section, option, value):
    137         if not self.config.has_section(section):
    138             self.config.add_section(section)
    139         self.config.set(section, option, value)
    140         assert self.config.get(section, option) == value
    141 
    142137    def read_config(self):
    143138        self.error_about_old_config_files()
     
    146141        tahoe_cfg = os.path.join(self.basedir, "tahoe.cfg")
    147142        try:
    148             f = open(tahoe_cfg, "rb")
    149             try:
    150                 # Skip any initial Byte Order Mark. Since this is an ordinary file, we
    151                 # don't need to handle incomplete reads, and can assume seekability.
    152                 if f.read(3) != '\xEF\xBB\xBF':
    153                     f.seek(0)
    154                 self.config.readfp(f)
    155             finally:
    156                 f.close()
     143            self.config = configutil.get_config(tahoe_cfg)
    157144        except EnvironmentError:
    158145            if os.path.exists(tahoe_cfg):
     
    166153            try:
    167154                file_tubport = fileutil.read(self._portnumfile).strip()
    168                 self.set_config("node", "tub.port", file_tubport)
     155                configutil.set_config(self.config, "node", "tub.port", file_tubport)
    169156            except EnvironmentError:
    170157                if os.path.exists(self._portnumfile):
  • TabularUnified src/allmydata/scripts/common.py

    r41cf6008 r6d4a8bc  
    5858
    5959    def parseArgs(self, basedir=None):
    60         if self.parent['node-directory'] and self['basedir']:
     60        # This finds the node-directory option correctly even if we are in a subcommand.
     61        root = self.parent
     62        while root.parent is not None:
     63            root = root.parent
     64
     65        if root['node-directory'] and self['basedir']:
    6166            raise usage.UsageError("The --node-directory (or -d) and --basedir (or -C) options cannot both be used.")
    62         if self.parent['node-directory'] and basedir:
     67        if root['node-directory'] and basedir:
    6368            raise usage.UsageError("The --node-directory (or -d) option and a basedir argument cannot both be used.")
    6469        if self['basedir'] and basedir:
     
    6974        elif self['basedir']:
    7075            b = argv_to_abspath(self['basedir'])
    71         elif self.parent['node-directory']:
    72             b = argv_to_abspath(self.parent['node-directory'])
     76        elif root['node-directory']:
     77            b = argv_to_abspath(root['node-directory'])
    7378        elif self.default_nodedir:
    7479            b = self.default_nodedir
     
    7681            raise usage.UsageError("No default basedir available, you must provide one with --node-directory, --basedir, or a basedir argument")
    7782        self['basedir'] = b
     83        self['node-directory'] = b
    7884
    7985    def postOptions(self):
  • TabularUnified src/allmydata/scripts/tahoe_add_alias.py

    r41cf6008 r6d4a8bc  
    22import os.path
    33import codecs
     4
     5from allmydata.util.assertutil import precondition
     6
    47from allmydata import uri
    58from allmydata.scripts.common_http import do_http, check_http_error
     
    3033    nodedir = options['node-directory']
    3134    alias = options.alias
     35    precondition(isinstance(alias, unicode), alias=alias)
    3236    cap = options.cap
    3337    stdout = options.stdout
     
    5761    nodedir = options['node-directory']
    5862    alias = options.alias
     63    precondition(isinstance(alias, unicode), alias=alias)
    5964    stdout = options.stdout
    6065    stderr = options.stderr
  • TabularUnified src/allmydata/test/test_cli.py

    r41cf6008 r6d4a8bc  
    3737from allmydata.util.assertutil import precondition
    3838from allmydata.util.encodingutil import listdir_unicode, unicode_platform, \
    39     get_io_encoding, get_filesystem_encoding
     39    get_io_encoding, get_filesystem_encoding, unicode_to_argv
    4040
    4141timeout = 480 # deep_check takes 360s on Zandr's linksys box, others take > 240s
     
    5050class CLITestMixin(ReallyEqualMixin):
    5151    def do_cli(self, verb, *args, **kwargs):
     52        precondition(not [True for arg in args if not isinstance(arg, str)],
     53                     "arguments to do_cli must be strs -- convert using unicode_to_argv", args=args)
     54
     55        # client_num is used to execute client CLI commands on a specific client.
     56        client_num = kwargs.get("client_num", 0)
     57
    5258        nodeargs = [
    53             "--node-directory", self.get_clientdir(),
     59            "--node-directory", unicode_to_argv(self.get_clientdir(i=client_num)),
    5460            ]
    5561        argv = nodeargs + [verb] + list(args)
  • TabularUnified src/allmydata/test/test_encodingutil.py

    r41cf6008 r6d4a8bc  
    401401
    402402    def test_quote_output_default(self):
    403         encodingutil.io_encoding = 'ascii'
     403        self.patch(encodingutil, 'io_encoding', 'ascii')
    404404        self.test_quote_output_ascii(None)
    405405
    406         encodingutil.io_encoding = 'latin1'
     406        self.patch(encodingutil, 'io_encoding', 'latin1')
    407407        self.test_quote_output_latin1(None)
    408408
    409         encodingutil.io_encoding = 'utf-8'
     409        self.patch(encodingutil, 'io_encoding', 'utf-8')
    410410        self.test_quote_output_utf8(None)
    411411
  • TabularUnified src/allmydata/uri.py

    r41cf6008 r6d4a8bc  
    731731def from_string(u, deep_immutable=False, name=u"<unknown name>"):
    732732    if not isinstance(u, str):
    733         raise TypeError("unknown URI type: %s.." % str(u)[:100])
     733        raise TypeError("URI must be str: %r" % (u,))
    734734
    735735    # We allow and check ALLEGED_READONLY_PREFIX or ALLEGED_IMMUTABLE_PREFIX
  • TabularUnified src/allmydata/util/encodingutil.py

    r41cf6008 r6d4a8bc  
    8989                               (quote_output(s), io_encoding))
    9090
    91 def argv_to_abspath(s):
     91def argv_to_abspath(s, **kwargs):
    9292    """
    9393    Convenience function to decode an argv element to an absolute path, with ~ expanded.
    9494    If this fails, raise a UsageError.
    9595    """
    96     return abspath_expanduser_unicode(argv_to_unicode(s))
     96    decoded = argv_to_unicode(s)
     97    if decoded.startswith(u'-'):
     98        raise usage.UsageError("Path argument %s cannot start with '-'.\nUse %s if you intended to refer to a file."
     99                               % (quote_output(s), quote_output(os.path.join('.', s))))
     100    return abspath_expanduser_unicode(decoded, **kwargs)
    97101
    98102def unicode_to_argv(s, mangle=False):
  • TabularUnified src/allmydata/util/fileutil.py

    r41cf6008 r6d4a8bc  
    44
    55import sys, exceptions, os, stat, tempfile, time, binascii
     6
     7if sys.platform == "win32":
     8    from ctypes import WINFUNCTYPE, WinError, windll, POINTER, byref, c_ulonglong, \
     9        create_unicode_buffer, get_last_error
     10    from ctypes.wintypes import BOOL, DWORD, LPCWSTR, LPWSTR
    611
    712from twisted.python import log
     
    336341have_GetDiskFreeSpaceExW = False
    337342if sys.platform == "win32":
    338     from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_ulonglong, create_unicode_buffer, \
    339         get_last_error
    340     from ctypes.wintypes import BOOL, DWORD, LPCWSTR, LPWSTR
    341 
    342343    # <http://msdn.microsoft.com/en-us/library/windows/desktop/ms683188%28v=vs.85%29.aspx>
    343344    GetEnvironmentVariableW = WINFUNCTYPE(
    344         DWORD,
    345           LPCWSTR, LPWSTR, DWORD,
     345        DWORD,  LPCWSTR, LPWSTR, DWORD,
    346346        use_last_error=True
    347       )(("GetEnvironmentVariableW", windll.kernel32))
     347    )(("GetEnvironmentVariableW", windll.kernel32))
    348348
    349349    try:
     
    353353        # <http://msdn.microsoft.com/en-us/library/aa364937%28VS.85%29.aspx>
    354354        GetDiskFreeSpaceExW = WINFUNCTYPE(
    355             BOOL,
    356               LPCWSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER,
     355            BOOL,  LPCWSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER,
    357356            use_last_error=True
    358           )(("GetDiskFreeSpaceExW", windll.kernel32))
     357        )(("GetDiskFreeSpaceExW", windll.kernel32))
    359358
    360359        have_GetDiskFreeSpaceExW = True
     
    404403        if err == ERROR_ENVVAR_NOT_FOUND:
    405404            return None
    406         raise OSError("Windows error %d attempting to read size of environment variable %r"
    407                       % (err, name))
     405        raise OSError("WinError: %s\n attempting to read size of environment variable %r"
     406                      % (WinError(err), name))
    408407    if n == 1:
    409408        # Avoid an ambiguity between a zero-length string and an error in the return value of the
     
    417416        if err == ERROR_ENVVAR_NOT_FOUND:
    418417            return None
    419         raise OSError("Windows error %d attempting to read environment variable %r"
    420                       % (err, name))
     418        raise OSError("WinError: %s\n attempting to read environment variable %r"
     419                      % (WinError(err), name))
    421420    if retval >= n:
    422421        raise OSError("Unexpected result %d (expected less than %d) from GetEnvironmentVariableW attempting to read environment variable %r"
     
    460459                                               byref(n_free_for_root))
    461460        if retval == 0:
    462             raise OSError("Windows error %d attempting to get disk statistics for %r"
    463                           % (get_last_error(), whichdir))
     461            raise OSError("WinError: %s\n attempting to get disk statistics for %r"
     462                          % (WinError(get_last_error()), whichdir))
    464463        free_for_nonroot = n_free_for_nonroot.value
    465464        total            = n_total.value
  • TabularUnified src/allmydata/windows/fixups.py

    r41cf6008 r6d4a8bc  
    1010
    1111    import codecs, re
    12     from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_int
     12    from ctypes import WINFUNCTYPE, WinError, windll, POINTER, byref, c_int, get_last_error
    1313    from ctypes.wintypes import BOOL, HANDLE, DWORD, UINT, LPWSTR, LPCWSTR, LPVOID
     14
    1415    from allmydata.util import log
    1516    from allmydata.util.encodingutil import canonical_encoding
    1617
    1718    # <https://msdn.microsoft.com/en-us/library/ms680621%28VS.85%29.aspx>
    18     SetErrorMode = WINFUNCTYPE(UINT, UINT)(("SetErrorMode", windll.kernel32))
     19    SetErrorMode = WINFUNCTYPE(
     20        UINT,  UINT,
     21        use_last_error=True
     22    )(("SetErrorMode", windll.kernel32))
     23
    1924    SEM_FAILCRITICALERRORS = 0x0001
    2025    SEM_NOOPENFILEERRORBOX = 0x8000
     
    5156        # BOOL WINAPI GetConsoleMode(HANDLE hConsole, LPDWORD lpMode);
    5257
    53         GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(("GetStdHandle", windll.kernel32))
     58        GetStdHandle = WINFUNCTYPE(
     59            HANDLE,  DWORD,
     60            use_last_error=True
     61        )(("GetStdHandle", windll.kernel32))
     62
    5463        STD_OUTPUT_HANDLE = DWORD(-11)
    5564        STD_ERROR_HANDLE  = DWORD(-12)
    56         GetFileType = WINFUNCTYPE(DWORD, DWORD)(("GetFileType", windll.kernel32))
     65
     66        GetFileType = WINFUNCTYPE(
     67            DWORD,  DWORD,
     68            use_last_error=True
     69        )(("GetFileType", windll.kernel32))
     70
    5771        FILE_TYPE_CHAR   = 0x0002
    5872        FILE_TYPE_REMOTE = 0x8000
    59         GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))(("GetConsoleMode", windll.kernel32))
     73
     74        GetConsoleMode = WINFUNCTYPE(
     75            BOOL,  HANDLE, POINTER(DWORD),
     76            use_last_error=True
     77        )(("GetConsoleMode", windll.kernel32))
     78
    6079        INVALID_HANDLE_VALUE = DWORD(-1).value
    6180
     
    89108
    90109        if real_stdout or real_stderr:
     110            # <https://msdn.microsoft.com/en-us/library/windows/desktop/ms687401%28v=vs.85%29.aspx>
    91111            # BOOL WINAPI WriteConsoleW(HANDLE hOutput, LPWSTR lpBuffer, DWORD nChars,
    92112            #                           LPDWORD lpCharsWritten, LPVOID lpReserved);
    93113
    94             WriteConsoleW = WINFUNCTYPE(BOOL, HANDLE, LPWSTR, DWORD, POINTER(DWORD), LPVOID) \
    95                                 (("WriteConsoleW", windll.kernel32))
     114            WriteConsoleW = WINFUNCTYPE(
     115                BOOL,  HANDLE, LPWSTR, DWORD, POINTER(DWORD), LPVOID,
     116                use_last_error=True
     117            )(("WriteConsoleW", windll.kernel32))
    96118
    97119            class UnicodeOutput:
     
    140162                                # passed to WriteConsoleW (see #1232).
    141163                                retval = WriteConsoleW(self._hConsole, text, min(remaining, 10000), byref(n), None)
    142                                 if retval == 0 or n.value == 0:
    143                                     raise IOError("WriteConsoleW returned %r, n.value = %r" % (retval, n.value))
     164                                if retval == 0:
     165                                    raise IOError("WriteConsoleW failed with WinError: %s" % (WinError(get_last_error()),))
     166                                if n.value == 0:
     167                                    raise IOError("WriteConsoleW returned %r, n.value = 0" % (retval,))
    144168                                remaining -= n.value
    145169                                if remaining == 0: break
     
    170194
    171195    # This works around <http://bugs.python.org/issue2128>.
    172     GetCommandLineW = WINFUNCTYPE(LPWSTR)(("GetCommandLineW", windll.kernel32))
    173     CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int)) \
    174                             (("CommandLineToArgvW", windll.shell32))
     196
     197    # <https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156%28v=vs.85%29.aspx>
     198    GetCommandLineW = WINFUNCTYPE(
     199        LPWSTR,
     200        use_last_error=True
     201    )(("GetCommandLineW", windll.kernel32))
     202
     203    # <https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391%28v=vs.85%29.aspx>
     204    CommandLineToArgvW = WINFUNCTYPE(
     205        POINTER(LPWSTR),  LPCWSTR, POINTER(c_int),
     206        use_last_error=True
     207    )(("CommandLineToArgvW", windll.shell32))
    175208
    176209    argc = c_int(0)
    177210    argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))
     211    if argv_unicode is None:
     212        raise WinError(get_last_error())
    178213
    179214    # Because of <http://bugs.python.org/issue8775> (and similar limitations in
Note: See TracChangeset for help on using the changeset viewer.