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

add a new command opm clean #22

Closed
wants to merge 7 commits into from

Conversation

xiangnanscu
Copy link
Contributor

to delete folder and .gz file created by opm build or opm upload

to delete folder and .gz file created by opm build or opm upload
@agentzh
Copy link
Member

agentzh commented Oct 5, 2016

@Pronan You patch looks wrong to me. I don't think your code even runs:

$ perl -c bin/opm
Global symbol "$root_dir" requires explicit package name (did you forget to declare "my $root_dir"?) at bin/opm line 2203.
Global symbol "$root_dir" requires explicit package name (did you forget to declare "my $root_dir"?) at bin/opm line 2204.

fully tested and passed
@xiangnanscu
Copy link
Contributor Author

@agentzh Sorry, chun ge, I fixed it. This time this commit is fully tested and passed. lua-resty-query is just uploaded and cleaned via sudo opm build && sudo opm upload && sudo opm dist-clean

@bungle
Copy link
Member

bungle commented Oct 5, 2016

It only cleans the latest. Should it clean the previously created versions as well?

@xiangnanscu
Copy link
Contributor Author

xiangnanscu commented Oct 5, 2016

@bungle do you mean change my $root_dir = "$dist_name-$version"; to my $root_dir = "$dist_name-*"; so the final string will be like rm -rf ./lua-resty-datetime-* ?

@bungle
Copy link
Member

bungle commented Oct 5, 2016

@Pronan, I mean that maybe that should be considered as well. Not sure if that should be a default or should we introduce a command line argument.

# clean latest / current or all?
opm dist-clean

# clean current
opm dist-clean current    # or
opm dist-clean --current  # or
opm dist-clean -c


# clean all
opm dist-clean all        # or
opm dist-clean --all      # or
opm dist-clean -a

Or something? Also I'm not sure if it should be called dist-clean or just clean (currently only server-build is two words - I have seen @agentzh suggesting dist-clean on #21, though).

@agentzh
Copy link
Member

agentzh commented Oct 5, 2016

@Pronan Sorry, I changed my mind a bit. It seems like opm clean dist is better. So that we can also have opm clean cache in the future ;) What do you think?

I agree that opm clean dist should also clean up tarballs and directories belonging to the older versions. But I do think @bungle's current argument or --current option are overkill.

I think it's good enough to just cleanup whatever looks like our leftover. Basically we can iterate through the current working directory, checking each file or directory, looking for

  1. a directory whose name matches the pattern /^\Q$name\E-[.\w]*\d[.\w]*$/ and also containing a file dist.ini in itself; and
  2. a file whose name matches the pattern /^\Q$name\E-[.\w]*\d[.\w]*\.tar\.gz$/.

And remove them all.

To iterate through the cwd, we can just use the opendir, readdir, and closedir sequence. We do have such examples in the existing opm code base :)

What's your opinion?

e.g. if your package name is `lua-resty-foo`, now with option `dist`, i.e. `opm clean dist` will delete all folders like `lua-resty-foo-1.0` and files like `lua-resty-foo-1.0.tar.gz`
@xiangnanscu
Copy link
Contributor Author

@agentzh hello, chun ge. I agree with you.PR is tested and passed with your regex pattern.

