<commit_before># Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

<#
  .SYNOPSIS
    GCE Base Modules.
  .DESCRIPTION
    Base modules needed for GCE Powershell scripts to run scripts to run.

  #requires -version 3.0
#>

# Default Values
$global:write_to_serial = $false
$global:metadata_server = 'metadata.google.internal'
$global:hostname = [System.Net.Dns]::GetHostName()
$global:log_file = $null

# Functions
function _AddToPath {
 <#
    .SYNOPSIS
      Adds GCE tool dir to SYSTEM PATH
    .DESCRIPTION
      This is a helper function which adds location to path
  #>
  param (
    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('path')]
      $path_to_add
  )

  # Check if folder exists on the file system.
  if (!(Test-Path $path_to_add)) {
    Write-Log "$path_to_add does not exist, cannot be added to $env:PATH."
    return
  }

  try {
    $path_reg_key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
    $current_path = (Get-ItemProperty $path_reg_key).Path
    $check_path = ($current_path).split(';') | ? {$_ -like $path_to_add}
  }
  catch {
    Write-Log 'Could not read path from the registry.'
    _PrintError
  }
  # See if the folder is already in the path.
  if ($check_path) {
    Write-Log 'Folder already in system path.'
  }
  else {
    try {
      Write-Log "Adding $path_to_add to SYSTEM path."
      $new_path = $current_path + ';' + $path_to_add
      $env:Path = $new_path
      Set-ItemProperty $path_reg_key -name 'Path' -value $new_path
    }
    catch {
      Write-Log 'Failed to add to SYSTEM path.'
      _PrintError
    }
  }
}


function Clear-EventLogs {
  <#
    .SYNOPSIS
      Clear all eventlog enteries.
    .DESCRIPTION
      This uses the Get-Eventlog and Clear-EventLog powershell functions to
      clean the eventlogs for a machine.
  #>

  Write-Log 'Clearing events in EventViewer.'
  Get-WinEvent -ListLog * |
    Where-Object {($_.IsEnabled -eq 'True') -and ($_.RecordCount -gt 0)} |
    ForEach-Object {
      try{[System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog($_.LogName)}catch{}
    }
}


function Clear-TempFolders {
  <#
    .SYNOPSIS
      Delete all files from temp folder location.
    .DESCRIPTION
      This function calls an array variable which contain location of all the
      temp files and folder which needs to be cleared out. We use the
      Remove-Item routine to delete the files in the temp directories.
  #>

  # Array of files and folder that need to be deleted.
  @("C:\Windows\Temp\*", "C:\Windows\Prefetch\*",
    "C:\Documents and Settings\*\Local Settings\temp\*\*",
    "C:\Users\*\Appdata\Local\Temp\*\*",
    "C:\Users\*\Appdata\Local\Microsoft\Internet Explorer\*",
    "C:\Users\*\Appdata\LocalLow\Temp\*\*",
    "C:\Users\*\Appdata\LocalLow\Microsoft\Internet Explorer\*") | ForEach-Object {
    if (Test-Path $_) {
      Remove-Item $_ -recurse -force -ErrorAction SilentlyContinue
    }
  }
}


function Get-MetaData {
  <#
    .SYNOPSIS
      Get attributes from GCE instances metadata.<commit_msg>Ignore errors in Clear-TempFolders (#183)<commit_after># Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

<#
  .SYNOPSIS
    GCE Base Modules.
  .DESCRIPTION
    Base modules needed for GCE Powershell scripts to run scripts to run.

  #requires -version 3.0
#>

# Default Values
$global:write_to_serial = $false
$global:metadata_server = 'metadata.google.internal'
$global:hostname = [System.Net.Dns]::GetHostName()
$global:log_file = $null

# Functions
function _AddToPath {
 <#
    .SYNOPSIS
      Adds GCE tool dir to SYSTEM PATH
    .DESCRIPTION
      This is a helper function which adds location to path
  #>
  param (
    [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('path')]
      $path_to_add
  )

  # Check if folder exists on the file system.
  if (!(Test-Path $path_to_add)) {
    Write-Log "$path_to_add does not exist, cannot be added to $env:PATH."
    return
  }

  try {
    $path_reg_key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
    $current_path = (Get-ItemProperty $path_reg_key).Path
    $check_path = ($current_path).split(';') | ? {$_ -like $path_to_add}
  }
  catch {
    Write-Log 'Could not read path from the registry.'
    _PrintError
  }
  # See if the folder is already in the path.
  if ($check_path) {
    Write-Log 'Folder already in system path.'
  }
  else {
    try {
      Write-Log "Adding $path_to_add to SYSTEM path."
      $new_path = $current_path + ';' + $path_to_add
      $env:Path = $new_path
      Set-ItemProperty $path_reg_key -name 'Path' -value $new_path
    }
    catch {
      Write-Log 'Failed to add to SYSTEM path.'
      _PrintError
    }
  }
}


function Clear-EventLogs {
  <#
    .SYNOPSIS
      Clear all eventlog enteries.
    .DESCRIPTION
      This uses the Get-Eventlog and Clear-EventLog powershell functions to
      clean the eventlogs for a machine.
  #>

  Write-Log 'Clearing events in EventViewer.'
  Get-WinEvent -ListLog * |
    Where-Object {($_.IsEnabled -eq 'True') -and ($_.RecordCount -gt 0)} |
    ForEach-Object {
      try{[System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog($_.LogName)}catch{}
    }
}


function Clear-TempFolders {
  <#
    .SYNOPSIS
      Delete all files from temp folder location.
    .DESCRIPTION
      This function calls an array variable which contain location of all the
      temp files and folder which needs to be cleared out. We use the
      Remove-Item routine to delete the files in the temp directories.
  #>

  # Array of files and folder that need to be deleted.
  @("C:\Windows\Temp\*", "C:\Windows\Prefetch\*",
    "C:\Documents and Settings\*\Local Settings\temp\*\*",
    "C:\Users\*\Appdata\Local\Temp\*\*",
    "C:\Users\*\Appdata\Local\Microsoft\Internet Explorer\*",
    "C:\Users\*\Appdata\LocalLow\Temp\*\*",
    "C:\Users\*\Appdata\LocalLow\Microsoft\Internet Explorer\*") | ForEach-Object {
    if (Test-Path $_) {
      Remove-Item $_ -Recurse -Force -ErrorAction Ignore
    }
  }
}


function Get-MetaData {
  <#
    .SYNOPSIS
      Get attributes from GCE instances metadata.
{'fraction_non_alphanumeric': 0.10906086477554393, 'fraction_numerical': 0.003442577802258331, 'mean_word_length': 3.1126840317100792, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7268019', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2233, 'n_words': 717}