packmail.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » lib-old » 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 » Lib » lib old » packmail.py
# Module 'packmail' -- create a self-unpacking shell archive.

# This module works on UNIX and on the Mac; the archives can unpack
# themselves only on UNIX.

import os
from stat import ST_MTIME

# Print help
def help():
  print 'All fns have a file open for writing as first parameter'
  print 'pack(f, fullname, name): pack fullname as name'
  print 'packsome(f, directory, namelist): selected files from directory'
  print 'packall(f, directory): pack all files from directory'
  print 'packnotolder(f, directory, name): pack all files from directory'
  print '                        that are not older than a file there'
  print 'packtree(f, directory): pack entire directory tree'

# Pack one file
def pack(outfp, file, name):
  fp = open(file, 'r')
  outfp.write('echo ' + name + '\n')
  outfp.write('sed "s/^X//" >"' + name + '" <<"!"\n')
  while 1:
    line = fp.readline()
    if not line: break
    if line[-1:] != '\n':
      line = line + '\n'
    outfp.write('X' + line)
  outfp.write('!\n')
  fp.close()

# Pack some files from a directory
def packsome(outfp, dirname, names):
  for name in names:
    print name
    file = os.path.join(dirname, name)
    pack(outfp, file, name)

# Pack all files from a directory
def packall(outfp, dirname):
  names = os.listdir(dirname)
  try:
      names.remove('.')
  except:
      pass
  try:
      names.remove('..')
  except:
      pass
  names.sort()
  packsome(outfp, dirname, names)

# Pack all files from a directory that are not older than a give one
def packnotolder(outfp, dirname, oldest):
  names = os.listdir(dirname)
  try:
      names.remove('.')
  except:
      pass
  try:
      names.remove('..')
  except:
      pass
  oldest = os.path.join(dirname, oldest)
  st = os.stat(oldest)
  mtime = st[ST_MTIME]
  todo = []
  for name in names:
    print name, '...',
    st = os.stat(os.path.join(dirname, name))
    if st[ST_MTIME] >= mtime:
      print 'Yes.'
      todo.append(name)
    else:
      print 'No.'
  todo.sort()
  packsome(outfp, dirname, todo)

# Pack a whole tree (no exceptions)
def packtree(outfp, dirname):
  print 'packtree', dirname
  outfp.write('mkdir ' + unixfix(dirname) + '\n')
  names = os.listdir(dirname)
  try:
      names.remove('.')
  except:
      pass
  try:
      names.remove('..')
  except:
      pass
  subdirs = []
  for name in names:
    fullname = os.path.join(dirname, name)
    if os.path.isdir(fullname):
      subdirs.append(fullname)
    else:
      print 'pack', fullname
      pack(outfp, fullname, unixfix(fullname))
  for subdirname in subdirs:
    packtree(outfp, subdirname)

def unixfix(name):
  comps = name.split(os.sep)
  res = ''
  for comp in comps:
    if comp:
      if res: res = res + '/'
      res = res + comp
  return res
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.