<commit_before>(import os sys shutil tempfile
        [Cython.Build.Cythonize [main :as cythonize-main]]
        [Cython.Build.BuildExecutable [build :as cython-build]]
        [hy-compiler.core.translate [to-python]])

(defn print-and-exit [msg]
  (print "error:" msg)
  (sys.exit))

(defn build [module &optional [output None] [shared False] [with-c False] [with-python False]]
  (print (.format "compiling: {}" module))
  (if-not (os.path.exists module)
          (print-and-exit (.format "file does not exist: {}" module)))
  (try
   (setv pysrc (to-python module))
   (except [] (print-and-exit (.format "cannot convert to python: {}" module))))
  (setv temp-dir (tempfile.mkdtemp)
        module-dir (os.path.dirname module))
  (setv py-filepath
        (os.path.join
         temp-dir
         (+ (lif output output
                 (first (os.path.splitext (os.path.basename module)))) ".py")))

  (with [fp (open py-filepath "w")]
        (.write fp pysrc))
  (with [null (open os.devnull "w")]
        (setv stderr sys.stderr
              stdout sys.stdout
              sys.stderr null
              sys.stdout null)
        (setv exe-filepath
              (if shared
                (do (cythonize-main [(str py-filepath) (str "--inplace") (str "--quiet")])
                    (last (sorted (list-comp (os.path.join temp-dir x)
                                             [x (os.listdir temp-dir)])
                                  :key os.path.getatime)))
                (cython-build py-filepath))
              sys.stderr stderr
              sys.stdout stdout))
  (if with-python
    (do
     (shutil.copy py-filepath
                  (os.path.join module-dir (os.path.basename py-filepath)))
     (print (.format "-> {}" (os.path.join module-dir
                                           (os.path.basename py-filepath))))))
  (if with-c
    (do
     (setv c-filepath (.replace py-filepath ".py" ".c"))
     (shutil.copy c-filepath
                  (os.path.join (os.path.dirname module)
                                (os.path.basename c-filepath)))
     (print (.format "-> {}" (os.path.join (os.path.dirname module)
                                           (os.path.basename c-filepath))))))

  (shutil.copy exe-filepath (os.path.join module-dir
                                          (os.path.basename exe-filepath)))
  (print (.format "-> {}" (os.path.join module-dir
                                        (os.path.basename exe-filepath))))
  (shutil.rmtree temp-dir))<commit_msg>Refactor<commit_after>(import os sys
        [tempfile [mkdtemp]]
        [shutil [copy rmtree]]
        [Cython.Build.Cythonize [main :as cythonize-main]]
        [Cython.Build.BuildExecutable [build :as cython-build]]
        [hy-compiler.core.translate [to-python]])

(defn print-and-exit [msg]
  (print "error:" msg)
  (sys.exit))

(defn build-executable [filepath]
  (cython-build filepath))

(defn build-shared-library [filepath]
  (cythonize-main [(str filepath) (str "--inplace") (str "--quiet")])
  (setv dirpath (os.path.dirname filepath))
  (last (sorted (list-comp (os.path.join dirpath x) [x (os.listdir dirpath)])
                :key os.path.getatime)))

(defn do-quiet [func &rest args]
  (with [null (open os.devnull "w")]
        (setv stderr sys.stderr
              stdout sys.stdout
              sys.stderr null
              sys.stdout null
              ret (apply func args))
        (setv sys.stderr stderr
              sys.stdout stdout))
  ret)

(defn mkcopy [module-dir filepath]
  (setv dest-filepath (os.path.join module-dir (os.path.basename filepath)))
  (copy filepath dest-filepath)
  (print (.format "-> {}" dest-filepath)))

(defn build [module &optional [output None]
                              [shared False]
                              [with-c False]
                              [with-python False]]

  (print (.format "compiling: {}" module))
  (if-not (os.path.exists module)
          (print-and-exit (.format "file does not exist: {}" module)))
  (if-not (= (last (os.path.splitext module)) ".hy")
          (print-and-exit (.format "only `.hy` file is acceptable: {}" module)))

  (setv temp-dir (mkdtemp))

  (lif output
       (setv py-filepath (os.path.join temp-dir
                                       (+ (os.path.basename output) ".py"))
             dest-dir (os.path.dirname output))
       (setv py-filepath (os.path.join temp-dir
                                       (.replace (os.path.basename module) ".hy" ".py"))
             dest-dir (os.path.dirname module)))
  (if-not dest-dir
          (setv dest-dir "./"))
  (if-not (os.path.exists dest-dir)
          (os.makedirs dest-dir))


  (try
   (setv pysrc (to-python module))
   (except [] (print-and-exit (.format "cannot convert to python: {}" module))))

  (with [fp (open py-filepath "w")]
        (.write fp pysrc))

  (setv build-func (if shared build-shared-library build-executable)
        bin-filepath (do-quiet build-func py-filepath)
        )

  (if with-python
    (mkcopy dest-dir py-filepath))
  (setv c-filepath (.replace py-filepath ".py" ".c"))
  (if with-c
    (mkcopy dest-dir c-filepath))
  (mkcopy dest-dir bin-filepath)
  (rmtree temp-dir))
{'fraction_non_alphanumeric': 0.14796310530361262, 'fraction_numerical': 0.0, 'mean_word_length': 1.8788716814159292, 'pattern_counts': {'":': 0, '<': 3, '<?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': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1971688', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1642, 'n_words': 410}