<commit_before>#!/usr/bin/env python

import os
import sys
import glob
import shutil
import subprocess as sp

try:
    from setuptools import setup, Extension
    from setuptools.command.build_ext import build_ext
except ImportError:
    from distutils.core import setup, Extension
    from distutils.command.build_ext import build_ext


class build_fsps(build_ext):

    def run(self):
        # Generate the Fortran signature/interface.
        cmd = "cd fsps;f2py fsps.f90"
        cmd += " -m _fsps -h fsps.pyf"
        cmd += " --overwrite-signature"
        print("Running: {0}".format(cmd))
        sp.call(cmd, shell=True)

        # Find the FSPS source files.
        fsps_dir = os.path.join(os.environ["SPS_HOME"], "src")
        fns = [f for f in glob.glob(os.path.join(fsps_dir, "*.o"))
               if os.path.basename(f) not in ["autosps.o", "simple.o",
                                              "lesssimple.o"]]

        # Check to make sure that all of the required modules exist.
        flag = len(fns)
        flag *= os.path.exists(os.path.join(fsps_dir, "sps_utils.mod"))
        flag *= os.path.exists(os.path.join(fsps_dir, "sps_vars.mod"))
        if not flag:
            raise RuntimeError("You need to run make in $SPS_HOME/src first")

        # Add the interface source files to the file list.
        fns += ["fsps.f90", "fsps.pyf"]

        # Compile the library.
        cmd = "cd fsps;f2py -c -I{0} ".format(fsps_dir)
        cmd += " ".join(fns)
        cmd += " --f90flags=-cpp"
        cmd += " --f90flags=-fPIC"
        print("Running: {0}".format(cmd))
        sp.call(cmd, shell=True)

        # Move the compiled library to the correct directory.
        infn = os.path.abspath(os.path.join("fsps", "_fsps.so"))
        outfn = os.path.abspath(self.get_ext_fullpath("fsps._fsps"))
        if infn != outfn:
            try:
                os.makedirs(os.path.dirname(outfn))
            except os.error:
                pass
            print("Moving {0} to {1}".format(infn, outfn))
            shutil.copyfile(infn, outfn)


if "publish" in sys.argv[-1]:
    os.system("python setup.py sdist upload")
    sys.exit()
<commit_msg>use f2py via python rather than shell script<commit_after>#!/usr/bin/env python

import os
import sys
import glob
import shutil
import subprocess as sp

try:
    from setuptools import setup, Extension
    from setuptools.command.build_ext import build_ext
except ImportError:
    from distutils.core import setup, Extension
    from distutils.command.build_ext import build_ext

def invoke_f2py(files, flags=[], wd=None):
    from numpy.f2py import main

    olddir = os.path.abspath(os.curdir)
    oldargv = list(sys.argv)
    try:
        if wd is not None:
            os.chdir(wd)
        sys.argv = ['f2py']
        sys.argv.extend(files)
        sys.argv.extend(flags)

        main()
    finally:
        sys.argv = oldargv
        os.chdir(olddir)

class build_fsps(build_ext):

    def run(self):
        # Generate the Fortran signature/interface.
        files = ['fsps.f90']
        flags = " -m _fsps -h fsps.pyf --overwrite-signature".split()
        print("Running f2py on {0} with flags {1}".format(files, flags))
        invoke_f2py(['fsps.f90'], flags, wd='fsps')

        # Find the FSPS source files.
        fsps_dir = os.path.join(os.environ["SPS_HOME"], "src")
        fns = [f for f in glob.glob(os.path.join(fsps_dir, "*.o"))
               if os.path.basename(f) not in ["autosps.o", "simple.o",
                                              "lesssimple.o"]]

        # Check to make sure that all of the required modules exist.
        flag = len(fns)
        flag *= os.path.exists(os.path.join(fsps_dir, "sps_utils.mod"))
        flag *= os.path.exists(os.path.join(fsps_dir, "sps_vars.mod"))
        if not flag:
            raise RuntimeError("You need to run make in $SPS_HOME/src first")

        # Add the interface source files to the file list.
        fns += ["fsps.f90", "fsps.pyf"]

        # Compile the library.
        flags = '-c -I{0} --f90flags=-cpp --f90flags=-fPIC'.format(fsps_dir).split()
        print("Running f2py on {0} with flags {1}".format(fns, flags))
        invoke_f2py(fns, flags, wd='fsps')

        # Move the compiled library to the correct directory.
        infn = os.path.abspath(os.path.join("fsps", "_fsps.so"))
        outfn = os.path.abspath(self.get_ext_fullpath("fsps._fsps"))
        if infn != outfn:
            try:
                os.makedirs(os.path.dirname(outfn))
            except os.error:
                pass
            print("Moving {0} to {1}".format(infn, outfn))
            shutil.copyfile(infn, outfn)


if "publish" in sys.argv[-1]:
    os.system("python setup.py sdist upload")
    sys.exit()

{'fraction_non_alphanumeric': 0.1288292068820814, 'fraction_numerical': 0.008812421317666807, 'mean_word_length': 2.180120080053369, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4272225', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1575, 'n_words': 397}