Skip to content

Commit

Permalink
complete sbs first version
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhyc committed Aug 1, 2021
1 parent b1faf81 commit 34e9f3a
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
__pycache__
build
build
output
153 changes: 135 additions & 18 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env python3
import os, sys, time, logging, argparse
import os, sys, time, shutil, configparser, argparse, subprocess as sp
from pathlib import Path
import halo, termcolor
import json

DBG = 1
OUTPUT_DIRECTORY = os.getenv('VDM_CAPABILITY_OUTPUT_DIRECTORY', './output')
INSTALL_DIRECTORY = os.getenv('VDM_CAPABILITY_INSTALL_DIRECTORY', '~/.vdm/capability')
POSIX = lambda x: x.as_posix()

class TypeWriter:
def __init__(self):
Expand Down Expand Up @@ -71,18 +72,87 @@ def __init__(self):
self.install_dir = Path(INSTALL_DIRECTORY).expanduser().resolve()
pass

def _install_cargo(self):
def __install_cargo(self, logger=None):
pass

def _install_pip(self):
def __install_pip(self, logger=None):
pass

def _install_conan(self):
def __install_conan(self, logger=None):
pass

def _load_manifest(self):
manifest = json.load( Path('./manifest.json').as_posix() )
self.name = manifest['name']
def _check_dependency(self, dep_map:dict, logger=None):
for cmd,args in dep_map.items():
if cmd=='cargo':
self.__install_cargo(logger)
for arg in args:
logger.text = self._title%'cargo install %s'%arg
sp.check_call(['bash', '-c', 'cargo install "%s"'%arg],
stdout=sp.PIPE, stderr=sp.PIPE)
# logger.succeed()
elif cmd=='pip':
self.__install_pip(logger)
for arg in args:
logger.text = self._title%'pip3 install %s'%arg
sp.check_call(['bash', '-c', 'pip3 install "%s"'%arg],
stdout=sp.PIPE, stderr=sp.PIPE)
# logger.succeed()
elif cmd=='conan':
self.__install_conan(logger)
raise Exception('Conan is not supported now')
pass

def _copy_files(self, src_dir:Path, dst_dir:Path, logger=None):
for src_file,dst_file in self.output:
_dst_path = dst_dir/(dst_file if dst_file else src_file)
_dst_path.parent.mkdir(parents=True, exist_ok=True)
src_path = src_dir / src_file
dst_path = _dst_path if dst_file else _dst_path.parent
shutil.copy( POSIX(src_path.resolve()), POSIX(dst_path.resolve()) )
pass

def _exec_build(self, logger=None):
for cmd in self.build_script:
logger.text = self._title%'Building: %s'%cmd
sp.check_call(['bash', '-c', cmd], stdout=sp.PIPE, stderr=sp.PIPE)
pass
pass

def _write_config_file(self, manifest):
_output = [ x[1] if x[1] else x[0] for x in self.output ]
_runtime = manifest['runtime']
#
cfg = configparser.ConfigParser()
cfg['DEFAULT'] = {
'entry':Path(_output[0]).name, 'files': _output
}
#
cfg['runtime'] = {
'status':_runtime['status'], 'enable':_runtime['enable'], 'disable':_runtime['disable']
}
cfg['metadata'] = {
'name':manifest['name'], 'class':manifest['type'], 'version':manifest['version']
}
#
for name,meta in manifest['metadata'].items():
cfg['metadata.func.%s'%name] = {
'restype': meta['restype'], 'args': meta['args']
}
#
_cfg_path = self.output_dir/self.name; _cfg_path.mkdir(parents=True, exist_ok=True)
with open(_cfg_path/'.conf', 'w') as cfg_file:
cfg.write(cfg_file)
pass

def load_manifest(self):
with open(Path('./manifest.json')) as fd:
manifest = json.load( fd )
try:
self.name = manifest['name']
self.output = manifest['build']['output']
except Exception as e:
raise Exception('%s section missing in maifest file.'%e)
#
try:
self.build_dependency = manifest['build']['dependency']
except:
Expand All @@ -96,21 +166,67 @@ def _load_manifest(self):
except:
self.build_script = None
#
self.output = manifest['output']
pass
_output = [x.split('@') for x in self.output]
_output = [(x[0],None) if len(x)==1 else (x[0],x[1]) for x in _output]
self.output = _output
return manifest

