home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / linuxmint / mintDisk / mintDisk.py < prev    next >
Encoding:
Python Source  |  2007-02-15  |  8.9 KB  |  219 lines

  1. #!/usr/bin/env python
  2.  
  3. # MintDisk
  4. #    No Copyright (What for?) Clem <root@linuxmint.com>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; Version 2
  9. # of the License.
  10.  
  11. try:
  12.     import os
  13.     import commands
  14.     import sys
  15.     import string
  16. except Exception, detail:
  17.     print detail
  18.     pass
  19.  
  20.  
  21. # prepare the log
  22. log = open("/usr/lib/linuxmint/mintDisk/mintDisk.log", "w")
  23.  
  24. # checking os-prober
  25. log.writelines("-------------------------\n")
  26. log.writelines("* checking the presence of os-prober...\n")
  27. if (commands.getoutput("which os-prober") == "/usr/bin/os-prober"):
  28.     log.writelines("  -- os-prober is present, exiting\n")    
  29.     sys.exit() 
  30. else:
  31.     log.writelines("  -- os-prober is absent\n")
  32.  
  33. # prepare the environment
  34. log.writelines("-------------------------\n")
  35. log.writelines("* checking system environment...\n")
  36. user = os.environ.get("USER")
  37. home = os.environ.get("HOME")
  38. locale = os.environ.get("LANG")
  39. log.writelines("  -- user = " + user + "\n")
  40. log.writelines("  -- home = " + home + "\n")
  41. log.writelines("  -- locale = " + locale + "\n")
  42.  
  43. # configure options
  44. log.writelines("-------------------------\n")
  45. log.writelines("* configuring options...\n")
  46. lnMountPoint = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/options | grep MOUNT_POINT")
  47. mountPointArray = string.split(lnMountPoint, "=")
  48. mountPoint = string.strip(mountPointArray[1])
  49. mountPoint = string.replace(mountPoint, "<<HOME>>", home)
  50. log.writelines("  -- mount point: " + mountPoint + "\n")
  51. lnShowLinks = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/options | grep SHOW_LINKS")
  52. showLinksArray = string.split(lnShowLinks, "=")
  53. showLinks = string.strip(showLinksArray[1])
  54. if showLinks == "1":
  55.     log.writelines("  -- show links: Yes\n")
  56. else:
  57.     log.writelines("  -- show links: No\n")    
  58. lnLinksLocation = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/options | grep LINKS_LOCATION")
  59. linksLocationArray = string.split(lnLinksLocation, "=")
  60. linksLocation = string.strip(linksLocationArray[1])
  61. linksLocation = string.replace(linksLocation, "<<HOME>>", home)
  62. log.writelines("  -- links location: " + linksLocation + "\n")
  63. log.writelines("-------------------------\n")
  64. # read the configuration
  65. fat32_supported = "0"
  66. fat32_mount_type = ""
  67. fat32_mount_options = ""
  68. fat32_type = "b"
  69. fat32_LBA_supported = "0"
  70. fat32_LBA_mount_type = ""
  71. fat32_LBA_mount_options = ""
  72. fat32_LBA_type = "c"
  73. ntfs_supported = "0"
  74. ntfs_mount_type = ""
  75. ntfs_mount_options = ""
  76. ntfs_type = "7"
  77. config_file = open("/usr/lib/linuxmint/mintDisk/mount_options")
  78. for config_item in config_file.readlines():
  79.     array_item = string.split(config_item)
  80.     if (len(array_item) == 4): 
  81.         (config_type, config_supported, config_mount_type, config_mount_options) = string.split(config_item)
  82.         if (fat32_type == config_type):
  83.             log.writelines("* loading support for FAT32...\n")
  84.             fat32_supported = config_supported
  85.             fat32_mount_type = config_mount_type
  86.             fat32_mount_options = config_mount_options
  87.             log.writelines("  -- partition type = " + fat32_type + "\n")
  88.             log.writelines("  -- mount type = " + fat32_mount_type + "\n")
  89.             log.writelines("  -- mount options = " + fat32_mount_options + "\n")
  90.         elif (fat32_LBA_type == config_type):
  91.             log.writelines("* loading support for FAT32 (LBA)...\n")
  92.             fat32_LBA_supported = config_supported
  93.             fat32_LBA_mount_type = config_mount_type
  94.             fat32_LBA_mount_options = config_mount_options
  95.             log.writelines("  -- partition type = " + fat32_LBA_type + "\n")
  96.             log.writelines("  -- mount type = " + fat32_LBA_mount_type + "\n")
  97.             log.writelines("  -- mount options = " + fat32_LBA_mount_options + "\n")
  98.         elif (ntfs_type == config_type):
  99.             log.writelines("* loading support for NTFS...\n")
  100.             ntfs_supported = config_supported
  101.             ntfs_mount_type = config_mount_type
  102.             ntfs_mount_options = config_mount_options
  103.             log.writelines("  -- partition type = " + ntfs_type + "\n")
  104.             log.writelines("  -- mount type = " + ntfs_mount_type + "\n")
  105.             log.writelines("  -- mount options = " + ntfs_mount_options + "\n")
  106.  
  107. # remove existing links
  108. log.writelines("-------------------------\n")
  109. log.writelines("* removing existing links (if any)...\n")
  110. status = commands.getoutput("rm " + home + "/mintDisk*.desktop")
  111. status = commands.getoutput("rm " + linksLocation + "mintDisk*.desktop")
  112.  
  113. # See what partitions are available
  114. log.writelines("-------------------------\n")
  115. log.writelines("* scanning partitions...\n")
  116. os.system("fdisk -l > /usr/lib/linuxmint/mintDisk/detected_partitions")
  117. detected_partitions_file = open("/usr/lib/linuxmint/mintDisk/detected_partitions")
  118. for detected_partition in detected_partitions_file.readlines():
  119.     try:
  120.         if (string.find(detected_partition, "/dev/") == 0):
  121.             array = string.split(detected_partition)
  122.             device = array[0]
  123.             type = array[4]
  124.             size = array[3]
  125.             if (array[1] == "*"):
  126.                 type = array[5]
  127.                 size = array[4]
  128.             size = string.replace(size, "+", "")
  129.             size = int(size)/1000000
  130.             strSize = str(size) + "GB"
  131.             log.writelines("  -- detected device "+ device + " of type " + type + " and size " + strSize + "\n")
  132.             mount_type = ""
  133.             mount_options = ""            
  134.             device_type = "none"
  135.             if (fat32_type == type and fat32_supported == "1"):
  136.                 device_type = "FAT32"
  137.                 mount_type = fat32_mount_type
  138.                 mount_options = fat32_mount_options    
  139.             elif (fat32_LBA_type == type and fat32_LBA_supported == "1"):
  140.                 device_type = "FAT32(LBA)"
  141.                 mount_type = fat32_LBA_mount_type
  142.                 mount_options = fat32_LBA_mount_options    
  143.             elif (ntfs_type == type and ntfs_supported == "1"):
  144.                 device_type = "NTFS"
  145.                 mount_type = ntfs_mount_type
  146.                 mount_options = ntfs_mount_options
  147.         
  148.             if (device_type != "none"):
  149.                 device_name = string.replace(device, "/dev/", "")                
  150.                 mount_point = string.replace(mountPoint, "<<DEVICE>>", device_name)
  151.                 
  152.                 # find alias for the device
  153.                 alias = strSize + " " + device_type + " Volume (" + device_name + ")"            
  154.                 nbAliases = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/aliases | grep " + device + " | wc -l")
  155.                 if (nbAliases == "1"):
  156.                     aliasStr = commands.getoutput("cat /usr/lib/linuxmint/mintDisk/aliases | grep " + device)
  157.                     aliasArray = string.split(aliasStr, "=")
  158.                     if (len(aliasArray)==2):
  159.                         alias = aliasArray[1]
  160.                         alias = string.lstrip(alias)
  161.                         log.writelines("     => alias: " + alias + "\n")
  162.                         mount_point = string.replace(mount_point, "<<ALIAS>>", alias)
  163.                 else:
  164.                     log.writelines("     => no alias defined\n")
  165.                     mount_point = string.replace(mount_point, "<<ALIAS>>", device_name)
  166.                     
  167.                 # check to see if this is not taken care by fstab...
  168.                 managed_by_fstab = commands.getoutput("cat /etc/fstab | grep " + device + " | wc -l")
  169.                 if (managed_by_fstab == "0"):
  170.                     # mounting the device if not already mounted
  171.                     alreadyMounted = commands.getoutput("mount | grep " + device + " | wc -l")
  172.                     if (alreadyMounted == "0"):
  173.                         # mounting the device
  174.                         log.writelines("     => mounting partition...\n")                        
  175.                         status = commands.getoutput("mkdir -p " + mount_point)
  176.                         status = commands.getoutput("mount -t " + mount_type + " -o " + mount_options + " " + device + " " + mount_point)
  177.                         log.writelines("     => " + status + "\n")                        
  178.                     else:    
  179.                         log.writelines("     => partition mounted already\n")
  180.  
  181.                     alreadyMounted = commands.getoutput("mount | grep " + device + " | wc -l")
  182.                     if (alreadyMounted == "0"):
  183.                         log.writelines("     => could not mount partition, no link created\n")
  184.                     else:
  185.                         if (showLinks == "1"):
  186.                             # get the mount point
  187.                             real_mount_point_line = commands.getoutput("mount | grep " + device )
  188.                             real_mount_point_array = real_mount_point_line.split()
  189.                             real_mount_point = real_mount_point_array[2]
  190.  
  191.                             # creating a link to the mount point
  192.                             log.writelines("     => creating a link to " + real_mount_point + "\n")                            
  193.                             linkPath = linksLocation + "mintDisk_" + device_name + ".desktop"
  194.                             try:                            
  195.                                 link = open(linkPath, "w")
  196.                             except Exception, detail:
  197.                                 os.system("mkdir -p " + linksLocation)
  198.                                                             os.system("chown " + user + ":" + user + " " + linksLocation)
  199.                                 link = open(linkPath, "w")
  200.                             link.writelines("[Desktop Entry]\n")
  201.                             link.writelines("Encoding=UTF-8\n")
  202.                             link.writelines("Version=1.0\n")
  203.                             link.writelines("Type=Link\n")
  204.                             link.writelines("URL=" + real_mount_point + "\n")
  205.                             link.writelines("Name=" + alias + "\n")
  206.                             link.writelines("Comment=" + strSize + " " + device_type + " Volume (" + device_name + ")\n")
  207.                             link.writelines("Icon=/usr/share/icons/gnome/scalable/devices/drive-harddisk.svg\n")
  208.                             link.close()
  209.                             os.system("chown " + user + ":" + user + " " + linkPath)
  210.                         else:
  211.                             log.writelines("     => no link created\n")
  212.                 else:
  213.                     log.writelines("     => partition managed by /etc/fstab\n")
  214.             else:
  215.                 log.writelines("     => partition type not supported\n")
  216.     except Exception, detail:
  217.         print "ERROR"         
  218.         log.writelines(detail)
  219.