Skip to content

Commit

Permalink
Accept user log path and enable debug option. Validate python version…
Browse files Browse the repository at this point in the history
… and proceed for valid one only
  • Loading branch information
Manish Kumar committed Feb 10, 2020
1 parent 66da6a8 commit d723082
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 21 deletions.
12 changes: 11 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ Changes log
0.0.1
-----

- Package creation with ``bobtemplates.gillux``
- Package Initialized with below feature
1. Get passed python version supported package
2. Log the progress report success/failure into the file

[Manish]

0.0.2
----
- Package modification with below feature
1. Accept custom log path to log the message
2. Accept user input to enable/disable debug message

[Manish]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ This is the master package for Py2pip using Python3.

It has the following functions:
1. Required to pass python version and 3rd party package name
2. Log will work only executed as a standalone script
2. Log will work only if executed as a standalone script.
3. Log path dir: ~/.local/py2pip/


Installation
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ requires=
aiohttp >= 3.6.2
beautifulsoup4 >= 4.8.2

post-install=scripts/postinstall.sh
35 changes: 24 additions & 11 deletions src/py2pip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,52 @@

import argparse
import logging
import pathlib
import time
import sys

from logging.handlers import RotatingFileHandler

import py2pip.config
import py2pip.utils
from py2pip.process import Py2PIPManager


MAX_FILE_SIZE = 2 * 1024 * 1024 # 2 MiB
MAX_BACKUP_FILE = 5


def get_logger():
def get_logger(log_file, enable_debug):
"""
Creates a rotating log
"""
# Create parent dir if not exists already.
if not log_file.parent.exists():
log_file.parent.mkdir(parents=True)

formatter = logging.Formatter('%(asctime)s.%(msecs)03d %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

logger = logging.getLogger("Py2pip handler")
logger.setLevel(logging.DEBUG if config.DEBUG else logging.INFO)
logger = logging.getLogger("Py2pip process handler")
logger.setLevel(logging.DEBUG if enable_debug else logging.INFO)

# add a rotating handler
handler = RotatingFileHandler(str(config.PATH_LOG_FILE), maxBytes=MAX_FILE_SIZE, backupCount=MAX_BACKUP_FILE, encoding='UTF-8')
handler = RotatingFileHandler(str(log_file), maxBytes=MAX_FILE_SIZE, backupCount=MAX_BACKUP_FILE, encoding='UTF-8')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger

log = get_logger()

def options():
parser = argparse.ArgumentParser('Verify-And-Install-Package')
parser.add_argument('-i', '--install', action='store_true',
help="Pass if you want to install package of supported Python version")
parser.add_argument('-v', '--version', dest='support', type=str, required=True,
help="Find Support Python Version i.e 2.7, 2.6, 3.1, 3.5 etc.")
parser.add_argument('--py-version', dest='python_support', type=str, required=True,
help="Find Support Python Version i.e 2.6, 2.7, 3.1, 3.5 etc.")
parser.add_argument('-p', '--package', type=str, help="Python Package Name registered to PIP", required=True)
parser.add_argument('-d', '--debug', dest="enable_debug", action="store_true", default=py2pip.config.DEBUG,
help="Enable Debug")
parser.add_argument('-l', '--log-file', dest='log_file_name', type=str, default=str(py2pip.config.PATH_LOG_FILE),
help="Log file path. Set default to app level")

args = parser.parse_args()
return args
Expand All @@ -47,17 +56,21 @@ def options():
def main():
try:
option = options()
if not option.support:
return
py2pip_manager = Py2PIPManager(option.package, option.support, option.install, log)
if not py2pip.utils.validPySupportVersion(option.python_support):
raise ValueError(f"Must pass support python version '--py-version'.")

log_file_path = pathlib.Path(option.log_file_name)
log = get_logger(log_file_path, option.enable_debug)

py2pip_manager = Py2PIPManager(option.package, option.python_support, option.install, log)

stime = time.time()
result = py2pip_manager.run()
etime = time.time()
print(f'Support Version: {result}\nExecution time: {etime - stime}')

except ValueError as e:
log.exception(f'Error: {str(e)}')
print(f'Error: {str(e)}')

if __name__ == '__main__':
sys.exit(main())
9 changes: 6 additions & 3 deletions src/py2pip/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
ENABLE_SIMULATOR = True


BASE_DIR = pathlib.Path(__file__).parent.resolve()
BASE_DIR = pathlib.Path.home()
RELATIVE_PROJECT_DIR = '.local/py2pip'

PATH_STATS_FILE = BASE_DIR / 'stats.txt'
PATH_STATS_FILE = BASE_DIR / RELATIVE_PROJECT_DIR / 'stats.txt'

SUFFIX_LOG_FILE = 'py2pip_status.log'
PATH_LOG_FILE = BASE_DIR / SUFFIX_LOG_FILE
PATH_LOG_FILE = BASE_DIR / RELATIVE_PROJECT_DIR / SUFFIX_LOG_FILE


PATH_PIP_COMMAND = 'pip'


class LookupTags():
GRAND_PARENT = 'div'
Expand Down
6 changes: 3 additions & 3 deletions src/py2pip/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, log):
self.log = log

def strip(self, text):
return text.strip() # Removes leading trailing and leading spaces and \n
return text.strip() # Removes trailing and leading spaces and \n

async def get_soup(self, content):
return BeautifulSoup(content, features=self.FEATURE)
Expand All @@ -33,7 +33,7 @@ async def read_versions(self, node):

version = self.strip(version_node.text)

# Exlude pre-release version
# Exclude pre-release version
if re.search('[a-z]+', version):
return

Expand Down Expand Up @@ -108,4 +108,4 @@ async def process(self, content, py_version, pkg_version):
task = asyncio.ensure_future(self.read(li, py_version, pkg_version))
tasks.append(task)

await asyncio.gather(*tasks, return_exceptions=True)
await asyncio.gather(*tasks, return_exceptions=True)
6 changes: 4 additions & 2 deletions src/py2pip/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class Py2PIPManager(object):

def __init__(self, project_name, support_py_version, install=False, log=Logger()):
def __init__(self, project_name, support_py_version, install=False, log=Logger(), **kwargs):
"""
Find the python supported package version.
:param project_name: Python Package Name, 3rd party registered to pip
Expand All @@ -37,14 +37,16 @@ def __init__(self, project_name, support_py_version, install=False, log=Logger()
self.package = project_name
self.install = install
self.support_py_version = support_py_version
self.enable_event_debug = kwargs.get('enable_debug', False)

self.history_page_parser = ParseHistoryHTML(self.log)
self.history_versions_page_parser = ParsePackageVersionHTML(self.log)

def start(self):
self.log.info(f'Commence io loop...')
self.loop = asyncio.get_event_loop()
# self.loop.set_debug(enabled=True)
self.loop.set_debug(enabled=self.enable_event_debug)

self.loop.run_until_complete(self.process_package_history_page()) # close will have no effect if called with run_until_complete

def get_support_pkg_version(self):
Expand Down
4 changes: 4 additions & 0 deletions src/py2pip/scripts/postinstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/bash -e

# Ensure local dir set
mkdir -p ~/local/py2pip
17 changes: 17 additions & 0 deletions src/py2pip/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@



def validPySupportVersion(py_support_version):
"""
Validate if the py_support version is a valid one
:param py_support_version: should be x.y (x>=2)
:return: Boolean
"""
try:
py_version_split = py_support_version.split('.')

# Must be 2 or more
return int(py_version_split[0]) >= 2

except (AttributeError, ValueError) as exp:
return False

0 comments on commit d723082

Please sign in to comment.