Ejemplo n.º 1
0
class TestBrinkFilesystem(BrinkTestCase):
    """
    System tests for `BrinkFileSystem`.
    """

    def setUp(self):
        super(TestBrinkFilesystem, self).setUp()

        self.brink_fs = BrinkFilesystem()
        self.test_segments = mk.fs.createFileInTemp(suffix='.bat')
        self.file_name = self.test_segments[-1:][0]
        folder_segments = self.test_segments[:-1]
        self.folder = mk.fs.getRealPathFromSegments(folder_segments)

    def test_which_file_exists(self):
        """
        Returns the full path to the specified command if the file is
        found.
        """
        full_path = mk.fs.getRealPathFromSegments(self.test_segments)

        if os.name == 'posix' and not sys.platform.startswith('darwin'):
            command = self.file_name.encode('utf-8')
            folder = self.folder.encode('utf-8')
            full_path = full_path.encode('utf-8')
        else:
            command = self.file_name
            folder = self.folder
        extra_paths = [mk.ascii(), folder]

        result = self.brink_fs.which(command, extra_paths=extra_paths)

        self.assertEqual(full_path, result)

    def test_which_file_exists_no_extension(self):
        """
        On Windows, it returns the full path to the specified command even
        when the executable extension was not provided.
        """
        if os.name != 'nt':
            raise self.skipTest()

        extra_paths = [mk.string(), self.folder]
        command = self.file_name.replace('.bat', '')
        full_path = mk.fs.getRealPathFromSegments(self.test_segments)

        result = self.brink_fs.which(command, extra_paths=extra_paths)

        self.assertEqual(full_path, result)

    def test_which_not_exist(self):
        """
        Returns `None` if the specified command could not be found.
        """
        unknown_command = mk.string()

        result = self.brink_fs.which(unknown_command)

        self.assertIsNone(result)
Ejemplo n.º 2
0
    def __init__(self, setup):
        self.setup = setup
        self._default_values = self._getDefaultValues()
        self.os_name = self._default_values['os_name']
        self.cpu = self._default_values['platform']
        self.python_version = self._default_values['python_version']

        self.fs = BrinkFilesystem()
        self.path = ProjectPaths(
            os_name=self.os_name,
            build_folder_name=self._default_values['build_folder'],
            folders=self.setup['folders'],
            filesystem=self.fs,
            )
        self.git = BrinkGit(filesystem=self.fs)

        self.sphinx = BrinkSphinx(paver=self)

        self.python_command_normal = [self.path.python_executable]
        if self.os_name == 'windows':
            self.python_command_super = [self.path.python_executable]
        elif self.os_name == 'sles10':
            self.python_command_super = [
                'sudo',
                self.path.python_executable,
                ]
        else:
            CODECOV_TOKEN = os.getenv('CODECOV_TOKEN', '')
            self.python_command_super = [
                'sudo',
                'CODECOV_TOKEN=%s' % (CODECOV_TOKEN,),
                self.path.python_executable,
                ]
Ejemplo n.º 3
0
    def setUp(self):
        super(TestBrinkFilesystem, self).setUp()

        self.brink_fs = BrinkFilesystem()
        self.test_segments = mk.fs.createFileInTemp(suffix='.bat')
        self.file_name = self.test_segments[-1:][0]
        folder_segments = self.test_segments[:-1]
        self.folder = mk.fs.getRealPathFromSegments(folder_segments)
Ejemplo n.º 4
0
    def setUp(self):
        super(TestBrinkFilesystem, self).setUp()

        self.brink_fs = BrinkFilesystem()
        self.brink_fs._isValidSystemPath = self.Mock(return_value=True)
