Skip to content

Commit

Permalink
Merge pull request #14 from ericsonj/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ericsonj committed Mar 24, 2021
2 parents e07c2fb + 10746ef commit 7eaece2
Show file tree
Hide file tree
Showing 15 changed files with 435 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Ericson Joseph'

# The full version, including alpha/beta/rc tags
release = '2.0.1'
release = '2.0.2'

version = release

Expand Down
33 changes: 29 additions & 4 deletions docs/source/user/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,40 @@ Installation of pymaketool
This part of the documentation covers the installation of pymaketool.
The first step to using any software package is getting it properly installed.

Install
-------
Ubuntu/debian
-------------

To install pymaketool (only support for python 3), simply run this simple command in your terminal of choice.
.. code-block:: bash
$ sudo apt install -y python3 python3-pip python3-gi python3-gi-cairo gir1.2-gtk-3.0 git time zip
$ pip3 install pymaketool
Fedora
------

.. code-block:: bash
$ sudo dnf install python3-gobject gtk3
$ sudo dnf install python3-pip
$ pip3 install pymaketool
Arch Linux
----------

.. code-block:: bash
$ pip install pymaketool
$ sudo pacman -S python-gobject gtk3
$ sudo pacman -S python-pip
$ pip install pymaketool
macOS
-----

.. code-block:: bash
$ brew install pygobject3 gtk+3
$ brew install python3
$ pip3 install pymaketool
Get the Source Code
-------------------
Expand Down
149 changes: 149 additions & 0 deletions pymakelib/nproject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
from os import execle
import tarfile
import tempfile
import re
from pathlib import Path
from abc import ABC,abstractmethod
from prompt_toolkit import prompt
from prompt_toolkit.completion import PathCompleter, WordCompleter
from prompt_toolkit.validation import Validator
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.keys import Keys
from . import Logger

log = Logger.getLogger()

class Object(object):
pass

class PromptUtil:

def __init__(self) -> None:
self.bindings = KeyBindings()

@self.bindings.add(Keys.ControlC)
def _(event):
exit(0)

def add_answers(self, answers: dict):
self.answers = answers

def parse_answers(self):
obj = Object()
for a in self.answers:
type = a['type']
name = a['name']
self.answer = a
res = getattr(self, type)()
setattr(obj, name, res)
return obj


def __get_commond_attr(self):
msg = self.answer['msg']
default = '' if not "default" in self.answer else self.answer["default"]
validator_dict = None if not "validator" in self.answer else self.answer["validator"]
validator = None
if validator_dict:
validator = Validator.from_callable(
validator_dict['callback'],
error_message=validator_dict['error_msg'],
move_cursor_to_end=True)
return msg, default, validator


def input(self):
msg, default, validator = self.__get_commond_attr()
return prompt(msg, default=default, validator=validator, key_bindings=self.bindings)


def input_path(self):
msg, default, validator = self.__get_commond_attr()
path_completer = PathCompleter(only_directories=True, min_input_len=1)
return prompt(msg, completer=path_completer, default=default, validator=validator, key_bindings=self.bindings)


def confirm(self):
msg, default, validator = self.__get_commond_attr()
word_completer = WordCompleter(["yes", "no"])
resp = prompt(msg, completer=word_completer, default=default, validator=validator, key_bindings=self.bindings)
return True if resp == 'yes' or resp == 'y' else False



class AbstractGenerator(ABC):

@abstractmethod
def info(self) -> dict:
pass

@abstractmethod
def exec_generator(self, **kwargs):
pass


class BasicGenerator(AbstractGenerator):

@abstractmethod
def get_attrs(self, **kwargs) -> dict:
pass


def parse_args(self, args:list) -> dict:
res = []
for sub in args:
if '=' in sub:
res.append(map(str.strip, sub.split('=', 1)))
else:
res.append((sub, None))
res = dict(res)
return res


def copyFile(self, input_file, output_file, tokens: dict):
p = Path(output_file.parent)
p.mkdir(parents=True, exist_ok=True)
input_file = open(input_file, 'r')
output_file = open(output_file, 'w')
for line in input_file:
for token, value in tokens.items():
line = re.sub("<%=[ ]*" + token + "[ ]*%>", value, line)
output_file.write(line)

def exec_generator(self, **kwargs):
try:
attrs = self.get_attrs(**kwargs)
self.temp_name = attrs['temp_name']
self.temp_gzip_file = attrs['temp_gzip_file']
self.temp_files = attrs['temp_files']
self.temp_tokens = attrs['temp_tokens']
self.output_folder = attrs['output_folder']

output_folder = Path(self.output_folder)
output_folder.mkdir()

gzipfile = Path(self.temp_gzip_file)
if not gzipfile.exists():
print(f"File {str(gzipfile)} not found")
exit(1)

tmpdir = tempfile.TemporaryDirectory()
gzipobj = tarfile.open(gzipfile)
gzipobj.extractall(tmpdir.name)
gzipobj.close()

dir_tmptemp = Path(Path(tmpdir.name) / Path(self.temp_name))

for tp in self.temp_files:
fin = Path(dir_tmptemp / Path(tp))
if fin.exists():
fout = Path( output_folder / Path(tp))
log.debug("{0} > {1}".format(str(fin), str(fout)))
if fin.is_dir():
fout.mkdir(parents=True, exist_ok=True)
else:
self.copyFile(fin, fout, self.temp_tokens)
except FileExistsError:
print(f"Folder {output_folder} already exist")
except Exception as ex:
log.exception(ex)
4 changes: 4 additions & 0 deletions pymakelib/pynewproject_cproject/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

