<commit_before>#! /usr/bin/env python
#
# Builds binary and source RPMs for a CSDMS model or tool.
#
# Examples:
#   $ python build.py --help
#   $ python build.py hydrotrend
#   $ python build.py babel
#   $ python build.py cem --tag 0.2
#
# <NAME> (<EMAIL>)

import sys, os, shutil
from subprocess import call
import string
import glob
import shlex
import tempfile
from packager.core import repo_tools as repo
from packager.core.flavor import debian_check

class BuildRPM:
    '''
    Uses `rpmbuild` to build a CSDMS model or tool into an RPM.
    '''
    def __init__(self, module_name, module_version, local_dir):
        self.module = module_name
        self.version = "head" if module_version == None else module_version

        # Get module setup files 1) from GitHub and store in a tmp directory,
        # or 2) from a local directory.
        if local_dir == None:
            self.tmpdir = tempfile.mkdtemp()
            self.module_dir = repo.get_module(self.module, dest=self.tmpdir)
        else:
            self.module_dir = local_dir
        if self.module_dir == None:
            print("The module '" + self.module + "' cannot be located.")
            sys.exit(3) # can't find module

        # Model setup files.
        self.deps_file = os.path.join(self.module_dir, "dependencies.txt")
        self.source_file = os.path.join(self.module_dir, "source.txt")
        self.spec_file = os.path.join(self.module_dir, self.module + ".spec")

        # Set up the local rpmbuild directory.
        self.rpmbuild = os.path.join(os.getenv("HOME"), "rpmbuild", "")
        self.prep_directory()

        # Download the module's source code. Make a tarball.
        self.get_source()
        if self.needs_tarball: self.make_tarball()

        # Apply patches, if any.
        self.patch()

        # Build the binary and source RPMs.
        self.is_debian = debian_check()
        self.get_dependencies()
        self.build()
        self.cleanup()
        print("Success!")

    def prep_directory(self):
        '''
        Prepares the RPM build directory (~/rpmbuild) with built-in RPM dev
        tools. Sets up member variables for paths in the build directory.
        '''
        print("Setting up rpmbuild directory structure.")
        if os.path.isdir(self.rpmbuild):
            call(["rpmdev-wipetree"])
        else:
            call(["rpmdev-setuptree"])
        self.sources_dir = os.path.join(self.rpmbuild,  "SOURCES", "")
        self.specs_dir = os.path.join(self.rpmbuild,  "SPECS", "")<commit_msg>Check validity of "--local" path<commit_after>#! /usr/bin/env python
#
# Builds binary and source RPMs for a CSDMS model or tool.
#
# Examples:
#   $ python build.py --help
#   $ python build.py hydrotrend
#   $ python build.py babel
#   $ python build.py cem --tag 0.2
#
# <NAME> (<EMAIL>)

import sys, os, shutil
from subprocess import call
import string
import glob
import shlex
import tempfile
from packager.core import repo_tools as repo
from packager.core.flavor import debian_check

class BuildRPM:
    '''
    Uses `rpmbuild` to build a CSDMS model or tool into an RPM.
    '''
    def __init__(self, module_name, module_version, local_dir):
        self.module = module_name
        self.version = "head" if module_version == None else module_version

        # Get module setup files 1) from GitHub and store in a tmp directory,
        # or 2) from a local directory.
        if local_dir == None:
            self.tmpdir = tempfile.mkdtemp()
            self.module_dir = repo.get_module(self.module, dest=self.tmpdir)
        else:
            self.module_dir = self.get_local_dir(local_dir)
        if self.module_dir == None:
            print("The module '" + self.module + "' cannot be located.")
            sys.exit(3) # can't find module

        # Model setup files.
        self.deps_file = os.path.join(self.module_dir, "dependencies.txt")
        self.source_file = os.path.join(self.module_dir, "source.txt")
        self.spec_file = os.path.join(self.module_dir, self.module + ".spec")

        # Set up the local rpmbuild directory.
        self.rpmbuild = os.path.join(os.getenv("HOME"), "rpmbuild", "")
        self.prep_directory()

        # Download the module's source code. Make a tarball.
        self.get_source()
        if self.needs_tarball: self.make_tarball()

        # Apply patches, if any.
        self.patch()

        # Build the binary and source RPMs.
        self.is_debian = debian_check()
        self.get_dependencies()
        self.build()
        self.cleanup()
        print("Success!")

    def get_local_dir(self, locdir):
        '''
        Checks that the directory path passed with "--local" is valid.
        '''
        if os.path.basename(os.path.normpath(locdir)) == self.module:
            return locdir
        elif os.path.isdir(os.path.join(locdir, self.module)):
            return os.path.join(locdir, self.module)
        else:
            print("The specified \"--local\" directory cannot be found.")
            sys.exit(5) # local directory doesn't exist

    def prep_directory(self):
        '''
        Prepares the RPM build directory (~/rpmbuild) with built-in RPM dev
        tools. Sets up member variables for paths in the build directory.
        '''
        print("Setting up rpmbuild directory structure.")
        if os.path.isdir(self.rpmbuild):
            call(["rpmdev-wipetree"])
        else:
            call(["rpmdev-setuptree"])
        self.sources_dir = os.path.join(self.rpmbuild,  "SOURCES", "")
        self.specs_dir = os.path.join(self.rpmbuild,  "SPECS", "")
{'fraction_non_alphanumeric': 0.11214285714285714, 'fraction_numerical': 0.0019642857142857144, 'mean_word_length': 2.365985576923077, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '76543', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1697, 'n_words': 482}