<commit_before># -*- coding: utf-8 -*-
#######################################################################################
#                                                                                     #
#   This file is part of the updater4pyi Project.                                     #
#                                                                                     #
#   Copyright (C) 2013, <NAME>                                                #
#   <EMAIL>                                                         #
#   All rights reserved.                                                              #
#                                                                                     #
#   Redistribution and use in source and binary forms, with or without                #
#   modification, are permitted provided that the following conditions are met:       #
#                                                                                     #
#   1. Redistributions of source code must retain the above copyright notice, this    #
#      list of conditions and the following disclaimer.                               #
#   2. Redistributions in binary form must reproduce the above copyright notice,      #
#      this list of conditions and the following disclaimer in the documentation      #
#      and/or other materials provided with the distribution.                         #
#                                                                                     #
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   #
#   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     #
#   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            #
#   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR   #
#   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    #
#   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      #
#   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND       #
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT        #
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     #
#   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      #
#                                                                                     #
#######################################################################################


import re
import sys
import inspect
import os
import os.path
import stat
import collections
import zipfile
import tarfile
import json
import glob
import subprocess
import tempfile
import shutil

from . import util
from . import upd_version
from .upd_log import logger
from .upd_defs import RELTYPE_UNKNOWN, RELTYPE_EXE, RELTYPE_ARCHIVE, RELTYPE_BUNDLE_ARCHIVE
from .upd_defs import Updater4PyiError
import upd_downloader


# --------------------------------


FileToUpdate = collections.namedtuple('FileToUpdate', ('fn', 'reltype', 'executable',));


def determine_file_to_update():
    """
    Inspects the program currently running, and determines the location of the file one
    should replace in the event of a software update.

    Returns a named tuple `FileToUpdate(fn=.., reltype=.., executable=...)`. The values
    are:

        - `fn`: the actual file we should update. This could be a directory in the case of
          a onedir PyInstaller package. For a MAC OS X bundle, it is the `XYZ.app` file.
          
        - `reltype`: the release type we have. This may be one of
          :py:const:`upd_defs.RELTYPE_EXE`, :py:const:`upd_defs.RELTYPE_ARCHIVE`,
          :py:const:`upd_defs.RELTYPE_BUNDLE_ARCHIVE`.

        - `executable`: the actual executable file. This may be different from `fn`, for
          example in Mac OS X bundles, where `executable` is the actual file being
          executed within the bundle.

    .. _PyInstaller: http://www.pyinstaller.org/
    """
    
    executable = sys.executable
    updatefile = os.path.realpath(sys.executable)
    reltype = None

    logger.debug("trying to determine pyi executable to update. sys.executable=%s; sys._MEIPASS=%s",
                 sys.executable, (sys._MEIPASS if hasattr(sys, '_MEIPASS') else '<no sys._MEIPASS>'))

    if (sys.platform.startswith('darwin')):
        # see if we are a Mac OS X bundle
        (alllastdir,fn) = os.path.split(sys.executable);
        (allbeforelastdir,lastdir) = os.path.split(alllastdir);
        (allbeforebeforelastdir,beforelastdir) = os.path.split(allbeforelastdir);

        logger.debug("platform is Mac OS X; alllastdir=%s, beforelastdir=%s, lastdir=%s, fn=%s",
                     alllastdir, beforelastdir, lastdir, fn)

        if (lastdir == 'MacOS' and beforelastdir == 'Contents'):
            # we're in a Mac OS X bundle, so the actual "executable" should point to the .app file
            reltype = RELTYPE_BUNDLE_ARCHIVE
            updatefile = allbeforebeforelastdir
            logger.debug("We're a bundle: updatefile=%s", updatefile)

    if reltype is None:
        # if we're not already a bundle, check whether we're a directory to update
        if (hasattr(sys, '_MEIPASS')):
            meipass = os.path.realpath(sys._MEIPASS);
            if (updatefile.startswith(meipass)):
                # pyinstaller files are installed directly: it's the dir we need to update
                reltype = RELTYPE_ARCHIVE
                updatefile = meipass;

    if reltype is None:
        # otherwise, we're a self-contained executable.
        reltype = RELTYPE_EXE


    logger.debug("got FileToUpdate(fn=%r, reltype=%d, executable=%s)",
                 updatefile, reltype, executable)

    return FileToUpdate(fn=updatefile, reltype=reltype, executable=executable)





# ------------------------------------------------------------------------

_updater = None

def get_updater():
    return _updater



class Updater(object):
    """
    The main Updater object.

    This class is responsible for actually checking for updates and performing the
    software update.

    It does not take care of scheduling the checks, however. That's done with an
    UpdaterInterface.

    This class needs to be specified a *source* for updates. See
    :py:class:`upd_source.UpdateSource`.
    """
    def __init__(self, current_version, update_source):
        """
        Instantiates an `Updater`, with updates provided by the source `update_source` (a
        `upd_source.UpdateSource` subclass instance).

        The `current_version` is the current version string of the software, and will
        be provided to the `update_source`.
        """

        # sys._MEIPASS seems to be set all the time, even we don't self-extract.
        if (not hasattr(sys, '_MEIPASS')):
            raise Updater4PyiError("This installation is not built with pyinstaller.")
