Skip to content

Commit

Permalink
Add setup script
Browse files Browse the repository at this point in the history
  • Loading branch information
alexesprit committed May 19, 2020
1 parent 06c913f commit 1bab3ea
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 59 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.idea
*.iml
*.pyc
/build/
/dist/

*.egg-info

*.egg
*.pyc
2 changes: 0 additions & 2 deletions alias.bat

This file was deleted.

31 changes: 31 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[bdist_wheel]
universal = 1

[metadata]
name = alias-windows
version = attr: alias.__version__
license = MIT
license_file = LICENSE.md
description = Manage command aliases in Windows
long_description = file: README.md
long_description_content_type = text/markdown
author = alexesprit
url = https://github.com/alexesprit/alias
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Operating System :: Microsoft :: Windows

[options]
package_dir=
=src
packages = find:

python_requires = >=3.6

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
alias = alias.alias:main
32 changes: 32 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import sys

from setuptools import setup
from setuptools.command.install import install

sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))


from alias import ALIASES_DIR_VAR # noqa: E402
from alias import get_aliases_dir # noqa: E402


def set_env_var(var, value):
os.system(f'setx {var} "{value}"')


class PostInstallCommand(install):
"""Post-installation for installation mode."""
def run(self):
install.run(self)

if ALIASES_DIR_VAR not in os.environ:
aliases_dir = get_aliases_dir()
set_env_var(ALIASES_DIR_VAR, aliases_dir)


setup(
cmdclass={
'install': PostInstallCommand,
}
)
13 changes: 13 additions & 0 deletions src/alias/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os

__version__ = '1.0.0'

ALIASES_DIR_VAR = 'ALIASES_DIR'
ALIASES_DIR_MACRO = '%USERPROFILE%\\Documents\\Scripts\\Aliases'


def get_aliases_dir():
aliases_dir = os.getenv(ALIASES_DIR_VAR)
if not aliases_dir:
aliases_dir = os.path.expandvars(ALIASES_DIR_MACRO)
return aliases_dir
57 changes: 3 additions & 54 deletions alias.py → src/alias/alias.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import glob
import os
import re
import sys

from argparse import ArgumentParser, RawTextHelpFormatter

prog = 'alias'
version = '1.0.0'
from alias import __version__, get_aliases_dir

ALIASES_DIR_MACRO = '%USERPROFILE%\\Documents\\Scripts\\Aliases'
ALIASES_DIR_VAR = 'ALIASES_DIR'
prog = 'alias'

param_pattern = r'\%[0-9\*]'

Expand All @@ -31,45 +28,6 @@
'''


def is_alias_installed():
return ALIASES_DIR_VAR in os.environ


def is_install_allowed():
answer = 'N'
while True:
answer = input(SETUP_PROMPT)
answer = answer if answer else 'N'
if answer in 'YyNn':
break
return answer in 'Yy'


def set_env_var(var, value):
os.system(f'setx {var} "{value}"')


def install_alias():
aliases_dir = get_aliases_dir()
set_env_var(ALIASES_DIR_VAR, aliases_dir)

path = os.environ['PATH']
if aliases_dir not in path:
path = f'{path};{aliases_dir}'
set_env_var('PATH', path)

if not is_alias_exists('alias'):
command = f'{sys.argv[0]} %*'
add_alias('alias', command)


def get_aliases_dir():
aliases_dir = os.getenv(ALIASES_DIR_VAR)
if not aliases_dir:
aliases_dir = os.path.expandvars(ALIASES_DIR_MACRO)
return aliases_dir


def get_alias_path(alias):
aliases_dir = get_aliases_dir()
return os.path.join(aliases_dir, f'{alias}{ALIAS_EXTENSION}')
Expand Down Expand Up @@ -192,7 +150,7 @@ def create_arg_parser():
arg_parser.add_argument('-v', '--verbose', action='store_true',
help='Verbosed output')
arg_parser.add_argument('--version', action='version',
version=f'{prog} v{version}')
version=f'{prog} v{__version__}')
return arg_parser


Expand Down Expand Up @@ -223,15 +181,6 @@ def parse_args(arg_parser):


def main():
if not is_alias_installed():
if not is_install_allowed():
return 1
install_alias()

arg_parser = create_arg_parser()
parse_args(arg_parser)
return 0


if __name__ == '__main__':
sys.exit(main())

0 comments on commit 1bab3ea

Please sign in to comment.