Skip to content

Instantly share code, notes, and snippets.

@jeetsukumaran
Last active April 27, 2022 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeetsukumaran/61f423cfc7d35f8abfffd29a3f0da3d0 to your computer and use it in GitHub Desktop.
Save jeetsukumaran/61f423cfc7d35f8abfffd29a3f0da3d0 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
###############################################################################
##
## Copyright 2012 Jeet Sukumaran.
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Fountion; either version 3 of the License, or
## (at your option) any ler version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program. If not, see <http://www.gnu.org/licenses/>.
##
###############################################################################
"""
Creates Python project skeleton.
"""
import sys
import os
import argparse
from datetime import datetime
import logging
__prog__ = os.path.basename(__file__)
__version__ = "1.0.0"
__description__ = __doc__
__author__ = 'Jeet Sukumaran'
__copyright__ = 'Copyright (C) 2014 Jeet Sukumaran.'
# Templates {{{1
project_creation_timestamp = datetime.now()
project_author = "Jeet Sukumaran"
project_author_email = "jeetsukumaran@gmail.com"
project_license_brief = """\
Copyright (c) {year} {author}.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL JEET SUKUMARAN BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
""".format(year=project_creation_timestamp.year,
author=project_author)
templates = {}
templates["gitignore"] = """\
*.pyc
__pycache__
*.egg-info*
build/*
dist/*
setup.cfg
setuptools-*.egg
.DS_Store
"""
templates["manifest"] = """\
# core docs
include README.md
include CHANGES.md
include LICENSE
include docs/Makefile
# main docs
graft docs/source
# package data
graft resources
# litter
global-exclude *.py[cod]
global-exclude .DS_Store
"""
templates["entry_points"] = """\
entry_points={{
"console_scripts": [
# "name-of-executable = module.with:function_to_execute"
"{normalized_project_cli_name} = {normalized_project_name}.application.{normalized_project_name}:main",
]
}},"""
templates["bin_scripts"] = """\
scripts=["bin/{normalized_project_clI_name}",],
"""
templates["setup.py"] = """\
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import re
from setuptools import setup, find_packages
def _read(path_components, **kwargs):
path = os.path.join(os.path.dirname(__file__), *path_components)
if sys.version_info.major < 3:
return open(path, "rU").read()
else:
with open(path, encoding=kwargs.get("encoding", "utf8")) as src:
s = src.read()
return s
def _read_requirements(path):
return [
line.strip()
for line in _read([path]).split("\\n")
if not line.startswith(('"', "#", "-", "git+"))
]
project_init = _read(["src", "{normalized_project_name}", "__init__.py"])
__version__ = re.match(r".*^__version__\\s*=\\s*['\\"](.*?)['\\"]\\s*$.*", project_init, re.S | re.M).group(1)
__project__ = re.match(r".*^__project__\\s*=\\s*['\\"](.*?)['\\"]\\s*$.*", project_init, re.S | re.M).group(1)
setup(
name=__project__,
version=__version__,
author="{author}",
author_email="{author_email}",
packages=find_packages("src"),
package_dir={{"": "src"}},
{application_export_block}
include_package_data=True,
# MANIFEST.in: only used in source distribution packaging.
# ``package_data``: only used in binary distribution packaging.
package_data={{
"": [
"*.txt",
"*.md",
"*.rst",
],
"{normalized_project_name}": [
# For files in this package's direct namespace
# (e.g., "src/{{normalized_project_name}}/*.json")
# "*.json",
# For files in a (non-subpackage) subdirectory direct namespace
# (e.g., "src/{{normalized_project_name}}/resources/config/*.json")
# "resources/config/*.json",
# For files located in 'src/{normalized_project_name}-data/'
# "../{normalized_project_name}-data/*.json",
# For files located in 'resources'/'
# "../../resources/*.json",
],
}},
test_suite = "{project_test_subdir}",
# url="http://pypi.python.org/pypi/{normalized_project_name}",
url="https://github.com/jeetsukumaran/{normalized_project_name}",
license="LICENSE",
description="A Project",
long_description=_read(["README.md"]),
long_description_content_type="text/markdown",
# long_description_content_type="text/x-rst",
install_requires=_read_requirements("requirements.txt"),
extras_require={{"test": _read_requirements("requirements-test.txt")}},
)
"""
templates["requirements"] = """\
# [Requirements syntax](https://pip.pypa.io/en/stable/cli/pip_install/):
# python-package-name==3.14.2
# yakherd @ git+https://github.com/jeetsukumaran/yakherd@main
# dendropy @ git+https://github.com/jeetsukumaran/dendropy@development-main
"""
templates["__init__"] = """\
__project__ = "{{project_name}}"
# __version__ = "{dot_version}"
__version__ = "{datetime_version}"
""".format(
dot_version="0.1.0",
datetime_version="{}.{:02d}.{:02d}".format(project_creation_timestamp.year, project_creation_timestamp.month, project_creation_timestamp.day)
)
templates["test_path_module"] = """\
import os
TESTS_DIR = os.path.dirname(__file__)
TESTS_DATA_DIR = os.path.join(TESTS_DIR, "data")
"""
templates["test_module"] = """\
import unittest
if __name__ == "__main__":
import _pathmap
else:
from . import _pathmap
class TestCase(unittest.TestCase):
pass
if __name__ == "__main__":
unittest.main()
"""
templates["application_script"] = """\
import os
import pathlib
import sys
import argparse
def main():
parser = argparse.ArgumentParser(description=None)
parser.add_argument(
"src_paths",
action="store",
nargs="+",
metavar="FILE",
help="Path to source file(s).")
parser.add_argument(
"-o", "--output-prefix",
action="store",
default="output",
help="Prefix for output files [default=%(default)s].")
args = parser.parse_args()
print("Hello, world.")
if __name__ == '__main__':
main()
"""
# }}}1
class Project(object):
@staticmethod
def compose_normalized_project_name(name):
return name.lower()
def __init__(self,
project_name,
parent_dir,
application_package_model,
):
self.project_name = project_name
self.parent_dir = os.path.abspath(os.path.expandvars(os.path.expanduser(parent_dir)))
self.project_dir = os.path.join(self.parent_dir, self.project_name)
self.normalized_project_name = Project.compose_normalized_project_name(self.project_name)
self.normalized_project_cli_name = self.normalized_project_name.replace("_", "-")
self.application_package_model = application_package_model
self.project_source_subdir = os.path.join("src", self.normalized_project_name)
self.project_test_parent_dir = ""
self.project_test_subdir = "tests"
self.project_doc_subdir = "docs"
self.project_bin_subdir = "bin"
self.project_application_subpackage_subdir = os.path.join(self.project_source_subdir, "application")
self.requirements_filename = "requirements.txt"
self.tests_requirements_filename = "requirements-test.txt"
self.tests_pathmap_filename = "_pathmap.py"
self.test_module_stub_filename = "test_{}.py".format(self.normalized_project_name)
self.application_module_stub_filename = "{}.py".format(self.normalized_project_name)
def ensure_directory(self, path=None):
if path is None:
path = self.project_dir
if os.path.exists(path):
return
try:
os.makedirs(path)
except OSError:
sys.exit(1)
def ensure_project_subdirectory(self, subdir):
path = os.path.join(self.project_dir, subdir)
self.ensure_directory(path)
def create_gitignores(self):
fn = ".gitignore"
fp = os.path.join(self.project_dir, fn)
f = open(fp, "a")
f.write(templates["gitignore"])
f.close()
def create_license(self):
fn = "LICENSE"
fp = os.path.join(self.project_dir, fn)
f = open(fp, "w")
f.write(project_license_brief)
f.close()
def create_meta_files(self):
for fn in ["CHANGES.md", "README.md"]:
fp = os.path.join(self.project_dir, fn)
f = open(fp, "w")
f.write("")
f.close()
def create_manifest(self):
fn = "MANIFEST.in"
fp = os.path.join(self.project_dir, fn)
f = open(fp, "w")
f.write(templates["manifest"])
f.close()
def project_test_subdir_path(self):
return os.path.join(self.project_test_parent_dir, self.project_test_subdir) if self.project_test_parent_dir else self.project_test_subdir
def create_setup(self):
fn = "setup.py"
fp = os.path.join(self.project_dir, fn)
f = open(fp, "w")
if self.application_package_model == "console_scripts":
application_export_block_template = templates["entry_points"]
elif self.application_package_model == "bin":
application_export_block_template = templates["bin_scripts"]
elif self.application_package_model is None or self.application_package_model == "none":
application_export_block_template = ""
application_export_block = application_export_block_template.format(
normalized_project_name=self.normalized_project_name,
normalized_project_cli_name=self.normalized_project_cli_name,
)
f.write(templates["setup.py"].format(
project_name=self.project_name,
normalized_project_name=self.normalized_project_name,
normalized_project_cli_name=self.normalized_project_cli_name,
application_export_block=application_export_block,
author=project_author,
author_email=project_author_email,
project_test_parent_dir=self.project_test_parent_dir + "." if self.project_test_parent_dir else "",
project_test_subdir=self.project_test_subdir))
f.close()
def create_module(self, path, body):
with open(path, "w") as f:
f.write("#! /usr/bin/env python\n# -*- coding: utf-8 -*-\n\n")
f.write("#" * 78)
f.write("\n")
for line in project_license_brief.split("\n"):
f.write("## {}\n".format(line))
f.write("#" * 78)
if body:
f.write("\n\n")
if isinstance(body, str):
f.write(body)
if body[-1] != "\n":
f.write("\n")
else:
f.write("\n".join(body))
f.write("\n")
def create_python_file(self, filename, subdir, body=None):
fp = os.path.join(self.project_dir, subdir, filename)
self.create_module(fp, body=body)
def create_file(self, filename, subdir, body=None):
fp = os.path.join(self.project_dir, subdir, filename)
with open(fp, "w") as dest:
dest.write(body)
def create_init(self, subdir, body=None):
self.create_python_file(
filename="__init__.py",
subdir=subdir,
body=templates["__init__"].format(project_name=self.normalized_project_name))
def build(self):
self.ensure_directory()
self.create_gitignores()
self.create_license()
self.create_meta_files()
self.create_manifest()
self.create_setup()
self.ensure_project_subdirectory(self.project_source_subdir)
self.create_init(self.project_source_subdir, body=templates["__init__"])
self.ensure_project_subdirectory(self.project_test_subdir_path())
self.create_init(self.project_test_subdir_path(), body=None)
self.create_python_file(
self.tests_pathmap_filename,
self.project_test_subdir_path(),
body=templates["test_path_module"])
self.create_python_file(
self.test_module_stub_filename,
self.project_test_subdir_path(),
body=templates["test_module"])
self.ensure_project_subdirectory(self.project_doc_subdir)
self.create_file(self.requirements_filename, self.project_dir, body=templates["requirements"])
self.create_file(self.tests_requirements_filename, self.project_dir, body="")
if self.application_package_model == "console_scripts":
self.ensure_project_subdirectory(self.project_application_subpackage_subdir)
self.create_init(self.project_application_subpackage_subdir)
self.create_python_file(
self.application_module_stub_filename,
self.project_application_subpackage_subdir,
body=templates["application_script"])
elif self.application_package_model == "bin":
self.ensure_project_subdirectory(self.project_bin_subdir)
def main():
"""
Main CLI handler.
"""
parser = argparse.ArgumentParser(description=__description__)
parser.add_argument("--version", action="version", version="%(prog)s " + __version__)
parser.add_argument("project_name",
metavar="PROJECT_NAME",
type=str,
help="name of project")
parser.add_argument("-d", "--destination-directory",
dest="dest_dir",
type=str,
default=".",
help="parent directory in which to host project (default=current directory)")
parser.add_argument("--bin-scripts",
action="store_true",
help="Use 'bin/' subdirectory in project root to hold application scripts."
" Otherwise application scripts will be managed in 'src/<project-name>/application'"
" and made available through the 'console_scripts' mechanism."
)
args = parser.parse_args()
if args.bin_scripts:
application_package_model = "bin"
else:
application_package_model = "console_scripts"
project = Project(
project_name=args.project_name,
parent_dir=args.dest_dir,
application_package_model=application_package_model,
)
project.build()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment