unshar.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » scripts » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » scripts » unshar.py
# Extract files from a SHAR archive.
# Run this on the Mac.
# Usage:
# >>> import unshar
# >>> f = open('SHAR')
# >>> unshar.unshar(f)

import string

def unshar(fp, verbose=0, overwrite=0):
  ofp = None
  file = None
  while 1:
    line = fp.readline()
    if verbose > 3: print 'Got:', `line`
    if line[:1] == 'X':
      # Most common case first
      if ofp: ofp.write(line[1:])
      continue
    if not line:
      if verbose: print 'EOF'
      if ofp:
        print 'Unterminated file -- closing'
        ofp.close()
        ofp = None
      break
    if line[0] == '#':
      if verbose: print line,
      continue
    if line[:14] == 'sed "s/^X//" >':
      if verbose: print "!!!", `line`
      i = string.find(line, "'")
      j = string.find(line, "'", i+1)
      if i >= 0 and j > i:
        file = line[i+1:j]
        if '/' in file:
          words = string.splitfields(file, '/')
          for funny in '', '.':
            while funny in words: words.remove(funny)
          for i in range(len(words)):
            if words[i] == '..': words[i] = ''
          words.insert(0, '')
          file = string.joinfields(words, ':')
        try:
          ofp = open(file, 'r')
          ofp.close()
          ofp = None
          over = 1
        except IOError:
          over = 0
        if over and not overwrite:
          print 'Skipping', file, '(already exists) ...'
          continue
        ofp = open(file, 'w')
        if over:
          print 'Overwriting', file, '...'
        else:
          print 'Writing', file, '...'
      continue
    if line == 'END_OF_FILE\n':
      if not file:
        print 'Unexpected END_OF_FILE marker'
      if ofp:
        print 'done'
        ofp.close()
        ofp = None
      else:
        print 'done skipping'
      file = None
      continue
    if verbose: print "...", `line`
    
def main():
  import sys
  import os
  if len(sys.argv) > 1:
    for fname in sys.argv[1:]:
      fp = open(fname, 'r')
      dir, fn = os.path.split(fname)
      if dir:
        os.chdir(dir)
      unshar(fp)
  else:
    import macfs
    fss, ok = macfs.StandardGetFile('TEXT')
    if not ok:
      sys.exit(0)
    fname = fss.as_pathname()
    fp = open(fname, 'r')
    fss, ok = macfs.GetDirectory('Folder to save files in:')
    if not ok:
      sys.exit(0)
    os.chdir(fss.as_pathname())
    unshar(fp)
    
if __name__ == '__main__':
  main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.