nsinstall.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # ***** BEGIN LICENSE BLOCK *****
  2. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. #
  4. # The contents of this file are subject to the Mozilla Public License Version
  5. # 1.1 (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. # http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS IS" basis,
  10. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. # for the specific language governing rights and limitations under the
  12. # License.
  13. #
  14. # The Original Code is Mozilla.
  15. #
  16. # The Initial Developer of the Original Code is
  17. # the Mozilla Foundation.
  18. # Portions created by the Initial Developer are Copyright (C) 2007
  19. # the Initial Developer. All Rights Reserved.
  20. #
  21. # Contributor(s):
  22. # Axel Hecht <axel@pike.org>
  23. #
  24. # Alternatively, the contents of this file may be used under the terms of
  25. # either the GNU General Public License Version 2 or later (the "GPL"), or
  26. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. # in which case the provisions of the GPL or the LGPL are applicable instead
  28. # of those above. If you wish to allow use of your version of this file only
  29. # under the terms of either the GPL or the LGPL, and not to allow others to
  30. # use your version of this file under the terms of the MPL, indicate your
  31. # decision by deleting the provisions above and replace them with the notice
  32. # and other provisions required by the GPL or the LGPL. If you do not delete
  33. # the provisions above, a recipient may use your version of this file under
  34. # the terms of any one of the MPL, the GPL or the LGPL.
  35. #
  36. # ***** END LICENSE BLOCK *****
  37. # This is a partial python port of nsinstall.
  38. # It's intended to be used when there's no natively compile nsinstall
  39. # available, and doesn't intend to be fully equivalent.
  40. # Its major use is for l10n repackaging on systems that don't have
  41. # a full build environment set up.
  42. # The basic limitation is, it doesn't even try to link and ignores
  43. # all related options.
  44. from optparse import OptionParser
  45. import os
  46. import os.path
  47. import sys
  48. import shutil
  49. def nsinstall(argv):
  50. usage = "usage: %prog [options] arg1 [arg2 ...] target-directory"
  51. p = OptionParser(usage=usage)
  52. p.add_option('-D', action="store_true",
  53. help="Create a single directory only")
  54. p.add_option('-t', action="store_true",
  55. help="Preserve time stamp")
  56. p.add_option('-m', action="store",
  57. help="Set mode", metavar="mode")
  58. p.add_option('-d', action="store_true",
  59. help="Create directories in target")
  60. p.add_option('-R', action="store_true",
  61. help="Use relative symbolic links (ignored)")
  62. p.add_option('-l', action="store_true",
  63. help="Create link (ignored)")
  64. p.add_option('-L', action="store", metavar="linkprefix",
  65. help="Link prefix (ignored)")
  66. # The remaining arguments are not used in our tree, thus they're not
  67. # implented.
  68. def BadArg(option, opt, value, parser):
  69. parser.error('option not supported: %s' % opt)
  70. p.add_option('-C', action="callback", metavar="CWD",
  71. callback=BadArg,
  72. help="NOT SUPPORTED")
  73. p.add_option('-o', action="callback", callback=BadArg,
  74. help="Set owner (NOT SUPPORTED)", metavar="owner")
  75. p.add_option('-g', action="callback", callback=BadArg,
  76. help="Set group (NOT SUPPORTED)", metavar="group")
  77. (options, args) = p.parse_args(argv)
  78. if options.m:
  79. # mode is specified
  80. try:
  81. options.m = int(options.m, 8)
  82. except:
  83. sys.stderr.write('nsinstall: ' + options.m + ' is not a valid mode\n')
  84. return 1
  85. # just create one directory?
  86. if options.D:
  87. if len(args) != 1:
  88. return 1
  89. if os.path.exists(args[0]):
  90. if not os.path.isdir(args[0]):
  91. sys.stderr.write('nsinstall: ' + args[0] + ' is not a directory\n')
  92. sys.exit(1)
  93. if options.m:
  94. os.chmod(args[0], options.m)
  95. sys.exit()
  96. if options.m:
  97. os.makedirs(args[0], options.m)
  98. else:
  99. os.makedirs(args[0])
  100. return 0
  101. # nsinstall arg1 [...] directory
  102. if len(args) < 2:
  103. p.error('not enough arguments')
  104. def copy_all_entries(entries, target):
  105. for e in entries:
  106. dest = os.path.join(target,
  107. os.path.basename(os.path.normpath(e)))
  108. handleTarget(e, dest)
  109. if options.m:
  110. os.chmod(dest, options.m)
  111. # set up handler
  112. if options.d:
  113. # we're supposed to create directories
  114. def handleTarget(srcpath, targetpath):
  115. # target directory was already created, just use mkdir
  116. os.mkdir(targetpath)
  117. else:
  118. # we're supposed to copy files
  119. def handleTarget(srcpath, targetpath):
  120. if os.path.isdir(srcpath):
  121. if not os.path.exists(targetpath):
  122. os.mkdir(targetpath)
  123. entries = [os.path.join(srcpath, e) for e in os.listdir(srcpath)]
  124. copy_all_entries(entries, targetpath)
  125. # options.t is not relevant for directories
  126. if options.m:
  127. os.chmod(targetpath, options.m)
  128. elif options.t:
  129. shutil.copy2(srcpath, targetpath)
  130. else:
  131. shutil.copy(srcpath, targetpath)
  132. # the last argument is the target directory
  133. target = args.pop()
  134. # ensure target directory
  135. if not os.path.isdir(target):
  136. os.makedirs(target)
  137. copy_all_entries(args, target)
  138. return 0
  139. if __name__ == '__main__':
  140. sys.exit(nsinstall(sys.argv[1:]))