Ejemplo n.º 5
0
class TestBrinkFilesystem(BrinkTestCase):
    """
    Unit tests for `BrinkFilesystem`.
    """

    def setUp(self):
        super(TestBrinkFilesystem, self).setUp()

        self.brink_fs = BrinkFilesystem()
        self.brink_fs._isValidSystemPath = self.Mock(return_value=True)

    def test_which_extra_paths_not_defined(self):
        """
        Returns `None` if the command could not be found in system path or
        in the `extra_paths` argument.
        """
        command = mk.string()
        extra_paths = [mk.string()]
        self.brink_fs.listFolder = self.Mock(return_value=[])

        result = self.brink_fs.which(command, extra_paths=extra_paths)

        self.assertIsNone(result)

    def test_which_extra_paths_file_found(self):
        """
        Returns the full path to the specified command if found and the
        executable file exists.
        """
        folder = mk.string()
        command = mk.string()
        full_path_to_command = os.path.join(folder, command)
        extra_paths = [mk.string(), folder]

        def _folderListing(path):
            result = []
            if folder == path:
                result.append(command)
            return result

        self.brink_fs.listFolder = _folderListing

        result = self.brink_fs.which(command, extra_paths=extra_paths)

        self.assertEqual(full_path_to_command, result)

    def test_which_extra_paths_file_not_exists(self):
        """
        Returns `None` if the specified command is defined in `extra_paths`
        but the actual, executable, file does not exist.
        """
        command = mk.string()
        path_exe_file = "%s.exe" % command
        self.brink_fs.listFolder = self.Mock(return_value=[])
        extra_paths = [mk.string(), path_exe_file]

        result = self.brink_fs.which(command, extra_paths=extra_paths)

        self.assertIsNone(result)

    def test_parseUnixPaths(self):
        """
        It uses : to separate path entries.
        """
        path1 = mk.string()
        path2 = mk.string()
        paths = u"%s:%s" % (path1, path2)

        result = self.brink_fs._parseUnixPaths(paths)

        self.assertEqual(2, len(result))
        self.assertContains(path1, result)
        self.assertContains(path2, result)

    def test_parseWindowsPaths(self):
        """
        It uses ; to separate path entries.
        """
        path1 = mk.string()
        path2 = mk.string()
        paths = u"%s;%s" % (path1, path2)

        result = self.brink_fs._parseWindowsPaths(paths)

        self.assertEqual(2, len(result))
        self.assertContains(path1, result)
        self.assertContains(path2, result)

    def test_findCommand_ok_posix(self):
        """
        On Unix systems it does not validate Windows type executable files
        as valid commands.
        """
        if os.name == "nt":
            raise self.skipTest("Unix specific test.")

        command = mk.string()
        path = mk.string()
        full_path = "%s/%s" % (path, command)
        exe_command = "%s.exe" % command
        bat_command = "%s.bat" % command
        cmd_command = "%s.cmd" % command
        files = [exe_command, bat_command, cmd_command]
        self.brink_fs.listFolder = self.Mock(return_value=files)

        result = self.brink_fs._findCommand(command, path)

        self.assertIsNone(result)

        files.append(command)

        result = self.brink_fs._findCommand(command, path)

        self.assertEqual(full_path, result)

    def test_findCommand_ok_windows(self):
        """
        On Windows systems it validates files with .exe extension before
        checking files without extension.
        """
        if os.name != "nt":
            raise self.skipTest("Windows specific test.")

        command = mk.string()
        path = mk.string()
        exe_command = "%s.exe" % command
        full_path = "%s\%s" % (path, exe_command)
        files = [command, exe_command]
        self.brink_fs.listFolder = self.Mock(return_value=files)

        result = self.brink_fs._findCommand(command, path)

        self.assertEqual(full_path, result)

    def test_findCommand_ok_windows_with_extension(self):
        """
        On Windows systems it validates files with .exe extension, even
        when the command is specified with explicit .exe extension.
        """
        if os.name != "nt":
            raise self.skipTest("Windows specific test.")

        command = mk.string()
        path = mk.string()
        exe_command = "%s.exe" % command
        full_path = "%s\%s" % (path, exe_command)
        files = [command, exe_command]
        self.brink_fs.listFolder = self.Mock(return_value=files)

        result = self.brink_fs._findCommand(exe_command, path)

        self.assertEqual(full_path, result)

    def test_findCommand_checks_folders_only(self):
        """
        Returns `None` if the path argument is not a folder.
        """
        command = mk.string()
        path = mk.string()
        self.brink_fs._isValidSystemPath = self.Mock(return_value=False)

        result = self.brink_fs._findCommand(command, path)

        self.assertIsNone(result)

    def test_findCommand_no_result(self):
        """
        Returns `None` if the command could not be found in the specified
        path.
        """
        command = mk.string()
        path = mk.string()
        files = [mk.string(), mk.string()]
        self.brink_fs.listFolder = self.Mock(return_value=files)

        result = self.brink_fs._findCommand(command, path)

        self.assertIsNone(result)

    def test_getSearchPaths_default(self):
        """
        It returns a list of paths from operating system environment
        variable.
        """
        if os.name == "posix":
            paths = self.brink_fs._parseUnixPaths(os.environ["PATH"])
        else:
            paths = self.brink_fs._parseWindowsPaths(os.environ["PATH"])

        result = self.brink_fs._getSearchPaths()

        self.assertEqual(paths, result)

    def test_getSearchPaths_with_extra(self):
        """
        If a list of extra paths if provided, it return a list with paths
        defined in the operating system, but with the list of extra paths
        prepended to the result.
        """
        path1 = mk.string
        path2 = mk.string
        if os.name == "posix":
            path3 = self.brink_fs._parseUnixPaths(os.environ["PATH"])[0]
        else:
            path3 = self.brink_fs._parseWindowsPaths(os.environ["PATH"])[0]

        result = self.brink_fs._getSearchPaths(extra_paths=[path1, path2])

        self.assertEqual(path1, result[0])
        self.assertEqual(path2, result[1])
        self.assertEqual(path3, result[2])