<commit_msg>removed unused property "_updater" in upd_core.py<commit_after># -*- coding: utf-8 -*-
#######################################################################################
#                                                                                     #
#   This file is part of the updater4pyi Project.                                     #
#                                                                                     #
#   Copyright (C) 2013, <NAME>                                                #
#   <EMAIL>                                                         #
#   All rights reserved.                                                              #
#                                                                                     #
#   Redistribution and use in source and binary forms, with or without                #
#   modification, are permitted provided that the following conditions are met:       #
#                                                                                     #
#   1. Redistributions of source code must retain the above copyright notice, this    #
#      list of conditions and the following disclaimer.                               #
#   2. Redistributions in binary form must reproduce the above copyright notice,      #
#      this list of conditions and the following disclaimer in the documentation      #
#      and/or other materials provided with the distribution.                         #
#                                                                                     #
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   #
#   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     #
#   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            #
#   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR   #
#   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    #
#   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      #
#   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND       #
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT        #
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     #
#   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      #
#                                                                                     #
#######################################################################################


import re
import sys
import inspect
import os
import os.path
import stat
import collections
import zipfile
import tarfile
import json
import glob
import subprocess
import tempfile
import shutil

from . import util
from . import upd_version
from .upd_log import logger
from .upd_defs import RELTYPE_UNKNOWN, RELTYPE_EXE, RELTYPE_ARCHIVE, RELTYPE_BUNDLE_ARCHIVE
from .upd_defs import Updater4PyiError
import upd_downloader


# --------------------------------


FileToUpdate = collections.namedtuple('FileToUpdate', ('fn', 'reltype', 'executable',));


def determine_file_to_update():
    """
    Inspects the program currently running, and determines the location of the file one
    should replace in the event of a software update.

    Returns a named tuple `FileToUpdate(fn=.., reltype=.., executable=...)`. The values
    are:

        - `fn`: the actual file we should update. This could be a directory in the case of
          a onedir PyInstaller package. For a MAC OS X bundle, it is the `XYZ.app` file.
          
        - `reltype`: the release type we have. This may be one of
          :py:const:`upd_defs.RELTYPE_EXE`, :py:const:`upd_defs.RELTYPE_ARCHIVE`,
          :py:const:`upd_defs.RELTYPE_BUNDLE_ARCHIVE`.

        - `executable`: the actual executable file. This may be different from `fn`, for
          example in Mac OS X bundles, where `executable` is the actual file being
          executed within the bundle.

    .. _PyInstaller: http://www.pyinstaller.org/
    """
    
    executable = sys.executable
    updatefile = os.path.realpath(sys.executable)
    reltype = None

    logger.debug("trying to determine pyi executable to update. sys.executable=%s; sys._MEIPASS=%s",
                 sys.executable, (sys._MEIPASS if hasattr(sys, '_MEIPASS') else '<no sys._MEIPASS>'))

    if (sys.platform.startswith('darwin')):
        # see if we are a Mac OS X bundle
        (alllastdir,fn) = os.path.split(sys.executable);
        (allbeforelastdir,lastdir) = os.path.split(alllastdir);
        (allbeforebeforelastdir,beforelastdir) = os.path.split(allbeforelastdir);

        logger.debug("platform is Mac OS X; alllastdir=%s, beforelastdir=%s, lastdir=%s, fn=%s",
                     alllastdir, beforelastdir, lastdir, fn)

        if (lastdir == 'MacOS' and beforelastdir == 'Contents'):
            # we're in a Mac OS X bundle, so the actual "executable" should point to the .app file
            reltype = RELTYPE_BUNDLE_ARCHIVE
            updatefile = allbeforebeforelastdir
            logger.debug("We're a bundle: updatefile=%s", updatefile)

    if reltype is None:
        # if we're not already a bundle, check whether we're a directory to update
        if (hasattr(sys, '_MEIPASS')):
            meipass = os.path.realpath(sys._MEIPASS);
            if (updatefile.startswith(meipass)):
                # pyinstaller files are installed directly: it's the dir we need to update
                reltype = RELTYPE_ARCHIVE
                updatefile = meipass;

    if reltype is None:
        # otherwise, we're a self-contained executable.
        reltype = RELTYPE_EXE


    logger.debug("got FileToUpdate(fn=%r, reltype=%d, executable=%s)",
                 updatefile, reltype, executable)

    return FileToUpdate(fn=updatefile, reltype=reltype, executable=executable)





# ------------------------------------------------------------------------



class Updater(object):
    """
    The main Updater object.

    This class is responsible for actually checking for updates and performing the
    software update.

    It does not take care of scheduling the checks, however. That's done with an
    UpdaterInterface.

    This class needs to be specified a *source* for updates. See
    :py:class:`upd_source.UpdateSource`.
    """
    def __init__(self, current_version, update_source):
        """
        Instantiates an `Updater`, with updates provided by the source `update_source` (a
        `upd_source.UpdateSource` subclass instance).

        The `current_version` is the current version string of the software, and will
        be provided to the `update_source`.
        """

        # sys._MEIPASS seems to be set all the time, even we don't self-extract.
        if (not hasattr(sys, '_MEIPASS')):
            raise Updater4PyiError("This installation is not built with pyinstaller.")

{'fraction_non_alphanumeric': 0.11058553716046729, 'fraction_numerical': 0.0014333835017558947, 'mean_word_length': 1.913152400835073, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6166097', 'n_tokens_mistral': 3545, 'n_tokens_neox': 3336, 'n_words': 1257}