2017-08-16 12:45:00 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2017-09-19 23:29:59 -04:00
|
|
|
import argparse
|
2017-08-16 12:45:00 -04:00
|
|
|
import os
|
|
|
|
import glob
|
2017-09-20 00:12:40 -04:00
|
|
|
import distutils.cmd
|
|
|
|
import distutils.log
|
2017-10-11 01:10:18 -04:00
|
|
|
from typing import IO, Iterator
|
2017-08-16 12:45:00 -04:00
|
|
|
|
|
|
|
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
BOTS_DIR = os.path.normpath(os.path.join(CURRENT_DIR, 'zulip_bots', 'bots'))
|
2017-09-19 23:29:59 -04:00
|
|
|
MANIFEST_PATH = os.path.join(CURRENT_DIR, 'MANIFEST.in')
|
2017-08-16 12:45:00 -04:00
|
|
|
|
2017-09-20 00:12:40 -04:00
|
|
|
class GenerateManifest(distutils.cmd.Command):
|
|
|
|
"""
|
|
|
|
A custom setup.py command to generate a MANIFEST.in
|
|
|
|
for the zulip_bots package.
|
|
|
|
"""
|
|
|
|
description = 'generate a MANIFEST.in for PyPA or for development'
|
|
|
|
user_options = [
|
|
|
|
('release', None, 'generate a MANIFEST for a PyPA release'),
|
|
|
|
]
|
|
|
|
|
|
|
|
def initialize_options(self):
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: () -> None
|
2017-09-20 00:12:40 -04:00
|
|
|
self.release = False
|
|
|
|
|
|
|
|
def finalize_options(self):
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: () -> None
|
2017-09-20 00:12:40 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
def run(self):
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: () -> None
|
2017-09-20 00:12:40 -04:00
|
|
|
if self.release:
|
|
|
|
generate_release_manifest()
|
2017-10-11 01:10:18 -04:00
|
|
|
self.announce( # type: ignore # error: "GenerateManifest" has no attribute "announce"
|
2017-09-20 00:12:40 -04:00
|
|
|
'Generating a MANIFEST for a PyPA release of zulip_bots.',
|
2017-10-11 01:10:18 -04:00
|
|
|
level=distutils.log.INFO # type: ignore # error: Module has no attribute "INFO"
|
2017-09-20 00:12:40 -04:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
generate_dev_manifest()
|
2017-10-11 01:10:18 -04:00
|
|
|
self.announce( # type: ignore
|
2017-09-20 00:12:40 -04:00
|
|
|
'Generating a MANIFEST for zulip_bots\' development.',
|
2017-10-11 01:10:18 -04:00
|
|
|
level=distutils.log.INFO # type: ignore
|
2017-09-20 00:12:40 -04:00
|
|
|
)
|
|
|
|
|
2017-08-16 12:45:00 -04:00
|
|
|
def get_test_fixtures():
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: () -> Iterator[str]
|
2017-08-16 12:45:00 -04:00
|
|
|
glob_pattern = os.path.join(BOTS_DIR, '*', 'fixtures', '*.json')
|
|
|
|
fixtures_paths = map(
|
|
|
|
lambda fp: os.path.join(*fp.split(os.path.sep)[-5:]).replace(os.path.sep, '/'),
|
|
|
|
glob.glob(glob_pattern)
|
|
|
|
)
|
|
|
|
return fixtures_paths
|
|
|
|
|
|
|
|
def get_logos():
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: () -> Iterator[str]
|
2017-08-16 12:45:00 -04:00
|
|
|
glob_pattern = os.path.join(BOTS_DIR, '*', 'logo.*')
|
|
|
|
logo_paths = map(
|
|
|
|
lambda fp: os.path.join(*fp.split(os.path.sep)[-4:]).replace(os.path.sep, '/'),
|
|
|
|
glob.glob(glob_pattern)
|
|
|
|
)
|
|
|
|
return logo_paths
|
|
|
|
|
|
|
|
def get_docs():
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: () -> Iterator[str]
|
2017-08-16 12:45:00 -04:00
|
|
|
glob_pattern = os.path.join(BOTS_DIR, '*', 'doc.md')
|
|
|
|
doc_paths = map(
|
|
|
|
lambda fp: os.path.join(*fp.split(os.path.sep)[-4:]).replace(os.path.sep, '/'),
|
|
|
|
glob.glob(glob_pattern)
|
|
|
|
)
|
|
|
|
return doc_paths
|
|
|
|
|
2017-09-19 23:15:35 -04:00
|
|
|
def get_assets():
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: () -> Iterator[str]
|
2017-09-19 23:15:35 -04:00
|
|
|
glob_pattern = os.path.join(BOTS_DIR, '*', 'assets', '*')
|
|
|
|
assets_files = map(
|
|
|
|
lambda fp: os.path.join(*fp.split(os.path.sep)[-5:]).replace(os.path.sep, '/'),
|
|
|
|
glob.glob(glob_pattern)
|
|
|
|
)
|
|
|
|
return assets_files
|
|
|
|
|
2017-09-19 23:29:59 -04:00
|
|
|
def generate_and_write(filepaths, file_obj):
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: (Iterator[str], IO[str]) -> None
|
2017-09-19 23:29:59 -04:00
|
|
|
template = 'include {line}\n'
|
|
|
|
lines = map(lambda line: template.format(line=line), filepaths)
|
|
|
|
|
|
|
|
file_obj.writelines(lines)
|
|
|
|
file_obj.write('\n')
|
|
|
|
|
|
|
|
def generate_dev_manifest():
|
|
|
|
# type: () -> None
|
|
|
|
with open(MANIFEST_PATH, 'w') as fp:
|
|
|
|
generate_and_write(get_test_fixtures(), fp)
|
|
|
|
generate_and_write(get_logos(), fp)
|
|
|
|
generate_and_write(get_docs(), fp)
|
|
|
|
generate_and_write(get_assets(), fp)
|
|
|
|
|
|
|
|
def generate_release_manifest():
|
|
|
|
# type: () -> None
|
|
|
|
with open(MANIFEST_PATH, 'w') as fp:
|
|
|
|
generate_and_write(get_docs(), fp)
|
|
|
|
generate_and_write(get_assets(), fp)
|
|
|
|
|
|
|
|
def parse_args():
|
2017-10-11 01:10:18 -04:00
|
|
|
# type: () -> argparse.Namespace
|
2017-09-19 23:29:59 -04:00
|
|
|
usage = """
|
|
|
|
To generate a MANIFEST.in for a PyPA release, run:
|
|
|
|
|
|
|
|
./generate_manifest.py --release
|
|
|
|
|
|
|
|
To generate a MANIFEST.in for development, run without arguments:
|
|
|
|
|
|
|
|
./generate_manifest.py
|
|
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser(usage=usage)
|
|
|
|
|
|
|
|
parser.add_argument('--release', '-r',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
|
|
|
help='Generate MANIFEST.in for a PyPA release.')
|
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
2017-08-16 12:45:00 -04:00
|
|
|
def main():
|
|
|
|
# type: () -> None
|
2017-09-19 23:29:59 -04:00
|
|
|
options = parse_args()
|
|
|
|
if options.release:
|
|
|
|
generate_release_manifest()
|
|
|
|
else:
|
|
|
|
generate_dev_manifest()
|
|
|
|
|
2017-08-16 12:45:00 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|