Ejemplo n.º 6
0
class BrinkPaver(object):
    """
    Collection of methods to help with build system.
    """

    def __init__(self, setup):
        self.setup = setup
        self._default_values = self._getDefaultValues()
        self.os_name = self._default_values['os_name']
        self.cpu = self._default_values['platform']
        self.python_version = self._default_values['python_version']

        self.fs = BrinkFilesystem()
        self.path = ProjectPaths(
            os_name=self.os_name,
            build_folder_name=self._default_values['build_folder'],
            folders=self.setup['folders'],
            filesystem=self.fs,
            )
        self.git = BrinkGit(filesystem=self.fs)

        self.sphinx = BrinkSphinx(paver=self)

        self.python_command_normal = [self.path.python_executable]
        if self.os_name == 'windows':
            self.python_command_super = [self.path.python_executable]
        elif self.os_name == 'sles10':
            self.python_command_super = [
                'sudo',
                self.path.python_executable,
                ]
        else:
            CODECOV_TOKEN = os.getenv('CODECOV_TOKEN', '')
            self.python_command_super = [
                'sudo',
                'CODECOV_TOKEN=%s' % (CODECOV_TOKEN,),
                self.path.python_executable,
                ]

    def _getDefaultValues(self):
        '''Get the default build folder and python version.'''
        with open('DEFAULT_VALUES') as default_values:
            output = default_values.read()
        results = output.strip().split(' ')
        default_values = {
            'build_folder': results[0],
            'python_version': results[1],
            'os_name': results[2],
            'platform': results[3],
            }
        return default_values

    def execute(self, *args, **kwargs):
        """
        Shortcut to execute function.

        This is here to avoid importing the execute function and also help
        with testing.
        """
        return execute(*args, **kwargs)

    def pip(self, command='install', arguments=None,
            exit_on_errors=True, index_url=None, only_cache=False,
            install_hook=None, silent=False):
        """
        Execute the pip command.
        """

        # Reset packages state before each run.
        # Pip does not support multiple runs from a single instances,
        # so installed packages are cached per instance.
        from pkg_resources import working_set
        working_set.entries = []
        working_set.entry_keys = {}
        working_set.by_key = {}
        list(map(working_set.add_entry, sys.path))

        from pip import main

        pip_build_path = [self.path.build, 'pip-build']
        self.fs.deleteFolder(pip_build_path)

        if index_url is None:
            index_url = self.setup['pypi']['index_url']

        if arguments is None:
            arguments = []

        pip_arguments = [command]

        if command == 'install':
            pip_arguments.extend(['--trusted-host', 'pypi.chevah.com'])
            if only_cache:
                pip_arguments.extend(['--no-index'])
            else:
                pip_arguments.extend(
                    ['--index-url=' + index_url])

            if install_hook:
                pip_arguments.extend([
                    '--install-hook=%s' % (install_hook)])

            pip_arguments.extend(
                ['--cache-dir=' + self.path.cache])

            pip_build = self.fs.join(pip_build_path)
            if self.os_name != 'windows':
                # On Non Windows, pip will fail if we pass an Unicode
                # build path.
                pip_build = pip_build.encode('utf-8')
            pip_arguments.extend(['--build', pip_build])

            pip_arguments.extend([
                '--use-wheel',
                ])

        if silent:
            pip_arguments.extend(['-q'])

        pip_arguments.extend(arguments)

        result = main(args=pip_arguments)

        if result != 0 and exit_on_errors:
            print("Failed to run:\npip %s" % (' '.join(pip_arguments)))
            sys.exit(result)

        return result

    def getOption(
            self, options, task_name, option_name,
            default_value=None, required=False):
        '''Return the paver option_name passed to task_name.'''
        try:
            task_options = options[task_name]
        except KeyError:
            return default_value

        try:
            value = task_options[option_name]
        except KeyError:
            if required:
                print('It is required to provide option "%s".' % (option_name))
                sys.exit(1)
            value = default_value
        return value

    def getIPAddress(self, gateway='172.20.0.1'):
        '''Return the local public IP address.'''
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect((gateway, 80))
        address = s.getsockname()[0]
        s.close()
        return address

    def getHostname(self):
        '''Return the hostname of the local computer.'''
        hostname = socket.gethostname()
        if hostname.startswith('buildslave-'):
            hostname = hostname[11:]
        if hostname.startswith('bs-'):
            hostname = hostname[3:]
        return hostname

    def createTarGZArchive(self, folder_name):
        tar_name = folder_name + '.tar'
        tar_command = [
            'pax', '-wf', tar_name, folder_name]
        execute(tar_command)
        gzip_command = ['gzip', '-f', tar_name]
        execute(gzip_command)

    def createZipArchive(self, source, destination, exclude=None):
        """
        Create a zip file at `destination` based on files from `source`.
        """
        """
        Create a zip file at `destination` based on files from `source`.
        """
        if exclude is None:
            exclude = []

        source_path = self.fs.join(source)
        parent_path = os.path.dirname(source_path)
        archivename = self.fs.join(destination)
        with closing(ZipFile(archivename, 'w', ZIP_DEFLATED)) as z:
            for root, dirs, files in os.walk(source_path):
                # Write all files.
                for fn in files:
                    if fn in exclude:
                        continue
                    absolute_filename = os.path.join(root, fn)
                    zip_filename = absolute_filename[len(parent_path):]
                    # See http://bugs.python.org/issue1734346
                    # for adding unicode support.
                    z.write(str(absolute_filename), str(zip_filename))

                # For empty folders, we need to create a special ZipInfo
                # entry.
                # 16 works, but some places suggest using 48.
                if not files and not dirs:
                    foldername = root[len(parent_path):] + '/'
                    zip_info = ZipInfo(foldername)
                    zip_info.external_attr = 16
                    z.writestr(zip_info, "")

    def createMD5Sum(self, source):
        '''
        Returns an MD5 hash for the file specified by file_path.
        '''
        md5hash = md5()

        with open(self.fs.join(source), 'rb') as input_file:
            while True:
                read_buffer = input_file.read(8096)
                if not read_buffer:
                    break
                md5hash.update(read_buffer)

            result = md5hash.hexdigest()

        return result

    def createNSIS(
            self, folder_name, product_name, product_version,
            product_url, product_publisher):
        '''Generate a self extracted install file using NSIS.'''
        defines = (
            '!define PRODUCT_NAME "%s"\n'
            '!define PRODUCT_VERSION "%s"\n'
            '!define PRODUCT_PUBLISHER "%s"\n'
            '!define PRODUCT_URL "%s"\n'
            '!define DISTRIBUTABLE_NAME "%s"\n' % (
                product_name,
                product_version,
                product_publisher,
                product_url,
                folder_name,
                ))

        target = self.fs.join([self.path.dist, folder_name])
        target_nsis_path = self.fs.join([target, 'windows-installer.nsi'])
        template_nsis_path = self.fs.join([
            self.path.product,
            self.setup['folders']['source'],
            self.setup['folders']['static'],
            self.setup['folders']['nsis'],
            'windows-installer.nsi.in',
            ])

        with open(target_nsis_path, 'w') as nsis_file:
            # Write constants file.
            nsis_file.write(defines)
            # Append template nsis file
            for line in open(template_nsis_path):
                nsis_file.write(line)

        nsis_locations = [
            r'C:\Program Files (x86)\NSIS',
            r'C:\Program Files\NSIS',
            ]
        make_nsis_path = self.fs.which('makensis', nsis_locations)
        if not make_nsis_path:
            print(
                'NullSoft Installer is not installed. '
                'On Ubuntu you can install it using '
                '"sudo apt-get install nsis".'
                )
            sys.exit(1)

        make_nsis_command = [make_nsis_path, '-V2', 'windows-installer.nsi']

        try:
            with self.fs.changeFolder([target]):
                print("Executing %s" % make_nsis_command)
                subprocess.call(make_nsis_command)
        except OSError, os_error:
            if os_error.errno != 2:
                raise
            print(
                'NullSoft Installer is not installed. '
                'On Ubuntu you can install it using '
                '"sudo apt-get install nsis".'
                )
            sys.exit(1)
        self.fs.deleteFile(target_nsis_path)
