Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first attempt at supporting opm build-install #40

Closed
wants to merge 4 commits into from

Conversation

jvanasco
Copy link
Contributor

This is my first attempt at handling #39

I haven't touched Perl in years so apologies in advance.

It seems to work, but there aren't any tests to plug into. I documented new functions inline, and also added docs for existing functions too.

Notes:

  • The command is invoked as opm build-install as it wraps the build process and installs locally (as requested)

  • A new core subroutine of do_build_install was created, which handles most of the overall logic

  • build-install will always remove an existing version, it will not compare revisions.

  • Variable names were standardized for easier maintenance:

    • $rcfile -> $rc_file (same style as $ini_file)
    • $data -> $rc_data, $ini_data
    • $default_sec -> $rc_default_sec, $ini_default_sec
  • I noted what the arguments meant to various subroutines, both when invoked and in the subroutine iteself

I tried to leverage existing code, and am not thrilled with my approach (but it does work)

  • install_target was dropped into two routines: install_target and install_distribution.
  • install_distribution handles installing the actual distribution file. Most metadata is calculated in install_target or do_build_install
  • in order to keep track of some package and directory data, I created a secondary context object with init_package_ctx. It's used alongside the existing init_installation_ctx, but is limited to the current package info, and mostly used for specifying the cache directory vs the current directory.

bin/opm Outdated
}

sub do_build_install () {
=pod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments should be comments instead of POD documentation. Also, they should appear before the sub prototype.

bin/opm Outdated
my $ini_data = read_ini($dist_file);
my $ini_default_sec = $ini_data->{default};
my $dist_name = $ini_default_sec->{name};
my $version = $ini_default_sec->{version};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use 4 space indentation consistently.

bin/opm Outdated
my $default_sec = $data->{default};
$account = delete $default_sec->{github_account};
my ($rc_file, $rc_data) = get_rc_file();
my $rc_default_sec = $rc_data->{default};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid such unrelated changes. I prefer minimal change sets in each PR. Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind if I do a PR first that standardizes the variable names? This was a bit of a headache to unwind, because $data and $default_sec would be recycled from the rc file to the ini file.

bin/opm Outdated
0 = build
1 = server-build
=cut
my $_build_type = shift;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid local variable names led by _.

bin/opm Outdated
1 = server-build
=cut
my $_build_type = shift;
my $server_build = ($_build_type == 1) ? 1 : 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid magical numbers in the build type values.

bin/opm Outdated
if (!$dist_abstract) {
err "$dist_file: key \"abstract\" not found in the default section.\n";
}

check_utf8_field($dist_file, 'abstract', $dist_abstract);

my $repo_link = delete $default_sec->{repo_link};
my $repo_link = delete $ini_default_sec->{repo_link};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid such unrelated changes. Thank you.

bin/opm Outdated
}


