def _create_repo(self, args):
        '''
        Scan a directory and create an SPM-METADATA file which describes
        all of the SPM files in that directory.
        '''
        if len(args) < 2:
            raise SPMInvocationError('A path to a directory must be specified')

        if args[1] == '.':
            repo_path = os.getcwdu()
        else:
            repo_path = args[1]

        old_files = []
        repo_metadata = {}
        for (dirpath, dirnames, filenames) in salt.utils.path.os_walk(repo_path):
            for spm_file in filenames:
                if not spm_file.endswith('.spm'):
                    continue
                spm_path = '{0}/{1}'.format(repo_path, spm_file)
                if not tarfile.is_tarfile(spm_path):
                    continue
                comps = spm_file.split('-')
                spm_name = '-'.join(comps[:-2])
                spm_fh = tarfile.open(spm_path, 'r:bz2')
                formula_handle = spm_fh.extractfile('{0}/FORMULA'.format(spm_name))
                formula_conf = salt.utils.yaml.safe_load(formula_handle.read())

                use_formula = True
                if spm_name in repo_metadata:
                    # This package is already in the repo; use the latest
                    cur_info = repo_metadata[spm_name]['info']
                    new_info = formula_conf
                    if int(new_info['version']) == int(cur_info['version']):
                        # Version is the same, check release
                        if int(new_info['release']) < int(cur_info['release']):
                            # This is an old release; don't use it
                            use_formula = False
                    elif int(new_info['version']) < int(cur_info['version']):
                        # This is an old version; don't use it
                        use_formula = False

                    if use_formula is True:
                        # Ignore/archive/delete the old version
                        log.debug(
                            '%s %s-%s had been added, but %s-%s will replace it',
                            spm_name, cur_info['version'], cur_info['release'],
                            new_info['version'], new_info['release']
                        )
                        old_files.append(repo_metadata[spm_name]['filename'])
                    else:
                        # Ignore/archive/delete the new version
                        log.debug(
                            '%s %s-%s has been found, but is older than %s-%s',
                            spm_name, new_info['version'], new_info['release'],
                            cur_info['version'], cur_info['release']
                        )
                        old_files.append(spm_file)

                if use_formula is True:
                    log.debug(
                        'adding %s-%s-%s to the repo',
                        formula_conf['name'], formula_conf['version'],
                        formula_conf['release']
                    )
                    repo_metadata[spm_name] = {
                        'info': formula_conf.copy(),
                    }
                    repo_metadata[spm_name]['filename'] = spm_file

        metadata_filename = '{0}/SPM-METADATA'.format(repo_path)
        with salt.utils.files.fopen(metadata_filename, 'w') as mfh:
            salt.utils.yaml.safe_dump(
                repo_metadata,
                mfh,
                indent=4,
                canonical=False,
                default_flow_style=False,
            )

        log.debug('Wrote %s', metadata_filename)

        for file_ in old_files:
            if self.opts['spm_repo_dups'] == 'ignore':
                # ignore old packages, but still only add the latest
                log.debug('%s will be left in the directory', file_)
            elif self.opts['spm_repo_dups'] == 'archive':
                # spm_repo_archive_path is where old packages are moved
                if not os.path.exists('./archive'):
                    try:
                        os.makedirs('./archive')
                        log.debug('%s has been archived', file_)
                    except IOError:
                        log.error('Unable to create archive directory')
                try:
                    shutil.move(file_, './archive')
                except (IOError, OSError):
                    log.error('Unable to archive %s', file_)
            elif self.opts['spm_repo_dups'] == 'delete':
                # delete old packages from the repo
                try:
                    os.remove(file_)
                    log.debug('%s has been deleted', file_)
                except IOError:
                    log.error('Unable to delete %s', file_)
                except OSError:
                    # The file has already been deleted
                    pass
def _create_repo ( self , args ) : if len ( args ) < 2 : raise SPMInvocationError ( 'A path to a directory must be specified' ) if args [ 1 ] == '.' : repo_path = os . getcwdu ( ) else : repo_path = args [ 1 ] old_files = [ ] repo_metadata = { } for ( dirpath , dirnames , filenames ) in salt . utils . path . os_walk ( repo_path ) : for spm_file in filenames : if not spm_file . endswith ( '.spm' ) : continue spm_path = '{0}/{1}' . format ( repo_path , spm_file ) if not tarfile . is_tarfile ( spm_path ) : continue comps = spm_file . split ( '-' ) spm_name = '-' . join ( comps [ : - 2 ] ) spm_fh = tarfile . open ( spm_path , 'r:bz2' ) formula_handle = spm_fh . extractfile ( '{0}/FORMULA' . format ( spm_name ) ) formula_conf = salt . utils . yaml . safe_load ( formula_handle . read ( ) ) use_formula = True if spm_name in repo_metadata : cur_info = repo_metadata [ spm_name ] [ 'info' ] new_info = formula_conf if int ( new_info [ 'version' ] ) == int ( cur_info [ 'version' ] ) : if int ( new_info [ 'release' ] ) < int ( cur_info [ 'release' ] ) : use_formula = False elif int ( new_info [ 'version' ] ) < int ( cur_info [ 'version' ] ) : use_formula = False if use_formula is True : log . debug ( '%s %s-%s had been added, but %s-%s will replace it' , spm_name , cur_info [ 'version' ] , cur_info [ 'release' ] , new_info [ 'version' ] , new_info [ 'release' ] ) old_files . append ( repo_metadata [ spm_name ] [ 'filename' ] ) else : log . debug ( '%s %s-%s has been found, but is older than %s-%s' , spm_name , new_info [ 'version' ] , new_info [ 'release' ] , cur_info [ 'version' ] , cur_info [ 'release' ] ) old_files . append ( spm_file ) if use_formula is True : log . debug ( 'adding %s-%s-%s to the repo' , formula_conf [ 'name' ] , formula_conf [ 'version' ] , formula_conf [ 'release' ] ) repo_metadata [ spm_name ] = { 'info' : formula_conf . copy ( ) , } repo_metadata [ spm_name ] [ 'filename' ] = spm_file metadata_filename = '{0}/SPM-METADATA' . format ( repo_path ) with salt . utils . files . fopen ( metadata_filename , 'w' ) as mfh : salt . utils . yaml . safe_dump ( repo_metadata , mfh , indent = 4 , canonical = False , default_flow_style = False , ) log . debug ( 'Wrote %s' , metadata_filename ) for file_ in old_files : if self . opts [ 'spm_repo_dups' ] == 'ignore' : log . debug ( '%s will be left in the directory' , file_ ) elif self . opts [ 'spm_repo_dups' ] == 'archive' : if not os . path . exists ( './archive' ) : try : os . makedirs ( './archive' ) log . debug ( '%s has been archived' , file_ ) except IOError : log . error ( 'Unable to create archive directory' ) try : shutil . move ( file_ , './archive' ) except ( IOError , OSError ) : log . error ( 'Unable to archive %s' , file_ ) elif self . opts [ 'spm_repo_dups' ] == 'delete' : try : os . remove ( file_ ) log . debug ( '%s has been deleted' , file_ ) except IOError : log . error ( 'Unable to delete %s' , file_ ) except OSError : pass
Scan a directory and create an SPM-METADATA file which describes
        all of the SPM files in that directory.