Ejemplo n.º 7
0
class TestBrinkFilesystem(BrinkTestCase):
    """
    Unit tests for `BrinkFilesystem`.
    """

    def setUp(self):
        super(TestBrinkFilesystem, self).setUp()

        self.sut = BrinkFilesystem()
        self.sut._isValidSystemPath = self.Mock(return_value=True)

    def test_which_extra_paths_not_defined(self):
        """
        Returns `None` if the command could not be found in system path or
        in the `extra_paths` argument.
        """
        command = mk.string()
        extra_paths = [mk.string()]
        self.sut.listFolder = self.Mock(return_value=[])

        result = self.sut.which(command, extra_paths=extra_paths)

        self.assertIsNone(result)

    def test_which_extra_paths_file_found(self):
        """
        Returns the full path to the specified command if found and the
        executable file exists.
        """
        folder = mk.string()
        command = mk.string()
        full_path_to_command = os.path.join(folder, command)
        extra_paths = [mk.string(), folder]

        def _folderListing(path):
            result = []
            if folder == path:
                result.append(command)
            return result

        self.sut.listFolder = _folderListing

        result = self.sut.which(command, extra_paths=extra_paths)

        self.assertEqual(full_path_to_command, result)

    def test_which_extra_paths_file_not_exists(self):
        """
        Returns `None` if the specified command is defined in `extra_paths`
        but the actual, executable, file does not exist.
        """
        command = mk.string()
        path_exe_file = '%s.exe' % command
        self.sut.listFolder = self.Mock(return_value=[])
        extra_paths = [mk.string(), path_exe_file]

        result = self.sut.which(command, extra_paths=extra_paths)

        self.assertIsNone(result)

    def test_parseUnixPaths(self):
        """
        It uses : to separate path entries.
        """
        path1 = mk.string()
        path2 = mk.string()
        paths = u'%s:%s' % (path1, path2)

        result = self.sut._parseUnixPaths(paths)

        self.assertEqual(2, len(result))
        self.assertContains(path1, result)
        self.assertContains(path2, result)

    def test_parseWindowsPaths(self):
        """
        It uses ; to separate path entries.
        """
        path1 = mk.string()
        path2 = mk.string()
        paths = u'%s;%s' % (path1, path2)

        result = self.sut._parseWindowsPaths(paths)

        self.assertEqual(2, len(result))
        self.assertContains(path1, result)
        self.assertContains(path2, result)

    def test_findCommand_ok_posix(self):
        """
        On Unix systems it does not validate Windows type executable files
        as valid commands.
        """
        if os.name == 'nt':
            raise self.skipTest("Unix specific test.")

        command = mk.string()
        path = mk.string()
        full_path = '%s/%s' % (path, command)
        exe_command = '%s.exe' % command
        bat_command = '%s.bat' % command
        cmd_command = '%s.cmd' % command
        files = [
            exe_command,
            bat_command,
            cmd_command,
            ]
        self.sut.listFolder = self.Mock(return_value=files)

        result = self.sut._findCommand(command, path)

        self.assertIsNone(result)

        files.append(command)

        result = self.sut._findCommand(command, path)

        self.assertEqual(full_path, result)

    def test_findCommand_ok_windows(self):
        """
        On Windows systems it validates files with .exe extension before
        checking files without extension.
        """
        if os.name != 'nt':
            raise self.skipTest("Windows specific test.")

        command = mk.string()
        path = mk.string()
        exe_command = '%s.exe' % command
        full_path = '%s\%s' % (path, exe_command)
        files = [
            command,
            exe_command,
            ]
        self.sut.listFolder = self.Mock(return_value=files)

        result = self.sut._findCommand(command, path)

        self.assertEqual(full_path, result)

    def test_findCommand_ok_windows_with_extension(self):
        """
        On Windows systems it validates files with .exe extension, even
        when the command is specified with explicit .exe extension.
        """
        if os.name != 'nt':
            raise self.skipTest("Windows specific test.")

        command = mk.string()
        path = mk.string()
        exe_command = '%s.exe' % command
        full_path = '%s\%s' % (path, exe_command)
        files = [
            command,
            exe_command,
            ]
        self.sut.listFolder = self.Mock(return_value=files)

        result = self.sut._findCommand(exe_command, path)

        self.assertEqual(full_path, result)

    def test_findCommand_checks_folders_only(self):
        """
        Returns `None` if the path argument is not a folder.
        """
        command = mk.string()
        path = mk.string()
        self.sut._isValidSystemPath = self.Mock(return_value=False)

        result = self.sut._findCommand(command, path)

        self.assertIsNone(result)

    def test_findCommand_no_result(self):
        """
        Returns `None` if the command could not be found in the specified
        path.
        """
        command = mk.string()
        path = mk.string()
        files = [mk.string(), mk.string()]
        self.sut.listFolder = self.Mock(return_value=files)

        result = self.sut._findCommand(command, path)

        self.assertIsNone(result)

    def test_getSearchPaths_default(self):
        """
        It returns a list of paths from operating system environment
        variable.
        """
        if os.name == 'posix':
            paths = self.sut._parseUnixPaths(os.environ['PATH'])
        else:
            paths = self.sut._parseWindowsPaths(os.environ['PATH'])

        result = self.sut._getSearchPaths()

        self.assertEqual(paths, result)

    def test_getSearchPaths_with_extra(self):
        """
        If a list of extra paths if provided, it return a list with paths
        defined in the operating system, but with the list of extra paths
        prepended to the result.
        """
        path1 = mk.string
        path2 = mk.string
        if os.name == 'posix':
            path3 = self.sut._parseUnixPaths(os.environ['PATH'])[0]
        else:
            path3 = self.sut._parseWindowsPaths(os.environ['PATH'])[0]

        result = self.sut._getSearchPaths(extra_paths=[path1, path2])

        self.assertEqual(path1, result[0])
        self.assertEqual(path2, result[1])
        self.assertEqual(path3, result[2])

    def test_copyFolder_no_overwrite(self):
        """
        It can copy the folder without overwriting existing files.
        """
        source_segments = mk.fs.createFolderInTemp(prefix=u'src-')
        destination_segments = mk.fs.createFolderInTemp(prefix=u'dst-')
        self.addCleanup(lambda: mk.fs.deleteFolder(source_segments))
        self.addCleanup(lambda: mk.fs.deleteFolder(destination_segments))
        existing_name = mk.makeFilename()
        existing_source_segments = source_segments + [existing_name]
        existing_destination_segments = destination_segments + [existing_name]
        mk.fs.createFile(existing_source_segments, content=b'source-exist')
        mk.fs.createFile(
            existing_destination_segments, content=b'destination-exist')
        mk.fs.createFile(source_segments + [u'other-file'], content=b'other')

        self.sut.copyFolder(
            source=[u'/'] + source_segments,
            destination=[u'/'] + destination_segments,
            overwrite=False,
            )

        self.assertTrue(
            mk.fs.exists(destination_segments + [u'other-file']))
        self.assertTrue(
            mk.fs.exists(existing_destination_segments))
        self.assertEqual(
            b'destination-exist',
            mk.fs.getFileContent(existing_destination_segments, utf8=False))

    def test_join_rejoin_unicode(self):
        """
        It can join and re-join the result from unicode path.
        """
        first_join = self.sut.join([mk.makeFilename(), mk.makeFilename()])
        second_join = self.sut.join([first_join, mk.makeFilename()])

        if os.name == 'posix':
            self.assertIsInstance(str, second_join)
        else:
            self.assertIsInstance(unicode, second_join)