1. Henrik Stuart
  2. repoman

Source

repoman / repoext / archive.py

# Copyright 2009-2012 Edlund A/S
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
'''
command to create an unversioned archive of all modules of a forest

This extension allows you to export an unversioned archive of a forest to a
directory.
'''

import os

import mercurial.commands 
import mercurial.util

from repoman import util


def archive(ui, f, dest, **opts):
    '''create an unversioned archive of all modules of a forest

    The revisions used are the parents of the working directories of 
    each of the modules in the forest. To archive different 
    revisions, update each module individually to the desired 
    revision before archiving.  

    DESTINATION should be a non-existing or empty directory. 
    '''
    # check destination. Create or abort if necessary.
    dest = os.path.abspath(dest)
    if not os.path.exists(dest):
        os.makedirs(dest)
    elif not os.path.isdir(dest):
        raise util.Abort('destination is not a directory')
    elif os.listdir(dest):
        raise util.Abort('destination directory is not empty')

    # archival status file buffer
    archival = [] 

    # perform mercurial archive for all modules
    for mod in f:
        repo = f[mod]
        module_dest = os.path.join(dest, mod)
        mercurial.commands.archive(ui, repo, module_dest,
                **util.get_mercurial_default_opts('archive'))

        # read (and remove) archival status into buffer
        hg_file_path = os.path.join(module_dest, '.hg_archival.txt')
        with open(hg_file_path, 'r') as hg_file:
            archival.append('mod: %s\n%s' % (mod, hg_file.read()))
        mercurial.util.unlink(hg_file_path)

    # write archival status to file
    with open(os.path.join(dest, '.repo_archival.txt'), 'w') as repo_file:
        repo_file.write('----------------------------------------------\n'.join(archival))



cmdtable = {
        'archive':
            (archive,
             [],
             [],
             'DESTINATION')
        }