Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoblon committed Mar 2, 2020
1 parent 063d43f commit e6c470a
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 34 deletions.
5 changes: 2 additions & 3 deletions rudder-lang/Makefile
Expand Up @@ -7,10 +7,9 @@ cargo:

deps:
apt update && apt upgrade -y
apt install -y python3-pip perl
apt install -y perl
echo | cpan
perl -MCPAN -e 'install TOML::Parser'
pip3 install toml
perl -MCPAN -e 'install Config::IniFiles'

build: deps
perl tools/generate-lib
Expand Down
3 changes: 1 addition & 2 deletions rudder-lang/README.adoc
Expand Up @@ -61,7 +61,6 @@ The compiler is very pedantic to avoid defining invalid states as much as possib

=== Required modules
- perl (script: *tools/generate-lib*)

cpan Config::IniFiles
=== Configuration
- *rudderc.conf* to define the cfengine and ncf binaries paths, and the compilation, translation paths
(will require to install the `toml` library: `pip3 install toml`)
19 changes: 10 additions & 9 deletions rudder-lang/libs/stdlib.rl
@@ -1,21 +1,21 @@
@format=0

resource permissions(p0)
resource group(p0)
resource environment(p0)
resource condition(p0)
resource kernel_module(p0)
resource environment(p0)
resource directory(p0)
resource sharedfile(p0)
resource group(p0)
resource file(p0)
resource command(p0)
resource schedule(p0)
resource user(p0)
resource permissions(p0)
resource http(p0)
resource service(p0)
resource monitoring(p0)
resource variable(p0,p1)
resource kernel_module(p0)
resource package(p0)
resource http(p0)
resource schedule(p0)
resource directory(p0)
resource service(p0)
resource file(p0)

command state execution(){}
command state execution_once(p1,p2,p3){}
Expand Down Expand Up @@ -184,3 +184,4 @@ variable state string_from_command(p2){}
variable state string_from_file(p2){}
variable state string_from_math_expression(p2,p3){}
variable state string_match(){}

1 change: 1 addition & 0 deletions rudder-lang/src/bin/main.rs
Expand Up @@ -112,6 +112,7 @@ fn main() {
input,
output
) = file_paths::get(exec_action, &config, &opt.base, &opt.input, &opt.output).unwrap();

