<commit_before># Copyright 2013 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.


"""Utilities for image bundling tool."""

import logging
import os
import subprocess
import time
import urllib2

METADATA_URL_PREFIX = 'http://169.254.169.254/computeMetadata/'
METADATA_V1_URL_PREFIX = METADATA_URL_PREFIX + 'v1/'


class MakeFileSystemException(Exception):
  """Error occurred in file system creation."""


class TarAndGzipFileException(Exception):
  """Error occurred in creating the tarball."""


class LoadDiskImage(object):
  """Loads raw disk image using kpartx."""
 
  def __init__(self, file_path, part_offset = 1024*1024):
    """Initializes LoadDiskImage object.

    Args:
      file_path: a path to a file containing raw disk image.
      
      virtual_image: a boolean specifying whether the file is virtual image (not raw file) supported by qemu-img

    Returns:
      A list of devices for every partition found in an image.
    """
    self._file_path = file_path
    self._part_offset = part_offset
    self._base_loop = "";
    self._part_loop = "";

  def __enter__(self):
    """Map disk image as a device."""
    SyncFileSystem()

    mountpath = self._file_path
    offset = self._part_offset
    #create base loop for the whole file
    losetup_cmd = ['losetup' , "--show" , "-f" , self._file_path]
    output = RunCommand(losetup_cmd)
    if not "/dev/loop" in output:
        raise OSError("Unknown loop device is built")
    self._base_loop = str(output).strip()
    # create a loop for partition
    losetup_cmd = ['losetup' , '--offset' , str(offset) , "--show" , "-f" , self._base_loop]
    output = RunCommand(losetup_cmd)
    if not "/dev/loop" in output:
        raise OSError("Unknown loop device is built")
    self._part_loop = str(output).strip()

    #feoff: !kpartx is shit, grub doesn't work with it at all
    #kpartx_cmd = ['kpartx', '-a', '-v', '-s', mountpath]
    #output = RunCommand(kpartx_cmd)
    # 
    devs = [self._part_loop]
    #for line in output.splitlines():
    #  split_line = line.split()
    #  if (len(split_line) > 2 and split_line[0] == 'add'
    #      and split_line[1] == 'map'):
          #feoff: we add an extra loopdev here - sometimes grub won't install on /dev/mapper-like links for the uncertain reason 
          #self._loop_dev = RunCommand(["losetup" , '--show', '--find' , '/dev/mapper/' + split_line[2]])
          #self._loop_dev = str(self._loop_dev).strip()
          # try to do it without loop dev
    #      devs.append('/dev/mapper/' + split_line[2])
    #      self._loop_dev = ""
          #devs.append(self._loop_dev)
    #time.sleep(2)
    return devs

  def __exit__(self, unused_exc_type, unused_exc_value, unused_exc_tb):
    """Unmap disk image as a device.

    Args:
      unused_exc_type: unused.
      unused_exc_value: unused.
      unused_exc_tb: unused.
    """
    SyncFileSystem()
    time.sleep(2)

    if self._part_loop:
        RunCommand(["losetup" , '-d' , self._part_loop ])

    time.sleep(2)

    if self._base_loop:
        RunCommand(["losetup" , '-d' , self._base_loop ])
    

  


class MountFileSystem(object):
  """Mounts a file system."""

  def __init__(self, dev_path, dir_path, fs_type):
    """Initializes MountFileSystem object.

    Args:
      dev_path: A path to a device to mount.
      dir_path: A path to a directory where a device is to be mounted.
    """
    self._dev_path = dev_path
    self._dir_path = dir_path
    self._fs_type = fs_type

  def __enter__(self):
    """Mounts a device.
    """
    # Since the bundled image can have the same uuid as the root disk,
    # we should prevent uuid conflicts for xfs mounts.
    if self._fs_type is 'xfs':
      mount_cmd = ['mount', '-o', 'nouuid', self._dev_path, self._dir_path]
    else:
      mount_cmd = ['mount', self._dev_path, self._dir_path]
    RunCommand(mount_cmd)

  def __exit__(self, unused_exc_type, unused_exc_value, unused_exc_tb):
    """Unmounts a file system.

    Args:
      unused_exc_type: unused.
      unused_exc_value: unused.
      unused_exc_tb: unused.
    """
    umount_cmd = ['umount', self._dir_path]
    RunCommand(umount_cmd)
    SyncFileSystem()


def SyncFileSystem():
  RunCommand(['sync'])

def GetMounts(root='/'):
  """Find all mount points under the specified root.

  Args:
    root: a path to look for a mount points.

  Returns:
    A list of mount points.
  """
  output = RunCommand(['/bin/mount', '-l'])
  mounts = []
  for line in output.splitlines():
    split_line = line.split()
    mount_point = split_line[2]
    if mount_point == root:
      continue
    # We are simply ignoring the fs_type of fs for now. But we can use that
    # later Just verify that these are actually mount points.
    if os.path.ismount(mount_point) and mount_point.startswith(root):
      mounts.append(mount_point)
  return mounts


def MakePartitionTable(file_path):
  """Create a partition table in a file.

  Args:
    file_path: A path to a file where a partition table will be created.
  """
  RunCommand(['parted', file_path, 'mklabel', 'msdos'])


