Пример #1
0
    def download_package(self, group: str, artifact: str, version: str,
                         tmp_artifact_dir: str,
                         output: AbstractOutputWriter) -> bool:
        artifact_dir = self._get_artifact_dir(group, artifact, version)

        # download a file
        copy_dir(artifact_dir, tmp_artifact_dir)

        return True
Пример #2
0
    def upload_package(self, group: str, artifact: str, version: str,
                       tmp_artifact_dir: str,
                       output: AbstractOutputWriter) -> bool:
        artifact_dir = self._get_artifact_dir(group, artifact, version)

        # upload a file
        os.makedirs(os.path.dirname(artifact_dir), exist_ok=True)
        copy_dir(tmp_artifact_dir, artifact_dir)

        return True
Пример #3
0
    def _build(self):
        """Builds package."""
        if not self.working_dir:
            raise ValueError('Package doesn\'t have working directory')

        # working directory
        working_dir = os.path.join(self.project_dir, self.working_dir)
        if not dir_exists(working_dir):
            raise ValueError('Working directory doesn\'t exist')

        # package paths
        artifact_dir = self.get_artifact_dir(self.ENV_TMP)
        info_path = self.get_artifact_info_path(self.ENV_TMP)
        data_dir = self.get_artifact_data_dir(self.ENV_TMP)

        # remove artifact directory if it exists
        if dir_exists(artifact_dir):
            rmtree(artifact_dir)

        # create artifact data directory
        os.makedirs(data_dir, exist_ok=True)

        # copy files to package data directory
        if self.files:
            for filename in self.files:
                src_path = os.path.join(working_dir, filename)
                if not file_exists(src_path):
                    raise FileNotFoundError(
                        'File "%s" doesn\'t exist in the working directory' %
                        filename)

                copy_file(src_path, os.path.join(data_dir, filename))
        else:
            copy_dir(working_dir, data_dir)

        # get the list of copied files
        files = []
        for cur_dir, directories, filenames in os.walk(data_dir):
            relative_dir = cur_dir.replace(data_dir, '').lstrip(os.sep)
            for filename in filenames:
                files.append(os.path.join(relative_dir, filename))

        # create info.json file
        package_info = OrderedDict([('group', self.group),
                                    ('artifact', self.artifact),
                                    ('version', self.version),
                                    ('files', files), ('name', self.name),
                                    ('description', self.description),
                                    ('hash', get_dir_hash(data_dir))])
        with open(info_path, 'w+') as f:
            json.dump(package_info, f, indent=2)

        return artifact_dir
Пример #4
0
    def download(self, output: AbstractOutputWriter = None):
        """Downloads a package.
        This method only puts the package to the central directory,
        without updating the package's working directory.
        """
        if not output:
            output = NullOutputWriter()

        output.write('Downloading package "%s:%s:%s"... ' %
                     (self.group, self.artifact, self.version))

        with output.indent():
            # check if a package is already downloaded or published locally
            package_info = self.get_package_info()
            if package_info:
                if package_info.local:
                    output.write('[+] It\'s a locally published package')
                else:
                    output.write('[+] The package was already downloaded')

                return package_info

            # download dependency
            driver = self.repository.driver
            tmp_artifact_dir = self.get_artifact_dir(self.ENV_TMP)
            os.makedirs(tmp_artifact_dir, exist_ok=True)

            try:
                driver.download_package(self.group, self.artifact,
                                        self.version, tmp_artifact_dir, output)
            except Exception as e:
                output.write('[-] ' + str(e))
                return None

            # TODO: check "tmp_dir" contains info.json file, format is correct and a list of files matches "data" directory

            # move temporary directory to production one
            artifact_dir = self.get_artifact_dir(self.ENV_PRODUCTION)
            copy_dir(tmp_artifact_dir, artifact_dir)
            rmtree(tmp_artifact_dir)

            package_info = self.get_package_info()

            output.write('[+] The package was successfully downloaded')

        return package_info
