#478 [dist-git] one-shot script script to remove data for already deleted coprs
Merged 5 years ago by msuchy. Opened 5 years ago by clime.

@@ -33,6 +33,8 @@ 

  Requires: findutils

  Requires: (copr-selinux if selinux-policy-targeted)

  

+ Recommends: python3-copr

+ 

  %{?fedora:Requires(post): policycoreutils-python-utils}

  %{?rhel:Requires(post): policycoreutils-python}

  

@@ -0,0 +1,75 @@ 

+ #!/usr/bin/python3

+ 

+ """

+ One-shot script to remove data on copr-dist-git for already deleted coprs.

+ Should be run as copr-dist-git user.

+ """

+ 

+ import os

+ import shutil

+ import sys

+ import pwd

+ import argparse

+ import copr

+ 

+ from copr.v3.client import Client as CoprClient

+ 

+ parser = argparse.ArgumentParser(description="Prune DistGit repositories and lookaside cache. "

+                                  "Requires to be run as copr-dist-git user. You will be asked before anything is deleted. "

+                                  "If you no longer want to be asked, answer 'Y', if you want to exit the script, answer 'N'.")

+ parser.add_argument('--repos', action='store', help='local path to a DistGit repository root', required=True)

+ parser.add_argument('--lookasidepkgs', action='store', help='local path to a DistGit lookaside cache pkgs root', required=True)

+ parser.add_argument('--copr-config', action='store', help='path to copr config with API credentials', required=True)

+ parser.add_argument('--always-yes', action='store_true', help="Assume answer 'yes' for each deletion.")

+ 

+ args = parser.parse_args()

+ 

+ if __name__ == "__main__":

+     if pwd.getpwuid(os.getuid())[0] != "copr-dist-git":

+         print("This script should be executed under the `copr-dist-git` user")

+         sys.exit(1)

+ 

+     client = CoprClient.create_from_config_file(args.copr_config)

+     os.chdir(args.repos)

+ 

+     always_yes = False

+ 

+     if not os.path.isdir(args.repos):

+         print("{0} is not a directory.".format(args.repos), file=sys.stderr)

+ 

+     for username in os.listdir(args.repos):

+         repos_user_path = os.path.join(args.repos, username)

+ 

+         if not os.path.isdir(repos_user_path):

+             continue

+ 

+         for projectname in os.listdir(repos_user_path):

+             repos_project_path = os.path.join(repos_user_path, projectname)

+ 

+             try:

+                 client.project_proxy.get(username, projectname)

+             except copr.v3.exceptions.CoprNoResultException:

+                 pass

+             else:

+                 continue

+ 

+             pkgs_project_path = os.path.join(args.lookasidepkgs, username, projectname)

+ 

+             answer = None

+             if args.always_yes:

+                 answer = 'y'

+ 

+             while not answer:

+                 a = input('Project {0}/{1} does not exist.\nDelete paths {2} and {3} [y/n]? '

+                           .format(username, projectname, repos_project_path, pkgs_project_path))

+ 

+                 if a in ['n', 'no']:

+                     answer = 'n'

+                 if a in ['y', 'yes']:

+                     answer = 'y'

+ 

+             if answer == 'y':

+                 print("Deleting {0}".format(repos_project_path))

+                 shutil.rmtree(repos_project_path, ignore_errors=True)

+                 print("Deleting {0}".format(pkgs_project_path))

+                 shutil.rmtree(pkgs_project_path, ignore_errors=True)

One-shot script to delete data from copr-dist-git for already deleted coprs.

prune-dist-git.py:65:19: R1714: Consider merging these comparisons with "in" to "a in ('n', 'no')" (consider-using-in)
Similary in rest of the file

1 new commit added

  • [dist-git] merge comparisons by using "in" operator
5 years ago

Updated. Please, take a look.

It is not clear here what are the differences between y and Y. I would suggest to remove the diferrences and implement -y option with "Automatically answer yes for all questions" as we are used to in many other scripts.

It is not clear here what are the differences between y and Y. I would suggest to remove the diferrences and implement -y option with "Automatically answer yes for all questions" as we are used to in many other scripts.

The point of this is that you first check that you are not deleting unwanted directories (e.g. when you wrongly specify some input path) and then you fire the whole action by answer 'Y'.

I would propose --distgitrepos and --lookasidecache to remove the ambiguity.

You need to Require it in spec file too.

You need to Require it in spec file too.

I don't think it python-copr should be required in spec for this one-shot python script. It was mainly written to alleviate current issues with disk space.

The point of this is that you first check that you are not deleting unwanted directories (e.g. when you wrongly specify some input path) and then you fire the whole action by answer 'Y'.

Then I would expect --dry-run. What you made is very non-standard and I would not expect this behavior at all.

I don't think it python-copr should be required in spec for this one-shot python script. It was mainly written to alleviate current issues with disk space.

... and likely run it periodically in cron. Anyway, without that Require it would fail as the library would not be installed - or it has to be installed manually.

1 new commit added

  • [dist-git] implement --always-yes, drop Y/N, rename --pkgs to --lookasidepkgs
5 years ago

I did the updates based on your review. Please, take a look.

1 new commit added

  • [dist-git] add python3-copr Recommends:
5 years ago

Pull-Request has been merged by msuchy

5 years ago