<commit_before>class FSItem extends KDObject

  ###
  CLASS CONTEXT
  ###

  escapeFilePath = FSHelper.escapeFilePath

  @create:(path, type, vmName, callback)->

    FSHelper.ensureNonexistentPath path, vmName, (err, response)->
      if err
        callback? err, response
        warn err
      else
        KD.getSingleton('vmController').run
          method           : if type is 'folder' then "fs.createDirectory" \
                             else "fs.writeFile"
          vmName           : vmName
          withArgs         :
            path           : FSHelper.plainPath response
            content        : ""
            donotoverwrite : yes
        , (err, res)->
          if err then warn err
          else
            file = FSHelper.createFile {path:response, type, vmName}
          callback? err, file

  @copy:(sourceItem, targetItem, callback)->

    sourceItem.emit "fs.job.started"
    targetPath = FSHelper.plainPath "#{targetItem.path}/#{sourceItem.name}"
    vmName = targetItem.vmName or FSHelper.getVMNameFromPath targetPath
    FSHelper.ensureNonexistentPath targetPath, vmName, (err, response)->
      if err
        warn err
        callback? err, response
      else
        KD.getSingleton('vmController').run
          vmName   : vmName
          withArgs : "cp -R #{escapeFilePath(sourceItem.path)} #{escapeFilePath(response)}"
        , (err, res)->
          sourceItem.emit "fs.job.finished"
          if err then warn err
          else
            file = FSHelper.createFileFromPath "#{targetItem.path}/#{sourceItem.name}", sourceItem.type
          callback? err, file

  @move:(sourceItem, targetItem, callback)->

    sourceItem.emit "fs.job.started"
    targetPath = FSHelper.plainPath "#{targetItem.path}/#{sourceItem.name}"
    vmName = targetItem.vmName or FSHelper.getVMNameFromPath targetPath
    FSHelper.ensureNonexistentPath targetPath, vmName, (err, response)->
      if err
        warn err
        callback? err, response
      else
        KD.getSingleton('vmController').run
          vmName   : vmName
          withArgs : "mv #{escapeFilePath(sourceItem.path)} #{escapeFilePath(response)}"
        , (err, res)->
          sourceItem.emit "fs.job.finished"
          if err then warn err
          else
            file = FSHelper.createFileFromPath targetPath, sourceItem.type
          callback? err, file

  @compress:(file, type, callback)->

    file.emit "fs.job.started"
    path = FSHelper.plainPath "#{file.path}.#{type}"
    vmName = file.vmName or FSHelper.getVMNameFromPath path
    FSHelper.ensureNonexistentPath path, vmName, (err, response)->
      if err
        warn err
        callback? err, response
      else
        command = switch type
          when "tar.gz" then "tar -pczf #{escapeFilePath response} #{escapeFilePath file.path}"
          else "zip -r #{escapeFilePath response} #{escapeFilePath file.path}"
        KD.getSingleton('vmController').run
          vmName   : vmName
          withArgs : command
        , (err, res)->
          file.emit "fs.job.finished"
          if err then warn err
          callback? err, res

  @extract:(file, callback)->

    reTar = /\.tar\.gz$/
    reZip = /\.zip$/

    file.emit "fs.job.started"
    path = FSHelper.plainPath file.path
    vmName = file.vmName or FSHelper.getVMNameFromPath path
    command = if reTar.test file.name
      extractFolder = file.path.replace reTar, ''
      "cd #{escapeFilePath file.parentPath};mkdir -p #{escapeFilePath extractFolder};tar -zxf #{escapeFilePath file.name} -C #{escapeFilePath extractFolder}"
    else if reZip.test file.name
      extractFolder = file.path.replace reZip, ''
      "cd #{escapeFilePath file.parentPath};unzip -o #{escapeFilePath file.name} -d #{escapeFilePath extractFolder}"

    if command
      KD.getSingleton('vmController').run
        vmName   : vmName
        withArgs : command
      , (err, res)->
        file.emit "fs.job.finished"
        if err then warn err
        folder = FSHelper.createFileFromPath extractFolder, 'folder'
        callback? err, folder
    else callback? yes

  @getFileExtension: (path) ->
    fileName = path or ''
    [name, extension...]  = fileName.split '.'
    extension = if extension.length is 0 then '' else extension.last

  @getFileType: (extension)->

    fileType = null

    _extension_sets =
      code    : [
        "php", "pl", "py", "jsp", "asp", "htm","html", "phtml","shtml"
        "sh", "cgi", "htaccess","fcgi","wsgi","mvc","xml","sql","rhtml"
        "js","json","coffee"
        "css","styl","sass"<commit_msg>re prefix to pattern suffix for regexps<commit_after>class FSItem extends KDObject

  ###
  CLASS CONTEXT
  ###

  escapeFilePath = FSHelper.escapeFilePath

  @create:(path, type, vmName, callback)->

    FSHelper.ensureNonexistentPath path, vmName, (err, response)->
      if err
        callback? err, response
        warn err
      else
        KD.getSingleton('vmController').run
          method           : if type is 'folder' then "fs.createDirectory" \
                             else "fs.writeFile"
          vmName           : vmName
          withArgs         :
            path           : FSHelper.plainPath response
            content        : ""
            donotoverwrite : yes
        , (err, res)->
          if err then warn err
          else
            file = FSHelper.createFile {path:response, type, vmName}
          callback? err, file

  @copy:(sourceItem, targetItem, callback)->

    sourceItem.emit "fs.job.started"
    targetPath = FSHelper.plainPath "#{targetItem.path}/#{sourceItem.name}"
    vmName = targetItem.vmName or FSHelper.getVMNameFromPath targetPath
    FSHelper.ensureNonexistentPath targetPath, vmName, (err, response)->
      if err
        warn err
        callback? err, response
      else
        KD.getSingleton('vmController').run
          vmName   : vmName
          withArgs : "cp -R #{escapeFilePath(sourceItem.path)} #{escapeFilePath(response)}"
        , (err, res)->
          sourceItem.emit "fs.job.finished"
          if err then warn err
          else
            file = FSHelper.createFileFromPath "#{targetItem.path}/#{sourceItem.name}", sourceItem.type
          callback? err, file

  @move:(sourceItem, targetItem, callback)->

    sourceItem.emit "fs.job.started"
    targetPath = FSHelper.plainPath "#{targetItem.path}/#{sourceItem.name}"
    vmName = targetItem.vmName or FSHelper.getVMNameFromPath targetPath
    FSHelper.ensureNonexistentPath targetPath, vmName, (err, response)->
      if err
        warn err
        callback? err, response
      else
        KD.getSingleton('vmController').run
          vmName   : vmName
          withArgs : "mv #{escapeFilePath(sourceItem.path)} #{escapeFilePath(response)}"
        , (err, res)->
          sourceItem.emit "fs.job.finished"
          if err then warn err
          else
            file = FSHelper.createFileFromPath targetPath, sourceItem.type
          callback? err, file

  @compress:(file, type, callback)->

    file.emit "fs.job.started"
    path = FSHelper.plainPath "#{file.path}.#{type}"
    vmName = file.vmName or FSHelper.getVMNameFromPath path
    FSHelper.ensureNonexistentPath path, vmName, (err, response)->
      if err
        warn err
        callback? err, response
      else
        command = switch type
          when "tar.gz" then "tar -pczf #{escapeFilePath response} #{escapeFilePath file.path}"
          else "zip -r #{escapeFilePath response} #{escapeFilePath file.path}"
        KD.getSingleton('vmController').run
          vmName   : vmName
          withArgs : command
        , (err, res)->
          file.emit "fs.job.finished"
          if err then warn err
          callback? err, res

  @extract:(file, callback)->

    tarPattern = /\.tar\.gz$/
    zipPattern = /\.zip$/

    file.emit "fs.job.started"
    path = FSHelper.plainPath file.path
    vmName = file.vmName or FSHelper.getVMNameFromPath path
    command = if tarPattern.test file.name
      extractFolder = file.path.replace tarPattern, ''
      "cd #{escapeFilePath file.parentPath};mkdir -p #{escapeFilePath extractFolder};tar -zxf #{escapeFilePath file.name} -C #{escapeFilePath extractFolder}"
    else if zipPattern.test file.name
      extractFolder = file.path.replace zipPattern, ''
      "cd #{escapeFilePath file.parentPath};unzip -o #{escapeFilePath file.name} -d #{escapeFilePath extractFolder}"

    if command
      KD.getSingleton('vmController').run
        vmName   : vmName
        withArgs : command
      , (err, res)->
        file.emit "fs.job.finished"
        if err then warn err
        folder = FSHelper.createFileFromPath extractFolder, 'folder'
        callback? err, folder
    else callback? yes

  @getFileExtension: (path) ->
    fileName = path or ''
    [name, extension...]  = fileName.split '.'
    extension = if extension.length is 0 then '' else extension.last

  @getFileType: (extension)->

    fileType = null

    _extension_sets =
      code    : [
        "php", "pl", "py", "jsp", "asp", "htm","html", "phtml","shtml"
        "sh", "cgi", "htaccess","fcgi","wsgi","mvc","xml","sql","rhtml"
        "js","json","coffee"
        "css","styl","sass"
{'fraction_non_alphanumeric': 0.11596091205211727, 'fraction_numerical': 0.00021715526601520088, 'mean_word_length': 2.3264716504153125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7468315', 'n_tokens_mistral': 2714, 'n_tokens_neox': 2662, 'n_words': 673}