Пример #5
0
    def publish(self,
                local: bool = False,
                rewrite_local: bool = False,
                output: AbstractOutputWriter = None) -> bool:
        """Publishes the package to the repository."""
        if not output:
            output = NullOutputWriter()

        # check if the package already exists on the local machine
        package_info = self.get_package_info()
        if package_info:
            if not package_info.local:
                output.write(
                    '[-] Version "%s" already exists in the repository' %
                    self.version)
                return False
            elif not rewrite_local:
                output.write(
                    '[-] Version "%s" already exists locally. Use "-r" flag to rewrite this version.'
                    % self.version)
                return False

        # paths for package
        artifact_dir = self.get_artifact_dir()
        local_artifact_dir = self.get_artifact_dir(Dependency.ENV_LOCAL)

        # build the package
        output.write('Building the package... ')

        with output.indent():
            try:
                tmp_artifact_dir = self._build()
            except Exception as e:
                output.write('[-] ' + str(e))
                return False

        # publish the package
        if local:
            output.write('Publishing the package locally... ')

            # move temporary directory to local one
            copy_dir(tmp_artifact_dir, local_artifact_dir)
            rmtree(tmp_artifact_dir)

            with output.indent():
                output.write(
                    '[+] Package "%s:%s:%s" was successfully published locally.'
                    % (self.group, self.artifact, self.version))
        else:
            output.write('Publishing the package... ')

            with output.indent():
                # upload a package
                driver = self.repository.driver

                try:
                    driver.upload_package(self.group, self.artifact,
                                          self.version, tmp_artifact_dir,
                                          output)
                except Exception as e:
                    rmtree(tmp_artifact_dir)  # remove building directory
                    output.write('[-] ' + str(e))
                    return False

                # move temporary directory to production one
                copy_dir(tmp_artifact_dir, artifact_dir)

                # remove building directory
                rmtree(tmp_artifact_dir)

                # remove local version of the same package if it exists
                if dir_exists(local_artifact_dir):
                    rmtree(local_artifact_dir)

                output.write(
                    '[+] Package "%s:%s:%s" was successfully published.' %
                    (self.group, self.artifact, self.version))

        # show published files
        package_info = self.get_package_info()

        output.write('\nPackage files:')
        with output.indent():
            for filename in package_info.files:
                output.write(filename)

        return True
Пример #6
0
    def update(self,
               rewrite_working_dir: bool = False,
               output: AbstractOutputWriter = None):
        """Downloads the package and updates the package's working directory."""
        if not output:
            output = NullOutputWriter()

        package_info = self.download(output)
        if not package_info or not self.working_dir:
            return

        # copy files to a working directory
        with output.indent():
            output.write('Copying files to the working directory "%s"...' %
                         self.working_dir)

            with output.indent():
                # create a working directory if it doesn't exist
                working_dir = os.path.join(self.project_dir, self.working_dir)
                os.makedirs(working_dir, exist_ok=True)

                # get package data directory
                env = Dependency.ENV_LOCAL if package_info.local else Dependency.ENV_PRODUCTION
                data_dir = self.get_artifact_data_dir(env)

                if self.files:
                    # copy only specified files if they don't exist in a target directory
                    for filename in self.files:
                        if filename in package_info.files:
                            src_path = os.path.join(data_dir, filename)
                            dst_path = os.path.join(working_dir, filename)

                            # adding to working directory only files which don't exist
                            if not file_exists(dst_path):
                                copy_file(src_path, dst_path)
                                output.write('[+] "%s": file copied' %
                                             filename)
                            elif rewrite_working_dir:
                                copy_file(src_path, dst_path)
                                output.write('[+] "%s": file rewritten' %
                                             filename)
                            else:
                                output.write('[-] "%s": file already exists' %
                                             filename)
                        else:
                            output.write(
                                '[-] "%s": file doesn\'t exist in the package'
                                % filename)
                else:
                    # copy all files only if a working directory is empty
                    if is_dir_empty(working_dir):
                        copy_dir(data_dir, working_dir)
                        output.write('[+] files copied to the "%s" directory' %
                                     self.working_dir)
                    elif rewrite_working_dir:
                        rmtree(working_dir)
                        copy_dir(data_dir, working_dir)
                        output.write('[+] directory "%s" was rewritten' %
                                     self.working_dir)
                    else:
                        output.write(
                            '[-] files not changed: directory "%s" is not empty'
                            % self.working_dir)