def MakePartition(file_path, partition_type, fs_type, start, end):
  """Create a partition in a file.<commit_msg>fixed mount command path<commit_after># Copyright 2013 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.


"""Utilities for image bundling tool."""

import logging
import os
import subprocess
import time
import urllib2

METADATA_URL_PREFIX = 'http://169.254.169.254/computeMetadata/'
METADATA_V1_URL_PREFIX = METADATA_URL_PREFIX + 'v1/'


class MakeFileSystemException(Exception):
  """Error occurred in file system creation."""


class TarAndGzipFileException(Exception):
  """Error occurred in creating the tarball."""


class LoadDiskImage(object):
  """Loads raw disk image using kpartx."""
 
  def __init__(self, file_path, part_offset = 1024*1024):
    """Initializes LoadDiskImage object.

    Args:
      file_path: a path to a file containing raw disk image.
      
      virtual_image: a boolean specifying whether the file is virtual image (not raw file) supported by qemu-img

    Returns:
      A list of devices for every partition found in an image.
    """
    self._file_path = file_path
    self._part_offset = part_offset
    self._base_loop = "";
    self._part_loop = "";

  def __enter__(self):
    """Map disk image as a device."""
    SyncFileSystem()

    mountpath = self._file_path
    offset = self._part_offset
    #create base loop for the whole file
    losetup_cmd = ['losetup' , "--show" , "-f" , self._file_path]
    output = RunCommand(losetup_cmd)
    if not "/dev/loop" in output:
        raise OSError("Unknown loop device is built")
    self._base_loop = str(output).strip()
    # create a loop for partition
    losetup_cmd = ['losetup' , '--offset' , str(offset) , "--show" , "-f" , self._base_loop]
    output = RunCommand(losetup_cmd)
    if not "/dev/loop" in output:
        raise OSError("Unknown loop device is built")
    self._part_loop = str(output).strip()

    #feoff: !kpartx is shit, grub doesn't work with it at all
    #kpartx_cmd = ['kpartx', '-a', '-v', '-s', mountpath]
    #output = RunCommand(kpartx_cmd)
    # 
    devs = [self._part_loop]
    #for line in output.splitlines():
    #  split_line = line.split()
    #  if (len(split_line) > 2 and split_line[0] == 'add'
    #      and split_line[1] == 'map'):
          #feoff: we add an extra loopdev here - sometimes grub won't install on /dev/mapper-like links for the uncertain reason 
          #self._loop_dev = RunCommand(["losetup" , '--show', '--find' , '/dev/mapper/' + split_line[2]])
          #self._loop_dev = str(self._loop_dev).strip()
          # try to do it without loop dev
    #      devs.append('/dev/mapper/' + split_line[2])
    #      self._loop_dev = ""
          #devs.append(self._loop_dev)
    #time.sleep(2)
    return devs

  def __exit__(self, unused_exc_type, unused_exc_value, unused_exc_tb):
    """Unmap disk image as a device.

    Args:
      unused_exc_type: unused.
      unused_exc_value: unused.
      unused_exc_tb: unused.
    """
    SyncFileSystem()
    time.sleep(2)

    if self._part_loop:
        RunCommand(["losetup" , '-d' , self._part_loop ])

    time.sleep(2)

    if self._base_loop:
        RunCommand(["losetup" , '-d' , self._base_loop ])
    

  


class MountFileSystem(object):
  """Mounts a file system."""

  def __init__(self, dev_path, dir_path, fs_type):
    """Initializes MountFileSystem object.

    Args:
      dev_path: A path to a device to mount.
      dir_path: A path to a directory where a device is to be mounted.
    """
    self._dev_path = dev_path
    self._dir_path = dir_path
    self._fs_type = fs_type

  def __enter__(self):
    """Mounts a device.
    """
    # Since the bundled image can have the same uuid as the root disk,
    # we should prevent uuid conflicts for xfs mounts.
    if self._fs_type is 'xfs':
      mount_cmd = ['mount', '-o', 'nouuid', self._dev_path, self._dir_path]
    else:
      mount_cmd = ['mount', self._dev_path, self._dir_path]
    RunCommand(mount_cmd)

  def __exit__(self, unused_exc_type, unused_exc_value, unused_exc_tb):
    """Unmounts a file system.

    Args:
      unused_exc_type: unused.
      unused_exc_value: unused.
      unused_exc_tb: unused.
    """
    umount_cmd = ['umount', self._dir_path]
    RunCommand(umount_cmd)
    SyncFileSystem()


def SyncFileSystem():
  RunCommand(['sync'])

def GetMounts(root='/'):
  """Find all mount points under the specified root.

  Args:
    root: a path to look for a mount points.

  Returns:
    A list of mount points.
  """
  output = RunCommand(['mount', '-l'])
  mounts = []
  for line in output.splitlines():
    split_line = line.split()
    mount_point = split_line[2]
    if mount_point == root:
      continue
    # We are simply ignoring the fs_type of fs for now. But we can use that
    # later Just verify that these are actually mount points.
    if os.path.ismount(mount_point) and mount_point.startswith(root):
      mounts.append(mount_point)
  return mounts


def MakePartitionTable(file_path):
  """Create a partition table in a file.

  Args:
    file_path: A path to a file where a partition table will be created.
  """
  RunCommand(['parted', file_path, 'mklabel', 'msdos'])


def MakePartition(file_path, partition_type, fs_type, start, end):
  """Create a partition in a file.
{'fraction_non_alphanumeric': 0.1116360454943132, 'fraction_numerical': 0.00699912510936133, 'mean_word_length': 3.1416666666666666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '1773500', 'n_tokens_mistral': 3706, 'n_tokens_neox': 3587, 'n_words': 1156}