def build(self, logger=None):
self._title = '[build] %s'
try:
self._load_manifest()
_title = '[build] %s'
self.load_manifest()
self._title = '[%s] %s'%(self.name, '%s')
#
logger.text = self._title%'Check build dependency ...'
self._check_dependency(self.build_dependency, logger)
#
logger.text = self._title%'Building ...'
self._exec_build(logger)
#
logger.text = self._title%'Release output files ...'
self._copy_files(Path('.'), self.output_dir)
#
logger.text = self._title%'build pass.'
except Exception as e:
raise Exception(e)
msg = self._title%'build failed. ' + str(e)
raise Exception(msg)
pass

def install(self, logger=None):
self._title = '[install] %s'
try:
manifest = self.load_manifest()
self._title = '[%s] %s'%(self.name, '%s')
#
logger.text = self._title%'Check runtime dependency ...'
self._check_dependency(self.runtime_dependency, logger)
#
logger.text = self._title%'Generate .conf file ...'
self._write_config_file(manifest)
#
logger.text = self._title%'Installing ...'
_output = [x[1] if x[1] else x[0] for x in self.output]
_output.append( POSIX(Path(self.name)/'.conf') )
_output = [(x,None) for x in _output]
self.output = _output
self._copy_files(self.output_dir, self.install_dir)
#
logger.text = self._title%'Installed.'
except Exception as e:
msg = self._title%'install failed. ' + str(e)
raise Exception(msg)
pass

def test(self, logger=None):
self._title = '[test] %s'
try:
self.load_manifest()
self._title = '[%s] %s'%(self.name, '%s')
#
logger.text = self._title%'test pass.'
except Exception as e:
msg = self._title%'test failed. ' + str(e)
raise Exception(msg)
pass

pass
Expand All @@ -121,11 +237,12 @@ def display_logo():
tw.write( list(' for VDM Capability Library'), color='cyan' )
pass

def validate_work_dirs(work_dirs):
def validate_work_dirs(work_dirs:list):
examiner = lambda _path: _path.is_dir() and (_path/'manifest.json').exists()
if type(work_dirs) is not list:
if len(work_dirs)==0:
work_dirs = list( filter(examiner, Path('./').glob('*')) )
else:
work_dirs = [Path(_dir) for _dir in work_dirs]
work_dirs = list( filter(examiner, work_dirs) )
return work_dirs

Expand All @@ -136,21 +253,21 @@ def apply(executor, work_dirs):
try:
executor(logger)
except Exception as e:
logger.fail(test=str(e))
logger.fail(text=str(e))
else:
logger.succeed()
pass
pass

def execute(sbs:SimpleBuildSystem, command:str, args):
assert( isinstance(sbs, SimpleBuildSystem) )
work_dirs = getattr(args, 'names', None)
work_dirs = getattr(args, 'names', [])
work_dirs = validate_work_dirs(work_dirs)

if len(work_dirs)==0:
return
if not args.no_logo:
display_logo()
# if not args.no_logo:
# display_logo()

if command=='install':
apply(sbs.install, work_dirs)
Expand Down
1 change: 1 addition & 0 deletions inotify-lookup/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
release
Cargo.lock
11 changes: 6 additions & 5 deletions inotify-lookup/manifest.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "inotify-lookup",
"type": "python",
"version": "0.1.0",

"build": {
"dependency": {
"cargo": [],
"pip": []
"pip": ["maturin"]
},
"script": [
"mkdir -p release",
"cd release; cmake ..; make"
],
"output": [
"./target/release/libinotify_lookup.so",
"./release/inotify-hook/inotify_hook.ko"
"./target/release/libinotify_lookup.so@libinotify_lookup.so",
"./release/inotify-hook/inotify_hook.ko@kmod/inotify_hook.ko"
]
},

Expand All @@ -22,10 +23,10 @@
"status":
"lsmod | grep inotify_hook",
"enable": [
"pkexec insmod inotify_hook"
"pkexec insmod kmod/inotify_hook"
],
"disable": [
"pkexec rmmod inotify_hook"
"pkexec rmmod kmod/inotify_hook"
]
},

Expand Down

0 comments on commit 34e9f3a

Please sign in to comment.