src/modules/misc.py
changeset 462 910600c14093
parent 461 37cf3ac75e37
child 487 9cb85c3d8491
equal deleted inserted replaced
461:37cf3ac75e37 462:910600c14093
    35 import urlparse
    35 import urlparse
    36 import sys
    36 import sys
    37 import zlib
    37 import zlib
    38 import time
    38 import time
    39 import calendar
    39 import calendar
       
    40 import shutil
       
    41 from stat import *
       
    42 
    40 import pkg.urlhelpers as urlhelpers
    43 import pkg.urlhelpers as urlhelpers
    41 import pkg.portable as portable
    44 import pkg.portable as portable
    42 from pkg.client.imagetypes import img_type_names, IMG_NONE
    45 from pkg.client.imagetypes import img_type_names, IMG_NONE
    43 from pkg import VERSION
    46 from pkg import VERSION
    44 
    47 
    45 def time_to_timestamp(t):
    48 def time_to_timestamp(t):
    46         """ convert seconds since epoch to %Y%m%dT%H%M%SZ format"""
    49         """convert seconds since epoch to %Y%m%dT%H%M%SZ format"""
    47         # XXX optimize?
    50         # XXX optimize?
    48         return time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(t))
    51         return time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(t))
    49 
    52 
    50 def timestamp_to_time(ts):
    53 def timestamp_to_time(ts):
    51         """ convert %Y%m%dT%H%M%SZ format to seconds since epoch"""
    54         """convert %Y%m%dT%H%M%SZ format to seconds since epoch"""
    52         # XXX optimize?
    55         # XXX optimize?
    53         return calendar.timegm(time.strptime(ts, "%Y%m%dT%H%M%SZ"))
    56         return calendar.timegm(time.strptime(ts, "%Y%m%dT%H%M%SZ"))
       
    57 
       
    58 def copyfile(src_path, dst_path):
       
    59         """copy a file, preserving attributes, ownership, etc. where possible"""
       
    60         stat = os.lstat(src_path)
       
    61         shutil.copy2(src_path, dst_path)
       
    62         try:
       
    63                 portable.chown(dst_path, stat.st_uid, stat.st_gid)
       
    64         except OSError, e:
       
    65                 if e.errno != errno.EPERM:
       
    66                         raise
    54 
    67 
    55 def hash_file_name(f):
    68 def hash_file_name(f):
    56         """Return the two-level path fragment for the given filename, which is
    69         """Return the two-level path fragment for the given filename, which is
    57         assumed to be a content hash of at least 8 distinct characters."""
    70         assumed to be a content hash of at least 8 distinct characters."""
    58         return os.path.join("%s" % f[0:2], "%s" % f[2:8], "%s" % f)
    71         return os.path.join("%s" % f[0:2], "%s" % f[2:8], "%s" % f)