installutils.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # copyright 1999 McMillan Enterprises, Inc.
  2. # demo code - use as you please.
  3. import os
  4. import stat
  5. def copyFile(srcFiles, destFile, append=0):
  6. '''
  7. Copy one or more files to another file. If srcFiles is a list, then all
  8. will be concatenated together to destFile. The append flag is also valid
  9. for single file copies.
  10. destFile will have the mode, ownership and timestamp of the last file
  11. copied/appended.
  12. '''
  13. if type(srcFiles) == type([]):
  14. # in case we need to overwrite on the first file...
  15. copyFile(srcFiles[0], destFile, append)
  16. for file in srcFiles[1:]:
  17. copyFile(file, destFile, 1)
  18. return
  19. mode = 'wb'
  20. if append:
  21. mode = 'ab'
  22. print " ", srcFiles, "->",
  23. input = open(srcFiles, 'rb')
  24. if input:
  25. print destFile
  26. output = open(destFile, mode)
  27. while 1:
  28. bytesRead = input.read(8192)
  29. if bytesRead:
  30. output.write(bytesRead)
  31. else:
  32. break
  33. input.close()
  34. output.close()
  35. stats = os.stat(srcFiles)
  36. os.chmod(destFile, stats[stat.ST_MODE])
  37. try: # FAT16 file systems have only one file time
  38. os.utime(destFile, (stats[stat.ST_ATIME], stats[stat.ST_MTIME]))
  39. except:
  40. pass
  41. try:
  42. os.chown(destFile, stats[stat.ST_UID], stats[stat.ST_GID])
  43. except:
  44. pass
  45. def ensure(dirct):
  46. dirnm = dirct
  47. plist = []
  48. try:
  49. while not os.path.exists(dirnm):
  50. dirnm, base = os.path.split(dirnm)
  51. if base == '':
  52. break
  53. plist.insert(0, base)
  54. for d in plist:
  55. dirnm = os.path.join(dirnm, d)
  56. os.mkdir(dirnm)
  57. except:
  58. return 0
  59. return 1
  60. def getinstalldir(prompt="Enter an installation directory: "):
  61. while 1:
  62. installdir = raw_input("Enter an installation directory: ")
  63. installdir = os.path.normpath(installdir)
  64. if ensure(installdir):
  65. break
  66. else:
  67. print installdir, "is not a valid pathname"
  68. r = raw_input("Try again (y/n)?: ")
  69. if r in 'nN':
  70. sys.exit(0)
  71. return installdir
  72. def installCArchive(nm, basedir, suffixdir):
  73. import carchive_rt
  74. fulldir = os.path.join(basedir, suffixdir)
  75. if ensure(fulldir):
  76. pkg = carchive_rt.CArchive(nm)
  77. for fnm in pkg.contents():
  78. stuff = pkg.extract(fnm)[1]
  79. outnm = os.path.join(fulldir, fnm)
  80. if ensure(os.path.dirname(outnm)):
  81. open(outnm, 'wb').write(stuff)
  82. pkg = None
  83. os.remove(nm)