sub install_distribution($$$){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a space before { and (.

bin/opm Outdated
* do_build_install 0
* do_remove 0
* do_upgrade 1
* do_update 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining this back-reference list is quite a burden. Better avoid it.

@jvanasco
Copy link
Contributor Author

I'll work on all the changes tomorrow. Sorry for the whitespace issues, my editor didn't pick up Perl and auto-expand the tabs (it does for .py and .pl files)

@jvanasco
Copy link
Contributor Author

I had to do a manual rebase and force-push, but I've got this working again.

bin/opm Outdated
opm server-build
install $dist_name-$version.opm.tar.gz # psuedocode
=end comment
=cut
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better put blank lines around POD directives.

bin/opm Outdated
$remove_old = 1;
}

# package_ctx is needed to get the directories right for `install_distribution`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation?

@@ -12,7 +12,7 @@ use File::Find ();
use File::Path qw( make_path );
use File::Spec ();
use Config ();
use Cwd qw( realpath cwd );
use Cwd qw( realpath cwd getcwd );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why import getcwd? Why not just use cwd?

bin/opm Outdated

# phase 2b - `cd `
my $versioned_directory = "$dist_name-$version";
chdir $versioned_directory;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check if chdir fails.

bin/opm Outdated
# for `build-install`:
# *always* `remove_old` if the `meta_file` exists, regardless of version
# however, `get` will still need to compare version numbers
$remove_old = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use 4-space indentations.

bin/opm Outdated
This installs a distribution file (.tar.gz)
=end comment
=cut
sub install_distribution($$$) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: need a space before (.

@@ -1406,7 +1527,7 @@ sub install_target ($$) {
err "no library files found in $dist_dir/.\n";
}

if ($remove_old) {
if ($package_ctx->{remove_old}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid such immediate hash table look ups since when there is a typo in the hash table key here, it is hard to detect and debug.

@jvanasco jvanasco force-pushed the feature-opm_build_install branch 5 times, most recently from 054e796 to ba45825 Compare January 6, 2017 21:11
@jvanasco jvanasco force-pushed the feature-opm_build_install branch 2 times, most recently from 1a8ae86 to 91c2d25 Compare January 6, 2017 23:10
changed source argument to `install_file`; now a full path (not local)
altered `remove_target` to chdir back to the opening directory (not install directory)
standardized the directory start logic
@jvanasco
Copy link
Contributor Author

jvanasco commented Jan 6, 2017

Sorry for the delay in response, I was on vacation.

I've pushed the changes, and will address everything above here:

  1. why getcwd? IIRC, it is supposed to work better with symlinks. I started using it in a few spots where paths seems to switch around.

  2. whitespace and formatting issues. I'm sorry for this. can you recommend a Perl linter or similar for simple stuff like this? It's been a very long time since I've used perl.

  3. the hash table lookup isn't immediate - it happens in another function several hundred lines away. in order for me to use the existing logic of build-install and upload and not duplicate code blocks in-line, I had to decompose some functions into re-usable components (and use a new per-installable context object).

Looking at this with some fresh eyes (and a little more familiarity with Perl), in the current changeset I also altered a few things.

I didn't quite understand how/why some things originally worked with the chdirs -- but now do (I think) and so I tried to more clearly convey what is going on. The functions now note the "start" directory before chdiring, and then explicitly return to that 'start' directory (instead of the "install" directory). This loosely emulates a context manager, and makes chaining or stacking functions a lot easier.

@agentzh
Copy link
Member

agentzh commented Jan 9, 2017

@jvanasco Some comments:

  1. I think it's confusing to use both cwd and getcwd at the same time, especially when they could be different. We need to pick just one consistently.
  2. I never trust tools to automatically format my code though I'm a big fan of creating tools to report any offenders.
  3. You put the hash lookup expression directly in an if statement condition which makes typos in the hash keys extremely hard to detect. I think you should always explicitly test the definedness of the values of those hash table keys before testing the boolean value in the if statement conditions. This makes potential typos in the hash keys nowhere to hide.

@jvanasco
Copy link
Contributor Author

jvanasco commented Jan 9, 2017

  1. I think getcwd might be better than cwd, because it tries to use the system links and falls back on cwd.

  2. I was hoping you knew of a tool like flake8 in python or jshint for javascript. they simply analyze your code and point out things like whitespace issues, style mistakes from the common formats, possible scope issues, etc. in my experience, they catch 90% of issues in a PR (from me or to me).

  3. Got it on the hash key issue. I will address!

@agentzh
Copy link
Member

agentzh commented Jan 9, 2017

@jvanasco Yes, Perl has similar tools as well. You can dig them up if you like.

We do prepare our own static analysis toolsets for OpenResty's C components and Lua components. But not yet for Perl.

@ktalebian
Copy link

@agentzh @jvanasco what's the status on this pr?

@jvanasco
Copy link
Contributor Author

@ktalebian I am closing this PR. There were several changes to OpenResty in the months after I first wrote it, which would have required far too much work to rebase or merge with, and I never had time to revisit this since. someone else will need to create this functionality.

@jvanasco jvanasco closed this Nov 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants