Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

Commit 59efc5a

Browse files
committed
Add hunter-cache.py script
1 parent dfc438e commit 59efc5a

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

hunter-cache.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (c) 2014, Ruslan Baratov
4+
# All rights reserved.
5+
6+
import argparse
7+
import sys
8+
import tarfile
9+
import os
10+
11+
parser = argparse.ArgumentParser(
12+
description='Pack/unpack hunter builded directories'
13+
)
14+
15+
tar_gz_help = 'tar gz filename (e.g. my-pack.tar.gz)'
16+
parser.add_argument(
17+
'--pack',
18+
metavar='PACK.tar.gz',
19+
help='Read HUNTER_ROOT environment variable '
20+
'and pack directory to PACK.tar.gz file'
21+
)
22+
23+
parser.add_argument(
24+
'--unpack',
25+
metavar='PACK.tar.gz',
26+
help='Unpack PACK.tar.gz to empty directory specified '
27+
'by HUNTER_ROOT environment variable'
28+
)
29+
30+
args = parser.parse_args()
31+
32+
if not args.pack and not args.unpack:
33+
print('Error: --pack or --unpack required\n')
34+
parser.print_help()
35+
sys.exit(1)
36+
37+
hunter_root = os.getenv('HUNTER_ROOT')
38+
if not hunter_root:
39+
sys.exit('HUNTER_ROOT environment variable is empty')
40+
41+
if not os.path.isabs(hunter_root):
42+
sys.exit('HUNTER_ROOT path is not absolute: `{}`'.format(hunter_root))
43+
44+
def pack_directory(dir, file):
45+
if os.path.exists(file):
46+
sys.exit('File `{}` exists'.format(file))
47+
if not os.path.exists(dir):
48+
sys.exit('Directory `{}` not exists'.format(dir))
49+
if len(os.listdir(dir)) == 0:
50+
sys.exit('Directory `{}` is empty'.format(dir))
51+
print('pack directory `{}` to file `{}`'.format(dir, file))
52+
archive = tarfile.open(name=file, mode='w:gz')
53+
54+
sources_dir = os.path.join(dir, 'Base', 'Source')
55+
download_dir = os.path.join(dir, 'Download')
56+
57+
for root, dirnames, filenames in os.walk(dir):
58+
for filename in filenames:
59+
filepath = os.path.join(root, filename)
60+
61+
if filepath.startswith(sources_dir):
62+
continue
63+
if filepath.startswith(download_dir):
64+
continue
65+
66+
name = os.path.relpath(filepath, start=dir)
67+
archive.add(filepath, arcname=name)
68+
archive.close
69+
print('done')
70+
71+
def unpack_directory(dir, file):
72+
print('unpack `{}` to directory `{}`'.format(file, dir))
73+
if not os.path.exists(file):
74+
sys.exit('File `{}` not exists'.format(file))
75+
if os.path.exists(dir) and len(os.listdir(dir)) > 0:
76+
sys.exit('Directory `{}` is not empty. Please remove it'.format(dir))
77+
archive = tarfile.open(name=file, mode='r:gz')
78+
archive.extractall(path=dir)
79+
archive.close()
80+
print('done')
81+
82+
if args.pack:
83+
pack_directory(hunter_root, args.pack)
84+
85+
if args.unpack:
86+
unpack_directory(hunter_root, args.unpack)

0 commit comments

Comments
 (0)