-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
60 lines (48 loc) · 1.63 KB
/
setup.py
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
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
"""zip source directory tree"""
import argparse
import fnmatch
import logging
import os
import re
import subprocess
import zipfile
def get_version():
command = ['git', 'describe', '--tags', '--dirty', '--always']
return subprocess.check_output(command).decode('utf-8')
def source_walk(root):
root = os.path.abspath(root)
regex = re.compile(fnmatch.translate('*.py[co]'))
for path, _, files in os.walk(root):
files[:] = [f for f in files if regex.match(f) is None]
for filename in files:
fullpath = os.path.join(path, filename)
yield fullpath, os.path.relpath(fullpath, root)
def setup():
argparser = argparse.ArgumentParser(description=__doc__)
argparser.add_argument(
'-d', '--debug',
action='store_true',
help='print debug information')
argparser.add_argument(
'-o',
metavar='zipfile',
dest='output',
help='output file name')
argparser.add_argument(
'source',
help='source directory')
args = argparser.parse_args()
loglevel = logging.DEBUG if args.debug else logging.WARNING
logging.basicConfig(format='%(levelname)s: %(message)s', level=loglevel)
if not os.path.isdir(args.source):
logging.critical('"%s" is not a directory', args.source)
return
if args.output is None:
args.output = args.source + '.zip'
with zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED) as fzip:
fzip.writestr('version.txt', get_version())
for path, relpath in source_walk(args.source):
fzip.write(path, relpath)
if __name__ == '__main__':
setup()