Exemplo n.º 1
0
    def get_version(executable):
        """
        Returns the caffe version as a (MAJOR, MINOR, PATCH) tuple or None

        Arguments:
        executable -- path to a caffe executable
        """
        # TODO: check `caffe --version` when it's implemented

        NVIDIA_SUFFIX = '-nv'

        if platform.system() == 'Linux':
            p = subprocess.Popen(['ldd', executable],
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            if p.wait():
                raise config_option.BadValue(p.stderr.read().strip())
            else:
                libname = 'libcaffe'
                caffe_line = None

                # Search output for caffe library
                for line in p.stdout:
                    if libname in line:
                        caffe_line = line
                        break
                if caffe_line is None:
                    raise config_option.BadValue('%s not found in ldd output' %
                                                 libname)

                # Read the symlink for libcaffe from ldd output
                symlink = caffe_line.split()[2]
                filename = os.path.basename(os.path.realpath(symlink))

                # Check for the nvidia suffix
                if NVIDIA_SUFFIX not in filename:
                    raise config_option.BadValue(
                        'Library at "%s" does not have expected suffix "%s". Are you using the NVIDIA/caffe fork?'
                        % (filename, NVIDIA_SUFFIX))

                # parse the version string
                match = re.match(
                    r'%s%s\.so\.((\d|\.)+)$' % (libname, NVIDIA_SUFFIX),
                    filename)
                if match:
                    version_str = match.group(1)
                    return tuple(int(n) for n in version_str.split('.'))
                else:
                    return None

        elif platform.system() == 'Darwin':
            # XXX: guess and let the user figure out errors later
            return (0, 11, 0)
        elif platform.system() == 'Windows':
            # XXX: guess and let the user figure out errors later
            return (0, 11, 0)
        else:
            print 'WARNING: platform "%s" not supported' % platform.system()
            return None
Exemplo n.º 2
0
 def validate(cls, value):
     value = os.path.abspath(value)
     if os.path.exists(value):
         if not os.path.isdir(value):
             raise config_option.BadValue('Is not a directory')
         if not os.access(value, os.W_OK):
             raise config_option.BadValue(
                 'You do not have write permission')
         return value
     if not os.path.exists(os.path.dirname(value)):
         raise config_option.BadValue('Parent directory does not exist')
     if not os.access(os.path.dirname(value), os.W_OK):
         raise config_option.BadValue('You do not have write permission')
     return value
Exemplo n.º 3
0
    def validate(cls, value):
        if not value:
            return value

        if value == '<PATHS>':
            # Find the executable
            executable = cls.find_executable('caffe')
            if not executable:
                executable = cls.find_executable('caffe.exe')
            if not executable:
                raise config_option.BadValue('caffe binary not found in PATH')
            cls.validate_version(executable)

            # Find the python module
            try:
                imp.find_module('caffe')
            except ImportError:
                raise config_option.BadValue(
                    'caffe python package not found in PYTHONPATH')
            return value
        else:
            # Find the executable
            value = os.path.abspath(value)
            if not os.path.isdir(value):
                raise config_option.BadValue('"%s" is not a directory' % value)
            expected_path = os.path.join(value, 'build', 'tools', 'caffe')
            if not os.path.exists(expected_path):
                raise config_option.BadValue('caffe binary not found at "%s"' %
                                             value)
            cls.validate_version(expected_path)

            # Find the python module
            pythonpath = os.path.join(value, 'python')
            sys.path.insert(0, pythonpath)
            try:
                imp.find_module('caffe')
            except ImportError as e:
                raise config_option.BadValue(
                    'Error while importing caffe from "%s": %s' %
                    (pythonpath, e.message))
            finally:
                # Don't actually add this until apply() is called
                sys.path.pop(0)

            return value
Exemplo n.º 4
0
    def filenameValidator(filename):
        """
        Returns True if this is a valid file to edit
        """
        if os.path.isfile(filename):
            if not os.access(filename, os.W_OK):
                raise config_option.BadValue(
                    'You do not have write permission')
            else:
                return filename

        if os.path.isdir(filename):
            raise config_option.BadValue('This is a directory')
        dirname = os.path.dirname(os.path.realpath(filename))
        if not os.path.isdir(dirname):
            raise config_option.BadValue('Path not found: %s' % dirname)
        elif not os.access(dirname, os.W_OK):
            raise config_option.BadValue('You do not have write permission')
        return filename
Exemplo n.º 5
0
    def validate(cls, value):
        if not value:
            return value
        value = os.path.abspath(value)
        dirname = os.path.dirname(value)

        if os.path.isfile(value):
            if not os.access(value, os.W_OK):
                raise config_option.BadValue(
                    'You do not have write permissions')
            if not os.access(dirname, os.W_OK):
                raise config_option.BadValue(
                    'You do not have write permissions for "%s"' % dirname)
            return value
        elif os.path.isdir(value):
            raise config_option.BadValue('"%s" is a directory' % value)
        else:
            if os.path.isdir(dirname):
                if not os.access(dirname, os.W_OK):
                    raise config_option.BadValue(
                        'You do not have write permissions for "%s"' % dirname)
                # filename is in a valid directory
                return value
            previous_dir = os.path.dirname(dirname)
            if not os.path.isdir(previous_dir):
                raise config_option.BadValue('"%s" not found' % value)
            if not os.access(previous_dir, os.W_OK):
                raise config_option.BadValue(
                    'You do not have write permissions for "%s"' %
                    previous_dir)
            # the preceding directory can be created later (in apply())
            return value
Exemplo n.º 6
0
    def validate_version(cls, executable):
        """
        Utility for checking the caffe version from within validate()
        Throws BadValue

        Arguments:
        executable -- path to a caffe executable
        """
        minimum_version = (0, 11)

        version = cls.get_version(executable)
        if version is None:
            raise config_option.BadValue(
                'Could not get version information from caffe at "%s". Are you using the NVIDIA fork?'
                % executable)
        elif minimum_version > version:
            raise config_option.BadValue(
                'Required version "%s" is greater than "%s". Upgrade your installation.'
                % ('.'.join(str(n) for n in minimum_version), '.'.join(
                    str(n) for n in version)))
        else:
            return True
Exemplo n.º 7
0
    def validate(cls, value):
        if not value:
            return value

        if value == '<PATHS>':
            # Find the executable
            executable = cls.find_executable('th')
            if not executable:
                raise config_option.BadValue('torch binary not found in PATH')
            #cls.validate_version(executable)
            return value
        else:
            # Find the executable
            value = os.path.abspath(value)
            if not os.path.isdir(value):
                raise config_option.BadValue('"%s" is not a directory' % value)
            expected_path = os.path.join(value, 'bin', 'th')
            if not os.path.exists(expected_path):
                raise config_option.BadValue('torch binary not found at "%s"' %
                                             value)
            #cls.validate_version(expected_path)
            return value
Exemplo n.º 8
0
def load_option(
    option,
    mode,
    newConfig,
    instanceConfig=None,
    userConfig=None,
    systemConfig=None,
):
    """
    Called from load_config() [below]

    Arguments:
    option -- an Option instance
    mode -- see docstring for load_config()
    newConfig -- an instance of ConfigFile
    instanceConfig -- the current InstanceConfigFile
    userConfig -- the current UserConfigFile
    systemConfig -- the current SystemConfigFile
    """
    if 'DIGITS_MODE_TEST' in os.environ and option.has_test_value():
        option.set(option.test_value())
        return

    suggestions = []
    instance_value = instanceConfig.get(option.config_file_key())
    if instance_value is not None:
        suggestions.append(
            prompt.Suggestion(instance_value,
                              '',
                              desc='Previous',
                              default=True))
    user_value = userConfig.get(option.config_file_key())
    if user_value is not None:
        suggestions.append(
            prompt.Suggestion(user_value, 'U', desc='User', default=True))
    system_value = systemConfig.get(option.config_file_key())
    if system_value is not None:
        suggestions.append(
            prompt.Suggestion(system_value, 'S', desc='System', default=True))
    suggestions += option.suggestions()
    if option.optional():
        suggestions.append(
            prompt.Suggestion('', 'N', desc='none', default=True))

    # Try to use the default values for options less than
    #   or equal to (LTE) this value
    try_defaults_lte = config_option.Visibility.DEFAULT

    if mode == 'verbose':
        try_defaults_lte = config_option.Visibility.NEVER
    elif mode == 'normal':
        try_defaults_lte = config_option.Visibility.HIDDEN
    elif mode == 'quiet':
        pass
    elif mode == 'force':
        pass
    else:
        raise config_option.BadValue('Unknown mode "%s"' % mode)

    valid = False
    if option.visibility() <= try_defaults_lte:
        # check for a valid default value
        for s in [s for s in suggestions if s.default]:
            try:
                option.set(s.value)
                valid = True
                break
            except config_option.BadValue as e:
                print 'Default value for %s "%s" invalid:' % (
                    option.config_file_key(), s.value)
                print '\t%s' % e
    if not valid:
        if mode == 'force':
            raise RuntimeError(
                'No valid default value found for configuration option "%s"' %
                option.config_file_key())
        else:
            # prompt user for value
            prompt.print_section_header(option.prompt_title())
            value = prompt.get_input(
                message=option.prompt_message(),
                validator=option.validate,
                suggestions=suggestions,
                is_path=option.is_path(),
            )
            print
            option.set(value)
            newConfig.set(option.config_file_key(), option._config_file_value)