<commit_before>
--- Functions for command-line scripts.
local cmd = {}

local unpack = unpack or table.unpack

local util = require("luarocks.util")
local cfg = require("luarocks.core.cfg")
local path = require("luarocks.path")
local dir = require("luarocks.dir")
local deps = require("luarocks.deps")
local fs = require("luarocks.fs")
local fun = require("luarocks.fun")

local program = util.this_program("luarocks")

cmd.errorcodes = {
   OK = 0,
   UNSPECIFIED = 1,
   PERMISSIONDENIED = 2,
   CONFIGFILE = 3,
   CRASH = 99
}

local function error_handler(err)
   return debug.traceback("LuaRocks "..cfg.program_version..
      " bug (please report at https://github.com/luarocks/luarocks/issues).\n"..err, 2)
end

--- Display an error message and exit.
-- @param message string: The error message.
-- @param exitcode number: the exitcode to use
local function die(message, exitcode)
   assert(type(message) == "string", "bad error, expected string, got: " .. type(message))
   util.printerr("\nError: "..message)

   local ok, err = xpcall(util.run_scheduled_functions, error_handler)
   if not ok then
      util.printerr("\nError: "..err)
      exitcode = cmd.errorcodes.CRASH
   end

   os.exit(exitcode or cmd.errorcodes.UNSPECIFIED)
end

local function replace_tree(flags, tree)
   tree = dir.normalize(tree)
   flags["tree"] = tree
   path.use_tree(tree)
end

local function is_ownership_ok(directory)
   local me = fs.current_user()
   for _ = 1,3 do -- try up to grandparent
      local owner = fs.attributes(directory, "owner")
      if owner then
         return owner == me
      end
      directory = dir.dir_name(directory)
   end
   return false
end

local function find_project_dir()
   local try = "."
   for _ = 1, 10 do -- FIXME detect when root dir was hit instead
      local abs = fs.absolute_name(try)
      if fs.is_dir(abs .. "/.luarocks") and fs.is_dir(abs .. "/lua_modules") then
         abs = abs:gsub("/.$", "")
         return abs, abs .. "/lua_modules"
      end
      try = try .. "/.."
   end
end

--- Main command-line processor.
-- Parses input arguments and calls the appropriate driver function
-- to execute the action requested on the command-line, forwarding
-- to it any additional arguments passed by the user.
-- Uses the global table "commands", which contains
-- the loaded modules representing commands.
-- @param ... string: Arguments given on the command-line.
function cmd.run_command(description, commands, ...)
   local args = {...}
   local cmdline_vars = {}<commit_msg>cmd: move local functions inside<commit_after>
--- Functions for command-line scripts.
local cmd = {}

local unpack = unpack or table.unpack

local util = require("luarocks.util")
local cfg = require("luarocks.core.cfg")
local path = require("luarocks.path")
local dir = require("luarocks.dir")
local deps = require("luarocks.deps")
local fs = require("luarocks.fs")
local fun = require("luarocks.fun")

local program = util.this_program("luarocks")

cmd.errorcodes = {
   OK = 0,
   UNSPECIFIED = 1,
   PERMISSIONDENIED = 2,
   CONFIGFILE = 3,
   CRASH = 99
}

local function replace_tree(flags, tree)
   tree = dir.normalize(tree)
   flags["tree"] = tree
   path.use_tree(tree)
end

local function is_ownership_ok(directory)
   local me = fs.current_user()
   for _ = 1,3 do -- try up to grandparent
      local owner = fs.attributes(directory, "owner")
      if owner then
         return owner == me
      end
      directory = dir.dir_name(directory)
   end
   return false
end

local function find_project_dir()
   local try = "."
   for _ = 1, 10 do -- FIXME detect when root dir was hit instead
      local abs = fs.absolute_name(try)
      if fs.is_dir(abs .. "/.luarocks") and fs.is_dir(abs .. "/lua_modules") then
         abs = abs:gsub("/.$", "")
         return abs, abs .. "/lua_modules"
      end
      try = try .. "/.."
   end
end

--- Main command-line processor.
-- Parses input arguments and calls the appropriate driver function
-- to execute the action requested on the command-line, forwarding
-- to it any additional arguments passed by the user.
-- Uses the global table "commands", which contains
-- the loaded modules representing commands.
-- @param ... string: Arguments given on the command-line.
function cmd.run_command(description, commands, ...)

   local function error_handler(err)
      return debug.traceback("LuaRocks "..cfg.program_version..
         " bug (please report at https://github.com/luarocks/luarocks/issues).\n"..err, 2)
   end

   --- Display an error message and exit.
   -- @param message string: The error message.
   -- @param exitcode number: the exitcode to use
   local function die(message, exitcode)
      assert(type(message) == "string", "bad error, expected string, got: " .. type(message))
      util.printerr("\nError: "..message)

      local ok, err = xpcall(util.run_scheduled_functions, error_handler)
      if not ok then
         util.printerr("\nError: "..err)
         exitcode = cmd.errorcodes.CRASH
      end

      os.exit(exitcode or cmd.errorcodes.UNSPECIFIED)
   end

   local args = {...}
   local cmdline_vars = {}
{'fraction_non_alphanumeric': 0.12195121951219512, 'fraction_numerical': 0.0046829268292682925, 'mean_word_length': 3.0457774269928968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '74249', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1568, 'n_words': 494}