@xiangnanscu xiangnanscu changed the title add a new command opm dist-clean add a new command opm clean Oct 6, 2016
sorry, I've tested several version, and commited a wrong vesion.
this one is correct
opendir(my $dh, './') || die "Can't opendir";
my @dots = readdir($dh);
foreach my $x (@dots) {
if(-d $x){
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 after if and also before {. Please keep the coding style consistent with the rest of the code base. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

shell "rm -rf $x";
say 'delete:',$x;
}
}else{
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 and after else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if(-d $x){
if ($x =~ /^\Q$dist_name\E-[.\w]*\d[.\w]*$/){
shell "rm -rf $x";
say 'delete:',$x;
Copy link
Member

Choose a reason for hiding this comment

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

Better avoid using say to be consistent with the rest of the code base. Also need a space after the comma (,).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

my $data = read_ini($dist_file);
my $default_sec = $data->{default};
my $dist_name = $default_sec->{name};
opendir(my $dh, './') || die "Can't opendir";
Copy link
Member

Choose a reason for hiding this comment

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

Error message in case of opendir failures is not descriptive enough. Please see similar places in this file for examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if (@_ == 0) {
err "no clean option specified.\n";
}
if ($_[0] eq 'dist') {
Copy link
Member

Choose a reason for hiding this comment

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

Style: 2 spaces before eq while only one is needed.

Copy link
Member

Choose a reason for hiding this comment

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

Also, need a blank line before this if statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

}
}
}
closedir $dh;
Copy link
Member

Choose a reason for hiding this comment

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

Need error handling when closedir returns an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
}else{
if ($x =~ /^\Q$dist_name\E-[.\w]*\d[.\w]*\.tar\.gz$/){
shell "rm -rf $x";
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 the Perl builtin function unlink to remove a single file. rm -rf is overkill here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

my $default_sec = $data->{default};
my $dist_name = $default_sec->{name};
opendir(my $dh, './') || die "Can't opendir";
my @dots = readdir($dh);
Copy link
Member

Choose a reason for hiding this comment

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

dots is a bad name. Maybe entities is better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

my $dist_name = $default_sec->{name};
opendir(my $dh, './') || die "Can't opendir";
my @dots = readdir($dh);
foreach my $x (@dots) {
Copy link
Member

Choose a reason for hiding this comment

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

x is a bad name. we need more descriptive names like entity, I think.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, BTW, I think you can just use for here instead of foreach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
closedir $dh;
}else{
err "invalid clean option: ".$_[0];
Copy link
Member

Choose a reason for hiding this comment

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

Again, we need spaces around the . operator. I think the error message unrecognized argument for clean is better. Also, we should give a list of recognized clean arguments here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@agentzh
Copy link
Member

agentzh commented Oct 6, 2016

Thanks for your work on this! Please address the issues in my latest round of review :) Thanks!

foreach my $x (@dots) {
if(-d $x){
if ($x =~ /^\Q$dist_name\E-[.\w]*\d[.\w]*$/){
shell "rm -rf $x";
Copy link
Member

Choose a reason for hiding this comment

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

Oh, forgot to mention that, we also need to test if there is an dist.ini file in this directory before removing it recursively. rm -rf is scary and we can never be too cautious here ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean if there is a dist.ini file in it, it should not be removed even if it matches the regex?

Copy link
Member

Choose a reason for hiding this comment

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

I mean, if there is no dist.ini in a directory, then we should not remove it. Since the dist directory generated by opm must have a dist.ini file in it (unless there is a failure in the middle of the previous opm build run).

@agentzh
Copy link
Member

agentzh commented Oct 6, 2016

And...we also need to update the usage text in the program as well as the documentation for this new feature BTW :)

Copy link
Contributor Author

@xiangnanscu xiangnanscu left a comment

Choose a reason for hiding this comment

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

hello, chun ge.

}
closedir $dh;
}else{
err "invalid clean option: ".$_[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if (@_ == 0) {
err "no clean option specified.\n";
}
if ($_[0] eq 'dist') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

my $dist_file = "dist.ini";
my $data = read_ini($dist_file);
my $default_sec = $data->{default};
my $dist_name = $default_sec->{name};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

a little hard for me. wait

my $data = read_ini($dist_file);
my $default_sec = $data->{default};
my $dist_name = $default_sec->{name};
opendir(my $dh, './') || die "Can't opendir";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

my $default_sec = $data->{default};
my $dist_name = $default_sec->{name};
opendir(my $dh, './') || die "Can't opendir";
my @dots = readdir($dh);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

}
}else{
if ($x =~ /^\Q$dist_name\E-[.\w]*\d[.\w]*\.tar\.gz$/){
shell "rm -rf $x";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
}
}
closedir $dh;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
}
closedir $dh;
}else{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

foreach my $x (@dots) {
if(-d $x){
if ($x =~ /^\Q$dist_name\E-[.\w]*\d[.\w]*$/){
shell "rm -rf $x";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean if there is a dist.ini file in it, it should not be removed even if it matches the regex?

my $dist_file = "dist.ini";
my $data = read_ini($dist_file);
my $default_sec = $data->{default};
my $dist_name = $default_sec->{name};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

name is a varible not a string literal, right? but I can't find where it is defined in do_build.

@agentzh
Copy link
Member

agentzh commented Oct 7, 2016

@Pronan $default_sec->{name} is reading the data from the dist.ini file so it could be empty or bad. Just look at other similar places in the opm source file for examples of checking validity of this value.

@xiangnanscu
Copy link
Contributor Author

@agentzh Hello, Spring big brother, all done and test passed.

}
}
closedir $dh
or err "failed to close directory $dh!\n";
Copy link
Member

Choose a reason for hiding this comment

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

We usually do not use ! at the end of our error string messages. Instead, you should use the special variable $! here to output the system error string for the failed closedir operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

my @valid_options = ('dist');

if (@_ == 0) {
err "no clean option specified.\n";
Copy link
Member

Choose a reason for hiding this comment

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

I think "dist" is a clean argument instead of an option. In the CLI terminology, options are usually something like --cwd and --help.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

{
err "$dist_file: bad dist name: $dist_name\n";
}
opendir(my $dh, './')
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 blank line after the if statement block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

err "$dist_file: bad dist name: $dist_name\n";
}
opendir(my $dh, './')
or err "failed to open directory './'!\n";
Copy link
Member

Choose a reason for hiding this comment

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

We need to use : $! instead of ! here. See other similar places. Be careful ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

{
err "$dist_file: bad dist name: $dist_name\n";
}
opendir(my $dh, './')
Copy link
Member

Choose a reason for hiding this comment

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

./ looks strange to me. You can just use . here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


if (-e "$entity/dist.ini") {
shell "rm -rf $entity";
print "delete folder: $entity\n";
Copy link
Member

Choose a reason for hiding this comment

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

"delete" => "removed",
"folder" => "directory",
":" => ""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


if ($entity =~ /^\Q$dist_name\E-[.\w]*\d[.\w]*\.tar\.gz$/) {
unlink $entity;
print "delete file: $entity\n";
Copy link
Member

Choose a reason for hiding this comment

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

"delete" => "removed"
": " => ""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

if (length $dist_name < 3
|| $dist_name =~ /[^-\w]|^(?:nginx|luajit|resty|openresty|opm|restydoc.*|ngx_.*|.*-nginx-module)$/i)
Copy link
Member

Choose a reason for hiding this comment

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

Because this check is also used elsewhere. Maybe we should introduce a small Perl sub to avoid code duplication here? Like sub is_valid_dist_name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -2195,6 +2198,57 @@ sub rebase_path ($$$) {
return undef;
}

sub do_clean {
my @valid_options = ('dist');
Copy link
Member

Choose a reason for hiding this comment

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

We won't have many different kinds of arguments, especially when we only have one right now. No need to introduce a Perl array here. It is just referenced once and simply adds line noises IMHO :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

closedir $dh
or err "failed to close directory $dh!\n";
} else {
err "unrecognized argument for clean: $_[0]. recognized clean arguments are: " . join(", ", @valid_options);
Copy link
Member

Choose a reason for hiding this comment

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

This line exceeds 80 columns a lot, I'm afraid ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@agentzh agentzh mentioned this pull request Oct 8, 2016
@xiangnanscu
Copy link
Contributor Author

Hello, chun ge. All done but I'm not sure how to merge my two PR so I did nothing for that.

@agentzh
Copy link
Member

agentzh commented Oct 9, 2016

@Pronan You can just git cherry-pick the commit(s) in your branch for PR #26 over to your branch for this PR.

@xiangnanscu
Copy link
Contributor Author

@agentzh hello, Should I do this in local computer or just on github webpage? I clone my fork pronan/opm to my comupter, but I found it is same with yours. I do change two files. I don't know why.

@agentzh
Copy link
Member

agentzh commented Oct 9, 2016

@Pronan You do git cherry-pick in your local branches and when your local branch is ready, you just git push. Unlike SVN, one seldom or never does such operations directly on the remote git branches.

@agentzh
Copy link
Member

agentzh commented Oct 9, 2016

@Pronan Do not use GitHub's web UI to make changes. That tends to suck a lot.

@xiangnanscu
Copy link
Contributor Author

@agentzh Saddly, I was using github web UI to make changes.It sucks. Now I'm working on local branch.

@agentzh
Copy link
Member

agentzh commented Oct 9, 2016

cool.

@xiangnanscu xiangnanscu mentioned this pull request Oct 9, 2016
@agentzh
Copy link
Member

agentzh commented Nov 1, 2016

This patch has been updated and merged with #26 to master.

@agentzh agentzh closed this Nov 1, 2016
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