let result;
if is_compile_default {
result = compile_file(&input, &output, is_compile_default, &libs_dir);
Expand Down
5 changes: 1 addition & 4 deletions rudder-lang/src/compile.rs
Expand Up @@ -63,14 +63,11 @@ pub fn compile_file(source: &Path, dest: &Path, technique: bool, libs_dir: &Path
// read and add files
let oses = libs_dir.join("oslib.rl");
let corelib = libs_dir.join("corelib.rl");
let cfenginecore = libs_dir.join("cfengine_core.rl");
// let cfenginecore = libs_dir.join("cfengine_core.rl");
let stdlib = libs_dir.join("stdlib.rl");
let input_filename = source.to_string_lossy();
let output_filename = dest.to_string_lossy();

println!("CORELIB {:?}", corelib);
println!("RUDDERCLIBS: {:?}", libs_dir);

info!(
"{} of {} into {}",
"Processing compilation".bright_green(),
Expand Down
2 changes: 1 addition & 1 deletion rudder-lang/src/file_paths.rs
Expand Up @@ -62,7 +62,7 @@ pub fn get(exec_action: &str, default_paths: &PathBuf, opt_base: &Option<PathBuf
let err_gen = |e: &str| Err(Error::User(format!("{}", e)));

let config: toml::Value = match std::fs::read_to_string(default_paths) {
Err(_) => return err_gen("Could not read toml config file"),
Err(e) => return err_gen(&format!("Could not read toml config file: {}", e)),
Ok(config_data) => match toml::from_str(&config_data) {
Ok(config) => config,
Err(_) => return err_gen("Could not parse (probably faulty) toml config file"),
Expand Down
21 changes: 15 additions & 6 deletions rudder-lang/tests/helpers/cfjson_tester
Expand Up @@ -4,20 +4,29 @@
Ncf / rudder-lang compatibility tester
Usage:
ncf ncf-to-json <file.cf> <new.json>
ncf compare-json <first.json> <new.json>
ncf compare-cf <first.cf> <new.cf>
ncf ncf-to-json <file.cf> <new.json> [<local_config_path.conf>]
ncf compare-json <first.json> <new.json> [<local_config_path.conf>]
ncf compare-cf <first.cf> <new.cf> [<local_config_path.conf>]
"""

import configparser
import sys
import os
import configparser

cfgpath = '/tools/rudderc.conf'
if len(sys.argv) >= 5:
cfgpath = sys.argv[4]
cfg_full_path = os.getcwd()+cfgpath
if not os.path.exists(cfg_full_path):
print("No such file: " + cfg_full_path)
exit(1)

cfg = configparser.ConfigParser()
cfg.read(os.getcwd()+'/tools/rudderc.conf')
cfg.read(os.getcwd()+cfgpath)
ncf_dir = cfg['default_paths']['ncf'].strip('\"')
cfengine_path = cfg['default_paths']['cfengine'].strip('\"')

import sys
sys.path.append(ncf_dir+'/tools')
import ncf
import codecs
Expand Down
8 changes: 4 additions & 4 deletions rudder-lang/tests/tester.sh
Expand Up @@ -8,7 +8,7 @@ cfjson_tester=$PWD/tests/helpers/cfjson_tester
mkdir -p $dir/target/

# Take original technique an make a json
$cfjson_tester ncf-to-json $dir/cf/${name}.cf $dir/json/${name}.json
$cfjson_tester ncf-to-json $dir/cf/${name}.cf $dir/json/${name}.json /tools/rudderc-dev.conf

# Take json and produce a rudder-lang technique
cargo run -- --translate -i $dir/json/${name}.json -o $dir/rl/${name}.json.rl
Expand All @@ -18,11 +18,11 @@ cargo run -- --translate -i $dir/json/${name}.json -o $dir/rl/${name}.json.rl
cargo run -- --compile -i $dir/rl/${name}.rl -o $dir/target/${name}.rl.cf

# take generated cf file a new json
$cfjson_tester ncf-to-json $dir/target/${name}.rl.cf $dir/target/${name}.rl.cf.json
$cfjson_tester ncf-to-json $dir/target/${name}.rl.cf $dir/target/${name}.rl.cf.json /tools/rudderc-dev.conf

# TODO compare generated json
$cfjson_tester compare-json $dir/json/${name}.json $dir/target/${name}.rl.cf.json
$cfjson_tester compare-json $dir/json/${name}.json $dir/target/${name}.rl.cf.json /tools/rudderc-dev.conf

# TODO compare generated cf files
$cfjson_tester compare-cf $dir/cf/${name}.cf $dir/target/${name}.rl.cf
$cfjson_tester compare-cf $dir/cf/${name}.cf $dir/target/${name}.rl.cf /tools/rudderc-dev.conf
# diff --width=210 --suppress-common-lines --side-by-side ${dir}/cf/${name}.cf ${dir}/target/${name}.rl.cf
14 changes: 9 additions & 5 deletions rudder-lang/tools/generate-lib
Expand Up @@ -4,13 +4,18 @@ use warnings;
use strict;
use warnings;
use strict;
use Config::IniFiles;
# it actually is toml format
use Config::IniFiles;

my %resource = ();
my @state = ();
my @config_lines=();
my @config_lines= ();

my $cfg = Config::IniFiles->new( -file => "./tools/rudderc.conf" );
my $cfgpath = "./tools/rudderc.conf";
if (@ARGV) {
($cfgpath) = @ARGV;
}
my $cfg = Config::IniFiles->new( -file => $cfgpath );
my $ncf_methods = $cfg->val( 'default_paths', 'ncf' ) . "/tree/30_generic_methods/*";
foreach my $file (glob $ncf_methods) {
# file exclusion
Expand Down Expand Up @@ -69,7 +74,6 @@ foreach my $file (glob $ncf_methods) {
$resource{"resource $resource(".join(",",@resource_params).")"}=1;
push @state, "$resource state $state(".join(",",@state_params)."){}";
push @config_lines, "$gm = { class_prefix=\"$prefix\", class_parameter_id = $class_parameter }";
#print "$gm = { class_prefix=\"$prefix\", class_parameter_id = $class_parameter }\n";
}

open(my $stdlib,">./libs/stdlib.rl") or die "Cannot write stdlib";
Expand All @@ -81,7 +85,7 @@ print $stdlib join("\n", @state);
print $stdlib "\n\n";
close($stdlib);

my $config_header = "COUCOU[resources]
my $config_header = "[resources]
variable = { parameter_count=2 }
[classes]
Expand Down

0 comments on commit e6c470a

Please sign in to comment.