generators = [
"pynewproject_clinuxgcc.CLinuxGCC"
]
68 changes: 68 additions & 0 deletions pymakelib/pynewproject_cproject/pynewproject_clinuxgcc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import datetime
import os
from pathlib import Path
from pymakelib import nproject
from pkg_resources import resource_filename
import argparse

class CLinuxGCC(nproject.BasicGenerator):
"""Generate basic C linux gcc project
"""
def info(self):
return {
"name": "C Project",
"desc": "C Linux GCC Project"
}

def print_help(self):
print("""
Basic C Linux GCC Project
Tokens:
author
project_name
Command: pynewproject CLinuxGCC author=\\\"Author\\\" project_name=project
""")

def get_temp_files(self):
return [
'app/inc/main.h',
'app/src/main.c',
'app/app_mk.py',
'makefile.mk',
'Makefile',
'Makefile.py',
".project",
".pymakeproj/.cproject_template",
".pymakeproj/.language.settings_template",
".settings",
]

def get_attrs(self, **kwargs) -> dict:

args = self.parse_args(kwargs['args'])

if 'help' in args:
self.print_help()
exit(0)

tokens = {
'author': 'Ericson Joseph',
'date': datetime.datetime.now().strftime("%b %d %Y"),
'project_name': 'c_temp'
}
tokens['author'] = input("(author) Your name: ") if not 'author' in args else args['author']
tokens['project_name'] = input("(project_name) Your project name: ") if not 'project_name' in args else args['project_name']

output_dir = Path( Path(os.getcwd()) / Path(tokens['project_name']))

gzip_file = resource_filename("pymakelib.resources.templates", "clinuxgcc.tar.gz")

return {
"temp_name": "clinuxgcc",
"temp_gzip_file": gzip_file,
"temp_files": self.get_temp_files(),
"temp_tokens": tokens,
"output_folder": output_dir,
}
16 changes: 0 additions & 16 deletions pymakelib/resources/gtk/pybagui.glade
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">pybuildanalyzer</property>
<property name="show_close_button">True</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkNotebook">
<property name="visible">True</property>
Expand Down
Empty file.
Binary file added pymakelib/resources/templates/clinuxgcc.tar.gz
Binary file not shown.
35 changes: 18 additions & 17 deletions pymakelib/toolchain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import subprocess
import tempfile

from pathlib import Path

def getGCCHeaderFiles(cmd_gcc):
gcc_includes = []
Expand Down Expand Up @@ -53,22 +53,23 @@ def confLinuxGCC(binLocation='', extIncludes=[]):


def confGCC(binLocation='', prefix='', extIncludes=[]):
cmd_gcc = binLocation + prefix + 'gcc'
cmd_gxx = binLocation + prefix + 'g++'
cmd_ld = binLocation + prefix + 'gcc'
cmd_ar = binLocation + prefix + 'ar'
cmd_as = binLocation + prefix + 'as'
cmd_objcopy = binLocation + prefix + 'objcopy'
cmd_size = binLocation + prefix + 'size'
cmd_objdump = binLocation + prefix + 'objdump'
cmd_nm = binLocation + prefix + 'nm'
cmd_ranlib = binLocation + prefix + 'ranlib'
cmd_strings = binLocation + prefix + 'strings'
cmd_strip = binLocation + prefix + 'strip'
cmd_cxxfilt = binLocation + prefix + 'c++filt'
cmd_addr2line = binLocation + prefix + 'addr2line'
cmd_readelf = binLocation + prefix + 'readelf'
cmd_elfedit = binLocation + prefix + 'elfedit'
binpath = Path(binLocation)
cmd_gcc = str(binpath / (prefix + 'gcc'))
cmd_gxx = str(binpath / (prefix + 'g++'))
cmd_ld = str(binpath / (prefix + 'gcc'))
cmd_ar = str(binpath / (prefix + 'ar'))
cmd_as = str(binpath / (prefix + 'as'))
cmd_objcopy = str(binpath / (prefix + 'objcopy'))
cmd_size = str(binpath / (prefix + 'size'))
cmd_objdump = str(binpath / (prefix + 'objdump'))
cmd_nm = str(binpath / (prefix + 'nm'))
cmd_ranlib = str(binpath / (prefix + 'ranlib'))
cmd_strings = str(binpath / (prefix + 'strings'))
cmd_strip = str(binpath / (prefix + 'strip'))
cmd_cxxfilt = str(binpath / (prefix + 'c++filt'))
cmd_addr2line = str(binpath / (prefix + 'addr2line'))
cmd_readelf = str(binpath / (prefix + 'readelf'))
cmd_elfedit = str(binpath / (prefix + 'elfedit'))

gcc_includes = getGCCHeaderFiles(cmd_gcc)
gcc_includes = gcc_includes + extIncludes
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@

prompt_toolkit
2 changes: 1 addition & 1 deletion scripts/pymaketool
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ log = Logger.getLogger()
parser = argparse.ArgumentParser()
parser.add_argument('goal', type=str, help='Makefile command goal', default=None, nargs='?')
parser.add_argument('--init', type=str, help='initialize project', const=os.path.basename(os.getcwd()), dest='project_name', nargs='?')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 2.0.1')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 2.0.2')
args = parser.parse_args()

goal = args.goal
Expand Down
Loading

0 comments on commit 7eaece2

Please sign in to comment.