File size: 1,376 Bytes
a65138c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
import sys
import os.path
import logging
import tempfile
import shutil

import setuptools.archive_util


logger = logging.getLogger(__name__)


def main():
    logging.basicConfig()
    logger.setLevel(logging.DEBUG)

    src = sys.argv[1]
    dst = sys.argv[2]

    strip_toplevel_dir = True

    if not os.path.exists(dst):
        os.makedirs(dst)

    if not os.path.isdir(dst):
        logger.error('%s: not a directory', dst)

    if strip_toplevel_dir:
        tempdir = tempfile.mkdtemp()
        try:
            setuptools.archive_util.unpack_archive(src, tempdir)
            toplevel_items = os.listdir(tempdir)
            if len(toplevel_items) > 1:
                logger.error('%s has no single top-level directory', src)
                raise SystemExit(1)
            root = os.path.join(tempdir, toplevel_items[0])
            for item in os.listdir(root):
                src_item = os.path.join(root, item)
                dst_item = os.path.join(dst, item)
                if os.path.exists(dst_item):
                    if os.path.isdir(dst_item):
                        shutil.rmtree(dst_item)
                    else:
                        os.unlink(dst_item)
                shutil.move(src_item, dst)
        finally:
            shutil.rmtree(tempdir)
    else:
        setuptools.archive_util.unpack_archive(src, dst)