Skip to content

Commit

Permalink
Release 0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
katyukha committed Mar 23, 2023
2 parents 5a039ed + 642b048 commit ba963f0
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 44 deletions.
3 changes: 3 additions & 0 deletions source/odood/package.d
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
module odood;

public import odood.cli;
public import odood.lib;
2 changes: 2 additions & 0 deletions subpackages/cli/source/odood/cli/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private import odood.cli.commands.venv: CommandVenv;
private import odood.cli.commands.discover: CommandDiscover;
private import odood.cli.commands.log: CommandLogView;
private import odood.cli.commands.script: CommandScript;
private import odood.cli.commands.psql: CommandPSQL;


/** Class that represents main OdoodProgram
Expand All @@ -48,6 +49,7 @@ class App: OdoodProgram {
this.add(new CommandDiscover());
this.add(new CommandLogView());
this.add(new CommandScript());
this.add(new CommandPSQL());

// shortcuts
this.add(new CommandServerStart());
Expand Down
13 changes: 13 additions & 0 deletions subpackages/cli/source/odood/cli/commands/database.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CommandDatabaseCreate: OdoodCommand {
this() {
super("create", "Create new odoo database.");
this.add(new Flag("d", "demo", "Load demo data for this db"));
this.add(new Flag(
null, "recreate", "Recreate database if it already exists."));
this.add(new Option(
"l", "lang",
"Language of database, specified as ISO code of language."
Expand All @@ -63,6 +65,17 @@ class CommandDatabaseCreate: OdoodCommand {

auto project = Project.loadProject;
string dbname = args.arg("name");
if (project.lodoo.databaseExists(dbname)) {
if (args.flag("recreate")) {
warningf(
"Dropting database %s before recreating it " ~
"(because --recreate option specified).", dbname);
project.lodoo.databaseDrop(dbname);
} else {
throw new OdoodException(
"Database %s already exists!".format(dbname));
}
}
project.lodoo.databaseCreate(
dbname,
args.flag("demo"),
Expand Down
54 changes: 54 additions & 0 deletions subpackages/cli/source/odood/cli/commands/psql.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module odood.cli.commands.psql;

private import std.logger;
private import std.format: format;
private import std.conv: to, ConvException;

private import commandr: Argument, Option, Flag, ProgramArgs;

private import odood.cli.core: OdoodCommand;
private import odood.lib.project: Project;


class CommandPSQL: OdoodCommand {
this() {
super("psql", "Run psql for specified database");
this.add(new Option(
"d", "db", "Name of database to connect to.").required);
}

public override void execute(ProgramArgs args) {
import std.process;
import std.algorithm;
import std.array;
auto project = Project.loadProject;

string[string] env = environment.toAA;

auto odoo_conf = project.getOdooConfig();

env["PGDATABASE"] = args.option("db");

if (odoo_conf["options"].hasKey("db_host"))
env["PGHOST"] = odoo_conf["options"].getKey("db_host");
if (odoo_conf["options"].hasKey("db_port")) {
auto db_port = odoo_conf["options"].getKey("db_port");
try {
env["PGPORT"] = db_port.to!(int).to!string;
} catch (ConvException) {
warningf("Unparsable value for db port: %s", db_port);
}
}

env["PGUSER"] = odoo_conf["options"].hasKey("db_user") ?
odoo_conf["options"].getKey("db_user") : "odoo";
env["PGPASSWORD"] = odoo_conf["options"].hasKey("db_password") ?
odoo_conf["options"].getKey("db_password") : "odoo";

execvpe(
"psql", ["psql"],
env.byKeyValue.map!(
(i) => "%s=%s".format(i.key, i.value)).array);
}
}

24 changes: 24 additions & 0 deletions subpackages/cli/source/odood/cli/commands/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ class CommandTest: OdoodCommand {
super("test", "Run tests for mudles.");
this.add(new Flag(
"t", "temp-db", "Create temporary database for tests."));
this.add(new Flag(
null, "no-drop-db",
"Do not drop temporary database after test completed."));
this.add(new Flag(
null, "isw", "Ignore warnings that are considered safe."));
this.add(new Flag(
null, "migration", "Run migration against stable branch."));
this.add(new Flag(
null, "coverage", "Calculate code coverage."));
this.add(new Flag(
Expand All @@ -64,6 +69,12 @@ class CommandTest: OdoodCommand {
this.add(new Option(
null, "dir-r",
"Directory to recursively search for addons to test").repeating);
this.add(new Option(
null, "migration-start-ref",
"git reference (branch/commit/tag) to start migration from"));
this.add(new Option(
null, "migration-repo",
"run migration tests for repo specified by path"));
this.add(new Argument(
"addon", "Name of addon to run tests for.").optional.repeating);
}
Expand Down Expand Up @@ -114,6 +125,19 @@ class CommandTest: OdoodCommand {

testRunner.setCoverage(coverage);

if (args.flag("migration"))
testRunner.enableMigrationTest();
if (!args.option("migration-repo").empty)
testRunner.setMigrationRepo(Path(args.option("migration-repo")));
if (!args.option("migration-start-ref").empty)
testRunner.setMigrationStartRef(args.option("migration-start-ref"));

if (testRunner.test_migration && !testRunner.migration_repo)
testRunner.setMigrationRepo(Path.current);

if (args.flag("no-drop-db"))
testRunner.setNoDropDatabase();

auto res = testRunner.run();

if (coverage)
Expand Down
4 changes: 2 additions & 2 deletions subpackages/lib/source/odood/lib/addons/manager.d
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ struct AddonManager {
gitClone(git_url, dest, branch);

// TODO: Do we need to create instance of repo here?
auto repo = AddonRepository(_project, dest);
auto repo = new AddonRepository(_project, dest);
link(repo.path, true);

// If there is odoo_requirements.txt file present, then we have to
Expand All @@ -391,6 +391,6 @@ struct AddonManager {
enforce!OdoodException(
path.join(".git").exists,
"Is not a git root directory.");
return AddonRepository(_project, path);
return new AddonRepository(_project, path);
}
}
36 changes: 34 additions & 2 deletions subpackages/lib/source/odood/lib/addons/repository.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ private import std.regex;
private import std.exception: enforce;
private import std.format: format;
private import std.logger;
private static import std.process;

private import thepath: Path;

Expand All @@ -14,11 +15,11 @@ private import odood.lib.git: parseGitURL, gitClone;


// TODO: Do we need this struct?
struct AddonRepository {
class AddonRepository {
private const Project _project;
private const Path _path;

@disable this();
//@disable this();

this(in Project project, in Path path) {
_project = project;
Expand Down Expand Up @@ -47,4 +48,35 @@ struct AddonRepository {
}
}

/** Find the name of current git branch for this repo
**/
string getCurrBranch() {
import std.string: chompPrefix, strip;
string git_ref = runCmdE(
["git", "symbolic-ref", "-q", "HEAD"],
_path,
null,
std.process.Config.stderrPassThrough,
).output;

return git_ref.strip().chompPrefix("refs/heads/");
}

/** Fetch remote 'origin'
**/
void fetchOrigin() {
runCmdE(["git", "fetch", "origin"], _path);
}

/// ditto
void fetchOrigin(in string branch) {
runCmdE(["git", "fetch", "origin", branch], _path);
}

/** Switch repo to specified branch
**/
void switchBranchTo(in string branch_name) {
runCmdE(["git", "checkout", branch_name], _path);

}
}
39 changes: 33 additions & 6 deletions subpackages/lib/source/odood/lib/odoo/lodoo.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module odood.lib.odoo.lodoo;
private import std.logger;
private import std.exception: enforce;
private import std.format: format;
private import std.typecons: Nullable;
private static import std.process;

private import thepath: Path;

private import odood.lib.project: Project;
Expand All @@ -27,19 +30,39 @@ const struct LOdoo {

/** Run lodoo with provided args
**/
auto run(in string[] args...) {
auto run(
in string[] args,
std.process.Config config) {
tracef("Running LOdoo with args %s", args);
return _project.venv.run(
["lodoo", "--conf", _odoo_conf.toString] ~ args);
["lodoo", "--conf", _odoo_conf.toString] ~ args,
Nullable!Path.init, // workdir
null, // env
config);
}

/// ditto
auto run(in string[] args...) {
return run(args, std.process.Config.none);
}

/** Run lodoo with provided args, raising error
* in case of non-zero exit status.
**/
auto runE(in string[] args...) {
auto runE(
in string[] args,
std.process.Config config) {
tracef("Running LOdoo with args %s", args);
return _project.venv.runE(
["lodoo", "--conf", _odoo_conf.toString] ~ args);
["lodoo", "--conf", _odoo_conf.toString] ~ args,
Nullable!Path.init, // workdir
null, // env
config);
}

/// ditto
auto runE(in string[] args...) {
return runE(args, std.process.Config.none);
}

public:
Expand All @@ -55,7 +78,8 @@ const struct LOdoo {
string[] databaseList() {
import std.array: split, array;
import std.algorithm.iteration: filter;
auto res = runE("db-list");
import std.process: Config;
auto res = runE(["db-list"], Config.stderrPassThrough);
return res.output.split("\n").filter!(db => db && db != "").array;
}

Expand Down Expand Up @@ -95,9 +119,12 @@ const struct LOdoo {
return runE("db-drop", name);
}

/** Check if database eixsts
/** Check if database exists
**/
bool databaseExists(in string name) {
// TODO: replace with project's db wrapper to check if database exists
// This could simplify performance by avoiding call to python
// interpreter
auto res = run("db-exists", name);
return res.status == 0;
}
Expand Down
Loading

0 comments on commit ba963f0

Please sign in to comment.